Skip to content

[BUG] XML name incorrect when referenced or when item of an array #3223

Closed
@Sevenarth

Description

@Sevenarth
Description

The endpoints described in the spec file are supposed to all read (and produce if the model is set in the response body):

<Entries>
  <Entry value="1234" />
  <Entry value="1234" />
</Entries>

In this case I generated the models with the Java generator, which seems to be the best implementation fir POJOs, and I plugged them into a Spring Boot server stub. (It was just easier to get responses to this mock spec this way for the purpose of this issue)
Normally I work with the JaxRS Resteasy and Spring Boot generator customised with the Java generator POJO templates (since the other two are lacking templates for XML support)

For each case these are what the system is expecting (or generating as a response) instead:

arrayInObject: (EntriesObject response)

<EntriesObject>
    <entries>
        <entries>
            <value>1234</value>
        </entries>
    </entries>
</EntriesObject>

wholeObject: (WholeObject response)

<WholeObject>
    <entries>
        <entries>
            <value>1234</value>
        </entries>
    </entries>
</WholeObject>

rootArray: (List response)

<List>
    <item>
        <value>1234</value>
    </item>
</List>

I am using JAXB for the serialisation process.

In writing a demonstration for this issue, lots of other issues came up, but my original issue was only regarding the XML name of array items: the generator generates the following: (for WholeObject and EntriesObject)

  // Is a container wrapped=false
  // items.name=entries items.baseName=entries items.xmlName= items.xmlNamespace=
  // items.example= items.type=Entry
  @XmlElement(name = "entries")
  private List<Entry> entries = new ArrayList<>();

Even if I try to change names to the YAML properties or xmlName, the name assigned to the XmlElement is always the variable name, instead of xmlName or the YAML property name as a fallback. (In this case it should be @XmlElement(name = "Entry"))

I take the opportunity to also report that when an object (that has a xmlName) is referenced to a property (of another object), it takes the property name instead of the xmlName:

MyObject:
  type: object
  xml:
    name: PrimaryObject
  properties:
    otherObject:
      $ref: '#/components/schemas/OtherObject'
OtherObject:
  type: object
  xml:
    name: SecondaryObject
  properties:
    value:
      ...

generates:

<PrimaryObject>
  <otherObject>
    <value>...</value>
  </otherObject>
</PrimaryObject>

instead of:

<PrimaryObject>
  <SecondaryObject>
    <value>...</value>
  </SecondaryObject>
</PrimaryObject>
openapi-generator version

4.0.2

OpenAPI declaration url

https://gist.github.com/Sevenarth/595ce008748a0fa1e43bcfcdcaffe229

Command line used for generation

To retrieve the generated models:

java -jar openapi-generator-cli.jar generate \
  -i xmlname_bug.yaml \
  -o OAPIxmlNameBug \
  -g java \
  --additional-properties withXml=true \
  --additional-properties java8=true

(As said before, these models have been tested in the Spring Boot server stub)

Steps to reproduce
  1. Generate models using the Java generator
  2. Generate server stub using the Spring Boot generator
  3. Adapt the Java generator models to the Spring Boot server stub
  4. Run Spring Boot server in debug or Create example response models and run server
  5. Analyse request bodies or response bodies
Related issues/PRs

Unknown

Suggest a fix

The fix to my original issue is to assign XmlElement(name = "...") to entries.items.xml.name (xmlName in container items), and @XmlElementWrapper(name="...") to entries.xml.name when entries.xml.wrapped=true.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions