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: nested objects with the same attribute names. #910

Closed
Cohey0727 opened this issue Aug 8, 2023 · 4 comments · Fixed by #1042
Closed

MSW: nested objects with the same attribute names. #910

Cohey0727 opened this issue Aug 8, 2023 · 4 comments · Fixed by #1042
Assignees
Labels
bug Something isn't working
Milestone

Comments

@Cohey0727
Copy link

What are the steps to reproduce this issue?

  1. Set Up the Environment:

    • Install orval v6.17.0 on your machine.
  2. Provide the OpenAPI Definition:
    Create an OpenAPI document in which the parent attribute appears multiple times in a nested structure.

    openapi: "3.0.0"
    info:
      version: 1.0.0
      title: Swagger Petstore
      license:
        name: MIT
    servers:
      - url: http://petstore.swagger.io/v1
    paths:
      /pets/{petId}:
        get:
          summary: Info for a specific pet
          operationId: showPetById
          parameters:
            - name: petId
              in: path
              required: true
              description: The id of the pet to retrieve
              schema:
                type: string
          responses:
            "200":
              description: Expected response to a valid request
              content:
                application/json:
                  schema:
                    $ref: "#/components/schemas/Pet"
    components:
      schemas:
        Pet:
          type: object
          required:
            - id
            - name
          properties:
            id:
              type: integer
              format: int64
            name:
              type: string
            tag:
              type: string
            parent:
              $ref: "#/components/schemas/Parent"
        Parent:
          type: object
          required:
            - id
            - name
          properties:
            id:
              type: integer
              format: int64
            name:
              type: string
            tag:
              type: string
            parent:
              $ref: "#/components/schemas/GrandParent"
        GrandParent:
          type: object
          required:
            - id
            - name
            - parent
          properties:
            id:
              type: integer
              format: int64
            name:
              type: string
            tag:
              type: string
            parent:
              $ref: "#/components/schemas/GreatParent"
        GreatParent:
          type: object
          required:
            - id
            - name
            - parent
          properties:
            id:
              type: integer
              format: int64
            name:
              type: string
            tag:
              type: string
  3. Run Orval Code Generator:
    run command with config mock:true.

    orval 
  4. Inspect the Generated MSW Mock:

    /**
     * Generated by orval v6.17.0 🍺
     * Do not edit manually.
     * Swagger Petstore
     * OpenAPI spec version: 1.0.0
     */
    import { rest } from "msw";
    import { faker } from "@faker-js/faker";
    
    export const getShowPetByIdMock = () => ({
      id: faker.datatype.number({ min: undefined, max: undefined }),
      name: faker.random.word(),
      tag: faker.helpers.arrayElement([faker.random.word(), undefined]),
      parent: faker.helpers.arrayElement([
        {
          id: faker.datatype.number({ min: undefined, max: undefined }),
          name: faker.random.word(),
          tag: faker.helpers.arrayElement([faker.random.word(), undefined]),
          parent: faker.helpers.arrayElement([
            {
              id: faker.datatype.number({ min: undefined, max: undefined }),
              name: faker.random.word(),
              tag: faker.helpers.arrayElement([faker.random.word(), undefined]),
            },
            undefined,
          ]),
        },
        undefined,
      ]),
    });
    
    export const getDefaultMSW = () => [
      rest.get("*/pets/:petId", (_req, res, ctx) => {
        return res(ctx.delay(100), ctx.status(200, "Mocked status"), ctx.json(getShowPetByIdMock()));
      }),
    ];

What happens?

The GreatParent type, which should appear nested under the GrandParent schema's parent attribute in the generated MSW mock, is missing.

What were you expecting to happen?

The GreatParent mock should be generated in the MSW mock file, reflecting its definition and the structure provided in the OpenAPI spec.

Any logs, error output, etc?

No specific error messages are shown during the code generation, but the generated code is missing the expected mock structure.

Any other comments?

It's crucial for our application to have complete mock data for accurate testing, including nested types.

What versions are you using?

**Operating System: Mac Ventura 13.4.1
**Package Version: Orval: v6.17.0, Node:20.5.0
**Browser Version: -

@melloware
Copy link
Collaborator

@Will-Mann-16 wanna verify this one?

@melloware melloware added the bug Something isn't working label Nov 2, 2023
@Will-Mann-16
Copy link
Collaborator

Will take a look here

@melloware melloware changed the title Code generator fails to produce MSW source code for nested objects with the same attribute names. MSW: nested objects with the same attribute names. Nov 9, 2023
@Will-Mann-16
Copy link
Collaborator

I can validate this happens, I'll investigate and see if I can find a fix.

@melloware
Copy link
Collaborator

Awesome I assigned it to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants