Search in sources :

Example 61 with SwaggerParser

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

the class SwaggerDeserializerTest method testNestedDefinitions.

@Test
public void testNestedDefinitions() {
    String json = "{\n" + "  \"swagger\": \"2.0\",\n" + "  \"definitions\": {\n" + "    \"Person\": {\n" + "      \"required\": [\n" + "        \"id\",\n" + "        \"name\"\n" + "      ],\n" + "      \"properties\": {\n" + "        \"id\": {\n" + "          \"type\": \"integer\",\n" + "          \"format\": \"int64\"\n" + "        },\n" + "        \"name\": {\n" + "          \"type\": \"string\"\n" + "        },\n" + "        \"address\": {\n" + "        \t\"$ref\": \"#/definitions/Address\"\n" + "        }\n" + "      }\n" + "    },\n" + "    \"Address\": {\n" + "    \t\"required\": [\"zip\"],\n" + "    \t\"properties\": {\n" + "    \t\t\"street\": {\n" + "    \t\t\t\"type\": \"string\"\n" + "    \t\t},\n" + "    \t\t\"zip\": {\n" + "    \t\t\t\"type\": \"integer\",\n" + "    \t\t\t\"format\": \"int32\",\n" + "    \t\t\t\"minimum\": 0,\n" + "    \t\t\t\"exclusiveMinimum\": true,\n" + "    \t\t\t\"maximum\": 99999,\n" + "    \t\t\t\"exclusiveMaximum\": true\n" + "    \t\t}\n" + "    \t}\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(result.getSwagger().getDefinitions().get("Person") instanceof ModelImpl);
    assertTrue(result.getSwagger().getDefinitions().get("Address") instanceof ModelImpl);
    ModelImpl person = (ModelImpl) result.getSwagger().getDefinitions().get("Person");
    Property property = person.getProperties().get("address");
    assertTrue(property instanceof RefProperty);
    Property zip = ((ModelImpl) result.getSwagger().getDefinitions().get("Address")).getProperties().get("zip");
    assertTrue(zip instanceof IntegerProperty);
    IntegerProperty zipProperty = (IntegerProperty) zip;
    assertEquals(zipProperty.getMinimum(), new BigDecimal("0"));
    assertTrue(zipProperty.getExclusiveMinimum());
    assertEquals(zipProperty.getMaximum(), new BigDecimal("99999"));
    assertTrue(zipProperty.getExclusiveMaximum());
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) BigDecimal(java.math.BigDecimal) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 62 with SwaggerParser

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

the class SwaggerDeserializerTest method testSecurityDefinitionWithMissingAttribute.

@Test
public void testSecurityDefinitionWithMissingAttribute() {
    String json = "{\n" + "  \"swagger\": \"2.0\",\n" + "  \"securityDefinitions\": {\n" + "    \"api_key\": {\n" + "      \"description\": \"api key description\"\n" + "    }\n" + "  }\n" + "}";
    SwaggerParser parser = new SwaggerParser();
    SwaggerDeserializationResult result = parser.readWithInfo(json);
    List<String> messageList = result.getMessages();
    Set<String> messages = new HashSet<>(messageList);
    assertTrue(messages.contains("attribute securityDefinitions.api_key.type is missing"));
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 63 with SwaggerParser

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

the class SwaggerDeserializerTest method testIssue247.

@Test
public void testIssue247() {
    String yaml = "swagger: '2.0'\n" + "info:\n" + "  description: 'bleh'\n" + "  version: '2.0.0'\n" + "  title: 'Test'\n" + "paths:\n" + "  /:\n" + "    get:\n" + "      parameters: []\n" + "      responses:\n" + "        200:\n" + "          description: 'OK'\n" + "    parameters: []\n" + "definitions:\n" + "  Pet:\n" + "    allOf:\n" + "      - type: 'object'\n" + "        required:\n" + "        - 'id'\n" + "        properties:\n" + "          id:\n" + "            type: 'integer'\n" + "            format: 'int64'";
    SwaggerParser parser = new SwaggerParser();
    SwaggerDeserializationResult result = parser.readWithInfo(yaml);
    Swagger swagger = result.getSwagger();
    assertNotNull(swagger.getDefinitions().get("Pet"));
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) Test(org.testng.annotations.Test)

Example 64 with SwaggerParser

use of io.swagger.parser.SwaggerParser in project records-management by Alfresco.

the class BaseYamlUnitTest method validateYamlFiles.

/**
 * Helper method to validate that all given yaml files are valid readable Swagger format
 */
protected void validateYamlFiles(final Set<String> yamlFileNames) throws ProcessingException, IOException {
    assertFalse("Expected at least 1 yaml file to validate", yamlFileNames.isEmpty());
    final JsonSchema swaggerSchema = getSwaggerSchema(SWAGGER_2_SCHEMA_LOCATION);
    assertNotNull("Failed to obtain the Swagger schema", swaggerSchema);
    for (String yamlFilePath : yamlFileNames) {
        try {
            // check the yaml file is valid against Swagger JSON schema
            assertTrue("Yaml file is not valid Swagger " + OPEN_API_SPECIFICATION + ": " + yamlFilePath, validateYamlFile(yamlFilePath, swaggerSchema));
            // check can read the swagger object to obtain the swagger version
            Swagger swagger = new SwaggerParser().read(yamlFilePath);
            assertEquals("Failed to obtain Swagger version from yaml file " + yamlFilePath, swagger.getSwagger(), OPEN_API_SPECIFICATION);
        } catch (ParserException ex) {
            // ensure the yaml filename is included in the message
            String context = String.format(yamlFilePath + ": %n" + ex.getContext());
            throw new ParserException(context, ex.getContextMark(), ex.getProblem(), ex.getProblemMark());
        }
    }
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) ParserException(com.fasterxml.jackson.dataformat.yaml.snakeyaml.parser.ParserException) JsonSchema(com.github.fge.jsonschema.main.JsonSchema) Swagger(io.swagger.models.Swagger)

Example 65 with SwaggerParser

use of io.swagger.parser.SwaggerParser in project syndesis by syndesisio.

the class UnifiedXmlDataShapeGeneratorShapeValidityTest method specifications.

@Parameters
public static Iterable<Object[]> specifications() {
    final List<String> specifications = Collections.singletonList("/swagger/petstore.swagger.json");
    final List<Object[]> parameters = new ArrayList<>();
    specifications.forEach(specification -> {
        String specificationContent;
        try (InputStream in = UnifiedXmlDataShapeGenerator.class.getResourceAsStream(specification)) {
            specificationContent = IOUtils.toString(in, StandardCharsets.UTF_8);
        } catch (final IOException e) {
            throw new AssertionError("Unable to load swagger specification in path: " + specification, e);
        }
        final SwaggerParser parser = new SwaggerParser();
        final Swagger swagger = parser.parse(specificationContent);
        swagger.getPaths().forEach((path, operations) -> {
            operations.getOperationMap().forEach((method, operation) -> {
                final Optional<BodyParameter> bodyParameter = BaseDataShapeGenerator.findBodyParameter(operation);
                if (!bodyParameter.isPresent()) {
                    // only parameters
                    return;
                }
                parameters.add(new Object[] { specificationContent, swagger, operation, specification });
            });
        });
    });
    return parameters;
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Swagger(io.swagger.models.Swagger) ArrayList(java.util.ArrayList) IOException(java.io.IOException) BodyParameter(io.swagger.models.parameters.BodyParameter) Parameters(org.junit.runners.Parameterized.Parameters)

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