Search in sources :

Example 16 with SwaggerParser

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);
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 17 with SwaggerParser

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");
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) Test(org.testng.annotations.Test)

Example 18 with SwaggerParser

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);
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) Test(org.testng.annotations.Test)

Example 19 with SwaggerParser

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"));
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) Test(org.testng.annotations.Test)

Example 20 with SwaggerParser

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");
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) SecuritySchemeDefinition(io.swagger.models.auth.SecuritySchemeDefinition) BasicAuthDefinition(io.swagger.models.auth.BasicAuthDefinition) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

SwaggerParser (io.swagger.parser.SwaggerParser)91 Swagger (io.swagger.models.Swagger)44 Test (org.testng.annotations.Test)37 HashSet (java.util.HashSet)21 HashMap (java.util.HashMap)14 Path (io.swagger.models.Path)11 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 Map (java.util.Map)10 Operation (io.swagger.models.Operation)9 List (java.util.List)9 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)9 SwaggerDeserializationResult (io.swagger.parser.util.SwaggerDeserializationResult)8 Test (org.junit.Test)8 HttpMethod (io.swagger.models.HttpMethod)7 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)7 File (java.io.File)7 OAuth2Definition (io.swagger.models.auth.OAuth2Definition)6 SecuritySchemeDefinition (io.swagger.models.auth.SecuritySchemeDefinition)6 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)6