forked from loafle/openapi-generator-original
Improved support for XML wrapped items (#6150)
* Improved support for XML wrapped items * XML @annotation whitespace fixed
This commit is contained in:
parent
b433afebdd
commit
b4e0854823
@ -60,6 +60,7 @@ public class CodegenProperty implements Cloneable {
|
|||||||
public String xmlPrefix;
|
public String xmlPrefix;
|
||||||
public String xmlName;
|
public String xmlName;
|
||||||
public String xmlNamespace;
|
public String xmlNamespace;
|
||||||
|
public boolean isXmlWrapped = false;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -135,6 +136,7 @@ public class CodegenProperty implements Cloneable {
|
|||||||
result = prime * result + ((xmlPrefix == null) ? 0 : xmlPrefix.hashCode());
|
result = prime * result + ((xmlPrefix == null) ? 0 : xmlPrefix.hashCode());
|
||||||
result = prime * result + ((xmlName == null) ? 0 : xmlName.hashCode());
|
result = prime * result + ((xmlName == null) ? 0 : xmlName.hashCode());
|
||||||
result = prime * result + ((xmlNamespace == null) ? 0 : xmlNamespace.hashCode());
|
result = prime * result + ((xmlNamespace == null) ? 0 : xmlNamespace.hashCode());
|
||||||
|
result = prime * result + ((isXmlWrapped ? 13:31));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -322,6 +324,9 @@ public class CodegenProperty implements Cloneable {
|
|||||||
if (!Objects.equals(this.xmlNamespace, other.xmlNamespace)) {
|
if (!Objects.equals(this.xmlNamespace, other.xmlNamespace)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (!Objects.equals(this.isXmlWrapped, other.isXmlWrapped)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1816,13 +1816,19 @@ public class DefaultCodegen {
|
|||||||
property.isListContainer = true;
|
property.isListContainer = true;
|
||||||
property.containerType = "array";
|
property.containerType = "array";
|
||||||
property.baseType = getSwaggerType(p);
|
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
|
// handle inner property
|
||||||
ArrayProperty ap = (ArrayProperty) p;
|
ArrayProperty ap = (ArrayProperty) p;
|
||||||
property.maxItems = ap.getMaxItems();
|
property.maxItems = ap.getMaxItems();
|
||||||
property.minItems = ap.getMinItems();
|
property.minItems = ap.getMinItems();
|
||||||
String itemName = (String) p.getVendorExtensions().get("x-item-name");
|
String itemName = (String) p.getVendorExtensions().get("x-item-name");
|
||||||
if (itemName == null) {
|
if (itemName == null) {
|
||||||
itemName = property.name;
|
itemName = property.name;
|
||||||
}
|
}
|
||||||
CodegenProperty cp = fromProperty(itemName, ap.getItems());
|
CodegenProperty cp = fromProperty(itemName, ap.getItems());
|
||||||
updatePropertyForArray(property, cp);
|
updatePropertyForArray(property, cp);
|
||||||
|
@ -16,8 +16,7 @@ import java.io.Serializable;
|
|||||||
{{/serializableModel}}
|
{{/serializableModel}}
|
||||||
{{#jackson}}
|
{{#jackson}}
|
||||||
{{#withXml}}
|
{{#withXml}}
|
||||||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
|
import com.fasterxml.jackson.dataformat.xml.annotation.*;
|
||||||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
|
|
||||||
{{/withXml}}
|
{{/withXml}}
|
||||||
{{/jackson}}
|
{{/jackson}}
|
||||||
{{#withXml}}
|
{{#withXml}}
|
||||||
|
@ -24,7 +24,15 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#parcela
|
|||||||
{{#jackson}}
|
{{#jackson}}
|
||||||
@JsonProperty("{{baseName}}")
|
@JsonProperty("{{baseName}}")
|
||||||
{{#withXml}}
|
{{#withXml}}
|
||||||
|
{{^isContainer}}
|
||||||
@JacksonXmlProperty({{#isXmlAttribute}}isAttribute = true, {{/isXmlAttribute}}{{#xmlNamespace}}namespace="{{xmlNamespace}}", {{/xmlNamespace}}localName = "{{#xmlName}}{{xmlName}}{{/xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}")
|
@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}}
|
{{/withXml}}
|
||||||
{{/jackson}}
|
{{/jackson}}
|
||||||
{{#withXml}}
|
{{#withXml}}
|
||||||
@ -32,7 +40,20 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#parcela
|
|||||||
@XmlAttribute(name = "{{#xmlName}}{{xmlName}}{{/xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}")
|
@XmlAttribute(name = "{{#xmlName}}{{xmlName}}{{/xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}")
|
||||||
{{/isXmlAttribute}}
|
{{/isXmlAttribute}}
|
||||||
{{^isXmlAttribute}}
|
{{^isXmlAttribute}}
|
||||||
|
{{^isContainer}}
|
||||||
@XmlElement({{#xmlNamespace}}namespace="{{xmlNamespace}}", {{/xmlNamespace}}name = "{{#xmlName}}{{xmlName}}{{/xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}")
|
@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}}
|
{{/isXmlAttribute}}
|
||||||
{{/withXml}}
|
{{/withXml}}
|
||||||
{{#gson}}
|
{{#gson}}
|
||||||
@ -174,7 +195,7 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#parcela
|
|||||||
}
|
}
|
||||||
return o.toString().replace("\n", "\n ");
|
return o.toString().replace("\n", "\n ");
|
||||||
}
|
}
|
||||||
|
|
||||||
{{#parcelableModel}}
|
{{#parcelableModel}}
|
||||||
public void writeToParcel(Parcel out, int flags) {
|
public void writeToParcel(Parcel out, int flags) {
|
||||||
{{#parent}} super.writeToParcel(out, flags); {{/parent}} {{#vars}}
|
{{#parent}} super.writeToParcel(out, flags); {{/parent}} {{#vars}}
|
||||||
@ -197,7 +218,7 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#parcela
|
|||||||
{{/isPrimitiveType}}
|
{{/isPrimitiveType}}
|
||||||
{{/vars}}
|
{{/vars}}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int describeContents() {
|
public int describeContents() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{{#withXml}}
|
{{#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}}")
|
@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}}
|
@ -654,6 +654,55 @@ public class JavaModelTest {
|
|||||||
Assert.assertEquals(property3.xmlPrefix, "my");
|
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")
|
@Test(description = "convert a boolean parameter")
|
||||||
public void booleanParameterTest() {
|
public void booleanParameterTest() {
|
||||||
final BooleanProperty property = new BooleanProperty();
|
final BooleanProperty property = new BooleanProperty();
|
||||||
@ -669,5 +718,4 @@ public class JavaModelTest {
|
|||||||
Assert.assertEquals(cp.getter, "isProperty");
|
Assert.assertEquals(cp.getter, "isProperty");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user