swagger-api / swagger-api/swagger-codegen
[Java] Elision of underscore characters in property names leads to invalid Java
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
The swagger file, being generated from an external model contains some property names that contain underscores e.g. "CHT_Provider". It also contains some other property names that are identical once the underscores are removed e.g. "CHTProvider".
Swagger-codegen version
λ java -jar swagger-codegen\snapshot\swagger-codegen-cli-2.2.2-SNAPSHOT.jar version
2.2.2-SNAPSHOT
NOTE:
Also replicates with 2.2.1
Swagger declaration file content or url
Below see an edited version of the swagger file, removed all paths and not required definitions
{
"swagger": "2.0",
"info": {
"version": "v2",
"title": "StayinFront.WebAPI V2"
},
"host": "localhost",
"basePath": "/revapi",
"schemes": [
"http"
],
"paths": {},
"definitions": {
"ClassInstanceBaseModel": {
"description": "Base data for a class instance",
"type": "object",
"properties": {
"class": {
"description": "The class name",
"type": "string"
}
}
},
"enums_CHTProvider": {
"description": "CHTProvider",
"type": "object",
"properties": {
"description": {
"description": "description",
"type": "string"
},
"data": {
"enum": [
"MOX",
"RC"
],
"type": "string"
}
}
},
"CHT_Configuration_Data": {
"type": "object",
"properties": {
"CHTProvider": {
"$ref": "#/definitions/enums_CHTProvider"
},
"CHT_Provider": {
"$ref": "#/definitions/ClassInstanceBaseModel"
}
}
}
}
}
Using the below command line to generate code, the following Java code is generated for the CHTConfigurationData class:
...
/**
* CHTConfigurationData
*/
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2017-02-16T11:28:41.907+13:00")
public class CHTConfigurationData {
@SerializedName("CHTProvider")
private EnumsCHTProvider cHTProvider = null;
@SerializedName("CHT_Provider")
private ClassInstanceBaseModel cHTProvider = null;
...
}
Obviously this fails to compile due to the duplicate member names. Adding underscore characters back into the generated Java in appropriate places seems to resolve the compilation errors.
I would expect that the underscores weren't removed, but failing that could an option be added to avoid this behaviour?
Command line used for generation
java -jar swagger-codegen\snapshot\swagger-codegen-cli-2.2.1.jar generate -i swagger-file.json -l java -o output-dir
Steps to reproduce
- generate Java source using the above command line
- attempt to compile
Related issues
Here is a similar issue relating to changing of case in property names:
https://github.com/swagger-api/swagger-codegen/issues/4066
Here is a similar issue in PHP language generation:
https://github.com/swagger-api/swagger-codegen/issues/4551
Here is a similar issue in node javascript code gen - there is a configuration option to disable the behaviour in this case:
https://github.com/swagger-api/swagger-codegen/issues/2766
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
Use the supplied Swagger definition and Java generation command to reproduce the duplicate cHTProvider fields and resulting compilation failure. Inspect the Java code-generation path that converts property names, then verify the generated CHTConfigurationData fields remain distinct and the output compiles without removing meaningful underscores.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100