swagger-api / swagger-api/swagger-codegen

[JAVA] Bug adding description to object definition includes unused io.swagger.annotations.ApiModel in generated code

Open
#6,965 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

We're using the maven plugin and the jaxrs-cxf-client generator and custom templates without any swagger annotation classes to generate API interfaces and pojos. When a description is added to an object definition like below, ApiModel is included in the imports, but never referenced in the generated code.

I've tried to use the importMappings and typeMappings configuration options but they only seem to be inclusive.

Swagger-codegen version

2.3.3, 2.3.0-SNAPSHOT, 3.0.0-SNAPSHOT

Swagger declaration file content or url

from petstore.yaml:

...
definitions:
  Order:
    title: Pet Order
    description: An order for a pets from the pet store
...

pojo.mustache

public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {
  {{#vars}}{{#isEnum}}{{^isContainer}}

{{>enumClass}}{{/isContainer}}{{/isEnum}}{{#items.isEnum}}{{#items}}

{{^isContainer}}{{>enumClass}}{{/isContainer}}{{/items}}{{/items.isEnum}}
    private {{{datatypeWithEnum}}} {{name}} = {{{defaultValue}}};{{/vars}}

  {{#vars}}
    /**
  {{#description}}
     * {{description}}
  {{/description}}
  {{^description}}
     * Get {{name}}
  {{/description}}
  {{#minimum}}
     * minimum: {{minimum}}
  {{/minimum}}
  {{#maximum}}
     * maximum: {{maximum}}
  {{/maximum}}
     * @return {{name}}
    **/
{{#vendorExtensions.extraAnnotation}}
  {{{vendorExtensions.extraAnnotation}}}
{{/vendorExtensions.extraAnnotation}}
{{#useBeanValidation}}{{>beanValidation}}{{/useBeanValidation}}    public {{{datatypeWithEnum}}} {{getter}}() {
      return {{name}};
    }

  {{^isReadOnly}}
    public void {{setter}}({{{datatypeWithEnum}}} {{name}}) {
      this.{{name}} = {{name}};
    }

  {{#isListContainer}}

    public {{classname}} add{{nameInCamelCase}}Item({{{items.datatypeWithEnum}}} {{name}}Item) {
      this.{{name}}.add({{name}}Item);
      return this;
    }
  {{/isListContainer}}
  {{#isMapContainer}}

    public {{classname}} put{{nameInCamelCase}}Item(String key, {{{items.datatypeWithEnum}}} {{name}}Item) {
      this.{{name}}.put(key, {{name}}Item);
      return this;
    }
  {{/isMapContainer}}
  {{/isReadOnly}}

  {{/vars}}

}
Command line used for generation

java -jar swagger-codegen-cli.jar generate -i petstore.yaml -l jaxrs-cxf-client -t .

Steps to reproduce
  1. cp swagger-codegen/master/modules/swagger-codegen/src/test/resources/2_0/petstore.yaml .
  2. cp pojo.mustache .
  3. generate the code using standard templates: java -jar swagger-codegen-cli.jar generate -i petstore.yaml -l jaxrs-cxf-client -t .
  4. head -3 ./src/gen/java/io/swagger/model/Pet.java
package io.swagger.model;

import io.swagger.annotations.ApiModel;

ApiModel is not used anywhere in the code. Compared to generating with standard templates, all imports are gone, except ApiModel:
java -jar swagger-codegen-cli.jar generate -i petstore.yaml -l jaxrs-cxf-client

Related issues/PRs

Not found

Suggest a fix/enhancement

ApiModel should not be imported unless used in the generated file

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 petstore.yaml, the custom pojo.mustache, and the jaxrs-cxf-client generation command. Compare the imports in the generated Pet.java with output from the standard templates; done means ApiModel is absent when the generated class does not reference it, while required imports remain.

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
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.