swagger-api / swagger-api/swagger-codegen
[JAVA SPRING] String parameter with format uuid not generated as java.util.UUID
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
Similar issue as explained in the related issue. String properties and containered type (like List<?>) with format uuid are generated properly, but parameters of type string and format uuid are not.
Swagger-codegen version
3.0.3
Swagger declaration file content or url
swagger: "2.0"
info:
description: "This is a sample server..."
version: "1.0.0"
title: "Swagger Petstore"
termsOfService: "http://swagger.io/terms/"
contact:
email: "apiteam@swagger.io"
license:
name: "Apache 2.0"
url: "http://www.apache.org/licenses/LICENSE-2.0.html"
host: "petstore.swagger.io"
basePath: "/v2"
schemes:
- "https"
- "http"
paths:
'/operators/{operatorId}/watchlist/add':
post:
tags:
- Products
parameters:
- name: operatorId
in: path
description: ID of operator
required: true
type: string
format: uuid
- name: productIds
in: body
description: array of product Ids
required: true
schema:
type: array
items:
type: string
format: uuid
responses:
'200':
description: Added successfuly
Generated code
default ResponseEntity<Void> _operatorsOperatorIdWatchlistAddPost(@ApiParam(value = "array of product Ids" ,required=true ) @Valid @RequestBody List<UUID> body,@ApiParam(value = "ID of operator",required=true) @PathVariable("operatorId") String operatorId) {
return operatorsOperatorIdWatchlistAddPost(body,operatorId);
}
Related issues/PRs
https://github.com/swagger-api/swagger-codegen/issues/1806#issuecomment-449035297
Workaround
I am currently using a custom generator to fix that issue.
It's quite naive solution since it adds the import to all api interfaces no matter if it's required in any of the methods.
package generators
import io.swagger.codegen.v3.CodegenParameter
import io.swagger.codegen.v3.generators.java.SpringCodegen
import java.util.regex.Pattern
class ExtendedSpringCodegen extends SpringCodegen {
void postProcessParameter(CodegenParameter parameter) {
if (Objects.isNull(parameter.dataType) || Objects.isNull(parameter.dataFormat)) {
return
}
if (parameter.dataType.equalsIgnoreCase("string")) {
if (parameter.dataFormat.equalsIgnoreCase("uuid")) {
parameter.dataType = "UUID"
}
}
}
Map<String, Object> postProcessOperations(Map<String, Object> objs) {
objs = super.postProcessOperations(objs)
List<Map<String, String>> imports = (List<Map<String, String>>) objs.get("imports")
Pattern pattern = Pattern.compile("java\\.util\\.UUID")
for (Iterator<Map<String, String>> itr = imports.iterator(); itr.hasNext();) {
String _import = itr.next().get("import")
if (pattern.matcher(_import).matches()) {
return objs
}
}
imports.add(new HashMap<String, String>() {{ put("import", "java.util.UUID") }})
return objs
}
}
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 with the SpringCodegen entry points shown in the workaround, especially postProcessParameter and postProcessOperations, and reproduce the issue using the provided Swagger 2.0 YAML. Compare the generated path parameter with the UUID body element; done means a string parameter with format uuid is generated as java.util.UUID with the required import.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi, spring
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100