Search in sources :

Example 86 with PathItem

use of io.swagger.v3.oas.models.PathItem in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method readContentObject.

@Test(dataProvider = "data")
public void readContentObject(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);
    PathItem petByStatusEndpoint = paths.get("/pet/findByStatusContent");
    Assert.assertNotNull(petByStatusEndpoint.getGet());
    Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters());
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().size(), 3);
    Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters().get(0).getContent());
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().size(), 1);
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("application/json").getSchema().getType(), "array");
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("application/json").getExample(), null);
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("application/json").getExamples().get("list").getSummary(), "List of Names");
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("application/json").getSchema().getType(), "array");
    Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters().get(1).getContent());
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(1).getContent().size(), 1);
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(1).getContent().get("application/xml").getExamples().get("list").getSummary(), "List of names");
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(1).getContent().get("application/xml").getExamples().get("list").getValue(), "<Users><User name='Bob'/><User name='Diane'/><User name='Mary'/><User name='Bill'/></Users>");
    Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters().get(1).getContent().get("application/xml").getExamples().get("empty").getSummary());
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(1).getContent().get("application/xml").getExamples().get("empty").getSummary(), "Empty list");
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(1).getContent().get("application/xml").getExamples().get("empty").getValue(), "<Users/>");
    Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters().get(2).getContent());
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(2).getContent().size(), 1);
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(2).getContent().get("text/plain").getExamples().get("list").getSummary(), "List of names");
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(2).getContent().get("text/plain").getExamples().get("list").getValue(), "Bob,Diane,Mary,Bill");
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(2).getContent().get("text/plain").getExamples().get("empty").getSummary(), "Empty");
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(2).getContent().get("text/plain").getExamples().get("empty").getValue(), "");
    PathItem petEndpoint = paths.get("/pet");
    Assert.assertNotNull(petEndpoint.getPut());
    Assert.assertNotNull(petEndpoint.getPut().getResponses().get("400").getContent().get("application/json"));
    Assert.assertEquals(petEndpoint.getPut().getResponses().get("400").getContent().size(), 1);
    Assert.assertEquals(petEndpoint.getPut().getResponses().get("400").getContent().get("application/json").getSchema().getType(), "array");
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) 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 87 with PathItem

use of io.swagger.v3.oas.models.PathItem in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method readOAS.

@Test
public void readOAS() throws /*JsonNode rootNode*/
Exception {
    final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    final JsonNode rootNode = mapper.readTree(Files.readAllBytes(java.nio.file.Paths.get(getClass().getResource("/oas4.yaml").toURI())));
    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.assertNotNull(paths);
    Assert.assertEquals(paths.size(), 114);
    PathItem stripe = paths.get("/v1/3d_secure");
    Assert.assertNotNull(stripe);
    Assert.assertNotNull(stripe.getPost());
    Assert.assertEquals(stripe.getPost().getDescription(), "");
    Assert.assertEquals(stripe.getPost().getOperationId(), "Create3DSecure");
    Assert.assertNotNull(stripe.getPost().getParameters());
    ApiResponses responses = stripe.getPost().getResponses();
    Assert.assertNotNull(responses);
    assertTrue(responses.containsKey("200"));
    ApiResponse response = responses.get("200");
    Assert.assertEquals(response.getDescription(), "Successful response.");
    Assert.assertEquals(response.getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/three_d_secure");
    PathItem stripeGet = paths.get("/v1/account/external_accounts");
    Assert.assertNotNull(stripeGet);
    Assert.assertNotNull(stripeGet.getGet());
    Assert.assertEquals(stripeGet.getGet().getDescription(), "");
    Assert.assertEquals(stripeGet.getGet().getOperationId(), "AllAccountExternalAccounts");
    Assert.assertNotNull(stripeGet.getGet().getParameters());
    ApiResponses responsesGet = stripeGet.getGet().getResponses();
    Assert.assertNotNull(responsesGet);
    assertTrue(responsesGet.containsKey("200"));
    ApiResponse responseGet = responsesGet.get("200");
    Assert.assertEquals(responseGet.getDescription(), "Successful response.");
    Map<String, Schema> properties = (Map<String, Schema>) responseGet.getContent().get("application/json").getSchema().getProperties();
    Assert.assertNotNull(properties);
    Assert.assertNull(properties.get("data").getType());
    Assert.assertEquals(properties.get("has_more").getDescription(), "True if this list has another page of items after this one that can be fetched.");
    assertTrue(properties.get("data") instanceof ComposedSchema);
    ComposedSchema data = (ComposedSchema) properties.get("data");
    assertTrue(data.getOneOf().get(0) instanceof ArraySchema);
    ArraySchema items = (ArraySchema) data.getOneOf().get(0);
    Assert.assertEquals(items.getItems().get$ref(), "#/components/schemas/bank_account");
}
Also used : DateSchema(io.swagger.v3.oas.models.media.DateSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) 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) BinarySchema(io.swagger.v3.oas.models.media.BinarySchema) JsonNode(com.fasterxml.jackson.databind.JsonNode) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) 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) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) Paths(io.swagger.v3.oas.models.Paths) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) Test(org.testng.annotations.Test)

Example 88 with PathItem

use of io.swagger.v3.oas.models.PathItem in project swagger-parser by swagger-api.

the class InlineModelResolverTest method testArbitraryRequestBody.

@Test
public void testArbitraryRequestBody() {
    OpenAPI openAPI = new OpenAPI();
    openAPI.path("/hello", new PathItem().get(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(new Schema()))))));
    new InlineModelResolver().flatten(openAPI);
    Operation operation = openAPI.getPaths().get("/hello").getGet();
    RequestBody requestBody = operation.getRequestBody();
    assertTrue(requestBody.getContent().get("*/*").getSchema() instanceof Schema);
    Schema schema = requestBody.getContent().get("*/*").getSchema();
    assertNull(schema.getType());
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) Content(io.swagger.v3.oas.models.media.Content) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) MediaType(io.swagger.v3.oas.models.media.MediaType) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Example 89 with PathItem

use of io.swagger.v3.oas.models.PathItem in project swagger-parser by swagger-api.

the class InlineModelResolverTest method resolveInlineArrayResponse.

@Test
public void resolveInlineArrayResponse() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    ObjectSchema items = new ObjectSchema();
    items.addExtension("x-ext", "ext-items");
    items.addProperties("name", new StringSchema());
    ArraySchema schema = new ArraySchema().items(items);
    schema.addExtension("x-ext", "ext-prop");
    ApiResponse response = new ApiResponse();
    response.addExtension("x-foo", "bar");
    response.description("it works!");
    response.content(new Content().addMediaType("*/*", new MediaType().schema(schema)));
    openAPI.path("/foo/baz", new PathItem().get(new Operation().responses(new ApiResponses().addApiResponse("200", response))));
    new InlineModelResolver().flatten(openAPI);
    ApiResponse apiResponse = openAPI.getPaths().get("/foo/baz").getGet().getResponses().get("200");
    assertNotNull(apiResponse);
    assertNotNull(apiResponse.getContent().get("*/*").getSchema());
    Schema responseProperty = apiResponse.getContent().get("*/*").getSchema();
    // no need to flatten more
    assertTrue(responseProperty instanceof ArraySchema);
    ArraySchema ap = (ArraySchema) responseProperty;
    assertEquals(1, ap.getExtensions().size());
    assertEquals("ext-prop", ap.getExtensions().get("x-ext"));
    Schema p = ap.getItems();
    assertNotNull(p);
    assertEquals("#/components/schemas/inline_response_200", p.get$ref());
    assertEquals(1, p.getExtensions().size());
    assertEquals("ext-items", p.getExtensions().get("x-ext"));
    Schema inline = openAPI.getComponents().getSchemas().get("inline_response_200");
    assertNotNull(inline);
    assertTrue(inline instanceof Schema);
    assertNotNull(inline.getProperties().get("name"));
    assertTrue(inline.getProperties().get("name") instanceof StringSchema);
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Content(io.swagger.v3.oas.models.media.Content) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) Test(org.testng.annotations.Test)

Example 90 with PathItem

use of io.swagger.v3.oas.models.PathItem in project swagger-parser by swagger-api.

the class InlineModelResolverTest method resolveInlineRequestBody.

@Test
public void resolveInlineRequestBody() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    ObjectSchema objectSchema = new ObjectSchema();
    objectSchema.addProperties("street", new StringSchema());
    Schema schema = new Schema();
    schema.addProperties("address", objectSchema);
    schema.addProperties("name", new StringSchema());
    MediaType mediaType = new MediaType();
    mediaType.setSchema(schema);
    Content content = new Content();
    content.addMediaType("*/*", mediaType);
    RequestBody requestBody = new RequestBody();
    requestBody.setContent(content);
    Operation operation = new Operation();
    operation.setRequestBody(requestBody);
    PathItem pathItem = new PathItem();
    pathItem.setGet(operation);
    openAPI.path("/hello", pathItem);
    new InlineModelResolver().flatten(openAPI);
    Operation getOperation = openAPI.getPaths().get("/hello").getGet();
    RequestBody body = getOperation.getRequestBody();
    assertTrue(body.getContent().get("*/*").getSchema().get$ref() != null);
    Schema bodySchema = openAPI.getComponents().getSchemas().get("hello_body");
    assertTrue(bodySchema instanceof Schema);
    assertNotNull(bodySchema.getProperties().get("address"));
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Content(io.swagger.v3.oas.models.media.Content) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Aggregations

PathItem (io.swagger.v3.oas.models.PathItem)135 OpenAPI (io.swagger.v3.oas.models.OpenAPI)109 Operation (io.swagger.v3.oas.models.Operation)104 Test (org.testng.annotations.Test)92 Schema (io.swagger.v3.oas.models.media.Schema)57 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)50 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)45 StringSchema (io.swagger.v3.oas.models.media.StringSchema)44 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)42 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)42 MediaType (io.swagger.v3.oas.models.media.MediaType)41 Content (io.swagger.v3.oas.models.media.Content)38 Paths (io.swagger.v3.oas.models.Paths)36 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)35 Parameter (io.swagger.v3.oas.models.parameters.Parameter)29 Components (io.swagger.v3.oas.models.Components)26 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)25 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)24 Map (java.util.Map)22 HashMap (java.util.HashMap)20