Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for XmlSchema annotation during model generation #4278

Merged
merged 6 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@
import javax.validation.constraints.Min;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlElementRefs;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlElementRefs;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchema;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -2013,10 +2014,18 @@ protected Discriminator resolveDiscriminator(JavaType type, ModelConverterContex
}

protected XML resolveXml(Annotated a, Annotation[] annotations, io.swagger.v3.oas.annotations.media.Schema schema) {
// if XmlRootElement annotation, construct an Xml object and attach it to the model
// if XmlRootElement annotation, construct a Xml object and attach it to the model
XmlRootElement rootAnnotation = null;
XmlSchema xmlSchema = null;
if (a != null) {
rootAnnotation = a.getAnnotation(XmlRootElement.class);
Class<?> rawType = a.getRawType();
if (rawType != null) {
Package aPackage = rawType.getPackage();
if (aPackage != null) {
xmlSchema = aPackage.getAnnotation(XmlSchema.class);
}
}
}
if (rootAnnotation == null) {
if (annotations != null) {
Expand All @@ -2028,16 +2037,25 @@ protected XML resolveXml(Annotated a, Annotation[] annotations, io.swagger.v3.oa
}
}
}

if (rootAnnotation != null && !"".equals(rootAnnotation.name()) && !JAXB_DEFAULT.equals(rootAnnotation.name())) {
XML xml = new XML().name(rootAnnotation.name());
if (rootAnnotation.namespace() != null && !"".equals(rootAnnotation.namespace()) && !JAXB_DEFAULT.equals(rootAnnotation.namespace())) {
if (xmlSchema != null && isNonTrivialXmlNamespace(xmlSchema.namespace())) {
xml.namespace(xmlSchema.namespace());
}
// Let XmlRootElement overwrite global XmlSchema namespace if present
if (isNonTrivialXmlNamespace(rootAnnotation.namespace())) {
xml.namespace(rootAnnotation.namespace());
}
return xml;
}
return null;
}

private boolean isNonTrivialXmlNamespace(String namespace) {
return namespace != null && !"".equals(namespace) && !JAXB_DEFAULT.equals(namespace);
}

protected Set<String> resolveIgnoredProperties(Annotations a, Annotation[] annotations) {

Set<String> propertiesToIgnore = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.swagger.v3.core.oas.models.xmltest;

import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;

@XmlRootElement(name = "RootName")
public class NestedModelWithJAXBAnnotations {
@XmlAttribute
public String id;

@XmlElement(name = "named")
public String name;

@XmlElement(name = "SubName")
public SubModelWithJAXBAnnotations subName;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.swagger.v3.core.oas.models.xmltest;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SubType", propOrder = {
"id",
"name"
})
public class SubModelWithJAXBAnnotations {
@XmlAttribute
public String id;

@XmlElement
public String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@javax.xml.bind.annotation.XmlSchema(
namespace = "https://www.openapis.org/test/nested",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED
)
package io.swagger.v3.core.oas.models.xmltest;
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.swagger.v3.core.oas.models.Address;
import io.swagger.v3.core.oas.models.Issue534;
import io.swagger.v3.core.oas.models.ModelWithJAXBAnnotations;
import io.swagger.v3.core.oas.models.xmltest.NestedModelWithJAXBAnnotations;
import io.swagger.v3.core.util.Yaml;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.XML;
Expand Down Expand Up @@ -106,6 +107,48 @@ public void processModelWithJAXBAnnotations() {
}
}

@Test(description = "it should process a nested model with JAXB annotations and a namespace")
public void processModelWithJAXBAnnotationsAndNamespace() {
final Map<String, Schema> schemas = ModelConverters.getInstance().readAll(NestedModelWithJAXBAnnotations.class);
assertEquals(schemas.size(), 2);

final Schema model = schemas.get("NestedModelWithJAXBAnnotations");
assertNotNull(model);
assertTrue(model instanceof Schema);

final XML rootXml = model.getXml();
assertNotNull(rootXml);
assertEquals(rootXml.getName(), "RootName");
assertEquals(rootXml.getNamespace(), "https://www.openapis.org/test/nested");

Map<String, Schema> props = model.getProperties();
for (Map.Entry<String, Schema> entry : props.entrySet()) {
final String name = entry.getKey();
final Schema property = entry.getValue();
if ("id".equals(name)) {
final XML xml = property.getXml();
assertNotNull(xml);
assertNull(xml.getName());
assertTrue(xml.getAttribute());
assertNull(xml.getWrapped());
} else if ("name".equals(name)) {
final XML xml = property.getXml();
assertNotNull(xml);
assertEquals(xml.getName(), "named");
assertNull(xml.getAttribute());
assertNull(xml.getWrapped());
} else if ("subName".equals(name)) {
final XML xml = property.getXml();
assertNotNull(xml);
assertEquals(xml.getName(), "SubName");
assertNull(xml.getAttribute());
assertNull(xml.getWrapped());
} else {
fail(String.format("Unexpected property: %s", name));
}
}
}

@Test(description = "it should deserialize a model")
public void deserializeModel() throws IOException {
final String yaml = "---\n" +
Expand Down