use of io.swagger.parser.SwaggerParser in project swagger-parser by swagger-api.
the class SwaggerDeserializerTest method testUntypedAdditionalProperties.
@Test(description = "it should deserialize untyped additionalProperties")
public void testUntypedAdditionalProperties() {
String json = "{\n" + " \"paths\": {\n" + " \"/store/inventory\": {\n" + " \"get\": {\n" + " \"responses\": {\n" + " \"200\": {\n" + " \"description\": \"successful operation\",\n" + " \"schema\": {\n" + " \"type\": \"object\",\n" + " \"description\": \"map of anything\",\n" + " \"additionalProperties\": {}\n" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n" + "}";
SwaggerParser parser = new SwaggerParser();
SwaggerDeserializationResult result = parser.readWithInfo(json);
List<String> messageList = result.getMessages();
Set<String> messages = new HashSet<String>(messageList);
Swagger swagger = result.getSwagger();
Property response = swagger.getPath("/store/inventory").getGet().getResponses().get("200").getSchema();
assertTrue(response instanceof MapProperty);
Property additionalProperties = ((MapProperty) response).getAdditionalProperties();
assertTrue(additionalProperties instanceof UntypedProperty);
assertEquals(additionalProperties.getType(), null);
}
use of io.swagger.parser.SwaggerParser in project swagger-parser by swagger-api.
the class SwaggerDeserializerTest method testDeserializeWithEnumDiscriminator.
@Test
public void testDeserializeWithEnumDiscriminator() {
String yaml = "swagger: '2.0'\n" + "definitions: \n" + " Animal:\n" + " type: object\n" + " discriminator: petType\n" + " description: |\n" + " A basic `Animal` object which can extend to other animal types.\n" + " required:\n" + " - commonName\n" + " - petType\n" + " properties:\n" + " commonName:\n" + " description: the household name of the animal\n" + " type: string\n" + " petType:\n" + " description: |\n" + " The discriminator for the animal type. It _must_\n" + " match one of the concrete schemas by name (i.e. `Cat`)\n" + " for proper deserialization\n" + " enum:\n" + " - cat\n" + " - dog";
SwaggerParser parser = new SwaggerParser();
SwaggerDeserializationResult result = parser.readWithInfo(yaml);
Map<String, Property> properties = result.getSwagger().getDefinitions().get("Animal").getProperties();
assertTrue(properties.containsKey("commonName"));
assertTrue(properties.containsKey("petType"));
assertEquals(properties.get("petType").getType(), "string");
}
use of io.swagger.parser.SwaggerParser in project swagger-parser by swagger-api.
the class SwaggerDeserializerTest method testIssue204_allOf.
@Test
public void testIssue204_allOf() throws Exception {
String json = "{\n" + " \"swagger\": \"2.0\",\n" + " \"info\": {\n" + " \"version\": \"2.0.0\",\n" + " \"title\": \"Test allOf API\",\n" + " \"description\": \"Tests the allOf API for parent, interface and child models.\"\n" + " },\n" + " \"paths\": {\n" + " \"/\": {\n" + " \"get\": {\n" + " \"responses\": {\n" + " \"200\": {\n" + " \"description\": \"OK\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + " },\n" + " \"definitions\": {\n" + " \"Pet\": {\n" + " \"type\": \"object\",\n" + " \"required\": [\n" + " \"id\"\n" + " ],\n" + " \"properties\": {\n" + " \"id\": {\n" + " \"type\": \"integer\",\n" + " \"format\": \"int64\"\n" + " }\n" + " }\n" + " },\n" + " \"Furry\": {\n" + " \"type\": \"object\",\n" + " \"required\": [\n" + " \"coatColour\"\n" + " ],\n" + " \"properties\": {\n" + " \"coatColour\": {\n" + " \"type\": \"string\"\n" + " }\n" + " }\n" + " },\n" + " \"Dog\": {\n" + " \"type\": \"object\",\n" + " \"allOf\": [\n" + " {\n" + " \"$ref\": \"#/definitions/Pet\"\n" + " },\n" + " {\n" + " \"$ref\": \"#/definitions/Furry\"\n" + " },\n" + " {\n" + " \"required\": [\n" + " \"name\"\n" + " ],\n" + " \"properties\": {\n" + " \"name\": {\n" + " \"type\": \"string\"\n" + " }\n" + " }\n" + " }\n" + " ]\n" + " }\n" + " }\n" + "}";
SwaggerParser parser = new SwaggerParser();
SwaggerDeserializationResult result = parser.readWithInfo(json);
assertTrue("Parser returned errors:", result.getMessages().isEmpty());
Swagger swagger = result.getSwagger();
assertNotNull("Parser result does not contain a Swagger instance", swagger);
Map<String, Model> definitions = swagger.getDefinitions();
assertNotNull("Swagger instance does not contain any definitions", definitions);
assertEquals("Missing/extraneous definition;", 3, definitions.size());
Model pet = definitions.get("Pet");
Model furry = definitions.get("Furry");
Model dog = definitions.get("Dog");
assertNotNull("Pet model not found", pet);
assertNotNull("Furry model not found", furry);
assertNotNull("Dog model not found", dog);
assertTrue("Dog model is not composed", dog instanceof ComposedModel);
ComposedModel dogComposed = (ComposedModel) dog;
assertNotNull("Dog does not implement any interfaces", dogComposed.getInterfaces());
assertEquals("Dog implements the wrong number of interfaces;", 2, dogComposed.getInterfaces().size());
RefModel dogInterfaceRef = dogComposed.getInterfaces().get(0);
Model dogInterface = definitions.get(dogInterfaceRef.getSimpleRef());
assertEquals("Dog does not implement Pet;", pet, dogInterface);
dogInterfaceRef = dogComposed.getInterfaces().get(1);
dogInterface = definitions.get(dogInterfaceRef.getSimpleRef());
assertEquals("Dog does not implement Furry;", furry, dogInterface);
assertTrue("Dog does not have child properties", dogComposed.getChild() instanceof ModelImpl);
}
use of io.swagger.parser.SwaggerParser in project swagger-parser by swagger-api.
the class SwaggerDeserializerTest method testIssue151.
@Test
public void testIssue151() throws Exception {
String json = "{\n" + " \"swagger\": \"2.0\",\n" + " \"info\": {\n" + " \"version\": \"2.0.0\",\n" + " \"title\": \"Test Issue 151\",\n" + " \"description\": \"Tests that ComposedModel vendor extensions are deserialized correctly.\"\n" + " },\n" + " \"paths\": {\n" + " \"/\": {\n" + " \"get\": {\n" + " \"responses\": {\n" + " \"200\": {\n" + " \"description\": \"OK\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + " },\n" + " \"definitions\": {\n" + " \"Pet\": {\n" + " \"type\": \"object\",\n" + " \"required\": [\n" + " \"id\"\n" + " ],\n" + " \"properties\": {\n" + " \"id\": {\n" + " \"type\": \"integer\",\n" + " \"format\": \"int64\"\n" + " }\n" + " }\n" + " },\n" + " \"Dog\": {\n" + " \"type\": \"object\",\n" + " \"allOf\": [\n" + " {\n" + " \"$ref\": \"#/definitions/Pet\"\n" + " },\n" + " {\n" + " \"required\": [\n" + " \"name\"\n" + " ],\n" + " \"properties\": {\n" + " \"name\": {\n" + " \"type\": \"string\"\n" + " }\n" + " }\n" + " }\n" + " ],\n" + " \"x-vendor-ext\": \"some data\"\n" + " }\n" + " }\n" + "}";
SwaggerParser parser = new SwaggerParser();
SwaggerDeserializationResult result = parser.readWithInfo(json);
assertTrue("Parser returned errors:", result.getMessages().isEmpty());
Swagger swagger = result.getSwagger();
Map<String, Model> definitions = swagger.getDefinitions();
assertTrue(definitions.size() == 2);
Model allOfModel = definitions.get("Dog");
assertTrue(allOfModel instanceof ComposedModel);
assertFalse(allOfModel.getVendorExtensions().isEmpty());
assertEquals("some data", allOfModel.getVendorExtensions().get("x-vendor-ext"));
}
use of io.swagger.parser.SwaggerParser in project swagger-parser by swagger-api.
the class SwaggerDeserializerTest method testSecurityDefinition.
@Test
public void testSecurityDefinition() {
String json = "{\n" + " \"swagger\": \"2.0\",\n" + " \"securityDefinitions\": {\n" + " \"basic_auth\": {\n" + " \"type\": \"basic\",\n" + " \"x-foo\": \"basicBar\"\n" + " },\n" + " \"api_key\": {\n" + " \"type\": \"apiKey\",\n" + " \"name\": \"api_key\",\n" + " \"in\": \"header\",\n" + " \"description\": \"api key description\",\n" + " \"x-foo\": \"apiKeyBar\"\n" + " }\n" + " },\n" + " \"paths\": {\n" + " \"/pet\": {\n" + " \"get\": {\n" + " \"security\": [\n" + " {\n" + " \"basic_auth\": [],\n" + " \"api_key\": []\n" + " }\n" + " ]\n" + " }\n" + " }\n" + " }\n" + "}";
SwaggerParser parser = new SwaggerParser();
SwaggerDeserializationResult result = parser.readWithInfo(json);
List<String> messageList = result.getMessages();
Set<String> messages = new HashSet<String>(messageList);
Swagger swagger = result.getSwagger();
assertNotNull(swagger.getSecurityDefinitions());
assertTrue(swagger.getSecurityDefinitions().keySet().size() == 2);
// Basic Authentication
SecuritySchemeDefinition definitionBasic = swagger.getSecurityDefinitions().get("basic_auth");
assertNotNull(definitionBasic);
assertTrue(definitionBasic instanceof BasicAuthDefinition);
assertEquals(definitionBasic.getVendorExtensions().get("x-foo"), "basicBar");
// API Key Authentication
SecuritySchemeDefinition definition = swagger.getSecurityDefinitions().get("api_key");
assertNotNull(definition);
assertTrue(definition instanceof ApiKeyAuthDefinition);
ApiKeyAuthDefinition apiKey = (ApiKeyAuthDefinition) definition;
assertEquals(apiKey.getName(), "api_key");
assertEquals(apiKey.getIn(), In.HEADER);
assertEquals(apiKey.getDescription(), "api key description");
assertEquals(apiKey.getVendorExtensions().get("x-foo"), "apiKeyBar");
}
Aggregations