[BUG][Java] Discriminator propertyName incorrectly lowercased
Created by: sebastien-rosset
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
What's the version of OpenAPI Generator used? -
Have you search for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Bounty to sponsor the fix (example)
Description
The discriminator property is not processed correctly during deserialization when the name of the discriminator property in the OAS specification starts with uppercase, or when it has specific patterns that cause the name to be transformed. I did some investigation, I believe the cause of the problem is:
-
The JSON.mustache template invokes the getDiscriminatorValue() passing {{{propertyName}}} as an argument: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/Java/JSON.mustache#L78
-
The generated createGson() method in JSON.mustache is used during deserialization. That method finds the discriminator property in the serialized JSON document. The call to getDiscriminatorValue() should be the name of the serialized discriminator property (in the JSON document), not the transformed Java property name.
-
But the {{propertyName}} tag is set in DefaultCodegen.java. The value of the tag is the name of the property in the OpenAPI specification, transformed by calling toVarName().
The toVarName() method is overridden in https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java#L575
Specifically, the name of the property is camelized: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java#L611 For example, if the name of the discriminator property is "ObjectType" in the OAS spec, the camelized name is "objectType", and that means the generated JSON.java will be looking for "objectType", but the serialized property will be "ObjectType". As another example, if the name of the property is "123ObjectType", then the generated name will be "_123objectType":
getDiscriminatorValue(readElement, "_123objectType"));
But it should be:
getDiscriminatorValue(readElement, "123ObjectType"));
openapi-generator version
4.2.1
OpenAPI declaration file content or url
Here is an OpenAPI spec with three data types that will cause problems during the Java code generation. The data types define discriminator properties with names that will be transformed:
https://gist.github.com/sebastien-rosset/f32f0abea11416bf82175577b48c251e
Command line used for generation
Steps to reproduce
Related issues/PRs
Suggest a fix
I think the fix is to use "propertyBaseName" tag instead of "propertyName". The "propertyBaseName" tag is the name of the serialized property, whereas "propertyName" is the Java variable name.
I will try a fix and submit a PR.