Search in sources :

Example 1 with Tag

use of io.swagger.v3.jaxrs2.resources.model.Tag in project swagger-parser by swagger-api.

the class OpenAPIDeserializer method getTag.

public Tag getTag(ObjectNode obj, String location, ParseResult result) {
    if (obj == null) {
        return null;
    }
    Tag tag = new Tag();
    String value = getString("name", obj, true, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        tag.setName(value);
    }
    value = getString("description", obj, false, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        tag.setDescription(value);
    }
    ObjectNode docs = getObject("externalDocs", obj, false, location, result);
    ExternalDocumentation externalDocs = getExternalDocs(docs, String.format("%s.%s", location, "externalDocs"), result);
    if (externalDocs != null) {
        tag.setExternalDocs(externalDocs);
    }
    Map<String, Object> extensions = getExtensions(obj);
    if (extensions != null && extensions.size() > 0) {
        tag.setExtensions(extensions);
    }
    Set<String> keys = getKeys(obj);
    for (String key : keys) {
        if (!TAG_KEYS.contains(key) && !key.startsWith("x-")) {
            result.extra(location, key, obj.get(key));
        }
    }
    return tag;
}
Also used : ExternalDocumentation(io.swagger.v3.oas.models.ExternalDocumentation) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Tag(io.swagger.v3.oas.models.tags.Tag)

Example 2 with Tag

use of io.swagger.v3.jaxrs2.resources.model.Tag in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method testResponseRemoteRefs.

private void testResponseRemoteRefs(String remoteRef) {
    final OpenAPI swagger = new OpenAPI();
    swagger.path("/fun", new PathItem().get(new Operation().responses(new ApiResponses().addApiResponse("200", new ApiResponse().content(new Content().addMediaType("*/*", new MediaType().schema(new Schema().$ref(remoteRef))))))));
    final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
    final ApiResponse response = swagger.getPaths().get("/fun").getGet().getResponses().get("200");
    final Schema ref = response.getContent().get("*/*").getSchema();
    assertEquals(ref.get$ref(), "#/components/schemas/Tag");
    assertNotNull(swagger.getComponents().getSchemas().get("Tag"));
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) Content(io.swagger.v3.oas.models.media.Content) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) MediaType(io.swagger.v3.oas.models.media.MediaType) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse)

Example 3 with Tag

use of io.swagger.v3.jaxrs2.resources.model.Tag in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method testSimpleRemoteModelProperty.

private void testSimpleRemoteModelProperty(String remoteRef) {
    final OpenAPI swagger = new OpenAPI();
    swagger.components(new Components().addSchemas("Sample", new Schema().addProperties("remoteRef", new Schema().$ref(remoteRef))));
    final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
    final Schema prop = (Schema) resolved.getComponents().getSchemas().get("Sample").getProperties().get("remoteRef");
    assertTrue(prop.get$ref() != null);
    assertEquals(prop.get$ref(), "#/components/schemas/Tag");
    assertNotNull(swagger.getComponents().getSchemas().get("Tag"));
}
Also used : Components(io.swagger.v3.oas.models.Components) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) OpenAPI(io.swagger.v3.oas.models.OpenAPI)

Example 4 with Tag

use of io.swagger.v3.jaxrs2.resources.model.Tag in project swagger-parser by swagger-api.

the class OpenAPIParserTest method testIssue813.

