swagger-api / swagger-api/swagger-codegen-generators
[JAVA] --type-mappings option not working in operation parameters
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 299
- Forks
- 439
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java and compare the two getTypeDeclaration methods. Reproduce the generation command with the Petstore declaration and inspect the generated PetApiController. Done means --type-mappings Pet=MyPet changes the operation parameter type as well as the return type.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100