swagger-api / swagger-api/swagger-codegen

jaxrs/jersey2: enums not serialized correctly

Open
#6,311 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

If using the automatically generated code, eg. a customer with SourceType "software b" is rendered in JSON as SOFTWARE_B and not as "software b" (see yaml below)
The Problem seems to be, that in the generated class JacksonJsonProvider
the following is missing:
.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING)//

Swagger-codegen version

2.2.3

Swagger declaration file content or url

swagger: '2.0'
info:
  title: test API
  description: "test"
  version: "1.0"
  contact:
    name: "test"

# the domain of the service
host: test.com
# array of all schemes that your API supports
schemes:
  - https
# will be prefixed to all paths
basePath: /basePath
produces:
  - application/json

paths:
  /:
    get:
      summary: get customer
      description: test
      parameters:
      tags:
        - customers
      responses:
        200:
          description: customer found
          schema:
            $ref: '#/definitions/customer'

definitions:

  customer:
    type: object
    description: customer
    properties:
      sourceSystem:
        $ref: '#/definitions/SourceType'



  SourceType:
    type: string
    description: The source of the customer
    enum:
      - "a software"
      - "software b"
      - "yet another software"
      - "GOODPRODUCT"


Command line used for generation

generated using the swagger gradle plugin:
org.hidetake.swagger.generator' version '2.4.2'

Steps to reproduce

generate server code with gradle plugin, then eg. add the following code in the generated class DefaultApiServiceImpl:

 @Override
    public Response rootGet(SecurityContext securityContext) throws NotFoundException {
        // do some magic!
        Customer c = new Customer();
        c.setSourceSystem(SourceType.SOFTWARE_B);
        return Response.ok().entity(c).build();
    }

a then running server will return the following JSON:

{
    "sourceSystem": "SOFTWARE_B"
}

instead of

{
    "sourceSystem": "software b"
}
Related issues/PRs
Suggest a fix/enhancement

The Problem seems to be, that in the generated class JacksonJsonProvider
the following is missing:
.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING)//

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 by locating the Jersey2 server template that generates JacksonJsonProvider and inspect how generated enums are serialized. Reproduce the issue with the supplied Swagger definition and generated Customer model, then add coverage showing that the declared enum value is preserved in JSON and verify the generated server tests pass.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.