OpenAPITools / OpenAPITools/openapi-generator

[Bug][Java] jaxrs-cxf-client missing annotation when generating Enum

Open
#5,077 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

Integer Enums are generated with missing @JsonCreator annotation and causing InvalidFormatException while deserialization.

com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `package.class$CodeEnum` from number 3: index value outside legal index range [0..2]

The Enum is defined for values {1, 2, 3} but the deserialization is trying to work with the index, so it is wrong for values 1 and 2 and fails for value 3.

openapi-generator version

4.2.2

OpenAPI declaration file content or url

yaml file:

components:
  schemas:
    CodeResponse:
      type: object
      required: [isValid]
      properties:
        isValid:
          type: boolean
        serial:
          type: string
          pattern: /^[abcdefghkmnpqrstuvwxyz23456789]{7,}$/i
        discount:
          type: integer
          format: int32
          minimum: 0
        reason:
          type: object
          required: [code, message]
          properties:
            code:
              type: integer
              enum: [1, 2, 3]
            message:
              type: object
              required: [en, de]
              properties:
                en:
                  type: string
                de:
                  type: string

generated java code:

@XmlType(name="CodeEnum")
@XmlEnum(Integer.class)
public enum CodeEnum {

@XmlEnumValue("1") NUMBER_1(Integer.valueOf(1)), @XmlEnumValue("2") NUMBER_2(Integer.valueOf(2)), @XmlEnumValue("3") NUMBER_3(Integer.valueOf(3));


    private Integer value;

    CodeEnum (Integer v) {
        value = v;
    }

    public Integer value() {
        return value;
    }

    @Override
    public String toString() {
        return String.valueOf(value);
    }

    public static CodeEnum fromValue(Integer value) {
        for (CodeEnum b : CodeEnum.values()) {
            if (b.value.equals(value)) {
                return b;
            }
        }
        throw new IllegalArgumentException("Unexpected value '" + value + "'");
    }
}
Command line used for generation

Gradle task:

  group = "build"
  description = "..."
  generatorName = "jaxrs-cxf-client"
  inputSpec = "...".toString()
  outputDir = "...".toString()
  apiPackage = "..."
  invokerPackage = "..."
  modelPackage = "..."
  generateModelTests = false
  generateModelDocumentation = false
  generateApiTests = false
  validateSpec = false
  withXml = false
  configOptions = [
      dateLibrary        : "java8",
      booleanGetterPrefix: "is"
  ]
Steps to reproduce
  • deserialize respsonse object like:
{
  "isValid": false,
  "reason": {
    "code": 3,
    "message": {
      "en": "...",
      "de": "..."
    }
  }
}
Related issues/PRs
Suggest a fix/enhancement

Adding @JsonCreator annotation to generated fromValue() method:

    @JsonCreator
    public static CodeEnum fromValue(Integer value) {
        for (CodeEnum b : CodeEnum.values()) {
            if (b.value.equals(value)) {
                return b;
            }
        }
        throw new IllegalArgumentException("Unexpected value '" + value + "'");
    }

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

Start with the jaxrs-cxf-client generator and the generated CodeEnum shown in the report; trace how integer enums are deserialized and how fromValue is emitted. Reproduce the YAML and JSON examples, then add coverage showing that numeric values 1, 2, and 3 deserialize correctly through the generated enum.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
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.