orval-labs / orval

orval is able to generate client with appropriate type-signatures (TypeScript) from any valid OpenAPI v3 or Swagger v2 specification, either in yaml or json formats. 🍺
https://orval.dev
MIT License
3.21k stars 337 forks source link

fix(msw): do not use spread objects if `nullable` object schema in `oneOf` #1626

Closed soartec-lab closed 2 months ago

soartec-lab commented 2 months ago

Status

READY

Description

Suppose I have a schema nullable object in oneOf as below.

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

In this case, the mock defined function cannot be spread and will result in a syntax error. Therefore, I modified it to avoid it in the case of nullable.

Before

Dog is nullable so occur syntax error

export const getGetOneOfObjectOrIntegerPetsResponseMock = (): Pet => (faker.helpers.arrayElement([{...getGetOneOfObjectOrIntegerPetsResponseDogMock()},{...getGetOneOfObjectOrIntegerPetsResponseCatMock()}]))

After

mock of Doc doesn't use spread.

export const getGetOneOfObjectOrIntegerPetsResponseMock = (): Pet => (faker.helpers.arrayElement([getGetOneOfObjectOrIntegerPetsResponseDogMock(),{...getGetOneOfObjectOrIntegerPetsResponseCatMock()}]))

Related PRs

none

Todos

Steps to Test or Reproduce

You can check by i added test case.