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

Type checking #159

Merged
merged 1 commit into from
Jan 7, 2016
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,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"));
}
}