Search in sources :

Example 91 with RequestBody

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

the class OpenAPIDeserializerTest method testArrayItems.

@Test
public void testArrayItems() {
    String yaml = "openapi: 3.0.0\n" + "info:\n" + "  title: Test\n" + "  version: 1.0.0\n" + "paths:\n" + "  \"/store/inventory\":\n" + "    post:\n" + "      requestBody:\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              type: array\n" + "              minItems: 1\n" + "      responses:\n" + "        '200':\n" + "          description: successful operation\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                items:\n" + "                  type: string";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    assertEquals(result.getMessages(), Arrays.asList("attribute paths.'/store/inventory'(post).requestBody.content.'application/json'.schema.items is missing"));
    OpenAPI openAPI = result.getOpenAPI();
    Schema body = openAPI.getPaths().get("/store/inventory").getPost().getRequestBody().getContent().get("application/json").getSchema();
    assertFalse(body.getClass().equals(ArraySchema.class), "body is an ArraySchema");
    assertEquals(body.getType(), "array");
    assertEquals(body.getMinItems(), Integer.valueOf(1));
    Schema response = openAPI.getPaths().get("/store/inventory").getPost().getResponses().get("200").getContent().get("application/json").getSchema();
    assertTrue(response.getClass().equals(ArraySchema.class), "response is an ArraySchema");
    assertEquals(body.getType(), "array");
}
Also used : ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) 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) 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 92 with RequestBody

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

the class OpenAPIDeserializerTest method testDeserializeBinaryString.

@Test
public void testDeserializeBinaryString() {
    String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + "  title: foo\n" + "  version: ''\n" + "paths:\n" + "  /test:\n" + "    post:\n" + "      responses:\n" + "        '200':\n" + "          description: ok\n" + "      requestBody:\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              type: string\n" + "              format: binary";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    final OpenAPI resolved = new OpenAPIResolver(result.getOpenAPI(), null).resolve();
    assertTrue(resolved.getPaths().get("/test").getPost().getRequestBody().getContent().get("application/json").getSchema() instanceof BinarySchema);
}
Also used : BinarySchema(io.swagger.v3.oas.models.media.BinarySchema) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) 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 93 with RequestBody

use of io.swagger.v3.oas.models.parameters.RequestBody 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 94 with RequestBody

use of io.swagger.v3.oas.models.parameters.RequestBody 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)

Example 95 with RequestBody

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

the class InlineModelResolverTest method notResolveNonModelRequestBody.

@Test
public void notResolveNonModelRequestBody() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.path("/hello", new PathItem().get(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(new Schema().type("string").format("binary")))))));
    new InlineModelResolver().flatten(openAPI);
    Operation operation = openAPI.getPaths().get("/hello").getGet();
    RequestBody body = operation.getRequestBody();
    assertTrue(body.getContent().get("*/*").getSchema() instanceof Schema);
    Schema schema = body.getContent().get("*/*").getSchema();
    assertEquals("string", schema.getType());
    assertEquals("binary", schema.getFormat());
}
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)

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