Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #4671
Closed
Open
Issue created Dec 02, 2019 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

[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:

  1. 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

  2. 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.

  3. 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.

Assignee
Assign to
Time tracking