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

Only 2xx mock responses are generated #1280

Closed
huwshimi opened this issue Mar 24, 2024 · 0 comments · Fixed by #1284
Closed

Only 2xx mock responses are generated #1280

huwshimi opened this issue Mar 24, 2024 · 0 comments · Fixed by #1284
Assignees
Labels
mock Related to mock generation
Milestone

Comments

@huwshimi
Copy link
Contributor

What are the steps to reproduce this issue?

  1. Create an openapi spec with 400 or 500 responses e.g.
openapi: '3.0.0'
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
servers:
  - url: http://petstore.swagger.io/v1
paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - pets
      parameters:
        - name: limit
          in: query
          description: How many items to return at one time (max 100)
          required: false
          schema:
            type: string
      responses:
        '200':
          description: A paged array of pets
          headers:
            x-next:
              description: A link to the next page of responses
              schema:
                type: string
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Labradoodle'
        '201':
          description: A paged array of pets
          headers:
            x-next:
              description: A link to the next page of responses
              schema:
                type: string
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Dachshund'
        '400':
          description: A paged array of pets
          headers:
            x-next:
              description: A link to the next page of responses
              schema:
                type: string
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Cat'
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
components:
  schemas:
    Labradoodle:
      type: object
      required: ['cuteness']
      properties:
        cuteness:
          type: integer
    Dachshund:
      type: object
      required: ['length']
      properties:
        length:
          type: integer
    Cat:
      type: object
      required: ['type']
      properties:
        petsRequested:
          type: integer
        type:
          type: string
          enum:
            - cat
    Error:
      type: object
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string
  1. Generate mocks using orval

What happens?

Orval only generates an http response with a 200 status (which picks from an array of all 2xx so these can't be used independently) and does not generate any code for error responses:

export const getListPetsMock = () =>
  faker.helpers.arrayElement([
    { cuteness: faker.number.int({ min: undefined, max: undefined }) },
    { length: faker.number.int({ min: undefined, max: undefined }) },
  ]);

export const getSwaggerPetstoreMSW = () => [
  http.get("*/pets", async () => {
    await delay(1000);
    return new HttpResponse(JSON.stringify(getListPetsMock()), {
      status: 200,
      headers: {
        "Content-Type": "application/json",
      },
    });
  }),
];

What were you expecting to happen?

Mocks for all responses to be generated so that each response can be tested.

Any logs, error output, etc?

Any other comments?

What versions are you using?

Operating System:
Package Version:
Browser Version:

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 a pull request may close this issue.

2 participants