swagger-api / swagger-api/swagger-codegen

[JAVA] codegen with negative integer enum values compile error

Open
#4,780 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Java Feature: Enum help wanted Issue: Workaround available
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

I have an enum in my swagger doc:

  enums/TimeValidationZone:
    description: TimeValidationZone
    type: object
    properties:
      description:
        description: description
        type: string
      data:
        enum:
          - '-720'
          - '-660'
          - '-600'
          - '-540'
          - '-480'
          - '-420'
          - '-360'
          - '-300'
          - '-240'
          - '-210'
          - '-180'
          - '-120'
          - '-60'
          - '0'
          - '60'
          - '120'
          - '180'
          - '210'
          - '240'
          - '270'
          - '300'
          - '330'
          - '345'
          - '360'
          - '390'
          - '420'
          - '480'
          - '540'
          - '570'
          - '600'
          - '660'
          - '720'
          - '780'
        type: string

The salient point being that there is negative values with the same absolute value e.g. -60 and 60.

This generates invalid Java code:

@ApiModel(description = "TimeValidationZone")
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2017-02-13T15:17:56.985+13:00")
public class EnumsTimeValidationZone {
  @SerializedName("description")
  private String description = null;

  /**
   * Gets or Sets data
   */
  public enum DataEnum {

    ...

    @SerializedName("-60")
    _60("-60"),
    
    @SerializedName("0")
    _0("0"),
    
    @SerializedName("60")
    _60("60"),

    ...
Swagger-codegen version
λ java -jar swagger-codegen\modules\swagger-codegen-cli\target\swagger-codegen-cli.jar version
2.2.2-SNAPSHOT
Swagger declaration file content or url

See above for relevant portion.

Command line used for generation

java -jar swagger-codegen\modules\swagger-codegen-cli\target\swagger-codegen-cli.jar generate -i http://localhost/revapi/swagger/docs/v2 -l java -o .\swagger-java-client\

Steps to reproduce

Create swagger file with enum type as declared above, run codegen.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the supplied Swagger declaration and Java generation command, then inspect the enum-name generation used by io.swagger.codegen.languages.JavaClientCodegen. Done means the generated DataEnum has distinct Java constants for values such as -60 and 60 and compiles successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.