Search in sources :

Example 1 with Tags

use of io.swagger.v3.oas.annotations.tags.Tags in project swagger-parser by swagger-api.

the class OpenAPIDeserializer method getOperation.

public Operation getOperation(ObjectNode obj, String location, ParseResult result) {
    if (obj == null) {
        return null;
    }
    Operation operation = new Operation();
    ArrayNode array = getArray("tags", obj, false, location, result);
    List<String> tags = getTagsStrings(array, String.format("%s.%s", location, "tags"), result);
    if (tags != null) {
        operation.setTags(tags);
    }
    String value = getString("summary", obj, false, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        operation.setSummary(value);
    }
    value = getString("description", obj, false, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        operation.setDescription(value);
    }
    ObjectNode externalDocs = getObject("externalDocs", obj, false, location, result);
    ExternalDocumentation docs = getExternalDocs(externalDocs, String.format("%s.%s", location, "externalDocs"), result);
    if (docs != null) {
        operation.setExternalDocs(docs);
    }
    value = getString("operationId", obj, false, location, result, operationIDs);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        operation.operationId(value);
    }
    ArrayNode parameters = getArray("parameters", obj, false, location, result);
    if (parameters != null) {
        operation.setParameters(getParameterList(parameters, String.format("%s.%s", location, "parameters"), result));
    }
    final ObjectNode requestObjectNode = getObject("requestBody", obj, false, location, result);
    if (requestObjectNode != null) {
        operation.setRequestBody(getRequestBody(requestObjectNode, String.format("%s.%s", location, "requestBody"), result));
    }
    ObjectNode responsesNode = getObject("responses", obj, true, location, result);
    ApiResponses responses = getResponses(responsesNode, String.format("%s.%s", location, "responses"), result, false);
    if (responses != null) {
        operation.setResponses(responses);
    }
    ObjectNode callbacksNode = getObject("callbacks", obj, false, location, result);
    Map<String, Callback> callbacks = getCallbacks(callbacksNode, String.format("%s.%s", location, "callbacks"), result, false);
    if (callbacks != null) {
        operation.setCallbacks(callbacks);
    }
    Boolean deprecated = getBoolean("deprecated", obj, false, location, result);
    if (deprecated != null) {
        operation.setDeprecated(deprecated);
    }
    array = getArray("servers", obj, false, location, result);
    if (array != null && array.size() > 0) {
        operation.setServers(getServersList(array, String.format("%s.%s", location, "servers"), result));
    }
    array = getArray("security", obj, false, location, result);
    if (array != null) {
        operation.setSecurity(getSecurityRequirementsList(array, String.format("%s.%s", location, "security"), result));
    }
    Map<String, Object> extensions = getExtensions(obj);
    if (extensions != null && extensions.size() > 0) {
        operation.setExtensions(extensions);
    }
    Set<String> keys = getKeys(obj);
    for (String key : keys) {
        if (!OPERATION_KEYS.contains(key) && !key.startsWith("x-")) {
            result.extra(location, key, obj.get(key));
        }
    }
    return operation;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Operation(io.swagger.v3.oas.models.Operation) ExternalDocumentation(io.swagger.v3.oas.models.ExternalDocumentation) Callback(io.swagger.v3.oas.models.callbacks.Callback) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses)

Example 2 with Tags

use of io.swagger.v3.oas.annotations.tags.Tags in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method readSchemaArray.

@Test(dataProvider = "data")
public void readSchemaArray(JsonNode rootNode) throws Exception {
    final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
    final SwaggerParseResult result = deserializer.deserialize(rootNode);
    Assert.assertNotNull(result);
    final OpenAPI openAPI = result.getOpenAPI();
    Assert.assertNotNull(openAPI);
    final Paths paths = openAPI.getPaths();
    Assert.assertNotNull(paths);
    Assert.assertEquals(paths.size(), 19);
    // parameters operation get
    PathItem petByStatusEndpoint = paths.get("/pet/findByTags");
    Assert.assertNotNull(petByStatusEndpoint.getGet());
    Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters());
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().size(), 1);
    Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters().get(0).getSchema());
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getSchema().getType(), "array");
    Assert.assertEquals(((ArraySchema) (petByStatusEndpoint.getGet().getParameters().get(0).getSchema())).getItems().getType(), "string");
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getName(), "tags");
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getExplode(), Boolean.TRUE);
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getStyle(), StyleEnum.FORM);
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Paths(io.swagger.v3.oas.models.Paths) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 3 with Tags

use of io.swagger.v3.oas.annotations.tags.Tags 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 4 with Tags

use of io.swagger.v3.oas.annotations.tags.Tags in project swagger-parser by swagger-api.

the class V2ConverterTest method testIssue5.

@Test(description = "Tags are missing in the converted spec")
public void testIssue5() throws Exception {
    OpenAPI oas = getConvertedOpenAPIFromJsonFile(PET_STORE_JSON);
    // Global Tags
    List<Tag> tags = oas.getTags();
    assertNotNull(tags);
    assertEquals(PET_TAG, tags.get(0).getName());
    // Operation Tag
    Operation petPut = oas.getPaths().get(PET_PATH).getPut();
    assertNotNull(petPut.getTags());
    assertEquals(PET_TAG, petPut.getTags().get(0));
}
Also used : Tag(io.swagger.v3.oas.models.tags.Tag) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 5 with Tags

use of io.swagger.v3.oas.annotations.tags.Tags in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method componentsResolver.

@Test
public void componentsResolver() throws Exception {
    final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    String pathFile = FileUtils.readFileToString(new File("src/test/resources/oas3.yaml.template"));
    pathFile = pathFile.replace("${dynamicPort}", String.valueOf(this.serverPort));
    final JsonNode rootNode = mapper.readTree(pathFile.getBytes());
    final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
    final SwaggerParseResult result = deserializer.deserialize(rootNode);
    Assert.assertNotNull(result);
    final OpenAPI openAPI = result.getOpenAPI();
    Assert.assertNotNull(openAPI);
    assertEquals(new OpenAPIResolver(openAPI, new ArrayList<>(), null).resolve(), openAPI);
    Map<String, Schema> schemas = openAPI.getComponents().getSchemas();
    // internal url schema
    Schema pet = schemas.get("Pet");
    Schema category = (Schema) pet.getProperties().get("category");
    assertEquals(category.get$ref(), "#/components/schemas/Category");
    // remote url schema
    Schema user = (Schema) pet.getProperties().get("user");
    assertEquals(user.get$ref(), "#/components/schemas/User");
    // ArraySchema items
    ArraySchema tagsProperty = (ArraySchema) pet.getProperties().get("tags");
    assertEquals(tagsProperty.getItems().get$ref(), "#/components/schemas/ExampleSchema");
    assertEquals(tagsProperty.getType(), "array");
    Assert.assertNotNull(openAPI.getComponents().getSchemas().get("ExampleSchema"));
    // Schema not
    assertEquals(schemas.get("OrderRef").getNot().get$ref(), "#/components/schemas/Category");
    // Schema additionalProperties
    assertTrue(schemas.get("OrderRef").getAdditionalProperties() instanceof Schema);
    Schema additionalProperties = (Schema) schemas.get("OrderRef").getAdditionalProperties();
    assertEquals(additionalProperties.get$ref(), "#/components/schemas/User");
    // AllOfSchema
    ComposedSchema extended = (ComposedSchema) schemas.get("ExtendedErrorModel");
    Schema root = (Schema) extended.getAllOf().get(0).getProperties().get("rootCause");
    assertEquals(root.get$ref(), "#/components/schemas/Category");
    Map<String, ApiResponse> responses = openAPI.getComponents().getResponses();
    // internal response headers
    ApiResponse illegalInput = responses.get("IllegalInput");
    assertEquals(illegalInput.getHeaders().get("X-Ref-Limit-Limit").get$ref(), "#/components/headers/X-Rate-Limit-Reset");
    // internal response links
    assertEquals(illegalInput.getLinks().get("address").get$ref(), "#/components/links/unsubscribe");
    // internal url response schema
    MediaType generalError = responses.get("GeneralError").getContent().get("application/json");
    assertEquals(generalError.getSchema().get$ref(), "#/components/schemas/ExtendedErrorModel");
    Map<String, RequestBody> requestBodies = openAPI.getComponents().getRequestBodies();
    // internal url requestBody schema
    RequestBody requestBody1 = requestBodies.get("requestBody1");
    MediaType xmlMedia = requestBody1.getContent().get("application/json");
    assertEquals(xmlMedia.getSchema().get$ref(), "#/components/schemas/Pet");
    // internal url requestBody ArraySchema
    RequestBody requestBody2 = requestBodies.get("requestBody2");
    MediaType jsonMedia = requestBody2.getContent().get("application/json");
    ArraySchema items = (ArraySchema) jsonMedia.getSchema();
    assertEquals(items.getItems().get$ref(), "#/components/schemas/User");
    // internal request body
    assertEquals("#/components/requestBodies/requestBody2", requestBodies.get("requestBody3").get$ref());
    // remote request body url
    assertEquals(requestBodies.get("reference").get$ref(), "#/components/requestBodies/remote_requestBody");
    Map<String, Parameter> parameters = openAPI.getComponents().getParameters();
    // remote url parameter
    assertEquals(parameters.get("remoteParameter").get$ref(), "#/components/parameters/parameter");
    // internal Schema Parameter
    assertEquals(parameters.get("newParam").getSchema().get$ref(), "#/components/schemas/Tag");
    // parameter examples
    assertEquals(parameters.get("contentParameter").getExamples().get("cat"), openAPI.getComponents().getExamples().get("cat"));
    // parameter content schema
    assertEquals(parameters.get("contentParameter").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/ExtendedErrorModel");
    // internal Schema header
    Map<String, Header> headers = openAPI.getComponents().getHeaders();
    // header remote schema ref
    assertEquals(headers.get("X-Rate-Limit-Remaining").getSchema().get$ref(), "#/components/schemas/User");
    // header examples
    assertEquals(headers.get("X-Rate-Limit-Reset").getExamples().get("headerExample").get$ref(), "#/components/examples/dog");
    // remote header ref
    assertEquals(headers.get("X-Ref-Limit-Limit").get$ref(), "#/components/headers/X-Rate-Limit-Reset");
    // header content
    assertEquals(headers.get("X-Rate-Limit-Reset").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/ExtendedErrorModel");
    Map<String, Example> examples = openAPI.getComponents().getExamples();
    // internal url example
    Example frogExample = examples.get("frog");
    assertEquals(frogExample.get$ref(), "#/components/examples/cat");
    // remote example url
    assertEquals(examples.get("referenceCat").get$ref(), "#/components/examples/example");
    // internal url securityScheme
    SecurityScheme scheme = openAPI.getComponents().getSecuritySchemes().get("reference");
    assertEquals(scheme.getType(), SecurityScheme.Type.APIKEY);
    SecurityScheme remoteScheme = openAPI.getComponents().getSecuritySchemes().get("remote_reference");
    assertEquals(remoteScheme.getType(), SecurityScheme.Type.OAUTH2);
    Map<String, Link> links = openAPI.getComponents().getLinks();
    // internal link
    assertEquals(openAPI.getComponents().getLinks().get("referenced").get$ref(), "#/components/links/unsubscribe");
    // remote ref link
    assertEquals(openAPI.getComponents().getLinks().get("subscribe").get$ref(), "#/components/links/link");
    Map<String, Callback> callbacks = openAPI.getComponents().getCallbacks();
    // internal callback reference
    assertEquals(callbacks.get("referenced").get$ref(), "#/components/callbacks/failed");
    // callback pathItem -> operation ->requestBody
    assertEquals(callbacks.get("heartbeat").get("$request.query.heartbeat-url").getPost().getRequestBody().get$ref(), "#/components/requestBodies/requestBody3");
    // remote callback ref
    assertEquals(callbacks.get("remoteCallback").get$ref(), "#/components/callbacks/callback");
}
Also used : OpenAPIDeserializer(io.swagger.v3.parser.util.OpenAPIDeserializer) 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) JsonNode(com.fasterxml.jackson.databind.JsonNode) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Example(io.swagger.v3.oas.models.examples.Example) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) MediaType(io.swagger.v3.oas.models.media.MediaType) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Callback(io.swagger.v3.oas.models.callbacks.Callback) Header(io.swagger.v3.oas.models.headers.Header) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) File(java.io.File) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Link(io.swagger.v3.oas.models.links.Link) Test(org.testng.annotations.Test)

Aggregations

OpenAPI (io.swagger.v3.oas.models.OpenAPI)20 Test (org.testng.annotations.Test)15 ArrayList (java.util.ArrayList)11 Operation (io.swagger.v3.oas.models.Operation)9 PathItem (io.swagger.v3.oas.models.PathItem)8 Schema (io.swagger.v3.oas.models.media.Schema)8 Tag (io.swagger.v3.oas.models.tags.Tag)8 Paths (io.swagger.v3.oas.models.Paths)7 MediaType (io.swagger.v3.oas.models.media.MediaType)5 Parameter (io.swagger.v3.oas.models.parameters.Parameter)5 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)5 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)5 Callback (io.swagger.v3.oas.models.callbacks.Callback)4 Content (io.swagger.v3.oas.models.media.Content)4 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)4 SecurityScheme (io.swagger.v3.oas.models.security.SecurityScheme)4 List (java.util.List)4 Map (java.util.Map)4 AnnotatedType (io.swagger.v3.core.converter.AnnotatedType)3 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)3