swagger-api / swagger-api/swagger-codegen

Enum JsonCreator without mode=DELEGATING failed when using jackson >= 2.11

Open
#11,740 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Currently when we have enum class generated, Deserialize will failed, error:
threw exception [Request processing failed; nested exception is feign.codec.DecodeException: Error while extracting response for type [x.y.z.a.b.c.class] and content type [application/vnd.x.y.z.v1+json]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Input mismatch reading Enum x.y.z.a.b.c.enum: properties-based @JsonCreator ([method x.y.z.a.b.c.enum#fromValue(class name)]) expects JSON Object (JsonToken.START_OBJECT), got JsonToken.VALUE_STRING; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Input mismatch reading Enum x.y.z.a.b.c.enum: properties-based @JsonCreator ([method x.y.z.a.b.c.enum#fromValue(x.y.z.a.b.c.enum)]) expects JSON Object (JsonToken.START_OBJECT), got JsonToken.VALUE_STRING

this one can refer to https://github.com/FasterXML/jackson-module-kotlin/issues/336

Swagger-codegen version

3.0.25

Swagger declaration file content or url
Command line used for generation
Steps to reproduce

any class that contain enum and called Deserialize error will throw

Related issues/PRs
Suggest a fix/enhancement

the simple fix will be change the mustache
https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/Java/modelEnum.mustache
change
{{#jackson}}
@JsonCreator
{{/jackson}}
public static {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}
.......
to
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
{{/jackson}}
public static {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}

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 modules/swagger-codegen/src/main/resources/Java/modelEnum.mustache and review the enum creator template described in the issue. Verify the generated enum can deserialize a JSON string with Jackson 2.11 or newer; the issue is done when the generated creator uses the requested delegating mode.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.