Forum Discussion

Ray's avatar
Ray
New Contributor
3 months ago

openapi codegen doesn't match swagger for xml array

I'm trying to get an xml array to work.  In swagger it is represented fine, but after I run the code generator and then run my code I get the parent tag twice with no child tag.

A snippet from my schema is:

FormatType:
  type: array
  items:
    properties:
      Mode:
        type: string
      Copy:
        type: string
    xml:
      name: Format
  xml:
    wrapped: true
    name: Formats
PackageType:
  properties:
    Formats:
      $ref: '#/components/schemas/FormatType'

The code that gets generated is:

public class PackageType
...
  @Schema(name = "Formats", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  @JsonProperty("Formats")
  public List<@Valid FormatTypeInner> getFormats() { return formats; }

The FormatTypeInner class that got generated just has the simple objects defined in it.

When I run the code I am getting:

<BillDelivery...
  <Package>
    ...
    <Formats>
      <Formats>
        <Mode>2</Mode>
        <Copy>Y</Copy>
      </Formats>
    </Formats>
...

If I change the annotation in the PackageType class generated code and replace the @JsonProperty with the following:

@JacksonXmlElementWrapper(localName = "Formats")
@JacksonXmlProperty(localName = "Format")

Then when I run the code the output looks right:

<BillDelivery...
  <Package>
    ...
    <Formats>
      <Format>
        <Mode>2</Mode>
        <Copy>Y</Copy>
      </Format>
    </Formats>
...

My schema is specifying openapi: 3.0.3 and the org.openapitools/openapi-generator-maven-plugin/version is 7.10.0.

Can anyone explain why the schema looks good in https://editor.swagger.io but the generated code doesn't generate the right xml document?

Can anyone tell me please how to change the schema so that the code generated produces the correct output?  I'm not supposed to change the annotations, that defeats the purpose of using openapi.

Thank you.

1 Reply

  • Ray's avatar
    Ray
    New Contributor

    FYI, changing to version 7.16.0 of openapi-generator-maven-plugin didn't change the code that was generated and therefore the XML output is still wrong.