Skip to content

Commit

Permalink
fix(msw): do not use spread objects if nullable object schema in `o…
Browse files Browse the repository at this point in the history
…neOf` (orval-labs#1626)

* fix(msw): not use unnecessary spread object when nullable object schema

* chore: add test case
  • Loading branch information
soartec-lab authored Sep 15, 2024
1 parent d9fcfd5 commit 261f8de
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/mock/src/faker/resolvers/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
52 changes: 52 additions & 0 deletions tests/specifications/one-of.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 261f8de

Please sign in to comment.