From f24c50a89b1fdebc83080298b327af3c38c870e1 Mon Sep 17 00:00:00 2001 From: Shon Vella Date: Wed, 15 Aug 2018 13:09:34 -0600 Subject: [PATCH] issue #2907 - consider more annotations in io.swagger.jackson.ModelResolver.ignore() to get a more accurate approximation of what Jackson does --- .../io/swagger/jackson/ModelResolver.java | 12 ++++++++- .../java/io/swagger/jackson/XMLInfoTest.java | 27 ++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/modules/swagger-core/src/main/java/io/swagger/jackson/ModelResolver.java b/modules/swagger-core/src/main/java/io/swagger/jackson/ModelResolver.java index 8aea946aa9..4f19b02dbc 100644 --- a/modules/swagger-core/src/main/java/io/swagger/jackson/ModelResolver.java +++ b/modules/swagger-core/src/main/java/io/swagger/jackson/ModelResolver.java @@ -24,6 +24,8 @@ 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.XmlRootElement; import javax.xml.bind.annotation.XmlSchema; @@ -34,6 +36,7 @@ import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.JsonIdentityReference; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonTypeInfo; @@ -691,11 +694,18 @@ protected boolean ignore(final Annotated member, final XmlAccessorType xmlAccess if (propertiesToIgnore.contains(propName)) { return true; } + if (member.hasAnnotation(JsonIgnore.class)) { + return true; + } if (xmlAccessorTypeAnnotation == null) { return false; } if (xmlAccessorTypeAnnotation.value().equals(XmlAccessType.NONE)) { - if (!member.hasAnnotation(XmlElement.class) && !member.hasAnnotation(XmlAttribute.class)) { + if (!member.hasAnnotation(XmlElement.class) && + !member.hasAnnotation(XmlAttribute.class) && + !member.hasAnnotation(XmlElementRef.class) && + !member.hasAnnotation(XmlElementRefs.class) && + !member.hasAnnotation(JsonProperty.class)) { return true; } } diff --git a/modules/swagger-core/src/test/java/io/swagger/jackson/XMLInfoTest.java b/modules/swagger-core/src/test/java/io/swagger/jackson/XMLInfoTest.java index b49fdee795..a381489973 100644 --- a/modules/swagger-core/src/test/java/io/swagger/jackson/XMLInfoTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/jackson/XMLInfoTest.java @@ -1,5 +1,6 @@ package io.swagger.jackson; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import io.swagger.annotations.ApiModel; import io.swagger.converter.ModelConverter; @@ -14,6 +15,8 @@ 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.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; import static org.testng.Assert.assertEquals; @@ -83,6 +86,12 @@ public void testReadingXmlAccessorTypeNone() throws Exception { assertNull(impl.getProperties().get("b")); assertNotNull(impl.getProperties().get("c")); + + assertNotNull(impl.getProperties().get("d")); + + assertNotNull(impl.getProperties().get("e")); + + assertNotNull(impl.getProperties().get("f")); } @XmlRootElement(name = "xmlDecoratedBean") @@ -97,6 +106,15 @@ static class XmlDecoratedBeanXmlAccessorNone { @XmlAttribute public String c; + + @XmlElementRef + public XmlDecoratedBean d; + + @XmlElementRefs(value = {@XmlElementRef}) + public List e; + + @JsonProperty + public int f; } @Test @@ -114,8 +132,12 @@ public void testReadingXmlAccessorTypePublic() throws Exception { final Property propertyA = impl.getProperties().get("a"); assertNotNull(propertyA); - Property propertyB = impl.getProperties().get("b"); + final Property propertyB = impl.getProperties().get("b"); assertNotNull(propertyB); + + final Property propertyC = impl.getProperties().get("c"); + assertNull(propertyC); + } @XmlRootElement(name = "xmlDecoratedBean") @@ -126,6 +148,9 @@ static class XmlDecoratedBeanXmlAccessorPublic { public int a; public String b; + + @JsonIgnore + public String c; } }