Why does Swagger Codegen convert an ON/OFF string enum to TRUE/FALSE?
Asked Answered
I

1

7

I am using Swagger Codegen 3.0.19, also tried OpenAPI Generator 4.0.3.

Java Environment:

Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

Runner:

java -jar ./libs/openapi-generator-cli-4.3.0.jar  generate \
       -i pet.yaml \
       -g spring \
       -o ./OUTPUT/api/

Here's my OpenAPI Schema:

openapi: "3.0.0"
....
CaptureStatus:
  type: object
  description: Holds current capture status.
  properties:
    status:
      type: string
      enum:
        - ON
        - OFF
      description: Capture status values.
....

Output is:

  ....
  public enum StatusEnum {
    TRUE("true"),

    FALSE("false");

    private String value;

    StatusEnum(String value) {
      this.value = value;
    }
  ....

Why does the codegen translate the ON/OFF enum to TRUE/FALSE? It does not do it when I generate using Swagger Editor GUI.

Integrity answered 11/4, 2020 at 13:34 Comment(0)
M
9

In YAML 1.1, on, off, y, n, yes and no are boolean values. This was changed in YAML 1.2, this version no longer considers these values as booleans.

It looks like Swagger Codegen and OpenAPI Generator use YAML 1.1 parsers. In this case you need to enclose 'ON' and 'OFF' in quotes to treat them as strings:

      enum:
        - 'ON'
        - 'OFF'
Marieann answered 11/4, 2020 at 14:43 Comment(2)
It worked but still curious why it works from swagger editor gui and not from command line. Anyways i have enough to continue now. Thank you.Integrity
Swagger Editor converts YAML to JSON before sending it to the codegen. Editor uses YAML 1.2 parser so it translates enum: [ON, OFF] to "enum": ["ON", "OFF"] (i.e. string enum, not boolean enum) without having to add quotes to the original values. That's why the editor's codegen produces correct result.Marieann

© 2022 - 2024 — McMap. All rights reserved.