Skip to content

Support default in schema.$ref for enums #3454

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

Open
1 task done
chiragjn opened this issue May 20, 2025 · 0 comments
Open
1 task done

Support default in schema.$ref for enums #3454

chiragjn opened this issue May 20, 2025 · 0 comments
Labels

Comments

@chiragjn
Copy link

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

I have gone through #2898 and #1041

My code looks like this

export enum LogsSearchOperatorType {
  EQUAL = 'equal',
  NOT_EQUAL = 'not_equal',
}

export class GetApplicationLogsQuery {
   ...
   @ApiPropertyOptional({
    enum: LogsSearchOperatorType,
    enumName: 'LogsSearchOperatorType',
    default: LogsSearchOperatorType.EQUAL,
    enumSchema: {
      description: 'Comparison operator logs search',
    },
    description: 'Comparison operator for filter. `equal` or `not_equal`',
  })
  @IsOptional()
  searchOperator?: LogsSearchOperatorType = LogsSearchOperatorType.EQUAL;
}

GetApplicationLogsQuery is used as query params in a route.

My goal is to have a default on this enum in this specific context.

While enumSchema has a field default, I do not want to declare the default there because this enum is shared across multiple places.

OAS 3.1.0 now allows default in schema as sibling of $ref

but nestjs/swagger removes all such fields in here:

return this.omitParamKeys(param);

And I get

{
            "name": "searchOperator",
            "required": false,
            "in": "query",
            "description": "Comparison operator for filter. `equal` or `not_equal`",
            "schema": {
              "$ref": "#/components/schemas/LogsSearchOperatorType"
            }
}

Example OAS 3.1 spec that can be tried at https://editor-next.swagger.io/

openapi: "3.1.0"
info:
  title: Enum default
  version: '0.1.0'
paths:
  /products:
    get:
      parameters:
        - in: query
          name: color
          required: false
          schema:
            $ref: "#/components/schemas/Color"
            default: "black"
      responses:
        "200":
          description: OK
components:
  schemas:
    Color:
      type: string
      enum:
        - black
        - white
        - red
        - green
        - blue

Describe the solution you'd like

Take the default from @ApiProperty and place it in schema

Teachability, documentation, adoption, migration strategy

No response

What is the motivation / use case for changing the behavior?

Correct defaults for downstream docs and SDK generation

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

No branches or pull requests

1 participant