Swagger Annotation for enum as a component

Viewed 1704

I have an enum:

public enum UserType {
  ADMIN("Admin"),
  GUEST("Guest"),
  SUPERVIOR("Supervisor"),
  NORMAL("Normal");

  private final String type;

  UserType(final String type) {
    this.type = type;
  }

And I'm using it as a query parameter with Swagger annotation:

@GET
@AsyncTimed
@Path("/all")
void all(
    @ApiParam @PathParam(USER_ID) @Parameter(in = ParameterIn.PATH, name = USER_ID, required = true) final UserId userId,
    @QueryParam(TYPES) final Set<UserType> userTypes,
    @Suspended final AsyncResponse asyncResponse
);

The generated OpenAPI file, however, is not creating a component out of the enum and instead is giving:

get:
  parameters:
  - name: UserId
    in: path
    required: true
    schema:
      $ref: '#/components/schemas/UserId'
  - name: Types
    in: query
    schema:
      uniqueItems: true
      type: array
      items:
        type: string
        enum:
          - ADMIN
          - GUEST
          - SUPERVISOR
          - NORMAL

Is there a way to make the enum a component schema instead? If I use this enum in other components, those components also use enum: instead of a $ref.

1 Answers

You need to annotate the enum and/or the corresponding parameter with @Schema(..., enumAsRef=true).

Related