swagger-api / swagger-api/swagger-codegen

[JAXRS-SPEC] Enum values are generating """, "&#3D;", imports are being written inside class declaration

Open
#3,856 2 comments 1 reaction 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

I wanted to generate API Definition with the swagger-codegen-maven-plugin, for jaxrs-spec so that I receive only the models and basic API implementations (i.e., no factories). For all models that have an enum however, it produces uncompilable source code.

Swagger-codegen version

The latest stable build, 2.2.1, although I have also tried 2.2.2-SNAPSHOT but it has not made a difference.

Swagger declaration file content or url
  T:
    required:
      - a
      - c
    properties:
      a:
        type: number
        format: double
      c:
        type: string
      state: 
        type: string
        enum:
          - initial
Command line used for generation

mvn clean io.swagger:swagger-codegen-maven-plugin:generate with the following plugin entry in the pom.xml:

            <plugin>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-codegen-maven-plugin</artifactId>
                <version>2.2.1</version>
                <configuration>
                    <inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
                    <language>jaxrs-spec</language>
                    <output>${project.build.directory}/generated-sources/payment</output>
                    <configOptions>
                        <sourceFolder>src/java/main</sourceFolder>
                        <dateLibrary>java8</dateLibrary>
                    </configOptions>
                    <groupId>com.product</groupId>
                    <artifactId>rest</artifactId>
                    <modelPackage>com.product.rest.model</modelPackage>
                    <invokerPackage>com.product.rest.invoker</invokerPackage>
                    <apiPackage>com.product.rest.api</apiPackage>
                </configuration>
                <executions>
                    <execution>
                        <id>generate-server-stubs</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
Steps to reproduce

Attempt to generate server stubs using the model definition above.

The generated source code then looks similar to the following:

package com.product.rest.model;

import java.time.OffsetDateTime;



import io.swagger.annotations.*;
import java.util.Objects;


public class T {

  private Double amount = null;
  private String currency = null;

import javax.xml.bind.annotation.XmlEnum;
import javax.xml.bind.annotation.XmlType;

@XmlType(name="T")
@XmlEnum
public enum T {
    {values&#x3D;[initial], enumVars&#x3D;[{name&#x3D;INITIAL, value&#x3D;&quot;initial&quot;}]}, 

    public String value() {
        return name();
    }

    public static T fromValue(String v) {
        return valueOf(v);
    }
}
  private StateEnum state = null;
...

(Ordering of imports is as-generated)

Related issues

A similar issue was reported for JaxRS-CXF in issue #3735 which was fixed a few weeks ago.

Suggest a Fix

The problem seems to be the mustache template that is used for the JaxRS-SPEC code generation. I attempted a fix but as I am wholly unfamiliar with mustache, did not get very far.

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 reproducing the issue with the jaxrs-spec generator, the supplied YAML model, and the Maven command. Compare the jaxrs-spec Mustache template with the related JaxRS-CXF fix from issue #3735. Done means generated enum models have valid escaping and keep imports outside class declarations so the Java source compiles.

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.