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 @@ -21,6 +21,8 @@
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 @@ -88,6 +90,17 @@ Property propertyFromNode(JsonNode node) {
if (items != null) {
return new MapProperty(items).description(description);
}
} else {
detailNode = node.get("properties");
Map<String, Property> properties = new HashMap<String, Property>();
if(detailNode != null){
for(Iterator<Map.Entry<String,JsonNode>> iter = detailNode.fields(); iter.hasNext();){
Map.Entry<String,JsonNode> field = iter.next();
Property property = propertyFromNode(field.getValue());
properties.put(field.getKey(), property);
}
}
return new ObjectProperty(properties);
}
}
if (ArrayProperty.isType(type)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import io.swagger.models.Swagger
import io.swagger.util.Json
import io.swagger.models.properties._
import org.junit.runner.RunWith
import org.scalatest.{FlatSpec, Matchers}
import org.scalatest.junit.JUnitRunner
import scala.collection.JavaConverters._

@RunWith(classOf[JUnitRunner])
class JsonDeserializationTest extends FlatSpec with Matchers {
val m = Json.mapper()

it should "deserialize the petstore" in {
val s = m.readValue(new java.io.File("src/test/scala/specFiles/petstore.json"), classOf[Swagger])
s.isInstanceOf[Swagger] should be(true)
}

it should "deserialize the composition test" in {
val c = m.readValue(new java.io.File("src/test/scala/specFiles/compositionTest.json"), classOf[Swagger])
c.isInstanceOf[Swagger] should be(true)

// Json.prettyPrint(c)
}

it should "deserialize a simple ObjectProperty" in {
val json = """
{
"type": "object",
"description" : "top level object",
"properties" : {
"property1" : {
"type": "string",
"description": "First property"
},
"property2" : {
"type": "string",
"description": "Second property"
},
"property3" : {
"type": "string",
"description": "Third property"
}
}
}
"""
val result = m.readValue(json, classOf[Property])
result.isInstanceOf[ObjectProperty] should be(true)
result.asInstanceOf[ObjectProperty].getProperties.asScala.size should be(3)

}

it should "deserialize nested ObjectProperty(s)" in {
val json = """
{
"type": "object",
"description" : "top level object",
"properties" : {
"property1" : {
"type": "string",
"description": "First property"
},
"property2" : {
"type": "string",
"description": "Second property"
},
"property3" : {
"type": "object",
"description": "Third property",
"properties": {
"property1" : {
"type": "string",
"description": "First nested property"
}
}
}
}
}
"""
val result = m.readValue(json, classOf[Property])
result.isInstanceOf[ObjectProperty] should be(true)

val topProperty = result.asInstanceOf[ObjectProperty]
val firstLevelProperties = topProperty.getProperties.asScala
firstLevelProperties.size should be(3)

val property3 = firstLevelProperties("property3")
property3.isInstanceOf[ObjectProperty] should be(true)

val secondLevelProperties = property3.asInstanceOf[ObjectProperty].getProperties.asScala
secondLevelProperties.size should be(1)
}


}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package io.swagger.models.properties;

import io.swagger.models.Xml;
import java.util.Map;

public class ObjectProperty extends AbstractProperty implements Property {
public static final String TYPE = "object";

Map<String, Property> properties;

public ObjectProperty() {
super.type = TYPE;
}

public ObjectProperty(Map<String, Property> properties) {
super.type = TYPE;
this.properties = properties;
}

public static boolean isType(String type) {
return TYPE.equals(type);
}
Expand All @@ -18,6 +26,19 @@ public static boolean isType(String type, String format) {
return isType(type);
}

public ObjectProperty properties(Map<String, Property> properties) {
this.setProperties(properties);
return this;
}

public Map<String, Property> getProperties(){
return this.properties;
}

public void setProperties(Map<String, Property> properties){
this.properties = properties;
}

public ObjectProperty xml(Xml xml) {
this.setXml(xml);
return this;
Expand Down