GeezerVison is showing github.com — Merge branch 'master' into bump-reflections-version · JavaFramesSource/swagger-core@eef121d
File treeExpand file treeCollapse file tree Open diff view settings main/java/io/swagger/jackson
swagger-hibernate-validations main/java/io/swagger/jaxrs/config org/myles/project/resources
main/java/io/swagger/models
main/java/io/swagger/servlet/extensions test/java/io/swagger/servlet
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: modules/swagger-annotations/pom.xml +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | <parent> | 4 | 4 | <groupId>io.swagger</groupId> | 5 | 5 | <artifactId>swagger-project</artifactId> | 6 | | - <version>1.5.13</version> | | 6 | + <version>1.5.14-SNAPSHOT</version> | 7 | 7 | <relativePath>../..</relativePath> | 8 | 8 | </parent> | 9 | 9 | <modelVersion>4.0.0</modelVersion> | 10 | 10 | <groupId>io.swagger</groupId> | 11 | 11 | <artifactId>swagger-annotations</artifactId> | 12 | | - <version>1.5.13</version> | | 12 | + <version>1.5.14-SNAPSHOT</version> | 13 | 13 | <packaging>bundle</packaging> | 14 | 14 | <name>swagger-annotations</name> | 15 | 15 | <build> | |
Collapse file Copy file name to clipboardExpand all lines: modules/swagger-core/pom.xml +7-1Lines changed: 7 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | <parent> | 4 | 4 | <groupId>io.swagger</groupId> | 5 | 5 | <artifactId>swagger-project</artifactId> | 6 | | - <version>1.5.13</version> | | 6 | + <version>1.5.14-SNAPSHOT</version> | 7 | 7 | <relativePath>../..</relativePath> | 8 | 8 | </parent> | 9 | 9 | <modelVersion>4.0.0</modelVersion> | | 129 | 129 | <artifactId>validation-api</artifactId> | 130 | 130 | <version>${validation-api-version}</version> | 131 | 131 | </dependency> | | 132 | + <dependency> | | 133 | + <groupId>org.immutables</groupId> | | 134 | + <artifactId>value</artifactId> | | 135 | + <version>2.4.4</version> | | 136 | + <scope>test</scope> | | 137 | + </dependency> | 132 | 138 | <dependency> | 133 | 139 | <groupId>org.testng</groupId> | 134 | 140 | <artifactId>testng</artifactId> | |
Collapse file Copy file name to clipboardExpand all lines: modules/swagger-core/src/main/java/io/swagger/jackson/ModelResolver.java +32-8Lines changed: 32 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 13 | 13 | import com.fasterxml.jackson.databind.ObjectMapper; | 14 | 14 | import com.fasterxml.jackson.databind.PropertyMetadata; | 15 | 15 | import com.fasterxml.jackson.databind.SerializationFeature; | | 16 | +import com.fasterxml.jackson.databind.annotation.JsonSerialize; | 16 | 17 | import com.fasterxml.jackson.databind.introspect.Annotated; | 17 | 18 | import com.fasterxml.jackson.databind.introspect.AnnotatedMember; | 18 | 19 | import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition; | | 55 | 56 | import javax.xml.bind.annotation.XmlAccessorType; | 56 | 57 | import javax.xml.bind.annotation.XmlElement; | 57 | 58 | import javax.xml.bind.annotation.XmlRootElement; | | 59 | +import javax.xml.bind.annotation.XmlSchema; | 58 | 60 | import java.lang.annotation.Annotation; | 59 | 61 | import java.lang.reflect.Type; | 60 | 62 | import java.math.BigDecimal; | @@ -211,7 +213,7 @@ public Model resolve(JavaType type, ModelConverterContext context, Iterator<Mode | 211 | 213 | return null; | 212 | 214 | } | 213 | 215 |
| 214 | | - final BeanDescription beanDesc = _mapper.getSerializationConfig().introspect(type); | | 216 | + BeanDescription beanDesc = _mapper.getSerializationConfig().introspect(type); | 215 | 217 | // Couple of possibilities for defining | 216 | 218 | String name = _typeName(type, beanDesc); | 217 | 219 |
| @@ -258,6 +260,12 @@ public Model resolve(JavaType type, ModelConverterContext context, Iterator<Mode | 258 | 260 | context.resolve(type.getContentType()); | 259 | 261 | return null; | 260 | 262 | } | | 263 | + | | 264 | + final ApiModel apiModel = beanDesc.getClassAnnotations().get(ApiModel.class); | | 265 | + if (apiModel != null && StringUtils.isNotEmpty(apiModel.reference())) { | | 266 | + model.setReference(apiModel.reference()); | | 267 | + } | | 268 | + | 261 | 269 | // if XmlRootElement annotation, construct an Xml object and attach it to the model | 262 | 270 | XmlRootElement rootAnnotation = beanDesc.getClassAnnotations().get(XmlRootElement.class); | 263 | 271 | if (rootAnnotation != null && !"".equals(rootAnnotation.name()) && !"##default".equals(rootAnnotation.name())) { | @@ -266,24 +274,37 @@ public Model resolve(JavaType type, ModelConverterContext context, Iterator<Mode | 266 | 274 | if (rootAnnotation.namespace() != null && !"".equals(rootAnnotation.namespace()) && !"##default".equals(rootAnnotation.namespace())) { | 267 | 275 | xml.namespace(rootAnnotation.namespace()); | 268 | 276 | } | | 277 | + else { | | 278 | + // If namespace was not given in the annotation, look for it in package-info | | 279 | + Package pkg = type.getRawClass().getPackage(); | | 280 | + if(pkg != null) { | | 281 | + XmlSchema xmlSchma = pkg.getAnnotation(XmlSchema.class); | | 282 | + if(xmlSchma != null) { | | 283 | + xml.namespace(xmlSchma.namespace()); | | 284 | + } | | 285 | + } | | 286 | + } | 269 | 287 | model.xml(xml); | 270 | 288 | } | 271 | 289 | final XmlAccessorType xmlAccessorTypeAnnotation = beanDesc.getClassAnnotations().get(XmlAccessorType.class); | 272 | 290 |
| | 291 | + //If JsonSerialize(as=...) is specified then use that bean to figure out all the json-like bits | | 292 | + JsonSerialize jasonSerialize = beanDesc.getClassAnnotations().get(JsonSerialize.class); | | 293 | + if (jasonSerialize != null) { | | 294 | + if (jasonSerialize.as() != null) { | | 295 | + JavaType asType = _mapper.constructType(jasonSerialize.as()); | | 296 | + beanDesc = _mapper.getSerializationConfig().introspect(asType); | | 297 | + } | | 298 | + } | | 299 | + | 273 | 300 | // see if @JsonIgnoreProperties exist | 274 | 301 | Set<String> propertiesToIgnore = new HashSet<String>(); | 275 | 302 | JsonIgnoreProperties ignoreProperties = beanDesc.getClassAnnotations().get(JsonIgnoreProperties.class); | 276 | 303 | if (ignoreProperties != null) { | 277 | 304 | propertiesToIgnore.addAll(Arrays.asList(ignoreProperties.value())); | 278 | 305 | } | 279 | 306 |
| 280 | | - final ApiModel apiModel = beanDesc.getClassAnnotations().get(ApiModel.class); | 281 | 307 | String disc = (apiModel == null) ? "" : apiModel.discriminator(); | 282 | | - | 283 | | - if (apiModel != null && StringUtils.isNotEmpty(apiModel.reference())) { | 284 | | - model.setReference(apiModel.reference()); | 285 | | - } | 286 | | - | 287 | 308 | if (disc.isEmpty()) { | 288 | 309 | // longer method would involve AnnotationIntrospector.findTypeResolver(...) but: | 289 | 310 | JsonTypeInfo typeInfo = beanDesc.getClassAnnotations().get(JsonTypeInfo.class); | @@ -605,7 +626,10 @@ protected boolean ignore(final Annotated member, final XmlAccessorType xmlAccess | 605 | 626 |
| 606 | 627 | private void handleUnwrapped(List<Property> props, Model innerModel, String prefix, String suffix) { | 607 | 628 | if (StringUtils.isBlank(suffix) && StringUtils.isBlank(prefix)) { | 608 | | - props.addAll(innerModel.getProperties().values()); | | 629 | + Map<String, Property> innerProps = innerModel.getProperties(); | | 630 | + if (innerProps != null) { | | 631 | + props.addAll(innerProps.values()); | | 632 | + } | 609 | 633 | } else { | 610 | 634 | if (prefix == null) { | 611 | 635 | prefix = ""; | |
Collapse file Copy file name to clipboardExpand all lines: modules/swagger-core/src/test/java/io/swagger/NumericFormatTest.java +39-36Lines changed: 39 additions & 36 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 15 | 15 | import static org.testng.Assert.assertEquals; | 16 | 16 |
| 17 | 17 | public class NumericFormatTest { | | 18 | + | | 19 | + private static final String NEWLINE = System.getProperty("line.separator"); | | 20 | + | 18 | 21 | @Test | 19 | 22 | public void testFormatOfInteger() { | 20 | 23 | final Map<String, Model> models = ModelConverters.getInstance().readAll(ModelWithIntegerFields.class); | 21 | 24 | assertEquals(models.size(), 1); | 22 | 25 |
| 23 | 26 | String json = Json.pretty(models); | 24 | 27 | assertEquals(json, | 25 | | - "{\n" + | 26 | | - " \"ModelWithIntegerFields\" : {\n" + | 27 | | - " \"type\" : \"object\",\n" + | 28 | | - " \"properties\" : {\n" + | 29 | | - " \"id\" : {\n" + | 30 | | - " \"type\" : \"integer\",\n" + | 31 | | - " \"format\" : \"int32\",\n" + | 32 | | - " \"minimum\" : 3\n" + | 33 | | - " }\n" + | 34 | | - " }\n" + | 35 | | - " }\n" + | | 28 | + "{" + NEWLINE + | | 29 | + " \"ModelWithIntegerFields\" : {" + NEWLINE + | | 30 | + " \"type\" : \"object\"," + NEWLINE + | | 31 | + " \"properties\" : {" + NEWLINE + | | 32 | + " \"id\" : {" + NEWLINE + | | 33 | + " \"type\" : \"integer\"," + NEWLINE + | | 34 | + " \"format\" : \"int32\"," + NEWLINE + | | 35 | + " \"minimum\" : 3" + NEWLINE + | | 36 | + " }" + NEWLINE + | | 37 | + " }" + NEWLINE + | | 38 | + " }" + NEWLINE + | 36 | 39 | "}"); | 37 | 40 | } | 38 | 41 |
| @@ -43,18 +46,18 @@ public void testFormatOfDecimal() { | 43 | 46 |
| 44 | 47 | String json = Json.pretty(models); | 45 | 48 | assertEquals(json, | 46 | | - "{\n" + | 47 | | - " \"ModelWithDecimalFields\" : {\n" + | 48 | | - " \"type\" : \"object\",\n" + | 49 | | - " \"properties\" : {\n" + | 50 | | - " \"id\" : {\n" + | 51 | | - " \"type\" : \"number\",\n" + | 52 | | - " \"format\" : \"double\",\n" + | 53 | | - " \"minimum\" : 3.3,\n" + | 54 | | - " \"exclusiveMinimum\" : false\n" + | 55 | | - " }\n" + | 56 | | - " }\n" + | 57 | | - " }\n" + | | 49 | + "{" + NEWLINE + | | 50 | + " \"ModelWithDecimalFields\" : {" + NEWLINE + | | 51 | + " \"type\" : \"object\"," + NEWLINE + | | 52 | + " \"properties\" : {" + NEWLINE + | | 53 | + " \"id\" : {" + NEWLINE + | | 54 | + " \"type\" : \"number\"," + NEWLINE + | | 55 | + " \"format\" : \"double\"," + NEWLINE + | | 56 | + " \"minimum\" : 3.3," + NEWLINE + | | 57 | + " \"exclusiveMinimum\" : false" + NEWLINE + | | 58 | + " }" + NEWLINE + | | 59 | + " }" + NEWLINE + | | 60 | + " }" + NEWLINE + | 58 | 61 | "}"); | 59 | 62 | } | 60 | 63 |
| @@ -66,19 +69,19 @@ public void testFormatOfBigDecimal() { | 66 | 69 | String json = Json.pretty(models); | 67 | 70 |
| 68 | 71 | assertEquals(json, | 69 | | - "{\n" + | 70 | | - " \"ModelWithoutScientificFields\" : {\n" + | 71 | | - " \"type\" : \"object\",\n" + | 72 | | - " \"properties\" : {\n" + | 73 | | - " \"id\" : {\n" + | 74 | | - " \"type\" : \"number\",\n" + | 75 | | - " \"minimum\" : -9999999999999999.99,\n" + | 76 | | - " \"maximum\" : 9999999999999999.99,\n" + | 77 | | - " \"exclusiveMinimum\" : false,\n" + | 78 | | - " \"exclusiveMaximum\" : false\n" + | 79 | | - " }\n" + | 80 | | - " }\n" + | 81 | | - " }\n" + | | 72 | + "{" + NEWLINE + | | 73 | + " \"ModelWithoutScientificFields\" : {" + NEWLINE + | | 74 | + " \"type\" : \"object\"," + NEWLINE + | | 75 | + " \"properties\" : {" + NEWLINE + | | 76 | + " \"id\" : {" + NEWLINE + | | 77 | + " \"type\" : \"number\"," + NEWLINE + | | 78 | + " \"minimum\" : -9999999999999999.99," + NEWLINE + | | 79 | + " \"maximum\" : 9999999999999999.99," + NEWLINE + | | 80 | + " \"exclusiveMinimum\" : false," + NEWLINE + | | 81 | + " \"exclusiveMaximum\" : false" + NEWLINE + | | 82 | + " }" + NEWLINE + | | 83 | + " }" + NEWLINE + | | 84 | + " }" + NEWLINE + | 82 | 85 | "}"); | 83 | 86 |
| 84 | 87 | } | |
Collapse file Copy file name to clipboard +41Lines changed: 41 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package io.swagger.jackson; | | 2 | + | | 3 | +import com.fasterxml.jackson.databind.ObjectMapper; | | 4 | +import com.fasterxml.jackson.databind.annotation.JsonSerialize; | | 5 | +import io.swagger.converter.ModelConverterContextImpl; | | 6 | +import io.swagger.models.Model; | | 7 | +import io.swagger.models.properties.Property; | | 8 | +import org.immutables.value.Value; | | 9 | +import org.testng.annotations.Test; | | 10 | + | | 11 | +import java.util.Iterator; | | 12 | +import java.util.Map; | | 13 | + | | 14 | +import static org.testng.Assert.assertEquals; | | 15 | + | | 16 | +public class ImmutablesGenerationTest extends SwaggerTestBase { | | 17 | + private final ModelResolver modelResolver = new ModelResolver(new ObjectMapper()); | | 18 | + private final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver); | | 19 | + | | 20 | + @Test | | 21 | + public void testImmutables() { | | 22 | + final Model model = context.resolve(TestWidget.class); | | 23 | + final Map<String, Property> props = model.getProperties(); | | 24 | + assertEquals(props.size(), 2); | | 25 | + | | 26 | + Iterator<Property> it = props.values().iterator(); | | 27 | + Property prop; | | 28 | + | | 29 | + prop = it.next(); | | 30 | + assertEquals(prop.getName(), "wibble"); | | 31 | + prop = it.next(); | | 32 | + assertEquals(prop.getName(), "wobble"); | | 33 | + } | | 34 | + | | 35 | + @Value.Immutable | | 36 | + @JsonSerialize(as=ImmutableTestWidget.class) | | 37 | + public interface TestWidget { | | 38 | + public String wibble(); | | 39 | + public String wobble(); | | 40 | + } | | 41 | +} |
Collapse file Copy file name to clipboard +54Lines changed: 54 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package io.swagger.jackson; | | 2 | + | | 3 | +import com.fasterxml.jackson.databind.JavaType; | | 4 | +import com.fasterxml.jackson.databind.ObjectMapper; | | 5 | +import io.swagger.converter.ModelConverterContextImpl; | | 6 | +import io.swagger.models.Link; | | 7 | +import io.swagger.models.Model; | | 8 | +import io.swagger.models.ModelImpl; | | 9 | +import io.swagger.models.Xml; | | 10 | +import org.testng.annotations.DataProvider; | | 11 | +import org.testng.annotations.Test; | | 12 | + | | 13 | +import javax.xml.bind.annotation.XmlRootElement; | | 14 | + | | 15 | +import static org.testng.Assert.*; | | 16 | + | | 17 | +public class ModelResolverTest extends SwaggerTestBase { | | 18 | + | | 19 | + @Test(dataProvider = "testXmlNamespaceData") | | 20 | + public void testXmlNamespace(Class clazz, String name, String namespace) throws Exception { | | 21 | + final ObjectMapper mapper = mapper(); | | 22 | + final ModelResolver modelResolver = new ModelResolver(mapper); | | 23 | + final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver); | | 24 | + | | 25 | + final JavaType javaType = mapper.constructType(clazz); | | 26 | + | | 27 | + final Model model = modelResolver.resolve(javaType, context, null); | | 28 | + | | 29 | + assertTrue(model instanceof ModelImpl); | | 30 | + | | 31 | + final Xml xml = ((ModelImpl) model).getXml(); | | 32 | + assertNotNull(xml); | | 33 | + assertEquals(xml.getName(), name); | | 34 | + assertEquals(xml.getNamespace(), namespace); | | 35 | + } | | 36 | + | | 37 | + @DataProvider | | 38 | + private Object[][] testXmlNamespaceData() { | | 39 | + return new Object[][]{ | | 40 | + {Link.class, "link", null}, | | 41 | + {TypeWithNamespace.class, "TypeWithNamespace", "http://io.swagger/jackson"}, | | 42 | + {TypeWithoutNamespace.class, "TypeWithOutNamespace", "http://io.swagger/jackson/package"} | | 43 | + }; | | 44 | + } | | 45 | + | | 46 | + @XmlRootElement(name = "TypeWithNamespace", namespace = "http://io.swagger/jackson") | | 47 | + public static class TypeWithNamespace { | | 48 | + } | | 49 | + | | 50 | + @XmlRootElement(name = "TypeWithOutNamespace") | | 51 | + public static class TypeWithoutNamespace { | | 52 | + } | | 53 | + | | 54 | +} |
Collapse file Copy file name to clipboardExpand all lines: modules/swagger-core/src/test/java/io/swagger/jackson/SimpleGenerationTest.java +34Lines changed: 34 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 10 | 10 | import com.fasterxml.jackson.annotation.JsonPropertyOrder; | 11 | 11 | import com.fasterxml.jackson.databind.ObjectMapper; | 12 | 12 |
| | 13 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | | 14 | +import com.fasterxml.jackson.databind.annotation.JsonSerialize; | 13 | 15 | import io.swagger.annotations.ApiModel; | 14 | 16 | import io.swagger.annotations.ApiModelProperty; | 15 | 17 | import io.swagger.converter.ModelConverterContextImpl; | @@ -117,6 +119,38 @@ public void testComplex() throws Exception { | 117 | 119 | assertEquals(props.size(), 6); | 118 | 120 | } | 119 | 121 |
| | 122 | + @Test | | 123 | + public void testNotBeanlike() throws Exception { | | 124 | + final Model model = context.resolve(NotBeanlike.class); | | 125 | + final Map<String, Property> props = model.getProperties(); | | 126 | + assertEquals(props.size(), 1); | | 127 | + | | 128 | + final Property prop = props.values().iterator().next(); | | 129 | + assertEquals(prop.getName(), "notGetter"); | | 130 | + } | | 131 | + | | 132 | + static class NotBeanlike { | | 133 | + @JsonProperty("notGetter") | | 134 | + public String notGetter() {return "Not a getter";} | | 135 | + } | | 136 | + | | 137 | + @Test | | 138 | + public void testEvenLessBeanlike() throws Exception { | | 139 | + final Model model = context.resolve(EvenLessBeanlike.class); | | 140 | + final Map<String, Property> props = model.getProperties(); | | 141 | + assertEquals(props.size(), 1); | | 142 | + | | 143 | + final Property prop = props.values().iterator().next(); | | 144 | + assertEquals(prop.getName(), "notGetter"); | | 145 | + } | | 146 | + | | 147 | + @JsonSerialize(as = NotBeanlike.class) | | 148 | + @JsonDeserialize(as = NotBeanlike.class) | | 149 | + static interface EvenLessBeanlike { | | 150 | + public String notGetter(); | | 151 | + } | | 152 | + | | 153 | + | 120 | 154 | /* | 121 | 155 | /********************************************************** | 122 | 156 | /* Test methods | |
Collapse file Copy file name to clipboard +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +@javax.xml.bind.annotation.XmlSchema(namespace = "http://io.swagger/jackson/package") | | 2 | +package io.swagger.jackson; |
Collapse file Copy file name to clipboardExpand all lines: modules/swagger-hibernate-validations/pom.xml +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | <parent> | 5 | 5 | <groupId>io.swagger</groupId> | 6 | 6 | <artifactId>swagger-project</artifactId> | 7 | | - <version>1.5.13</version> | | 7 | + <version>1.5.14-SNAPSHOT</version> | 8 | 8 | <relativePath>../..</relativePath> | 9 | 9 | </parent> | 10 | 10 | <modelVersion>4.0.0</modelVersion> | 11 | 11 | <groupId>io.swagger</groupId> | 12 | 12 | <artifactId>swagger-hibernate-validations</artifactId> | 13 | 13 | <packaging>jar</packaging> | 14 | | - <version>1.5.13</version> | | 14 | + <version>1.5.14-SNAPSHOT</version> | 15 | 15 | <name>swagger-hibernate-validations</name> | 16 | 16 | <build> | 17 | 17 | <sourceDirectory>src/main/java</sourceDirectory> | |
|