swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.03k stars 6.03k forks source link

[Java] No Import Statement generated for Nested additionalProperties field #9574

Open mkytkajr65 opened 5 years ago

mkytkajr65 commented 5 years ago
Description

I have a return type for a 200 response code that is a nested "additionalProperties" field that generates to Map<String, Map<String, Link>>. Within the generated SDK code, the generator omits the import for the Link class. It omits it for both the API class (WebMvcLinksHandlerApi.java) as well as the associated test class (WebMvcLinksHandlerApiTest.java). As a result, the Java code does not compile without manually adding the import.

Swagger-codegen version

3.0.10

I have also tried other version and it failed in those too.

Swagger declaration file content or url

Nested additional Properties

"additionalProperties":{
   "type":"object",
   "additionalProperties":{
      "$ref":"#/definitions/Link"
   }
}

Nested additional Properties with more JSON context

"/actuator":{
      "get":{
        "tags":[
          "web-mvc-links-handler"
        ],
        "summary":"links",
        "operationId":"linksUsingGET",
        "produces":[
          "application/json",
          "application/vnd.spring-boot.actuator.v2+json"
        ],
        "parameters":[
          {
            "name":"x-vol-site",
            "in":"header",
            "required":false,
            "type":"integer",
            "format":"int32"
          },
          {
            "name":"x-vol-tenant",
            "in":"header",
            "required":true,
            "type":"integer",
            "format":"int32"
          }
        ],
        "responses":{
          "200":{
            "description":"OK",
            "schema":{
              "type":"object",
              "additionalProperties":{
                "type":"object",
                "additionalProperties":{
                  "$ref":"#/definitions/Link"
                }
              }
            }
          },
          "401":{
            "description":"Unauthorized"
          },
          "403":{
            "description":"Forbidden"
          },
          "404":{
            "description":"Not Found"
          }
        },
        "deprecated":false
      }
    }
Command line used for generation

swagger-codegen generate -i swagger.json -l java -c config.json -o tmp/

Steps to reproduce

Generate and compile via mvn clean install

Related issues/PRs

Maybe https://github.com/swagger-api/swagger-codegen/issues/3140 ?

Suggest a fix/enhancement

N/A

Nephthys76 commented 3 years ago

I'm having this exact same issue. I'm using the swagger-codegen-maven-plugin.

The WebMvcLinksHandler uses "Link" in the method signatures, but does not import it.

The objects that are at the package root are imported, but "Link" is under package-root.resource... and is not imported.

When I manually add the import for Link... everything compiles fine.

ccaspanello commented 3 years ago

Just ran across the same issue.

samirshaik commented 2 years ago

What is the issue: No import statement added in the generated file for class "ReservationWindow"

swagger-codegen-plugin

<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>2.4.24</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/vmware-cloud-on-aws-general.json</inputSpec>
                <language>java</language>
                <output>${project.build.directory}/generated-sources</output>
                <apiPackage>com.stg.vmc.handler</apiPackage>
                <modelPackage>com.stg.vmc.model</modelPackage>
                <invokerPackage>com.stg.vmc.handler</invokerPackage>
            </configuration>
        </execution>
    </executions>
</plugin>

Snippet from swagger file that is causing the issue

"200": {
      "description": "Successful response",
      "schema": {
          "additionalProperties": {
              "items": {
                  "$ref": "#/definitions/ReservationWindow"
              },
              "type": "array"
          },
          "type": "object"
      }
  }