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
16.78k stars 6.02k forks source link

[JAVA] --type-mappings option not working in operation parameters #10229

Open pablo-ml opened 4 years ago

pablo-ml commented 4 years ago
Description

If you use --type-mappings Pet=MyPet to generate the java source code, the type Pet is no replaced with MyPet in operation parameters. It works correctly in return values and references between model objects.

Swagger-codegen version

I tried this in swagger-codegen-generators master as of Sat May 2, 2020, and also in codegen maven plugin 3.0.8 and 3.0.19.

Swagger declaration file content or url
{
  "openapi": "3.0.0",
  "info": {
    "title": "Swagger Petstore",
    "version": "1.0.0"
  },
  "paths": {
    "/pet": {
      "post": {
        "tags": [
          "pet"
        ],
        "operationId": "addPet",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Pet"
              }
            }
          }
        },
        "responses": {
          "200": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Pet"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Pet": {
        "properties": {
          "name": {
            "type": "string"
          }
        }
      }
    }
  }
}
Command line used for generation

The arguments used to the command line client:

java -jar swagger-codegen-cli.jar generate --type-mappings Pet=MyPet -i ../../modules/swagger-codegen/src/test/resources/3_0_0/petstore.json -o target/debug-generated -l spring
Steps to reproduce

You will notice in the generated PetApiController class the following operation:

public ResponseEntity<MyPet> addPet(@ApiParam(value = ""  )  @Valid @RequestBody Pet body) {

.... }

The return value is as expected, a MyPet, but the parameter is Pet instead of MyPet.

Related issues/PRs

Could not find any.

Suggest a fix/enhancement

I fixed it locally for this case with:

  --- a/src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java
  +++ b/src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java
  @@ -1152,6 +1152,9 @@ public abstract class DefaultCodegenConfig implements CodegenConfig {
        */
       @SuppressWarnings("static-method")
       public String getTypeDeclaration(String name) {
  +        if (typeMapping.containsKey(name)) {
  +            return typeMapping.get(name);
  +        }
           return name;
       }

As you can see, I just copied the logic from the operation:

public String getTypeDeclaration(Schema schema)

I don't know if this is the correct fix.

pablo-ml commented 4 years ago

I realized that the bug is from swagger-codegen-generators instead of this project.

pablo-ml commented 4 years ago

My suggestion for the fix did not work. I cannot find a clear way to add the mapping to the body parameter.