Search in sources :

Example 1 with SwaggerParser

use of io.swagger.parser.SwaggerParser in project camel by apache.

the class RestSwaggerEndpoint method loadSpecificationFrom.

/**
     * Loads the Swagger definition model from the given path. Tries to resolve
     * the resource using Camel's resource loading support, if it fails uses
     * Swagger's resource loading support instead.
     *
     * @param uri URI of the specification
     * @param camelContext context to use
     * @return the specification
     * @throws IOException
     */
static Swagger loadSpecificationFrom(final CamelContext camelContext, final URI uri) throws IOException {
    final ObjectMapper mapper = Json.mapper();
    final SwaggerParser swaggerParser = new SwaggerParser();
    final String uriAsString = uri.toString();
    try (InputStream stream = ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, uriAsString)) {
        final JsonNode node = mapper.readTree(stream);
        return swaggerParser.read(node);
    } catch (final IOException e) {
        // try Swaggers loader
        final Swagger swagger = swaggerParser.read(uriAsString);
        if (swagger != null) {
            return swagger;
        }
        throw new IllegalArgumentException("The given Swagger specification could not be loaded from `" + uri + "`. Tried loading using Camel's resource resolution and using Swagger's own resource resolution." + " Swagger tends to swallow exceptions while parsing, try specifying Java system property `debugParser`" + " (e.g. `-DdebugParser=true`), the exception that occured when loading using Camel's resource" + " loader follows", e);
    }
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) InputStream(java.io.InputStream) Swagger(io.swagger.models.Swagger) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with SwaggerParser

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

the class SwaggerDeserializerTest method testArrayModelDefinition.

@Test
public void testArrayModelDefinition() {
    String json = "{\n" + "  \"paths\": {\n" + "    \"/store/inventory\": {\n" + "      \"get\": {\n" + "        \"responses\": {\n" + "          \"200\": {\n" + "            \"description\": \"successful operation\",\n" + "            \"schema\": {\n" + "              \"type\": \"object\",\n" + "              \"additionalProperties\": {\n" + "                \"type\": \"integer\",\n" + "                \"format\": \"int32\"\n" + "              }\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);
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 3 with SwaggerParser

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

the class SwaggerDeserializerTest method testIssue386.

@Test
public void testIssue386() {
    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: body\n" + "        name: ugly\n" + "        schema:\n" + "          type: object\n" + "          enum:\n" + "          - id: fun\n" + "          properties:\n" + "            id:\n" + "              type: string\n" + "      responses:\n" + "        200:\n" + "          description: 'OK'\n" + "definitions:\n" + "  Fun:\n" + "    type: object\n" + "    properties:\n" + "      complex:\n" + "        enum:\n" + "        - id: 110\n" + "        type: object\n" + "        properties:\n" + "          id:\n" + "            type: string\n" + "  MyEnum:\n" + "    type: integer\n" + "    enum:\n" + "    - value: 3\n" + "      description: Value 1\n" + "    - value: 10\n" + "      description: Value 2";
    SwaggerParser parser = new SwaggerParser();
    SwaggerDeserializationResult result = parser.readWithInfo(yaml);
    Swagger swagger = result.getSwagger();
    assertNotNull(swagger);
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) Test(org.testng.annotations.Test)

Example 4 with SwaggerParser

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

the class SwaggerDeserializerTest method testPR246.

@Test
public void testPR246() throws Exception {
    String yaml = "swagger: '2.0'\n" + "info:\n" + "  description: 'Tests the allOf API for parent, interface and child models.'\n" + "  version: '2.0.0'\n" + "  title: 'Test allOf API'\n" + "paths:\n" + "  /:\n" + "    get:\n" + "      parameters: []\n" + "      responses:\n" + "        200:\n" + "          description: 'OK'\n" + "    parameters: []\n" + "definitions:\n" + "  Pet:\n" + "    type: 'object'\n" + "    required:\n" + "    - 'id'\n" + "    properties:\n" + "      id:\n" + "        type: 'integer'\n" + "        format: 'int64'\n" + "  Furry:\n" + "    type: 'object'\n" + "    required:\n" + "    - 'coatColour'\n" + "    properties:\n" + "      coatColour:\n" + "        type: 'string'\n" + "  Dog:\n" + "    allOf:\n" + "    - $ref: '#/definitions/Pet'\n" + "    - $ref: '#/definitions/Furry'\n" + "    - type: object\n" + "      required:\n" + "      - 'name'\n" + "      properties:\n" + "        name:\n" + "          type: 'string'\n" + "";
    SwaggerParser parser = new SwaggerParser();
    SwaggerDeserializationResult result = parser.readWithInfo(yaml);
    Swagger swagger = result.getSwagger();
    Model dog = swagger.getDefinitions().get("Dog");
    assertNotNull(dog);
    assertTrue(dog instanceof ComposedModel);
    ComposedModel composed = (ComposedModel) dog;
    assertTrue(composed.getChild() instanceof ModelImpl);
    assertTrue(composed.getInterfaces().size() == 2);
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) Test(org.testng.annotations.Test)

Example 5 with SwaggerParser

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

the class SwaggerDeserializerTest method testDefinitions.

@Test
public void testDefinitions() {
    String json = "{\n" + "  \"swagger\": \"2.0\",\n" + "  \"definitions\": {\n" + "    \"invalid\": true,\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" + "      }\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 definitions.invalid is not of type `object`"));
    assertTrue(result.getSwagger().getDefinitions().get("Person") instanceof ModelImpl);
    List<String> required = ((ModelImpl) result.getSwagger().getDefinitions().get("Person")).getRequired();
    Set<String> requiredKeys = new HashSet<String>(required);
    assertTrue(requiredKeys.contains("id"));
    assertTrue(requiredKeys.contains("name"));
    assertTrue(requiredKeys.size() == 2);
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) 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