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

fix(msw): correctly mock nested objects #1325

Merged
merged 2 commits into from
Apr 23, 2024

Conversation

AllieJonsson
Copy link
Contributor

@AllieJonsson AllieJonsson commented Apr 23, 2024

Status

READY

Description

Fixes #1210

This fixes the issue with nested objects that are more than one level deep (in petstore, there is an example of Pet, which is one of Dog or Cat, and Dog is one of Labradoodle or Dachshund). There is a problem where only the last oneOf object gets the base object properties. This PR fixes this issue, more info in linked issue.

Steps to Test or Reproduce

Before new code: go to tests/generated/swr/petstore/endpoints.ts, notice how getListPetsResponseMock contains:

faker.helpers.arrayElement([
      {
        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,
      },
...

Notice that the first object is missing '@id', callingCode, country, email, id, name and tag.

With new code, do the same generation and check the same file. See that the missing properties are now present in the first object.

@melloware melloware added the mock Related to mock generation label Apr 23, 2024
@melloware melloware requested a review from soartec-lab April 23, 2024 12:06
@AllieJonsson AllieJonsson force-pushed the fix/mock-nested-objects branch from fd75eb6 to ce50ab6 Compare April 23, 2024 12:08
melloware
melloware previously approved these changes Apr 23, 2024
@melloware
Copy link
Collaborator

LGTM i made one minor change to change const s since i don't love 1 letter variables 👍🏻 thanks for the PR!

@melloware melloware merged commit d7fa66d into orval-labs:master Apr 23, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mock Related to mock generation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mock generation doesn't guarantee the required property mocking
2 participants