From e588bdbdc97e5e73fd94329d3cf011d049f16a08 Mon Sep 17 00:00:00 2001 From: Christian Schuster Date: Mon, 21 Aug 2023 18:16:19 +0200 Subject: [PATCH] fix XML annotations on model properties (JavaSpring) * generate JAXB annotations for attributes and elements * generate wrapper annotations (JAXB and Jackson) * use XML config from items for annotations of containers --- .../main/resources/JavaSpring/model.mustache | 1 + .../main/resources/JavaSpring/pojo.mustache | 28 +++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/model.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/model.mustache index c343e07fefc0..23cf601c88ef 100644 --- a/modules/openapi-generator/src/main/resources/JavaSpring/model.mustache +++ b/modules/openapi-generator/src/main/resources/JavaSpring/model.mustache @@ -25,6 +25,7 @@ import org.hibernate.validator.constraints.*; {{#withXml}} import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; {{/withXml}} {{/jackson}} {{#swagger2AnnotationLibrary}} diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache index 3bbcddb75e51..786eb271b226 100644 --- a/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache +++ b/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache @@ -214,9 +214,31 @@ public class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}{{^parent}} {{/swagger1AnnotationLibrary}} {{#jackson}} @JsonProperty("{{baseName}}") - {{#withXml}} - @JacksonXmlProperty({{#isXmlAttribute}}isAttribute = true, {{/isXmlAttribute}}{{#xmlNamespace}}namespace="{{.}}", {{/xmlNamespace}}localName = "{{xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}") - {{/withXml}} + {{#withXml}} + {{^isContainer}} + {{#isXmlAttribute}} + @XmlAttribute({{#xmlNamespace}}namespace = "{{.}}", {{/xmlNamespace}}name = "{{xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}") + @JacksonXmlProperty(isAttribute = true, {{#xmlNamespace}}namespace = "{{.}}", {{/xmlNamespace}}localName = "{{xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}") + {{/isXmlAttribute}} + {{^isXmlAttribute}} + @XmlElement({{#xmlNamespace}}namespace="{{.}}", {{/xmlNamespace}}name = "{{xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}") + @JacksonXmlProperty({{#xmlNamespace}}namespace = "{{.}}", {{/xmlNamespace}}localName = "{{xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}") + {{/isXmlAttribute}} + {{/isContainer}} + {{#isContainer}} + {{#items}} + @XmlElement({{#xmlNamespace}}namespace="{{.}}", {{/xmlNamespace}}name = "{{xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}") + @JacksonXmlProperty({{#xmlNamespace}}namespace = "{{.}}", {{/xmlNamespace}}localName = "{{xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}") + {{/items}} + {{#isXmlWrapped}} + @XmlElementWrapper({{#xmlNamespace}}namespace="{{.}}", {{/xmlNamespace}}name = "{{xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}") + @JacksonXmlElementWrapper({{#xmlNamespace}}namespace = "{{.}}", {{/xmlNamespace}}localName = "{{xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}") + {{/isXmlWrapped}} + {{^isXmlWrapped}} + @JacksonXmlElementWrapper(useWrapping = false) + {{/isXmlWrapped}} + {{/isContainer}} + {{/withXml}} {{/jackson}} {{#deprecated}} @Deprecated