swagger-api / swagger-api/swagger-codegen

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

Open
#10,229 2 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

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.

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 in src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java, comparing getTypeDeclaration(String) with getTypeDeclaration(Schema). Reproduce with the supplied generate command and inspect the generated PetApiController; done means --type-mappings Pet=MyPet changes operation parameters as well as return values.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.