Skip to content

Commit 5584cac

Browse files
committed
fix for #1292, added xml to model properties upon deserialization
1 parent ef28827 commit 5584cac

File tree

4 files changed

+83
-4
lines changed

4 files changed

+83
-4
lines changed

modules/swagger-core/src/main/java/io/swagger/jackson/JAXBAnnotationsHelper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ private static void applyElement(AnnotatedMember member, Property property) {
4848
if (wrapper != null) {
4949
final Xml xml = getXml(property);
5050
xml.setWrapped(true);
51-
setName(wrapper.namespace(), wrapper.name(), property);
51+
if(!"##default".equals(wrapper.name()) && !wrapper.name().isEmpty())
52+
xml.setName(wrapper.name());
5253
}
5354
final XmlElement element = member.getAnnotation(XmlElement.class);
5455
if (element != null) {

modules/swagger-core/src/main/java/io/swagger/util/PropertyDeserializer.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
import com.fasterxml.jackson.databind.JsonDeserializer;
77
import com.fasterxml.jackson.databind.JsonNode;
88
import com.fasterxml.jackson.databind.node.ArrayNode;
9+
import com.fasterxml.jackson.databind.node.ObjectNode;
910
import com.fasterxml.jackson.databind.node.TextNode;
1011
import io.swagger.models.properties.ArrayProperty;
1112
import io.swagger.models.properties.MapProperty;
1213
import io.swagger.models.properties.ObjectProperty;
1314
import io.swagger.models.properties.Property;
1415
import io.swagger.models.properties.PropertyBuilder;
1516
import io.swagger.models.properties.RefProperty;
17+
import io.swagger.models.Xml;
1618
import org.slf4j.Logger;
1719
import org.slf4j.LoggerFactory;
1820

@@ -68,13 +70,48 @@ private static JsonNode getDetailNode(JsonNode node, PropertyBuilder.PropertyId
6870
public Property deserialize(JsonParser jp, DeserializationContext ctxt)
6971
throws IOException, JsonProcessingException {
7072
JsonNode node = jp.getCodec().readTree(jp);
71-
return propertyFromNode(node);
73+
Property property = propertyFromNode(node);
74+
property.setXml(getXml(node));
75+
return property;
76+
}
77+
78+
public Xml getXml(JsonNode node) {
79+
Xml xml = null;
80+
81+
if(node instanceof ObjectNode) {
82+
ObjectNode obj = (ObjectNode)((ObjectNode) node).get("xml");
83+
if(obj != null) {
84+
xml = new Xml();
85+
JsonNode n = obj.get("name");
86+
if(n != null) {
87+
xml.name(n.asText());
88+
}
89+
n = obj.get("namespace");
90+
if(n != null) {
91+
xml.namespace(n.asText());
92+
}
93+
n = obj.get("prefix");
94+
if(n != null) {
95+
xml.prefix(n.asText());
96+
}
97+
n = obj.get("attribute");
98+
if(n != null) {
99+
xml.attribute(n.asBoolean());
100+
}
101+
n = obj.get("wrapped");
102+
if(n != null) {
103+
xml.wrapped(n.asBoolean());
104+
}
105+
}
106+
}
107+
return xml;
72108
}
73109

74110
Property propertyFromNode(JsonNode node) {
75111
final String type = getString(node, PropertyBuilder.PropertyId.TYPE);
76112
final String format = getString(node, PropertyBuilder.PropertyId.FORMAT);
77113
final String description = getString(node, PropertyBuilder.PropertyId.DESCRIPTION);
114+
final Xml xml = getXml(node);
78115

79116
JsonNode detailNode = node.get("$ref");
80117
if (detailNode != null) {

modules/swagger-core/src/test/scala/XmlModelTest.scala

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class XmlModelTest extends FlatSpec with Matchers {
5757
var rootXml = model.asInstanceOf[ModelImpl].getXml()
5858
rootXml should not be (null)
5959
rootXml.getName() should equal("rootName")
60+
6061
for ((name, property) <- model.getProperties.asScala) {
6162
name match {
6263
case "id" =>
@@ -76,14 +77,53 @@ class XmlModelTest extends FlatSpec with Matchers {
7677
case "wrappedList" =>
7778
var xml = property.getXml
7879
xml should not be (null)
79-
xml.getName should be (null)
80+
xml.getName should be ("wrappedListItems")
8081
xml.getAttribute should be (null)
8182
xml.getWrapped should equal (true)
8283
case _ =>
8384
fail(s"""Property "${name}" was not expected""")
8485
}
8586
}
8687
}
88+
89+
it should "deserialize a model" in {
90+
val yaml = """
91+
---
92+
type: "object"
93+
properties:
94+
id:
95+
type: "string"
96+
xml:
97+
attribute: true
98+
name:
99+
type: "string"
100+
xml:
101+
name: "renamed"
102+
list:
103+
type: "array"
104+
items:
105+
type: "string"
106+
wrappedList:
107+
type: "array"
108+
xml:
109+
name: "wrappedListItems"
110+
wrapped: true
111+
items:
112+
type: "string"
113+
forcedElement:
114+
type: "array"
115+
items:
116+
type: "string"
117+
xml:
118+
name: "rootName"
119+
"""
120+
val model = io.swagger.util.Yaml.mapper().readValue(yaml, classOf[ModelImpl])
121+
122+
val wrappedList = model.getProperties.get("wrappedList")
123+
wrappedList should not be (null)
124+
wrappedList.getXml() should not be (null)
125+
wrappedList.getXml().getName() should be ("wrappedListItems")
126+
}
87127
}
88128

89129
@XmlRootElement(name = "monster")

modules/swagger-core/src/test/scala/models/ModelWithJAXBAnnotations.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public class ModelWithJAXBAnnotations {
2323

2424
public List<String> list;
2525

26-
@XmlElementWrapper
26+
@XmlElementWrapper(name="wrappedListItems")
27+
@XmlElement(name="wrappedList")
2728
public List<String> wrappedList;
2829

2930
@XmlAttribute(name = "doNotUseMe")

0 commit comments

Comments
 (0)