swagger-api / swagger-api/swagger-codegen
Uppercase property names lead to java code that produces wrong json.
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
If the swagger declaration contains properties with names containing only uppercase letters and underscores, the generated java code is not java bean conform. As a result jackson maps the properties twice. This actually is part of #4051 but the code currently handles this case earlier.
Swagger-codegen version
Tested with 2.2.1 and 2.2.2-SNAPSHOT
Swagger declaration file content or url
swagger: '2.0'
schemes:
- http
basePath: '/v0.1/foo'
consumes:
- 'application/json'
produces:
- 'application/json'
paths:
'/foo':
get:
responses:
200:
description: "successful operation"
schema:
type: "array"
items:
$ref: "#/definitions/Pet"
definitions:
Pet:
description: >
Pet
type: object
properties:
ATT_NAME:
description: >
Name of the pet
type: string
Command line used for generation
swagger-codegen-maven-plugin
Steps to reproduce
Build a jaxrs or spring server. Implement controller to return Pet array.
Or use jackson mapper and serialize with the generated Pet model code.
Related issues
https://github.com/swagger-api/swagger-codegen/issues/4051
Suggest a Fix
AbstractJavaCodegen
@Override
public String toVarName(String name) {
// sanitize name
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
if ("class".equals(name.toLowerCase())) {
return "propertyClass";
}
if("_".equals(name)) {
name = "_u";
}
// if it's all uppper case, do nothing
// if (name.matches("^[A-Z_]*$")) {
// return name;
// }
// camelize (lower first character) the variable name
// pet_id => petId
name = camelize(name, true);
// for reserved word or word starting with number, append _
if (isReservedWord(name) || name.matches("^\\d.*")) {
name = escapeReservedWord(name);
}
return name;
}
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 AbstractJavaCodegen.toVarName, using the supplied ATT_NAME Swagger declaration and the generated Pet model as the reproduction case. Run generation through the swagger-codegen-maven-plugin, then serialize the generated model with Jackson. Done means the uppercase property is Java-bean conform and is mapped only once in the resulting JSON.
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
- 42/100