swagger-api / swagger-api/swagger-codegen
[JAVA] Nested generics causes missing imports with openapi 3.0
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
When a method is using nested generics in an method argument, like Map<String, List<UUID>>, the generated Api class is missing an import for this model class. In this case 'UUID'.
Swagger-codegen version
3.0.27
Swagger declaration file content or url
definition in controller
@GetMapping(value = "/running", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, List<UUID>>> getAllRunningApps() {
Map<String, List<UUID>> runningApps = diagnosticService.loadRunning();
return ResponseEntity.ok().eTag( HashUtils.hash( runningApps ) ).body( runningApps );
}
Json for code generating:
{
"openapi": "3.0.1",
"info": {
"title": "Server",
"contact": {
"url": "http://google.com",
"email": "info@google.com"
},
"version": "0.0.20-5"
},
"servers": [
{
"url": "http://127.0.0.1:9018",
"description": "Generated server url"
}
],
"paths": {
"/rest-ws/diagnostic/v1/app/running": {
"get": {
"tags": [
"AppsController V1"
],
"summary": "Retrieves all running applications in map with computer name asi key",
"operationId": "getAllRunningApps",
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "object",
"additionalProperties": {
"type": "array",
"items": {
"type": "string",
"format": "uuid"
}
}
}
}
}
},
"413": {
"description": "Payload Too Large",
"content": {
"*/*": {
"schema": {
"type": "object"
}
}
}
},
"422": {
"description": "Unprocessable Entity",
"content": {
"*/*": {
"schema": {
"type": "object"
}
}
}
},
"501": {
"description": "Not Implemented",
"content": {
"*/*": {
"schema": {
"type": "object"
}
}
}
}
}
}
}
}
}
In generated class it is missing import java.util.UUID;
Steps to reproduce
Bug is also reproducible in Swagger Editor
- open https://editor.swagger.io/
- paste given json snapshot into it
- Generate Client and choose java
- Download and extract given project
- open class in AppsControllerV1Api.java in folder src\main\java\io\swagger\client\api
- you will find out, that import java.util.UUID is missing, so compiler is very unhappy
I use swagger-codegen-maven-plugin with very same results when I use resttemplate library.
Related issues/PRs
This issue was probably solved for OpenApi 2 in issue:
https://github.com/swagger-api/swagger-codegen/issues/5885
Suggest a fix/enhancement
Maybe there is missing recursive search in collections for import classes.
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 by reproducing the generated AppsControllerV1Api.java result in Swagger Editor or with the swagger-codegen-maven-plugin using the nested Map<String, List> response. Compare nested import handling with related issue 5885; done when java.util.UUID is emitted and the generated client compiles.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100