Search in sources :

Example 11 with PathItem

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

the class InlineModelResolverTest method testInlineResponseModel.

@Test
public void testInlineResponseModel() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    StringSchema stringSchema1 = new StringSchema();
    ObjectSchema objectSchema1 = new ObjectSchema();
    objectSchema1.addProperties("name", stringSchema1);
    objectSchema1.addExtension("x-ext", "ext-prop");
    MediaType mediaType1 = new MediaType();
    mediaType1.setSchema(objectSchema1);
    Content content1 = new Content();
    content1.addMediaType("*/*", mediaType1);
    ApiResponse response1 = new ApiResponse();
    response1.setDescription("it works!");
    response1.setContent(content1);
    ApiResponses responses1 = new ApiResponses();
    responses1.addApiResponse("200", response1);
    Operation operation1 = new Operation();
    operation1.setResponses(responses1);
    PathItem pathItem1 = new PathItem();
    pathItem1.setGet(operation1);
    openAPI.path("/foo/bar", pathItem1);
    StringSchema stringSchema2 = new StringSchema();
    ObjectSchema objectSchema2 = new ObjectSchema();
    objectSchema2.addProperties("name", stringSchema2);
    objectSchema2.addExtension("x-ext", "ext-prop");
    MediaType mediaType2 = new MediaType();
    mediaType2.setSchema(objectSchema2);
    Content content2 = new Content();
    content2.addMediaType("*/*", mediaType2);
    ApiResponse response2 = new ApiResponse();
    response2.setDescription("it works!");
    response2.addExtension("x-foo", "bar");
    response2.setContent(content2);
    ApiResponses responses2 = new ApiResponses();
    responses2.addApiResponse("200", response2);
    Operation operation2 = new Operation();
    operation2.setResponses(responses2);
    PathItem pathItem2 = new PathItem();
    pathItem2.setGet(operation2);
    openAPI.path("/foo/baz", pathItem2);
    new InlineModelResolver().flatten(openAPI);
    Map<String, ApiResponse> responses = openAPI.getPaths().get("/foo/bar").getGet().getResponses();
    ApiResponse response = responses.get("200");
    assertNotNull(response);
    Schema schema = response.getContent().get("*/*").getSchema();
    assertTrue(schema.get$ref() != null);
    assertEquals(1, schema.getExtensions().size());
    assertEquals("ext-prop", schema.getExtensions().get("x-ext"));
    Schema model = openAPI.getComponents().getSchemas().get("inline_response_200");
    assertTrue(model.getProperties().size() == 1);
    assertNotNull(model.getProperties().get("name"));
    assertTrue(model.getProperties().get("name") instanceof StringSchema);
}
Also used : 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) Operation(io.swagger.v3.oas.models.Operation) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) PathItem(io.swagger.v3.oas.models.PathItem) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Content(io.swagger.v3.oas.models.media.Content) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) Test(org.testng.annotations.Test)

Example 12 with PathItem

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

the class InlineModelResolverTest method testArbitraryObjectResponseArray.

@Test
public void testArbitraryObjectResponseArray() {
    OpenAPI openAPI = new OpenAPI();
    openAPI.path("/foo/baz", new PathItem().get(new Operation().responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("it works!").content(new Content().addMediaType("*/*", new MediaType().schema(new ArraySchema().items(new ObjectSchema()))))))));
    new InlineModelResolver().flatten(openAPI);
    ApiResponse response = openAPI.getPaths().get("/foo/baz").getGet().getResponses().get("200");
    assertTrue(response.getContent().get("*/*").getSchema() instanceof ArraySchema);
    ArraySchema arraySchema = (ArraySchema) response.getContent().get("*/*").getSchema();
    Schema items = arraySchema.getItems();
    assertTrue(items instanceof ObjectSchema);
    ObjectSchema op = (ObjectSchema) items;
    assertNull(op.getProperties());
}
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) 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) Test(org.testng.annotations.Test)

Example 13 with PathItem

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

the class InlineModelResolverTest method testArbitraryObjectResponseArrayInline.

