Skip to content

[BUG][JAVA][XML] Java generator with withXml option produces duplicate XML properties #4014

@RockyMM

Description

@RockyMM

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

When generating a Java POJO using "withXml" option the @XmlElement ends up above public static final String SERIALIZED_NAME_% constant, which in turn marks this constant as an XML property. On the other hand, @XmlAccessorType is set to FIELD, which in turn marks the field, thus producing duplicated definition of the property.

Example:

/**
 * ValidTakeoverProfilesProfile
 */
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2019-10-01T16:24:48.053+02:00[Europe/Prague]")
@XmlRootElement(name = "ValidTakeoverProfilesProfile")
@XmlAccessorType(XmlAccessType.FIELD)

public class ValidTakeoverProfilesProfile {
  @XmlElement(name = "profileName") // this marks the constant below as a property
  public static final String SERIALIZED_NAME_PROFILE_NAME = "profileName";
  @SerializedName(SERIALIZED_NAME_PROFILE_NAME)
  private String profileName; // this becomes a property since XmlAccessorType is FIELD

The outcome is that produced model is not usable for Spring app, as there are multiple properties with a same name inside single class.

openapi-generator version

4.1.2

After some investigation, version 4.1.0 result produces usable model for my use case.

OpenAPI declaration file content or url

Complete Maven example is here, with JUnit test: https://github.com/RockyMM/openapi-modelgen-test

Command line used for generation
mvn clean test
Steps to reproduce

Use the following options to generate the model from YAML

                        <generatorName>java</generatorName> 
                        <generateApis>false</generateApis>
                        <generateSupportingFiles>true</generateSupportingFiles>
                        <generateModelTests>false</generateModelTests>
                        <generateModelDocumentation>false</generateModelDocumentation>
                        <skipValidateSpec>true</skipValidateSpec>
                        <supportingFilesToGenerate>pom.xml</supportingFilesToGenerate>
                        <additionalProperties>
                            <additionalProperty>java8=true</additionalProperty>
                            <additionalProperty>dateLibrary=java8</additionalProperty>
                            <additionalProperty>jackson=false</additionalProperty>
                            <additionalProperty>withXml=true</additionalProperty>
                        </additionalProperties>
Related issues/PRs

Could not find any.

Suggest a fix

In my perspective, in https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/Java/pojo.mustache

both gson and jackson sections above a property must come above withXml sections. This could solve the issue.

Either this, or I am doing something very very wrong and missing some obvious thing.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions