From 261f8de8ce5b5f68e99538a702d201fc97ae394f Mon Sep 17 00:00:00 2001 From: Shodai Suzuki Date: Sun, 15 Sep 2024 20:12:16 +0900 Subject: [PATCH] fix(msw): do not use spread objects if `nullable` object schema in `oneOf` (#1626) * fix(msw): not use unnecessary spread object when nullable object schema * chore: add test case --- packages/mock/src/faker/resolvers/value.ts | 8 +++- tests/specifications/one-of.yaml | 52 ++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 tests/specifications/one-of.yaml diff --git a/packages/mock/src/faker/resolvers/value.ts b/packages/mock/src/faker/resolvers/value.ts index d75af3a53..98399e906 100644 --- a/packages/mock/src/faker/resolvers/value.ts +++ b/packages/mock/src/faker/resolvers/value.ts @@ -134,7 +134,13 @@ export const resolveMockValue = ({ const func = `export const ${funcName} = (${args}): ${newSchema.name} => ({...${value}, ...${overrideVarName}});`; splitMockImplementations?.push(func); } - scalar.value = `{...${funcName}()}`; + + if (newSchema.nullable) { + scalar.value = `${funcName}()`; + } else { + scalar.value = `{...${funcName}()}`; + } + scalar.imports.push({ name: newSchema.name, specKey: isRootKey(specKey, context.target) ? undefined : specKey, diff --git a/tests/specifications/one-of.yaml b/tests/specifications/one-of.yaml new file mode 100644 index 000000000..889173913 --- /dev/null +++ b/tests/specifications/one-of.yaml @@ -0,0 +1,52 @@ +openapi: 3.0.0 +info: + version: 1.0.0 + title: AnyOf Schema + license: + name: MIT + +paths: + /one-of-with-nullable-object: + get: + operationId: getOneOfWithNullableObject + tags: + - pets + description: |- + oneOf with nullable object. + responses: + '200': + description: Pet + content: + application/json: + schema: + $ref: '#/components/schemas/Pet' + +components: + schemas: + Pet: + oneOf: + - $ref: '#/components/schemas/Dog' + - $ref: '#/components/schemas/Cat' + Dog: + type: object + nullable: true + required: + - id + - name + properties: + id: + type: integer + format: int64 + name: + type: string + Cat: + type: object + required: + - id + - category + properties: + id: + type: integer + format: int64 + category: + type: string