Search code examples
javaswaggeropenapiswagger-codegenopenapi-generator

How to force Swagger Codegen to generate an enum which is not referenced from any other object?


I have an OpenAPI spec with multiple enums:

SocialLinkType:
  type: string
  enum:
    - FACEBOOK
    - YOUTUBE
    - INSTAGRAM
    - TWITTER
    - PINTEREST
    - LINKEDIN
    - TIKTOK
    - TUMBLR
    - WHATSAPP
    - VIMEO
    - SNAPCHAT
    - APPLE
    - ANDROID

ColorType:
  type: string
  enum:
    - ACCENT_1
    - ACCENT_2
    - BACKGROUND_1
    - BACKGROUND_2
    - TEXT
    - SOLID_BUTTON
    - OUTLINE_BUTTON

The issue I'm facing is about ColorType. Unlike SocialLinkType it is not referenced from any other component described in OpenAPI spec. As a result ColorType Java enum is not generated, so my question is about forcing Swagger Codegen to generate Java code for every item in the spec.

Can this be achieved somehow?


Solution

  • Finally I've solved the issue by moving the declaration of ColorType into top-level components section. Prior to fix I had only securitySchemes declaration under the section:

    components:
      securitySchemes:
        bearer:
          type: http
          scheme: bearer
    

    I've changed it to

    components:
      schemas:
        ColorType:
          type: string
          enum:
            - ACCENT_1
            - ACCENT_2
            - BACKGROUND_1
            - BACKGROUND_2
            - TEXT
            - SOLID_BUTTON
            - OUTLINE_BUTTON
      securitySchemes:
        bearer:
          type: http
          scheme: bearer
    

    This gave me desired Java enum among generated classes:

    public enum ColorType {
      ACCENT_1("ACCENT_1"),
      ACCENT_2("ACCENT_2"),
      BACKGROUND_1("BACKGROUND_1"),
      BACKGROUND_2("BACKGROUND_2"),
      TEXT("TEXT"), 
      SOLID_BUTTON("SOLID_BUTTON"),
      OUTLINE_BUTTON("OUTLINE_BUTTON");
    
      private String value;
    
      ColorType(String value) {
        this.value = value;
      }
    
      @Override
      @JsonValue
      public String toString() {
        return String.valueOf(value);
      }
    
      @JsonCreator
      public static ColorType fromValue(String value) {
        for (ColorType b : ColorType.values()) {
          if (b.value.equals(value)) {
            return b;
          }
        }
        throw new IllegalArgumentException("Unexpected value '" + value + "'");
      }
    }