orval icon indicating copy to clipboard operation
orval copied to clipboard

Feature request: Split polymorphic mocks into it's own functions

Open AffeJonsson opened this issue 10 months ago • 0 comments

What are the steps to reproduce this issue?

Using the following schema:

paths:
  /api/sample:
    get:
      summary: sample
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/TypeObj1'
                  - $ref: '#/components/schemas/TypeObj2'
components:
  schemas:
    Type:
      enum:
        - Type1
        - Type2
      type: string
    Base:
      type: object
      required:
        - type
      properties:
        type:
          allOf:
            - $ref: '#/components/schemas/Type'
      discriminator:
        propertyName: type
        mapping:
          Type1: '#/components/schemas/TypeObj1'
          Type2: '#/components/schemas/TypeObj2'
    TypeObj1:
      type: object
      allOf:
        - $ref: '#/components/schemas/Base'
      required:
        - type
      properties:
        moreProp:
          type: string
    TypeObj2:
      type: object
      allOf:
        - $ref: '#/components/schemas/Base'
      required:
        - type
      properties:
        prop:
          type: string

with mock: true in the configuration

What happens?

Generated mock is

export const getGetApiSampleResponseMock = (
  overrideResponse: any = {},
): GetApiSample200 =>
  faker.helpers.arrayElement([
    {
      ...overrideResponse,
      moreProp: faker.helpers.arrayElement([faker.word.sample(), undefined]),
      type: faker.helpers.arrayElement(['Type1'] as const),
      ...overrideResponse,
    },
    {
      ...overrideResponse,
      prop: faker.helpers.arrayElement([faker.word.sample(), undefined]),
      type: faker.helpers.arrayElement(['Type2'] as const),
      ...overrideResponse,
    },
  ]);

What were you expecting to happen?

I'd like the mock to be split up so I can reference a specific type, something like this:

export const getTypeObj1Mock = (overrideResponse: any ={}): TypeObj1 => ({
      moreProp: faker.helpers.arrayElement([faker.word.sample(), undefined]),
      type: faker.helpers.arrayElement(['Type1'] as const),
      ...overrideResponse,
});
export const getTypeObj2Mock = (overrideResponse: any ={}): TypeObj2 => ({
      prop: faker.helpers.arrayElement([faker.word.sample(), undefined]),
      type: faker.helpers.arrayElement(['Type2'] as const),
      ...overrideResponse,
});
export const getGetApiSampleResponseMock = (
  overrideResponse: any = {},
): GetApiSample200 =>
  faker.helpers.arrayElement([
    getTypeObj1Mock(overrideResponse),
    getTypeObj2Mock(overrideResponse),
  ]);

Any other comments?

This would help when testing, so I can test that when the api returns a specific type, the correct thing gets rendered.

What versions are you using?

Package Version: 6.27.1

AffeJonsson avatar Apr 18 '24 11:04 AffeJonsson