Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vendor extentions to ObjectProperty and MapProperty. [241] #1414

Merged
merged 18 commits into from
Sep 8, 2015
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
@@ -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;
Expand All @@ -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;
Expand All @@ -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<Property> {
Logger LOGGER = LoggerFactory.getLogger(PropertyDeserializer.class);

Expand Down Expand Up @@ -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");
Expand All @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
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;
import io.swagger.models.properties.MapProperty;
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);
Expand All @@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -22,6 +23,7 @@ public void readModelWithObjectProperty() throws IOException {
" }," +
" \"someObject\":{" +
" \"type\":\"object\"," +
" \"x-foo\": \"vendor x\"," +
" \"properties\":{" +
" \"innerId\":{" +
" \"type\":\"string\"" +
Expand All @@ -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");
}
}