Search in sources :

Example 96 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody in project swagger-parser by swagger-api.

the class InlineModelResolverTest method testArbitraryObjectBodyParamArrayInline.

@Test
public void testArbitraryObjectBodyParamArrayInline() {
    OpenAPI openAPI = new OpenAPI();
    ObjectSchema items = new ObjectSchema();
    items.addProperties("arbitrary", new ObjectSchema());
    openAPI.path("/hello", new PathItem().get(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(new ArraySchema().items(items)))))));
    new InlineModelResolver().flatten(openAPI);
    RequestBody requestBody = openAPI.getPaths().get("/hello").getGet().getRequestBody();
    Schema schema = requestBody.getContent().get("*/*").getSchema();
    assertTrue(schema instanceof ArraySchema);
    ArraySchema arraySchema = (ArraySchema) schema;
    Schema inner = arraySchema.getItems();
    assertTrue(inner.get$ref() != null);
    assertEquals(inner.get$ref(), "#/components/schemas/hello_body");
    Schema inline = openAPI.getComponents().getSchemas().get("hello_body");
    assertNotNull(inline);
    Schema p = (Schema) inline.getProperties().get("arbitrary");
    assertNotNull(p);
    assertTrue(p instanceof ObjectSchema);
}
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) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Example 97 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody in project swagger-parser by swagger-api.

the class InlineModelResolverTest method testArbitraryObjectRequestBodyInline.

@Test
public void testArbitraryObjectRequestBodyInline() {
    OpenAPI swagger = new OpenAPI();
    Schema schema = new Schema();
    schema.addProperties("arbitrary", new ObjectSchema());
    swagger.path("/hello", new PathItem().get(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(schema))))));
    new InlineModelResolver().flatten(swagger);
    Operation operation = swagger.getPaths().get("/hello").getGet();
    RequestBody requestBody = operation.getRequestBody();
    assertTrue(requestBody.getContent().get("*/*").getSchema().get$ref() != null);
    Schema body = swagger.getComponents().getSchemas().get("hello_body");
    assertTrue(body instanceof Schema);
    Schema property = (Schema) body.getProperties().get("arbitrary");
    assertNotNull(property);
    assertTrue(property instanceof ObjectSchema);
}
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) 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 98 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody in project swagger-parser by swagger-api.

the class InlineModelResolverTest method testSkipInlineMatchesTrue.

@Test
public void testSkipInlineMatchesTrue() {
    final OpenAPI openAPI = new OpenAPI();
    final InlineModelResolver inlineModelResolver = new InlineModelResolver(false, false, true);
    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(6, openAPI.getComponents().getSchemas().size());
}
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 99 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody in project swagger-parser by swagger-api.

the class InlineModelResolverTest method resolveInlineRequestBodyWithTitle.

@Test
public void resolveInlineRequestBodyWithTitle() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    ObjectSchema objectSchema = new ObjectSchema();
    objectSchema.addProperties("street", new StringSchema());
    objectSchema.addProperties("name", new StringSchema());
    Schema addressModelItem = new Schema();
    String addressModelName = "DetailedAddress";
    addressModelItem.setTitle(addressModelName);
    addressModelItem.addProperties("address", objectSchema);
    openAPI.path("/hello", new PathItem().get(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(addressModelItem))))));
    new InlineModelResolver().flatten(openAPI);
    Operation operation = openAPI.getPaths().get("/hello").getGet();
    RequestBody requestBody = operation.getRequestBody();
    assertTrue(requestBody.getContent().get("*/*").getSchema().get$ref() != null);
    Schema body = openAPI.getComponents().getSchemas().get(addressModelName);
    assertTrue(body instanceof Schema);
    assertNotNull(body.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)

Example 100 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody in project swagger-parser by swagger-api.

the class OpenAPIParserTest method testIssue799.

@Test
public void testIssue799() {
    OpenAPIParser openApiParser = new OpenAPIParser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setFlatten(true);
    OpenAPI openAPI = openApiParser.readLocation("issue799.json", null, options).getOpenAPI();
    Assert.assertEquals(((Schema) openAPI.getComponents().getSchemas().get("v1beta3.Binding").getProperties().get("metadata")).get$ref(), "#/components/schemas/v1beta3.ObjectMeta");
    RequestBody bodyParameter = openAPI.getPaths().get("/api/v1beta3/namespaces/{namespaces}/bindings").getPost().getRequestBody();
    Assert.assertEquals(bodyParameter.getContent().get("*/*").getSchema().get$ref(), "#/components/schemas/v1beta3.Binding");
    Assert.assertEquals(openAPI.getPaths().get("/api/v1beta3/namespaces/{namespaces}/componentstatuses/{name}").getGet().getResponses().get("200").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/v1beta3.ComponentStatus");
    Assert.assertEquals(openAPI.getPaths().get("/api/v1beta3/namespaces/{namespaces}/componentstatuses").getGet().getResponses().get("200").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/v1beta3.ComponentStatusList");
    Schema conditionsProperty = (Schema) openAPI.getComponents().getSchemas().get("v1beta3.ComponentStatus").getProperties().get("conditions");
    assertTrue(conditionsProperty instanceof ArraySchema);
    Schema items = ((ArraySchema) conditionsProperty).getItems();
    assertTrue(items.get$ref() != null);
    Assert.assertEquals(items.get$ref(), "#/components/schemas/v1beta3.ObjectReference");
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Schema(io.swagger.v3.oas.models.media.Schema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) OpenAPI(io.swagger.v3.oas.models.OpenAPI) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.junit.Test)

Aggregations

Test (org.testng.annotations.Test)67 OpenAPI (io.swagger.v3.oas.models.OpenAPI)62 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)59 Schema (io.swagger.v3.oas.models.media.Schema)46 Operation (io.swagger.v3.oas.annotations.Operation)41 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)40 MediaType (io.swagger.v3.oas.models.media.MediaType)36 StringSchema (io.swagger.v3.oas.models.media.StringSchema)35 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)32 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)32 Content (io.swagger.v3.oas.models.media.Content)31 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)28 Operation (io.swagger.v3.oas.models.Operation)27 PathItem (io.swagger.v3.oas.models.PathItem)23 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)21 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)19 Parameter (io.swagger.v3.oas.models.parameters.Parameter)15 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)14 Components (io.swagger.v3.oas.models.Components)13 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)12