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

@ApiHeader() schema is being overwritten if an enum is provided #2761

Open
2 of 4 tasks
jointhejourney opened this issue Dec 27, 2023 · 2 comments
Open
2 of 4 tasks

Comments

@jointhejourney
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Using the following @ApiHeader() with an enum:

  @ApiHeader({
    name: 'Journey-Version',
    description: 'API version',
    enum: ['1'],
    schema: {
      default: '1',
    }
  })

Generates the following OpenAPI schema:

 /test:
    post:
      operationId: TestController_testRequest
      parameters:
        - name: Journey-Version
          in: header
          description: Journey API version.
          required: false
          schema:
            enum:
              - '1'
            type: string

Minimum reproduction code

https://gist.github.com/jointhejourney/13b4cd7feba0864f11c8fd42fd069890

Steps to reproduce

No response

Expected behavior

It should include the default value as provided by the schema, such as:

 /test:
    post:
      operationId: TestController_testRequest
      parameters:
        - name: Journey-Version
          in: header
          description: Journey API version.
          required: false
          schema:
            type: string
            enum:
              - '1'
            default: '1'

Package version

7.1.10

NestJS version

9.3.9

Node.js version

18.19.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

This seems to be a fairly straightforward fix, the schema is being overwritten here if an enum is provided:

if (options.enum) {
const enumValues = getEnumValues(options.enum);
param.schema = {
enum: enumValues,
type: getEnumType(enumValues)
};
}

Maybe we can do it like this?

  if (options.enum) {
    const enumValues = getEnumValues(options.enum);
    param.schema = {
+     ...param.schema,
      enum: enumValues,
      type: getEnumType(enumValues)
    };
  }

This way, if a default value is provided in the schema object, it won't be ignored. This is a not a huge issue since I can just provide the enum directly into the schema, but if my suggestion makes sense, I could submit a PR. Thanks!

@alenap93
Copy link
Contributor

alenap93 commented Dec 29, 2023

Your solution seems to be a good fix

@kamilmysliwiec
Copy link
Member

Would you like to create a PR for this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants