Search in sources :

Example 56 with SwaggerParser

use of io.swagger.parser.SwaggerParser in project swagger-parser by swagger-api.

the class SwaggerDeserializerTest method testIssue343Parameter.

@Test
public void testIssue343Parameter() {
    String yaml = "swagger: '2.0'\n" + "info:\n" + "  description: 'bleh'\n" + "  version: '2.0.0'\n" + "  title: 'Test'\n" + "paths:\n" + "  /foo:\n" + "    post:\n" + "      parameters:\n" + "        - in: query\n" + "          name: skip\n" + "          type: integer\n" + "          format: int32\n" + "          multipleOf: 3\n" + "        - in: body\n" + "          name: body\n" + "          required: true\n" + "          schema:\n" + "            type: object\n" + "            additionalProperties:\n" + "              type: string\n" + "      responses:\n" + "        200:\n" + "          description: 'OK'\n" + "definitions:\n" + "  Fun:\n" + "    properties:\n" + "      id:\n" + "        type: integer\n" + "        format: int32\n" + "        multipleOf: 5\n" + "      mySet:\n" + "        type: array\n" + "        uniqueItems: true\n" + "        items:\n" + "          type: string";
    SwaggerParser parser = new SwaggerParser();
    SwaggerDeserializationResult result = parser.readWithInfo(yaml);
    Swagger swagger = result.getSwagger();
    QueryParameter qp = (QueryParameter) swagger.getPath("/foo").getPost().getParameters().get(0);
    assertEquals(qp.getMultipleOf(), 3.0);
    BodyParameter bp = (BodyParameter) swagger.getPath("/foo").getPost().getParameters().get(1);
    ModelImpl schema = (ModelImpl) bp.getSchema();
    assertTrue(schema.getAdditionalProperties() != null);
    IntegerProperty id = (IntegerProperty) swagger.getDefinitions().get("Fun").getProperties().get("id");
    assertEquals(id.getMultipleOf(), new BigDecimal("5"));
    ArrayProperty ap = (ArrayProperty) swagger.getDefinitions().get("Fun").getProperties().get("mySet");
    assertTrue(ap.getUniqueItems());
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) QueryParameter(io.swagger.models.parameters.QueryParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 57 with SwaggerParser

use of io.swagger.parser.SwaggerParser in project swagger-parser by swagger-api.

the class SwaggerDeserializerTest method testDeserializeWithNumericEnumDiscriminator.

@Test
public void testDeserializeWithNumericEnumDiscriminator() {
    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" + "        enum:\n" + "        - 1\n" + "        - 2";
    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(), "number");
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) Test(org.testng.annotations.Test)

Example 58 with SwaggerParser

use of io.swagger.parser.SwaggerParser in project swagger-parser by swagger-api.

the class SwaggerDeserializerTest method testPathsWithRefResponse.

@Test
public void testPathsWithRefResponse() {
    String json = "{\n" + "  \"swagger\": \"2.0\",\n" + "  \"paths\": {\n" + "    \"/pet\": {\n" + "      \"get\": {\n" + "        \"responses\": {\n" + "          \"200\": {\n" + "            \"$ref\": \"#/responses/OK\"" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  }\n" + "}";
    SwaggerParser parser = new SwaggerParser();
    SwaggerDeserializationResult result = parser.readWithInfo(json);
    Swagger swagger = result.getSwagger();
    Path path = swagger.getPath("/pet");
    assertNotNull(path);
    Operation operation = path.getGet();
    assertNotNull(operation);
    assertTrue(operation.getResponses().containsKey("200"));
    assertEquals(RefResponse.class, operation.getResponses().get("200").getClass());
    RefResponse refResponse = (RefResponse) operation.getResponses().get("200");
    assertEquals("#/responses/OK", refResponse.get$ref());
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) Test(org.testng.annotations.Test)

Example 59 with SwaggerParser

use of io.swagger.parser.SwaggerParser in project swagger-parser by swagger-api.

the class SwaggerDeserializerTest method testResponses.

@Test
public void testResponses() {
    String json = "{\n" + "\t\"swagger\": \"2.0\",\n" + "\t\"responses\": {\n" + "\t\t\"foo\": {\n" + "\t\t\t\"description\": \"description\",\n" + "\t\t\t\"bar\": \"baz\",\n" + "\t\t\t\"x-foo\": \"bar\"\n" + "\t\t}\n" + "\t}\n" + "}";
    SwaggerParser parser = new SwaggerParser();
    SwaggerDeserializationResult result = parser.readWithInfo(json);
    List<String> messageList = result.getMessages();
    Set<String> messages = new HashSet<String>(messageList);
    assertTrue(messages.contains("attribute responses.foo.bar is unexpected"));
    assertEquals(result.getSwagger().getResponses().get("foo").getVendorExtensions().get("x-foo").toString(), "bar");
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 60 with SwaggerParser

use of io.swagger.parser.SwaggerParser in project swagger-parser by swagger-api.

the class SwaggerDeserializerTest method testPaths.

@Test
public void testPaths() {
    String json = "{\n" + "  \"swagger\": \"2.0\",\n" + "  \"paths\": {\n" + "    \"/pet\": {\n" + "      \"foo\": \"bar\",\n" + "      \"get\": {\n" + "        \"security\": [\n" + "          {\n" + "            \"petstore_auth\": [\n" + "              \"write:pets\",\n" + "              \"read:pets\"\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);
    assertTrue(messages.contains("attribute paths.'/pet'.foo is unexpected"));
    Swagger swagger = result.getSwagger();
    Path path = swagger.getPath("/pet");
    assertNotNull(path);
    Operation operation = path.getGet();
    assertNotNull(operation);
    List<Map<String, List<String>>> security = operation.getSecurity();
    assertTrue(security.size() == 1);
    Map<String, List<String>> requirement = security.get(0);
    assertTrue(requirement.containsKey("petstore_auth"));
    List<String> scopesList = requirement.get("petstore_auth");
    Set<String> scopes = new HashSet<String>(scopesList);
    assertTrue(scopes.contains("read:pets"));
    assertTrue(scopes.contains("write:pets"));
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) List(java.util.List) Map(java.util.Map) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

SwaggerParser (io.swagger.parser.SwaggerParser)65 Test (org.testng.annotations.Test)37 Swagger (io.swagger.models.Swagger)25 HashSet (java.util.HashSet)19 HashMap (java.util.HashMap)10 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)9 ArrayList (java.util.ArrayList)7 List (java.util.List)7 SecuritySchemeDefinition (io.swagger.models.auth.SecuritySchemeDefinition)6 IOException (java.io.IOException)6 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)6 Path (io.swagger.models.Path)5 OAuth2Definition (io.swagger.models.auth.OAuth2Definition)5 Map (java.util.Map)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 HttpMethod (io.swagger.models.HttpMethod)4 Operation (io.swagger.models.Operation)4 BodyParameter (io.swagger.models.parameters.BodyParameter)4 File (java.io.File)4