OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Java] [withXml] xml name not used for items wrapped in an array

Open
#9,371 8 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I have the following swagger which defined an array of message object.

swagger: "2.0"
definitions:
  Status:
    type: "object"
    properties:
      messages:
        type: "array"
        items:
          $ref: "#/definitions/Message"
          xml:
            name: message
        xml:
          name: messages
          wrapped: true

i want to generate java code which matches the xml below. Note that the message array ( element) wrapped message items and each item are named .

	<status>
		<messages>
			<message>
			</message>
			<message>
			</message>
		</messages>
	</status>

The problem is in the generation of Status java class which contains the messages list :

public class Status implements Serializable {

        @XmlElement(name = "messages") // problem here
	@XmlElementWrapper(name = "messages")
	private List<Message> messages = null;
}

The value of attribut name in XmlElement annotation should be message (without s). The generator ignores the name tag under items tag in the swagger file.

openapi-generator version

5.1.0

OpenAPI declaration file content or url

I'm not sure if the bug is located in this template file but the following line in pojo.mustache seems strange; The value name uses the same variable 'xmlName' for both @XmlElement and @XmlElementWrapper.

// Is a container wrapped={{isXmlWrapped}}
      {{#items}}
  // items.name={{name}} items.baseName={{baseName}} items.xmlName={{xmlName}} items.xmlNamespace={{xmlNamespace}}
  // items.example={{example}} items.type={{dataType}}
  @XmlElement({{#xmlNamespace}}namespace="{{xmlNamespace}}", {{/xmlNamespace}}name = "{{#xmlName}}{{xmlName}}{{/xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}")
      {{/items}}
      {{#isXmlWrapped}}
  @XmlElementWrapper({{#xmlNamespace}}namespace="{{xmlNamespace}}", {{/xmlNamespace}}name = "{{#xmlName}}{{xmlName}}{{/xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}")
      {{/isXmlWrapped}}
Generation Details
Steps to reproduce
Related issues/PRs
Suggest a fix

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/openapi-generator/src/main/resources/Java/pojo.mustache and inspect how xmlName is supplied for array items versus the wrapped collection. Use the provided Status definition and XML example to compare the generated annotations. Done means the item annotation uses message while the wrapper retains messages.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.