OpenAPITools / OpenAPITools/openapi-generator
[BUG] Java codegen generates incorrect example for collections of objects in ApiModelProperty annotation
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
For the following sample model:
{
"swagger": "2.0",
"definitions": {
"sampleDef": {
"type": "object",e
"properties": {
"type": {
"type": "string"
},
"value": {
"type": "string"
}
}
},
"account": {
"type": "object",
"properties": {
"samples": {
"type": "array",
"items": {
"$ref": "#/definitions/sampleDef"
},
"example": [
{
"type": "sample",
"value": "some value"
}
]
}
}
}
}
the generated annotation is:
@ApiModelProperty(example = "[{type=sample, value=some value}]", value = "")
which leads to a malformed json (swagger.json generated by, for example, Springfox), for example:
JSON:
{
"type":"array",
"example":[
{
type=sample,
value=some value
}
],
"items":{
"$ref":"#/definitions/sampleDef"
}
}
YAML:
samples:
type: array
example:
- type=sample: null
value=some value: null
I think it the correct example should be:
@ApiModelProperty(example = "[{type: sample, value: some value}]", value = "")
which would give
samples:
type: array
example:
- type: sample
value: some value
I believe the issue exists in toExampleValue method in DefaultCodegen class which simply calls toString method on an object returned by schema.getExample() which in this case is java.util.ArrayList instance containing one java.util.LinkedHashMap object, which contains two key value pairs:
"type" -> "sample"
"value" -> "some value"
Using json serialization instead of toString could be a solution. Serializing would give
[{"type":"sample","value":"some value"}]
instead of
[{type=sample, value=some value}]
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 toExampleValue method in the DefaultCodegen class and reproduce the supplied Swagger 2.0 model. Inspect the generated ApiModelProperty example for the collection of objects; done means the example preserves valid JSON object syntax instead of Java collection and map toString output.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- api, devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100