@Test
public void testArbitraryObjectResponseArrayInline() {
    OpenAPI openAPI = new OpenAPI();
    ArraySchema arraySchema = new ArraySchema();
    ObjectSchema objectSchema = new ObjectSchema();
    objectSchema.addProperties("arbitrary", new ObjectSchema());
    arraySchema.items(objectSchema);
    ApiResponse apiResponse = new ApiResponse();
    apiResponse.addExtension("x-foo", "bar");
    apiResponse.description("it works!");
    apiResponse.content(new Content().addMediaType("*/*", new MediaType().schema(arraySchema)));
    openAPI.path("/foo/baz", new PathItem().get(new Operation().responses(new ApiResponses().addApiResponse("200", apiResponse))));
    new InlineModelResolver().flatten(openAPI);
    ApiResponse response = openAPI.getPaths().get("/foo/baz").getGet().getResponses().get("200");
    assertNotNull(response);
    assertNotNull(response.getContent().get("*/*").getSchema());
    Schema responseProperty = response.getContent().get("*/*").getSchema();
    assertTrue(responseProperty instanceof ArraySchema);
    ArraySchema arraySchema1 = (ArraySchema) responseProperty;
    Schema items = arraySchema1.getItems();
    assertNotNull(items);
    assertEquals("#/components/schemas/inline_response_200", items.get$ref());
    Schema inline = openAPI.getComponents().getSchemas().get("inline_response_200");
    assertNotNull(inline);
    assertTrue(inline instanceof Schema);
    Schema inlineProp = (Schema) inline.getProperties().get("arbitrary");
    assertNotNull(inlineProp);
    assertTrue(inlineProp instanceof ObjectSchema);
    assertNull(inlineProp.getProperties());
}
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) 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 14 with PathItem

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

the class InlineModelResolverTest method testSkipInlineMatchesFalse.

@Test
public void testSkipInlineMatchesFalse() {
    final OpenAPI openAPI = new OpenAPI();
    final InlineModelResolver inlineModelResolver = new InlineModelResolver();
    final Schema operationAlphaInAsset = new ObjectSchema();
    operationAlphaInAsset.setTitle("operationAlphaInAsset");
    operationAlphaInAsset.addProperties("id1", new IntegerSchema());
    operationAlphaInAsset.addProperties("id2", new IntegerSchema());
    final Schema operationAlphaIn = new ObjectSchema();
    operationAlphaIn.setTitle("operationAlphaIn");
    operationAlphaIn.addProperties("asset", operationAlphaInAsset);
    final Schema operationAlphaRequest = new ObjectSchema();
    operationAlphaRequest.setTitle("operationAlphaRequest");
    operationAlphaRequest.addProperties("in", operationAlphaIn);
    final Schema operationBetaInAsset = new ObjectSchema();
    operationBetaInAsset.setTitle("operationBetaInAsset");
    operationBetaInAsset.addProperties("id1", new IntegerSchema());
    operationBetaInAsset.addProperties("id2", new IntegerSchema());
    final Schema operationBetaIn = new ObjectSchema();
    operationBetaIn.setTitle("operationBetaIn");
    operationBetaIn.addProperties("asset", operationBetaInAsset);
    final Schema operationBetaRequest = new ObjectSchema();
    operationBetaRequest.setTitle("operationBetaRequest");
    operationBetaRequest.addProperties("in", operationBetaIn);
    openAPI.path("/operationAlpha", new PathItem().get(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(operationAlphaRequest))))));
    openAPI.path("/operationBeta", new PathItem().get(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(operationBetaRequest))))));
    inlineModelResolver.flatten(openAPI);
    assertNotNull(openAPI);
    assertNotNull(openAPI.getComponents());
    assertNotNull(openAPI.getComponents().getSchemas());
    assertEquals(openAPI.getComponents().getSchemas().size(), 6);
}
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) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) 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 15 with PathItem

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

the class OpenAPIDeserializerTest method readRequestBodyObject.

@Test(dataProvider = "data")
public void readRequestBodyObject(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/findByStatus");
    Assert.assertNotNull(petByStatusEndpoint.getGet().getRequestBody());
    Assert.assertEquals(petByStatusEndpoint.getGet().getRequestBody().getDescription(), "pet store to add to the system");
    assertTrue(petByStatusEndpoint.getGet().getRequestBody().getRequired(), "true");
    Assert.assertNotNull(petByStatusEndpoint.getGet().getRequestBody().getContent().get("multipart/mixed"));
    Assert.assertEquals(petByStatusEndpoint.getGet().getRequestBody().getContent().get("multipart/mixed").getSchema().getType(), "object");
    Assert.assertNotNull(petByStatusEndpoint.getGet().getRequestBody().getContent().get("multipart/mixed").getSchema().getProperties());
    Assert.assertEquals(petByStatusEndpoint.getGet().getRequestBody().getContent().get("multipart/mixed").getEncoding().get("historyMetadata").getContentType(), "application/xml; charset=utf-8");
    Assert.assertNotNull(petByStatusEndpoint.getGet().getRequestBody().getContent().get("multipart/mixed").getEncoding().get("profileImage").getHeaders());
    Assert.assertNotNull(petByStatusEndpoint.getGet().getRequestBody().getContent().get("multipart/mixed").getEncoding().get("profileImage").getHeaders().get("X-Rate-Limit"));
}
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)

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