Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
AffeJonsson opened this issue Apr 18, 2024 · 0 comments · Fixed by #1365
Closed

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

AffeJonsson opened this issue Apr 18, 2024 · 0 comments · Fixed by #1365
Assignees
Labels
enhancement New feature or request mock Related to mock generation
Milestone

Comments

@AffeJonsson
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request mock Related to mock generation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants