-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Java] Elision of underscore characters in property names leads to invalid Java #4805
Comments
I've tested a simple code change that seems to do what I want. Just for testing purposes, I commented out the code that removes underscores in
|
Same for typescript-angular2:
generates:
Completely unusable with this bug. As from generated interface is created json with wrong keys. |
@Ondra09 please generate TS Angular2 from
Ref: |
@gwynjudd For the following model definition:
I assume the property names "CHTProvider" and "CHT_Provider" cannot be changed. One workaround is to handle the special case in https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractJavaCodegen.java#L450 |
With modelPropertyNaming=original is everything working. This is often discussed in other threads too, now I can see it... It would be nice to have defaults set differently for different languages. Thank you for great answer. |
@Ondra09 can you also try generating the client from 2.3.0 branch to see if the deserialization logic works for you? Ideally we don't want developers changing the model property naming as we want model property naming conforming to the language style guide (e.g. camelCase for Typescript). |
Same on 2.3.0 branch. When is property renamed it will be automatically serialized with renamed name, instead of with correct one. |
@wing328 yes that's correct I have no way to modify the property or object names |
Just want to make sure. You've done a |
@gwynjudd understood. Have you tried the workaround I mentioned above? Would that help?
|
@wing328 do you mean the "if it's all uppper case, do nothing" bit? It wouldn't apply in my case because a number of classes do not match the regex:
|
@wing328 the typescript generator does not have something similar like the java one which does the naming convention matching via annotations like |
It was clean build. First try was from binaries obtained through brew. |
The problem is that both cHTProvider and CHT_Provider are mapped to the same name (cHTProvider) while maintaining a different baseName. Is that correct? I don't think that this is a bug with the Serializer/Deserializer. The serializer/deserializer assumes that a property appears only once in a class (and not twice because two properties are mapped to the same name). This is a part of the code which is generated based on the MWE in the first comment and illustrates the issue:
The problem is caused by the Java-Code generating the mapping for properties. It produces a mapping which maps two properties to the same name. Two possible solutions would be:
|
@TiFu I think the problem is that codegen generates code that can't compile because of the removal of underscores. The problem arises when underscores are removed from property names as shown in my original example, as well as if they are removed from other things such as class names. Just generally removing underscores is causing a problem because it results in duplicate names. To me it seems like having an option to not remove underscores is the best solution. It doesn't result in invalid code, I think, but maybe I am missing something. The first of your solutions would seem to mean that at codegen time, it would throw an error if this situation arises. Instead of being able to build the generated code, I would be unable to generate code at all. What would be the way forward in this case? The second may work, if the class name problem is also resolved. But to me it doesn't seem as nice as just not removing underscores. Can you explain why underscores are removed? I don't understand that bit, it may help me to understand this solution. |
The first solution is probably the worst of the possible solutions. The way forward, would be to change the modelPropertyNaming (e.g. use original instead), such that valid code can be generated. The reason underscores are removed is that camel case normally doesn't contain underscores. Introducing an option to not remove underscores would probably be the best option. Codegen should also detect duplicate class names/property names and at least print a warning/error (maybe with a hint to the appropriate flags to resolve duplicate identifiers). @wing328 : Would this be an acceptable solution? |
@TiFu we can introduce the option to keep/change the property naming but for this particular case, I think they can simply update https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractJavaCodegen.java#L450 to customize the behaviour in handling |
Is anyone working on this? As I am happy to fix it. |
@baynezy I don't think anyone is working on it. Can you elaborate on the solution you want to apply to fix the isuse? |
We have the same problem: "PRAWJ202": {"type": "object","properties": {
"ROW_ID": { "format": "int64", "type": "integer" },
"TABLE_ID": { "type": "string" },
"ROWID": { "type": "string" }}} and that is created: /**
* Get ROW_ID
* @return ROW_ID
**/
@ApiModelProperty(example = "null", value = "")
public Long getROWID() {
return ROW_ID;
}
/**
* Get ROWID
* @return ROWID
**/
@ApiModelProperty(example = "null", value = "")
public String getROWID() {
return ROWID;
} and that ends up in @baynezy : did you work on this? |
I didn't get onto it I'm afraid. I got swallowed up by other problems. |
@baynezy : Can you give me a hint which class to touch? then I will give it a try - because I need that |
@Uebergeek702 I didn't have time to come up with a good solution. I made a hack though which may help you to properly solve the problem. In DefaultCodegen.java, search for the text "// Remove all underscores" -my hack was to comment out the following lines, but a proper solution would involve making that behaviour controllable by option. swagger-codegen/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java Line 3155 in 4d70508
|
@Uebergeek702 https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractJavaCodegen.java#L458 (toVarName) is a good starting point on how customize the naming of properties. |
@wing328 thank you for the hint - I also wrote you an email. Can you assign me to the issue? (Is that usual?) and help me to set everything up to contribute? Thx. |
I did something - but I don't know how to contribute.... |
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
Using the below command line to generate code, the following Java code is generated for the
CHTConfigurationData
class: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
Related issues
Here is a similar issue relating to changing of case in property names:
#4066
Here is a similar issue in PHP language generation:
#4551
Here is a similar issue in node javascript code gen - there is a configuration option to disable the behaviour in this case:
#2766
The text was updated successfully, but these errors were encountered: