Skip to content

Commit

Permalink
Merge pull request #159 from swagger-api/issue-158
Browse files Browse the repository at this point in the history
Type checking
  • Loading branch information
fehguy committed Jan 7, 2016
2 parents 9b0e8c5 + 26e1586 commit 46cb2fb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,43 +1,10 @@
package io.swagger.parser.util;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;
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.ArrayModel;
import io.swagger.models.ComposedModel;
import io.swagger.models.Contact;
import io.swagger.models.ExternalDocs;
import io.swagger.models.Info;
import io.swagger.models.License;
import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.Operation;
import io.swagger.models.Path;
import io.swagger.models.RefModel;
import io.swagger.models.RefPath;
import io.swagger.models.RefResponse;
import io.swagger.models.Response;
import io.swagger.models.Scheme;
import io.swagger.models.SecurityRequirement;
import io.swagger.models.Swagger;
import io.swagger.models.Tag;
import io.swagger.models.Xml;
import io.swagger.models.auth.ApiKeyAuthDefinition;
import io.swagger.models.auth.BasicAuthDefinition;
import io.swagger.models.auth.In;
import io.swagger.models.auth.OAuth2Definition;
import io.swagger.models.auth.SecuritySchemeDefinition;
import io.swagger.models.parameters.AbstractSerializableParameter;
import io.swagger.models.parameters.FormParameter;
import io.swagger.models.parameters.HeaderParameter;
import io.swagger.models.parameters.Parameter;
import io.swagger.models.parameters.PathParameter;
import io.swagger.models.parameters.QueryParameter;
import io.swagger.models.parameters.RefParameter;
import com.fasterxml.jackson.databind.node.*;
import io.swagger.models.*;
import io.swagger.models.auth.*;
import io.swagger.models.parameters.*;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.PropertyBuilder;
import io.swagger.util.Json;
Expand Down Expand Up @@ -1322,6 +1289,9 @@ public Double getDouble(String key, ObjectNode node, boolean required, String lo
else if(v.getNodeType().equals(JsonNodeType.NUMBER)) {
value = v.asDouble();
}
else if(!v.isValueNode()) {
result.invalidType(location, key, "double", node);
}
return value;
}

Expand All @@ -1337,6 +1307,9 @@ public Number getNumber(String key, ObjectNode node, boolean required, String lo
else if(v.getNodeType().equals(JsonNodeType.NUMBER)) {
value = v.numberValue();
}
else if(!v.isValueNode()) {
result.invalidType(location, key, "number", node);
}
return value;
}

Expand All @@ -1352,6 +1325,9 @@ public Integer getInteger(String key, ObjectNode node, boolean required, String
else if(v.getNodeType().equals(JsonNodeType.NUMBER)) {
value = v.intValue();
}
else if(!v.isValueNode()) {
result.invalidType(location, key, "integer", node);
}
return value;
}

Expand All @@ -1364,6 +1340,9 @@ public String getString(String key, ObjectNode node, boolean required, String lo
result.invalid();
}
}
else if(!v.isValueNode()) {
result.invalidType(location, key, "string", node);
}
else {
value = v.asText();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -710,4 +710,21 @@ public void testDeserializeEnum() {
assertEquals(enumValues.get(0), "First");
assertEquals(enumValues.get(1), "Second");
}

@Test
public void testDeserializeWithMessages() {
String yaml = "swagger: '2.0'\n" +
"info:\n" +
" version: 0.0.0\n" +
" title:\n" +
" - bar";
SwaggerParser parser = new SwaggerParser();
SwaggerDeserializationResult result = parser.readWithInfo(yaml);

Set<String> messages = new HashSet<String>(result.getMessages());
assertTrue(messages.size() == 2);

assertTrue(messages.contains("attribute info.title is not of type `string`"));
assertTrue(messages.contains("attribute paths is missing"));
}
}

0 comments on commit 46cb2fb

Please sign in to comment.