Search in sources :

Example 21 with Pet

use of io.swagger.v3.jaxrs2.resources.model.Pet 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)

Example 22 with Pet

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

the class OpenAPIV3ParserTest method testIssue450.

@Test
public void testIssue450() {
    String desc = "An array of Pets";
    String xTag = "x-my-tag";
    String xVal = "An extension tag";
    String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + "  version: 0.0.0\n" + "  title: Simple API\n" + "paths:\n" + "  /:\n" + "    get:\n" + "      responses:\n" + "        '200':\n" + "          description: OK\n" + "components:\n" + "  schemas:\n" + "    PetArray:\n" + "      type: array\n" + "      items:\n" + "        $ref: '#/components/schemas/Pet'\n" + "      description: An array of Pets\n" + "      x-my-tag: An extension tag\n" + "    Pet:\n" + "      type: object\n" + "      properties:\n" + "        id:\n" + "          type: string";
    SwaggerParseResult result = new OpenAPIV3Parser().readContents(yaml, null, null);
    assertNotNull(result.getOpenAPI());
    final OpenAPI openAPI = result.getOpenAPI();
    Schema petArray = openAPI.getComponents().getSchemas().get("PetArray");
    assertNotNull(petArray);
    assertTrue(petArray instanceof ArraySchema);
    assertEquals(petArray.getDescription(), desc);
    assertNotNull(petArray.getExtensions());
    assertNotNull(petArray.getExtensions().get(xTag));
    assertEquals(petArray.getExtensions().get(xTag), xVal);
}
Also used : ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) 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) MapSchema(io.swagger.v3.oas.models.media.MapSchema) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 23 with Pet

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

the class OpenAPIV3ParserTest method doRelativeFileTest.

private OpenAPI doRelativeFileTest(String location) {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult readResult = parser.readLocation(location, null, options);
    if (readResult.getMessages().size() > 0) {
        Json.prettyPrint(readResult.getMessages());
    }
    final OpenAPI openAPI = readResult.getOpenAPI();
    final PathItem path = openAPI.getPaths().get("/health");
    // we successfully converted the RefPath to a Path
    assertEquals(path.getClass(), PathItem.class);
    final List<Parameter> parameters = path.getParameters();
    assertParamDetails(parameters, 0, QueryParameter.class, "param1", "query");
    assertParamDetails(parameters, 1, HeaderParameter.class, "param2", "header");
    final Operation operation = path.getGet();
    final List<Parameter> operationParams = operation.getParameters();
    assertParamDetails(operationParams, 0, PathParameter.class, "param3", "path");
    assertParamDetails(operationParams, 1, HeaderParameter.class, "param4", "header");
    final Map<String, ApiResponse> responsesMap = operation.getResponses();
    assertResponse(openAPI, responsesMap, "200", "application/json", "Health information from the server", "#/components/schemas/health");
    assertResponse(openAPI, responsesMap, "400", "*/*", "Your request was not valid", "#/components/schemas/error");
    assertResponse(openAPI, responsesMap, "500", "*/*", "An unexpected error occur during processing", "#/components/schemas/error");
    final Map<String, Schema> definitions = openAPI.getComponents().getSchemas();
    final Schema refInDefinitions = definitions.get("refInDefinitions");
    assertEquals(refInDefinitions.getDescription(), "The example model");
    expectedPropertiesInModel(refInDefinitions, "foo", "bar");
    final ArraySchema arrayModel = (ArraySchema) definitions.get("arrayModel");
    final Schema arrayModelItems = arrayModel.getItems();
    assertEquals(arrayModelItems.get$ref(), "#/components/schemas/foo");
    final Schema fooModel = definitions.get("foo");
    assertEquals(fooModel.getDescription(), "Just another model");
    expectedPropertiesInModel(fooModel, "hello", "world");
    final ComposedSchema composedCat = (ComposedSchema) definitions.get("composedCat");
    final Schema child = composedCat.getAllOf().get(2);
    expectedPropertiesInModel(child, "huntingSkill", "prop2", "reflexes", "reflexMap");
    final ArraySchema reflexes = (ArraySchema) child.getProperties().get("reflexes");
    final Schema reflexItems = reflexes.getItems();
    assertEquals(reflexItems.get$ref(), "#/components/schemas/reflex");
    assertTrue(definitions.containsKey(reflexItems.get$ref().substring(reflexItems.get$ref().lastIndexOf("/") + 1)));
    final Schema reflexMap = (Schema) child.getProperties().get("reflexMap");
    final Schema reflexMapAdditionalProperties = (Schema) reflexMap.getAdditionalProperties();
    assertEquals(reflexMapAdditionalProperties.get$ref(), "#/components/schemas/reflex");
    assertEquals(composedCat.getAllOf().size(), 3);
    assertEquals(composedCat.getAllOf().get(0).get$ref(), "#/components/schemas/pet");
    assertEquals(composedCat.getAllOf().get(1).get$ref(), "#/components/schemas/foo_1");
    return openAPI;
}
Also used : ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) 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) MapSchema(io.swagger.v3.oas.models.media.MapSchema) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Operation(io.swagger.v3.oas.models.Operation) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) PathItem(io.swagger.v3.oas.models.PathItem) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) HeaderParameter(io.swagger.v3.oas.models.parameters.HeaderParameter) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI)

Example 24 with Pet

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

the class OpenAPIV3ParserTest method testIssue_1292.

@Test
public void testIssue_1292() {
    OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult parseResult = openApiParser.readLocation("issue-1292/petstore.yml", null, options);
    OpenAPI openAPI = parseResult.getOpenAPI();
    assertNotNull(openAPI.getPaths().get("/pets").getGet().getResponses().get("200").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/Pets");
    assertNotNull(openAPI.getPaths().get("/pets").getGet().getResponses().getDefault().getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/Error");
    assertNotNull(openAPI.getComponents().getSchemas().get("Pet"));
    assertNotNull(openAPI.getComponents().getSchemas().get("Pets"));
    assertNotNull(openAPI.getComponents().getSchemas().get("Error"));
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 25 with Pet

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

the class OpenAPIV3ParserTest method testIssue1398.

@Test
public void testIssue1398() {
    ParseOptions options = new ParseOptions();
    SwaggerParseResult result = new OpenAPIV3Parser().readLocation("issue1398.yaml", null, options);
    assertEquals(result.getMessages().get(0), "attribute paths.'/pet/{petId}'(get).parameters.[petId].schemas.multipleOf value must be > 0");
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)69 OpenAPI (io.swagger.v3.oas.models.OpenAPI)60 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)37 Schema (io.swagger.v3.oas.models.media.Schema)32 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)28 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)28 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)24 StringSchema (io.swagger.v3.oas.models.media.StringSchema)24 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)20 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)17 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)17 PathItem (io.swagger.v3.oas.models.PathItem)16 ByteArraySchema (io.swagger.v3.oas.models.media.ByteArraySchema)15 MapSchema (io.swagger.v3.oas.models.media.MapSchema)12 DateSchema (io.swagger.v3.oas.models.media.DateSchema)10 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)10 ArrayList (java.util.ArrayList)10 Operation (io.swagger.v3.oas.models.Operation)9 Paths (io.swagger.v3.oas.models.Paths)9 Parameter (io.swagger.v3.oas.models.parameters.Parameter)8