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

MSW: anyOf items using allOf msw mock generation bug #1508

Closed
david10sing opened this issue Jul 8, 2024 · 2 comments · Fixed by #1635
Closed

MSW: anyOf items using allOf msw mock generation bug #1508

david10sing opened this issue Jul 8, 2024 · 2 comments · Fixed by #1635
Assignees
Labels
mock Related to mock generation msw MSW related issues
Milestone

Comments

@david10sing
Copy link
Contributor

david10sing commented Jul 8, 2024

What are the steps to reproduce this issue?

There are a couple of issues here.

anyOf with discriminator and items not using allOf

The output is wrong

What happens?

export const getListPetsResponseMock = (): PetsArray =>
  Array.from(
    { length: faker.number.int({ min: 1, max: 10 }) },
    (_, i) => i + 1,
  ).map(() =>
    faker.helpers.arrayElement([
      {
        category: faker.helpers.arrayElement([
          faker.helpers.arrayElement(['FELINE', 'CANINE'] as const),
          undefined,
        ]),
        id: (() => faker.number.int({ min: 1, max: 99999 }))(),
      },
      {
        superpowers: faker.helpers.arrayElement([
          faker.word.sample(),
          undefined,
        ]),
      },
    ]),
  );

What were you expecting to happen?

export const getListPetsResponseMock = (): PetsArray =>
  Array.from(
    { length: faker.number.int({ min: 1, max: 10 }) },
    (_, i) => i + 1,
  ).map(() =>
    faker.helpers.arrayElement([
      {
        id: (() => faker.number.int({ min: 1, max: 99999 }))(),
        category: 'FELINE',
        superpowers: faker.helpers.arrayElement([
          faker.word.sample(),
          undefined,
        ]),
      },
      {
        id: (() => faker.number.int({ min: 1, max: 99999 }))(),
        category: 'CANINE',
      },
    ]),
  );

anyOf with discriminator and items using allOf

The output is wrong

What happens?

export const getListPetsResponseMock = (): PetsArray => (Array.from({ length: faker.number.int({ min: 1, max: 10 }) }, (_, i) => i + 1).map(() => (faker.helpers.arrayElement([id: (() => faker.number.int({ min: 1, max: 99999 }))(),category: faker.helpers.arrayElement([faker.helpers.arrayElement(['FELINE','CANINE'] as const), undefined]),superpowers: faker.helpers.arrayElement([faker.word.sample(), undefined])]))))

image

The above cannot be formatted due to the output being wrong.

What were you expecting to happen?

export const getListPetsResponseMock = (): PetsArray =>
  Array.from(
    { length: faker.number.int({ min: 1, max: 10 }) },
    (_, i) => i + 1,
  ).map(() =>
    faker.helpers.arrayElement([
      {
        id: (() => faker.number.int({ min: 1, max: 99999 }))(),
        category: 'FELINE',
        superpowers: faker.helpers.arrayElement([
          faker.word.sample(),
          undefined,
        ]),
      },
      {
        id: (() => faker.number.int({ min: 1, max: 99999 }))(),
        category: 'CANINE',
      },
    ]),
  );

Because there is a discriminator, the expectation is that the generated mock already fills the category.

Any logs, error output, etc?

  • No log errors

Any other comments?

@david10sing
Copy link
Contributor Author

I've tried looking into this issue and see if I could put a PR up but it looks complicated. I might ping on discord over the weekend.

@david10sing david10sing changed the title Orval - anyOf schema using allOf bug Orval - anyOf schema using allOf msw mock generation bug Jul 8, 2024
@david10sing david10sing changed the title Orval - anyOf schema using allOf msw mock generation bug Orval - anyOf items using allOf msw mock generation bug Jul 8, 2024
@melloware melloware added mock Related to mock generation msw MSW related issues labels Jul 8, 2024
@melloware melloware changed the title Orval - anyOf items using allOf msw mock generation bug MSW: anyOf items using allOf msw mock generation bug Jul 23, 2024
@Georgegriff
Copy link

Georgegriff commented Sep 3, 2024

image

I appear to have also hit this issue, i started to look at the code in orval but didn't quite know where to start

Seemed possible its in the combine.ts file but not sure
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mock Related to mock generation msw MSW related issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants