diff --git a/modules/swagger-core/src/main/java/io/swagger/util/PropertyDeserializer.java b/modules/swagger-core/src/main/java/io/swagger/util/PropertyDeserializer.java index 52c833d68e..f7c7ff4f14 100644 --- a/modules/swagger-core/src/main/java/io/swagger/util/PropertyDeserializer.java +++ b/modules/swagger-core/src/main/java/io/swagger/util/PropertyDeserializer.java @@ -1,5 +1,15 @@ package io.swagger.util; +import java.io.IOException; +import java.util.ArrayList; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationContext; @@ -13,7 +23,6 @@ import com.fasterxml.jackson.databind.node.NumericNode; import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.TextNode; - import io.swagger.models.Xml; import io.swagger.models.properties.ArrayProperty; import io.swagger.models.properties.MapProperty; @@ -22,17 +31,6 @@ import io.swagger.models.properties.PropertyBuilder; import io.swagger.models.properties.RefProperty; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.EnumMap; -import java.util.List; -import java.util.Map; -import java.util.HashMap; -import java.util.Iterator; - public class PropertyDeserializer extends JsonDeserializer { Logger LOGGER = LoggerFactory.getLogger(PropertyDeserializer.class); @@ -155,7 +153,9 @@ Property propertyFromNode(JsonNode node) { if (detailNode != null) { Property items = propertyFromNode(detailNode); if (items != null) { - return new MapProperty(items).description(description); + MapProperty mapProperty = new MapProperty(items).description(description); + mapProperty.setVendorExtensionMap(getVendorExtensions(node)); + return mapProperty; } } else { detailNode = node.get("properties"); @@ -167,7 +167,9 @@ Property propertyFromNode(JsonNode node) { properties.put(field.getKey(), property); } } - return new ObjectProperty(properties); + ObjectProperty objectProperty = new ObjectProperty(properties); + objectProperty.setVendorExtensionMap(getVendorExtensions(node)); + return objectProperty; } } if (ArrayProperty.isType(type)) { diff --git a/modules/swagger-core/src/test/java/io/swagger/MapPropertyDeserializerTest.java b/modules/swagger-core/src/test/java/io/swagger/MapPropertyDeserializerTest.java index 3256c90d7b..bf28fa7f68 100644 --- a/modules/swagger-core/src/test/java/io/swagger/MapPropertyDeserializerTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/MapPropertyDeserializerTest.java @@ -1,7 +1,10 @@ package io.swagger; +import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertTrue; + +import org.testng.annotations.Test; import io.swagger.models.Operation; import io.swagger.models.Response; import io.swagger.models.properties.IntegerProperty; @@ -9,36 +12,35 @@ import io.swagger.models.properties.Property; import io.swagger.util.Json; -import org.testng.annotations.Test; - public class MapPropertyDeserializerTest { + private static final String json = "{" + + " \"tags\": [\"store\"]," + + " \"summary\": \"Returns pet inventories by status\"," + + " \"description\": \"Returns a map of status codes to quantities\"," + + " \"operationId\": \"getInventory\"," + + " \"produces\": [\"application/json\"]," + + " \"parameters\": []," + + " \"responses\": {" + + " \"200\": {" + + " \"description\": \"successful operation\"," + + " \"schema\": {" + + " \"type\": \"object\"," + + " \"x-foo\": \"vendor x\"," + + " \"additionalProperties\": {" + + " \"type\": \"integer\"," + + " \"format\": \"int32\"" + + " }" + + " }" + + " }" + + " }," + + " \"security\": [{" + + " \"api_key\": []" + + " }]" + + "}"; + @Test(description = "it should deserialize a response per #1349") public void testMapDerserilization () throws Exception { - String json = "{" + - " \"tags\": [\"store\"]," + - " \"summary\": \"Returns pet inventories by status\"," + - " \"description\": \"Returns a map of status codes to quantities\"," + - " \"operationId\": \"getInventory\"," + - " \"produces\": [\"application/json\"]," + - " \"parameters\": []," + - " \"responses\": {" + - " \"200\": {" + - " \"description\": \"successful operation\"," + - " \"schema\": {" + - " \"type\": \"object\"," + - " \"additionalProperties\": {" + - " \"type\": \"integer\"," + - " \"format\": \"int32\"" + - " }" + - " }" + - " }" + - " }," + - " \"security\": [{" + - " \"api_key\": []" + - " }]" + - "}"; - Operation operation = Json.mapper().readValue(json, Operation.class); Response response = operation.getResponses().get("200"); assertNotNull(response); @@ -50,4 +52,19 @@ public void testMapDerserilization () throws Exception { MapProperty mp = (MapProperty) responseSchema; assertTrue(mp.getAdditionalProperties() instanceof IntegerProperty); } + + @Test(description = "vendor extensions should be included with object type") + public void testMapDeserilizationVendorExtensions () throws Exception { + Operation operation = Json.mapper().readValue(json, Operation.class); + Response response = operation.getResponses().get("200"); + assertNotNull(response); + + Property responseSchema = response.getSchema(); + assertNotNull(responseSchema); + + MapProperty mp = (MapProperty) responseSchema; + assertTrue(mp.getVendorExtensions().size() > 0); + assertNotNull(mp.getVendorExtensions().get("x-foo")); + assertEquals(mp.getVendorExtensions().get("x-foo"), "vendor x"); + } } \ No newline at end of file diff --git a/modules/swagger-core/src/test/java/io/swagger/ObjectPropertyTest.java b/modules/swagger-core/src/test/java/io/swagger/ObjectPropertyTest.java index bb5759532c..2fad049dfe 100644 --- a/modules/swagger-core/src/test/java/io/swagger/ObjectPropertyTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/ObjectPropertyTest.java @@ -1,17 +1,18 @@ package io.swagger; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertTrue; +import java.io.IOException; + +import org.testng.annotations.Test; import io.swagger.models.ModelImpl; import io.swagger.models.properties.ObjectProperty; import io.swagger.models.properties.Property; import io.swagger.models.properties.StringProperty; import io.swagger.util.Json; -import org.testng.annotations.Test; - -import java.io.IOException; - public class ObjectPropertyTest { @Test (description = "convert a model with object properties") public void readModelWithObjectProperty() throws IOException { @@ -22,6 +23,7 @@ public void readModelWithObjectProperty() throws IOException { " }," + " \"someObject\":{" + " \"type\":\"object\"," + + " \"x-foo\": \"vendor x\"," + " \"properties\":{" + " \"innerId\":{" + " \"type\":\"string\"" + @@ -40,5 +42,9 @@ public void readModelWithObjectProperty() throws IOException { Property sp = op.getProperties().get("innerId"); assertTrue(sp instanceof StringProperty); + + assertTrue(op.getVendorExtensions() != null); + assertNotNull(op.getVendorExtensions().get("x-foo")); + assertEquals(op.getVendorExtensions().get("x-foo"), "vendor x"); } }