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

Mock generation doesn't guarantee the required property mocking #1210

Closed
DevSDK opened this issue Feb 8, 2024 · 1 comment · Fixed by #1325
Closed

Mock generation doesn't guarantee the required property mocking #1210

DevSDK opened this issue Feb 8, 2024 · 1 comment · Fixed by #1325
Assignees
Labels
bug Something isn't working mock Related to mock generation
Milestone

Comments

@DevSDK
Copy link

DevSDK commented Feb 8, 2024

What are the steps to reproduce this issue?

  1. yarn generate in the /test directory
  2. See tests/generated/angular/tags-split/pets/pets.msw.ts

What happens?

The element of generated result possibly does not have the required property.

The schemes:

https://github.com/anymaniax/orval/blob/288f48876509978bfbf5449f52ba214f54cc33e5/tests/specifications/petstore.yaml#L179-L186
https://github.com/anymaniax/orval/blob/288f48876509978bfbf5449f52ba214f54cc33e5/tests/specifications/petstore.yaml#L64-L119

One of actual generated mocking function:

export const getCreatePetsMock = (overrideResponse: any = {}): Pet =>
  faker.helpers.arrayElement([
    {
     // This element don't have **id** which is a required type
      breed: faker.helpers.arrayElement(['Labradoodle'] as const),
      cuteness: faker.number.int({ min: undefined, max: undefined }),
      ...overrideResponse,
      barksPerMinute: faker.helpers.arrayElement([
        faker.number.int({ min: undefined, max: undefined }),
        undefined,
      ]),
      type: faker.helpers.arrayElement(['dog'] as const),
      ...overrideResponse,
    },
    {
      breed: faker.helpers.arrayElement(['Dachshund'] as const),
      length: faker.number.int({ min: undefined, max: undefined }),
      ...overrideResponse,
      barksPerMinute: faker.helpers.arrayElement([
        faker.number.int({ min: undefined, max: undefined }),
        undefined,
      ]),
      type: faker.helpers.arrayElement(['dog'] as const),
      ...overrideResponse,
      '@id': faker.helpers.arrayElement([faker.word.sample(), undefined]),
      callingCode: faker.helpers.arrayElement([
        faker.helpers.arrayElement(['+33', '+420', '+33'] as const),
        undefined,
      ]),
      country: faker.helpers.arrayElement([
        faker.helpers.arrayElement([
          "People's Republic of China",
          'Uruguay',
        ] as const),
        undefined,
      ]),
      email: faker.helpers.arrayElement([faker.internet.email(), undefined]),
      id: faker.number.int({ min: undefined, max: undefined }),
      name: faker.word.sample(),
      tag: faker.helpers.arrayElement([faker.word.sample(), undefined]),
      ...overrideResponse,
    },
    {
      petsRequested: faker.helpers.arrayElement([
        faker.number.int({ min: undefined, max: undefined }),
        undefined,
      ]),
      type: faker.helpers.arrayElement(['cat'] as const),
      ...overrideResponse,
      '@id': faker.helpers.arrayElement([faker.word.sample(), undefined]),
      callingCode: faker.helpers.arrayElement([
        faker.helpers.arrayElement(['+33', '+420', '+33'] as const),
        undefined,
      ]),
      country: faker.helpers.arrayElement([
        faker.helpers.arrayElement([
          "People's Republic of China",
          'Uruguay',
        ] as const),
        undefined,
      ]),
      email: faker.helpers.arrayElement([faker.internet.email(), undefined]),
      id: faker.number.int({ min: undefined, max: undefined }),
      name: faker.word.sample(),
      tag: faker.helpers.arrayElement([faker.word.sample(), undefined]),
      ...overrideResponse,
    },
  ]);

What were you expecting to happen?

The required property must exist.

Any logs, error output, etc?

This can non-type error because the spread syntax makes the type 'any'. [1]

[1] https://www.typescriptlang.org/play?ts=5.2.2#code/FAFwngDgpgBAClEMC8MDeMB2BDAtrALhgGcQAnAS0wHMYBfYYAYwHtNSYA3bAGwFcoRBElRoc+IgCIQUXBEn1GrdkmyYwANV4CiasCnSLmbDjNJDEBjDAB0dvVv6wGwIA

Any other comments?

Further context:

#1199 (comment)

What versions are you using?

Current master 288f488

Operating System:
Package Version: 6.24.0 …
Browser Version:

@DevSDK DevSDK changed the title Mock generation doesn't guarantee the required property creation Mock generation doesn't guarantee the required property mocking Feb 8, 2024
@melloware melloware added the enhancement New feature or request label Feb 8, 2024
@melloware melloware added this to the 6.25.0 milestone Feb 8, 2024
@melloware melloware added mock Related to mock generation and removed enhancement New feature or request labels Feb 9, 2024
@melloware melloware modified the milestones: 6.25.0, 6.26.0 Feb 17, 2024
@AllieJonsson
Copy link
Contributor

Seems to be an issue when using oneOf in nested objects, regardless of if the properties are required or not

openapi: '3.0.0'
info:
  version: 1.0.0
  title: Swagger Nested
paths:
  /nested:
    post:
      summary: Create a nested item
      operationId: createItems
      responses:
        '200':
          description: Created Item
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Item'
components:
  schemas:
    Item:
      type: object
      oneOf:
        - $ref: '#/components/schemas/Item1'
      properties:
        prop:
          type: string
    Item1:
      type: object
      oneOf:
        - $ref: '#/components/schemas/Item11'
        - $ref: '#/components/schemas/Item12'
      properties:
        prop1:
          type: string
    Item11:
      type: object
      properties:
        prop11:
          type: string
    Item12:
      type: object
      properties:
        prop12:
          type: string

yields

export const getCreateItemsResponseMock = (overrideResponse: any = {}): Item =>
  faker.helpers.arrayElement([
    {
      prop11: faker.word.sample(),
      ...overrideResponse,
      prop1: faker.word.sample(),
      ...overrideResponse,
/*
Here we are missing these:
      prop: faker.word.sample(),
      ...overrideResponse,
*/
    },
    {
      prop12: faker.word.sample(),
      ...overrideResponse,
      prop1: faker.word.sample(),
      ...overrideResponse,
      prop: faker.word.sample(),
      ...overrideResponse,
    },
  ]);

Unsure how to fix this as it seems like prop: faker.word.sample(), ...overrideResponse, is added to the string

    {
      prop11: faker.word.sample(),
      ...overrideResponse,
      prop1: faker.word.sample(),
      ...overrideResponse,
    },
    {
      prop12: faker.word.sample(),
      ...overrideResponse,
      prop1: faker.word.sample(),
      ...overrideResponse,

🤔

@melloware melloware modified the milestones: 6.26.0, 6.27.0 Apr 2, 2024
@melloware melloware modified the milestones: 6.27.0, 6.28.0 Apr 11, 2024
@melloware melloware assigned AllieJonsson and unassigned soartec-lab and DevSDK Apr 23, 2024
@soartec-lab soartec-lab added the bug Something isn't working label Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mock Related to mock generation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants