Improved support for XML wrapped items (#6150)

* Improved support for XML wrapped items

* XML @annotation whitespace fixed
This commit is contained in:
mobreza 2017-08-03 16:44:17 +08:00 committed by wing328
parent b433afebdd
commit b4e0854823
6 changed files with 90 additions and 11 deletions

View File

@ -60,6 +60,7 @@ public class CodegenProperty implements Cloneable {
public String xmlPrefix;
public String xmlName;
public String xmlNamespace;
public boolean isXmlWrapped = false;
@Override
@ -135,6 +136,7 @@ public class CodegenProperty implements Cloneable {
result = prime * result + ((xmlPrefix == null) ? 0 : xmlPrefix.hashCode());
result = prime * result + ((xmlName == null) ? 0 : xmlName.hashCode());
result = prime * result + ((xmlNamespace == null) ? 0 : xmlNamespace.hashCode());
result = prime * result + ((isXmlWrapped ? 13:31));
return result;
}
@ -322,6 +324,9 @@ public class CodegenProperty implements Cloneable {
if (!Objects.equals(this.xmlNamespace, other.xmlNamespace)) {
return false;
}
if (!Objects.equals(this.isXmlWrapped, other.isXmlWrapped)) {
return false;
}
return true;
}

View File

@ -1816,13 +1816,19 @@ public class DefaultCodegen {
property.isListContainer = true;
property.containerType = "array";
property.baseType = getSwaggerType(p);
if (p.getXml() != null) {
property.isXmlWrapped = p.getXml().getWrapped() == null ? false : p.getXml().getWrapped();
property.xmlPrefix= p.getXml().getPrefix();
property.xmlNamespace = p.getXml().getNamespace();
property.xmlName = p.getXml().getName();
}
// handle inner property
ArrayProperty ap = (ArrayProperty) p;
property.maxItems = ap.getMaxItems();
property.minItems = ap.getMinItems();
String itemName = (String) p.getVendorExtensions().get("x-item-name");
if (itemName == null) {
itemName = property.name;
itemName = property.name;
}
CodegenProperty cp = fromProperty(itemName, ap.getItems());
updatePropertyForArray(property, cp);

View File

@ -16,8 +16,7 @@ import java.io.Serializable;
{{/serializableModel}}
{{#jackson}}
{{#withXml}}
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.*;
{{/withXml}}
{{/jackson}}
{{#withXml}}

View File

@ -24,7 +24,15 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#parcela
{{#jackson}}
@JsonProperty("{{baseName}}")
{{#withXml}}
{{^isContainer}}
@JacksonXmlProperty({{#isXmlAttribute}}isAttribute = true, {{/isXmlAttribute}}{{#xmlNamespace}}namespace="{{xmlNamespace}}", {{/xmlNamespace}}localName = "{{#xmlName}}{{xmlName}}{{/xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}")
{{/isContainer}}
{{#isContainer}}
{{#isXmlWrapped}}
// items.xmlName={{items.xmlName}}
@JacksonXmlElementWrapper(useWrapping = {{isXmlWrapped}}, {{#xmlNamespace}}namespace="{{xmlNamespace}}", {{/xmlNamespace}}localName = "{{#items.xmlName}}{{items.xmlName}}{{/items.xmlName}}{{^items.xmlName}}{{items.baseName}}{{/items.xmlName}}")
{{/isXmlWrapped}}
{{/isContainer}}
{{/withXml}}
{{/jackson}}
{{#withXml}}
@ -32,7 +40,20 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#parcela
@XmlAttribute(name = "{{#xmlName}}{{xmlName}}{{/xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}")
{{/isXmlAttribute}}
{{^isXmlAttribute}}
{{^isContainer}}
@XmlElement({{#xmlNamespace}}namespace="{{xmlNamespace}}", {{/xmlNamespace}}name = "{{#xmlName}}{{xmlName}}{{/xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}")
{{/isContainer}}
{{#isContainer}}
// 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}}
{{/isContainer}}
{{/isXmlAttribute}}
{{/withXml}}
{{#gson}}
@ -174,7 +195,7 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#parcela
}
return o.toString().replace("\n", "\n ");
}
{{#parcelableModel}}
public void writeToParcel(Parcel out, int flags) {
{{#parent}} super.writeToParcel(out, flags); {{/parent}} {{#vars}}
@ -197,7 +218,7 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#parcela
{{/isPrimitiveType}}
{{/vars}}
}
public int describeContents() {
return 0;
}

View File

@ -1,6 +1,6 @@
{{#withXml}}
{{#jackson}}
@JacksonXmlRootElement({{#xmlNamespace}}namespace="{{xmlNamespace}}", {{/xmlNamespace}}localName = "{{#xmlName}}{{xmlName}}{{/xmlName}}{{^xmlName}}{{classname}}{{/xmlName}}")
{{/jackson}}
@XmlRootElement({{#xmlNamespace}}namespace="{{xmlNamespace}}", {{/xmlNamespace}}name = "{{#xmlName}}{{xmlName}}{{/xmlName}}{{^xmlName}}{{classname}}{{/xmlName}}")
@XmlAccessorType(XmlAccessType.FIELD){{/withXml}}
@XmlAccessorType(XmlAccessType.FIELD)
{{#jackson}}
@JacksonXmlRootElement({{#xmlNamespace}}namespace="{{xmlNamespace}}", {{/xmlNamespace}}localName = "{{#xmlName}}{{xmlName}}{{/xmlName}}{{^xmlName}}{{classname}}{{/xmlName}}"){{/jackson}}{{/withXml}}

View File

@ -654,6 +654,55 @@ public class JavaModelTest {
Assert.assertEquals(property3.xmlPrefix, "my");
}
@Test(description = "test models with wrapped xml")
public void modelWithWrappedXmlTest() {
final Model model = new ModelImpl()
.description("a sample model")
.xml(new Xml()
.prefix("my")
.namespace("xmlNamespace")
.name("customXmlName"))
.property("id", new LongProperty())
.property("array", new ArrayProperty()
.xml(new Xml()
.prefix("my")
.wrapped(true)
.namespace("myNamespace")
.name("xmlArray")).items(new StringProperty()
.xml(new Xml()
.name("i"))))
.required("id");
final DefaultCodegen codegen = new JavaClientCodegen();
final CodegenModel cm = codegen.fromModel("sample", model);
Assert.assertEquals(cm.name, "sample");
Assert.assertEquals(cm.classname, "Sample");
Assert.assertEquals(cm.description, "a sample model");
Assert.assertEquals(cm.xmlPrefix, "my");
Assert.assertEquals(cm.xmlName, "customXmlName");
Assert.assertEquals(cm.xmlNamespace, "xmlNamespace");
Assert.assertEquals(cm.vars.size(), 2);
final List<CodegenProperty> vars = cm.vars;
final CodegenProperty property2 = vars.get(1);
Assert.assertEquals(property2.baseName, "array");
Assert.assertEquals(property2.getter, "getArray");
Assert.assertEquals(property2.setter, "setArray");
Assert.assertEquals(property2.datatype, "List<String>");
Assert.assertEquals(property2.name, "array");
Assert.assertEquals(property2.defaultValue, "new ArrayList<String>()");
Assert.assertEquals(property2.baseType, "List");
Assert.assertTrue(property2.isContainer);
Assert.assertTrue(property2.isXmlWrapped);
Assert.assertEquals(property2.xmlName, "xmlArray");
Assert.assertNotNull(property2.xmlNamespace);
Assert.assertNotNull(property2.items);
CodegenProperty items = property2.items;
Assert.assertEquals(items.xmlName, "i");
Assert.assertEquals(items.baseName, "array");
}
@Test(description = "convert a boolean parameter")
public void booleanParameterTest() {
final BooleanProperty property = new BooleanProperty();
@ -669,5 +718,4 @@ public class JavaModelTest {
Assert.assertEquals(cp.getter, "isProperty");
}
}
}