@Test
public void testIssue813() throws Exception {
    String inputSpec = "{\n" + "  \"swagger\": \"2.0\",\n" + "  \"info\": {\n" + "    \"description\": \"This is a sample server Petstore server.  You can find out more about Swagger at <a href=\\\"http://swagger.io\\\">http://swagger.io</a> or on irc.freenode.net, #swagger.  For this sample, you can use the api key \\\"special-key\\\" to test the authorization filters\",\n" + "    \"version\": \"1.0.0\",\n" + "    \"title\": \"Swagger Petstore\",\n" + "    \"termsOfService\": \"http://helloreverb.com/terms/\",\n" + "    \"contact\": {\n" + "      \"email\": \"apiteam@wordnik.com\"\n" + "    },\n" + "    \"license\": {\n" + "      \"name\": \"Apache-2.0\",\n" + "      \"url\": \"http://www.apache.org/licenses/LICENSE-2.0.html\"\n" + "    }\n" + "  },\n" + "  \"host\": \"petstore.swagger.io\",\n" + "  \"basePath\": \"/v2\",\n" + "  \"schemes\": [\n" + "    \"http\"\n" + "  ],\n" + "  \"paths\": {\n" + "    \"/pet\": {\n" + "      \"post\": {\n" + "        \"tags\": [\n" + "          \"pet\"\n" + "        ],\n" + "        \"summary\": \"Add a new pet to the store\",\n" + "        \"description\": \"\",\n" + "        \"operationId\": \"addPet\",\n" + "        \"consumes\": [\n" + "          \"application/json\",\n" + "          \"application/xml\"\n" + "        ],\n" + "        \"produces\": [\n" + "          \"application/json\",\n" + "          \"application/xml\"\n" + "        ],\n" + "        \"parameters\": [{\n" + "          \"in\": \"body\",\n" + "          \"name\": \"body\",\n" + "          \"description\": \"Pet object that needs to be added to the store\",\n" + "          \"required\": false,\n" + "          \"schema\": {\n" + "            \"$ref\": \"#/definitions/Pet\"\n" + "          }\n" + "        }],\n" + "        \"responses\": {\n" + "          \"405\": {\n" + "            \"description\": \"Invalid input\"\n" + "          }\n" + "        },\n" + "        \"security\": [{\n" + "          \"petstore_auth\": [\n" + "            \"write:pets\",\n" + "            \"read:pets\"\n" + "          ]\n" + "        }]\n" + "      },\n" + "      \"put\": {\n" + "        \"tags\": [\n" + "          \"pet\"\n" + "        ],\n" + "        \"summary\": \"Update an existing pet\",\n" + "        \"description\": \"\",\n" + "        \"operationId\": \"updatePet\",\n" + "        \"consumes\": [\n" + "          \"application/json\",\n" + "          \"application/xml\"\n" + "        ],\n" + "        \"produces\": [\n" + "          \"application/json\",\n" + "          \"application/xml\"\n" + "        ],\n" + "        \"parameters\": [{\n" + "          \"in\": \"body\",\n" + "          \"name\": \"body\",\n" + "          \"description\": \"Pet object that needs to be added to the store\",\n" + "          \"required\": false,\n" + "          \"schema\": {\n" + "            \"$ref\": \"#/definitions/Pet\"\n" + "          }\n" + "        }],\n" + "        \"responses\": {\n" + "          \"405\": {\n" + "            \"description\": \"Validation exception\"\n" + "          },\n" + "          \"404\": {\n" + "            \"description\": \"Pet not found\"\n" + "          },\n" + "          \"400\": {\n" + "            \"description\": \"Invalid ID supplied\"\n" + "          }\n" + "        },\n" + "        \"security\": [{\n" + "          \"petstore_auth\": [\n" + "            \"write:pets\",\n" + "            \"read:pets\"\n" + "          ]\n" + "        }]\n" + "      }\n" + "    },\n" + "    \"securityDefinitions\": {\n" + "      \"api_key\": {\n" + "        \"type\": \"apiKey\",\n" + "        \"name\": \"api_key\",\n" + "        \"in\": \"header\"\n" + "      },\n" + "      \"petstore_auth\": {\n" + "        \"type\": \"oauth2\",\n" + "        \"authorizationUrl\": \"http://petstore.swagger.io/api/oauth/dialog\",\n" + "        \"flow\": \"implicit\",\n" + "        \"scopes\": {\n" + "          \"write:pets\": \"modify pets in your account\",\n" + "          \"read:pets\": \"read your pets\"\n" + "        }\n" + "      }\n" + "    },\n" + "    \"definitions\": {\n" + "      \"Pet\": {\n" + "        \"required\": [\n" + "          \"name\",\n" + "          \"photoUrls\"\n" + "        ],\n" + "        \"properties\": {\n" + "          \"id\": {\n" + "            \"type\": \"integer\",\n" + "            \"format\": \"int64\"\n" + "          },\n" + "          \"category\": {\n" + "            \"$ref\": \"#/definitions/Category\"\n" + "          },\n" + "          \"name\": {\n" + "            \"type\": \"string\",\n" + "            \"example\": \"doggie\"\n" + "          },\n" + "          \"photoUrls\": {\n" + "            \"type\": \"array\",\n" + "            \"xml\": {\n" + "              \"name\": \"photoUrl\",\n" + "              \"wrapped\": true\n" + "            },\n" + "            \"items\": {\n" + "              \"type\": \"string\"\n" + "            }\n" + "          },\n" + "          \"tags\": {\n" + "            \"type\": \"array\",\n" + "            \"xml\": {\n" + "              \"name\": \"tag\",\n" + "              \"wrapped\": true\n" + "            },\n" + "            \"items\": {\n" + "              \"$ref\": \"#/definitions/Tag\"\n" + "            }\n" + "          },\n" + "          \"status\": {\n" + "            \"type\": \"string\",\n" + "            \"description\": \"pet status in the store\",\n" + "            \"enum\": [\n" + "              \"available\",\n" + "              \"pending\",\n" + "              \"sold\"\n" + "            ]\n" + "          }\n" + "        },\n" + "        \"xml\": {\n" + "          \"name\": \"Pet\"\n" + "        }\n" + "      }\n" + "    }\n" + "  }\n" + "}";
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setFlatten(true);
    SwaggerParseResult result = new OpenAPIParser().readContents(inputSpec, null, options);
    assertTrue(result.getOpenAPI() != null);
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Test(org.junit.Test)

Example 5 with Tag

use of io.swagger.v3.jaxrs2.resources.model.Tag in project swagger-parser by swagger-api.

the class V2ConverterTest method testIssue15.

@Test(description = "Convert extensions everywhere applicable #15")
public void testIssue15() throws Exception {
    OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_15_JSON);
    assertNotNull(oas);
    Info info = oas.getInfo();
    assertNotNull(info.getExtensions().get("x-apis-json"));
    assertNotNull(info.getLicense().getExtensions().get("x-notes"));
    assertNotNull(oas.getExternalDocs().getExtensions().get("x-docs-extension"));
    assertNotNull(oas.getTags().get(0).getExtensions().get("x-tag-extension"));
    assertNotNull(oas.getTags().get(0).getExternalDocs().getExtensions().get("x-tag-docs-extension"));
    PathItem pathItem = oas.getPaths().get("/something");
    assertNotNull(pathItem.getExtensions().get("x-path-item-extension"));
    Operation get = pathItem.getGet();
    assertNotNull(get.getExtensions().get("x-version"));
    assertNotNull(get.getExternalDocs().getExtensions().get("x-operation-docs-extension"));
    assertNotNull(get.getResponses().get("200").getExtensions().get("x-response-extension"));
    ArraySchema schema = (ArraySchema) get.getParameters().get(0).getSchema();
    assertNull(schema.getItems().getExtensions().get(X_EXAMPLE));
    Map<String, SecurityScheme> securitySchemes = oas.getComponents().getSecuritySchemes();
    assertNotNull(securitySchemes);
    assertNotNull(securitySchemes.get("OAuth2Implicit").getExtensions().get("x-auth-extension"));
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Operation(io.swagger.v3.oas.models.Operation) Info(io.swagger.v3.oas.models.info.Info) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) Test(org.testng.annotations.Test)

Aggregations

OpenAPI (io.swagger.v3.oas.models.OpenAPI)18 Schema (io.swagger.v3.oas.models.media.Schema)14 ArrayList (java.util.ArrayList)13 Tag (io.swagger.v3.oas.models.tags.Tag)12 Test (org.testng.annotations.Test)12 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)11 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)11 StringSchema (io.swagger.v3.oas.models.media.StringSchema)11 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)10 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)10 Operation (io.swagger.v3.oas.models.Operation)9 Components (io.swagger.v3.oas.models.Components)7 PathItem (io.swagger.v3.oas.models.PathItem)7 OpenAPIResolver (io.swagger.v3.parser.OpenAPIResolver)7 MediaType (io.swagger.v3.oas.models.media.MediaType)6 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)6 Content (io.swagger.v3.oas.models.media.Content)5 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)5 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)5 HashMap (java.util.HashMap)5