Skip to content
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 @@ -12,10 +12,12 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyMetadata;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.fasterxml.jackson.databind.jsontype.NamedType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.google.common.collect.Iterables;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.converter.ModelConverter;
Expand All @@ -37,19 +39,6 @@
import io.swagger.util.AllowableValues;
import io.swagger.util.AllowableValuesUtils;
import io.swagger.util.PrimitiveType;

import com.google.common.collect.Iterables;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.ArrayList;
Expand All @@ -62,6 +51,19 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ModelResolver extends AbstractModelConverter implements ModelConverter {
Logger LOGGER = LoggerFactory.getLogger(ModelResolver.class);
Expand Down Expand Up @@ -219,6 +221,7 @@ public Model resolve(JavaType type, ModelConverterContext context, Iterator<Mode
}
model.xml(xml);
}
final XmlAccessorType xmlAccessorTypeAnnotation = beanDesc.getClassAnnotations().get(XmlAccessorType.class);

// see if @JsonIgnoreProperties exist
Set<String> propertiesToIgnore = new HashSet<String>();
Expand Down Expand Up @@ -294,7 +297,7 @@ public Model resolve(JavaType type, ModelConverterContext context, Iterator<Mode

final AnnotatedMember member = propDef.getPrimaryMember();

if (member != null && !propertiesToIgnore.contains(propName)) {
if (member != null && !ignore(member, xmlAccessorTypeAnnotation, propName, propertiesToIgnore)) {
List<Annotation> annotationList = new ArrayList<Annotation>();
for (Annotation a : member.annotations()) {
annotationList.add(a);
Expand Down Expand Up @@ -437,6 +440,21 @@ public Model resolve(JavaType type, ModelConverterContext context, Iterator<Mode

return model;
}

protected boolean ignore(final Annotated member, final XmlAccessorType xmlAccessorTypeAnnotation, final String propName, final Set<String> propertiesToIgnore) {
if (propertiesToIgnore.contains(propName)) {
return true;
}
if (xmlAccessorTypeAnnotation == null) {
return false;
}
if (xmlAccessorTypeAnnotation.value().equals(XmlAccessType.NONE)) {
if (!member.hasAnnotation(XmlElement.class)) {
return true;
}
}
return false;
}

private enum GeneratorWrapper {
PROPERTY(ObjectIdGenerators.PropertyGenerator.class) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package io.swagger.jackson;

import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertEquals;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.testng.annotations.Test;

import io.swagger.annotations.ApiModel;
import io.swagger.converter.ModelConverter;
import io.swagger.converter.ModelConverterContextImpl;
import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.Xml;
import io.swagger.models.properties.Property;

import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;

public class XMLInfoTest extends SwaggerTestBase {

Expand Down Expand Up @@ -48,6 +48,7 @@ public void testSimple() throws Exception {
@XmlRootElement(name = "xmlDecoratedBean")
@ApiModel(description = "DESC")
static class XmlDecoratedBean {

@XmlElement(name = "elementB")
public int b;

Expand All @@ -58,4 +59,63 @@ static class XmlDecoratedBean {
@JsonProperty("elementC")
public String c;
}

@Test
public void testReadingXmlAccessorTypeNone() throws Exception {
final ModelConverter mr = modelResolver();
final Model model = mr.resolve(XmlDecoratedBeanXmlAccessorNone.class, new ModelConverterContextImpl(mr), null);
assertTrue(model instanceof ModelImpl);

final ModelImpl impl = (ModelImpl) model;

final Xml xml = impl.getXml();
assertNotNull(xml);
assertEquals(xml.getName(), "xmlDecoratedBean");

final Property property = impl.getProperties().get("a");
assertNotNull(property);

assertNull(impl.getProperties().get("b"));
}

@Test
public void testReadingXmlAccessorTypePublic() throws Exception {
final ModelConverter mr = modelResolver();
final Model model = mr.resolve(XmlDecoratedBeanXmlAccessorPublic.class, new ModelConverterContextImpl(mr), null);
assertTrue(model instanceof ModelImpl);

final ModelImpl impl = (ModelImpl) model;

final Xml xml = impl.getXml();
assertNotNull(xml);
assertEquals(xml.getName(), "xmlDecoratedBean");

final Property propertyA = impl.getProperties().get("a");
assertNotNull(propertyA);

Property propertyB = impl.getProperties().get("b");
assertNotNull(propertyB);
}

@XmlRootElement(name = "xmlDecoratedBean")
@XmlAccessorType(XmlAccessType.NONE)
@ApiModel
static class XmlDecoratedBeanXmlAccessorNone {

@XmlElement
public int a;

public String b;
}

@XmlRootElement(name = "xmlDecoratedBean")
@ApiModel
static class XmlDecoratedBeanXmlAccessorPublic {

@XmlElement
public int a;

public String b;
}

}