swagger-api / swagger-api/swagger-codegen

jaxrs-resteasy server library generates broken string enums from definitions section of swagger file

Open
#11,042 0 comments 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

Generating a server with the jaxrs-resteasy library via the swagger-cli interface results in a broken enum class with undesired characters that prevent correct building of the final artifact, here is an example of the result (this happens with all the enums of this swagger and the result is always the same).

package it.fastweb.oss.tmf641.services.model;

import java.util.Objects;
import java.util.ArrayList;
import io.swagger.v3.oas.annotations.media.Schema;
import com.fasterxml.jackson.annotation.JsonValue;
import javax.validation.constraints.*;
public enum ServiceOrderStateType {
    {values=[acknowledged, rejected, pending, held, inProgress, cancelled, completed, failed, partial, assessingCancellation, pendingCancellation], enumVars=[{name=ACKNOWLEDGED, value="acknowledged"}, {name=REJECTED, value="rejected"}, {name=PENDING, value="pending"}, {name=HELD, value="held"}, {name=INPROGRESS, value="inProgress"}, {name=CANCELLED, value="cancelled"}, {name=COMPLETED, value="completed"}, {name=FAILED, value="failed"}, {name=PARTIAL, value="partial"}, {name=ASSESSINGCANCELLATION, value="assessingCancellation"}, {name=PENDINGCANCELLATION, value="pendingCancellation"}]}
}
Swagger-codegen version

swagger-codegen-cli-3.0.25

Swagger declaration file content or url

Here is the enum declaration in the swagger yaml (Open API 3.0.1) file:

components:
  schemas:
    ServiceOrderStateType:
      type: string
      description: Possible values for the state of the order
      enum:
        - acknowledged
        - rejected
        - pending
        - held
        - inProgress
        - cancelled
        - completed
        - failed
        - partial
        - assessingCancellation
        - pendingCancellation
Command line used for generation

java -jar swagger-codegen-cli-3.0.25.jar generate -i /root/Desktop/WebServices/TMF641_Mobile5G/TMF641-Service_Ordering-v4.0.0.swagger.yaml -l jaxrs-resteasy -o /root/Desktop/WebServices/TMF641_Mobile5G/server_resteasy/ -c /root/Desktop/WebServices/TMF641_Mobile5G/config.json

Steps to reproduce

Generating the server stub results in the bad classes corresponding to all enum types.

Related issues/PRs

Same problem was found in this issue in 2018 (never solved apparently) #7606

Suggest a fix/enhancement

A user in the issue #7606 suggested that the problem might be in the mustache template files, I also have another web service that I personally wrote the swagger for. In that swagger I used enums as an attribute of a field in a schema, and that was generated correctly. Maybe the problem presents itself when we try to generate an enum declared at schema level like the one that I'm showing in this issue.

I think that people could fix the problem by changing the mustache template, but I also think that it is important that the "out of the box" templates should work fine in this type of simple situations (maybe for users that are less expert in this kind of templating operations).

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 swagger-codegen-cli-3.0.25 using the provided OpenAPI 3.0.1 schema, the jaxrs-resteasy generator, and the shown command. Compare schema-level enum output with an enum used as a schema field, then inspect the jaxrs-resteasy Mustache templates; done means generated enum classes contain valid Java and build correctly.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.