Search in sources :

Example 91 with PathItem

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

the class InlineModelResolverTest method testArrayResponse.

@Test
public void testArrayResponse() {
    OpenAPI openAPI = new OpenAPI();
    ObjectSchema objectSchema = new ObjectSchema();
    objectSchema.addProperties("name", new StringSchema());
    ArraySchema schema = new ArraySchema();
    schema.setItems(objectSchema);
    ApiResponse apiResponse = new ApiResponse();
    apiResponse.addExtension("x-foo", "bar");
    apiResponse.setDescription("it works!");
    apiResponse.setContent(new Content().addMediaType("*/*", new MediaType().schema(schema)));
    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");
    assertTrue(response.getContent().get("*/*").getSchema() instanceof ArraySchema);
    ArraySchema am = (ArraySchema) response.getContent().get("*/*").getSchema();
    Schema items = am.getItems();
    assertTrue(items.get$ref() != null);
    assertEquals(items.get$ref(), "#/components/schemas/inline_response_200");
    Schema inline = openAPI.getComponents().getSchemas().get("inline_response_200");
    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 92 with PathItem

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

Example 93 with PathItem

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

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

the class InlineModelResolverTest method resolveInlineParameter.

@Test
public void resolveInlineParameter() 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());
    Parameter parameter = new Parameter();
    parameter.setName("name");
    parameter.setSchema(schema);
    List parameters = new ArrayList();
    parameters.add(parameter);
    Operation operation = new Operation();
    operation.setParameters(parameters);
    PathItem pathItem = new PathItem();
    pathItem.setGet(operation);
    openAPI.path("/hello", pathItem);
    new InlineModelResolver().flatten(openAPI);
    Operation getOperation = openAPI.getPaths().get("/hello").getGet();
    Parameter param = getOperation.getParameters().get(0);
    assertTrue(param.getSchema().get$ref() != null);
    Schema bodySchema = openAPI.getComponents().getSchemas().get("name");
    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) 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) ArrayList(java.util.ArrayList) Parameter(io.swagger.v3.oas.models.parameters.Parameter) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 95 with PathItem

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

the class InlineModelResolverTest method testArbitraryObjectResponse.

@Test
public void testArbitraryObjectResponse() {
    OpenAPI openAPI = new OpenAPI();
    openAPI.path("/foo/bar", new PathItem().get(new Operation().responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("it works!").content(new Content().addMediaType("*/*", new MediaType().schema(new ObjectSchema())))))));
    new InlineModelResolver().flatten(openAPI);
    Map<String, ApiResponse> responses = openAPI.getPaths().get("/foo/bar").getGet().getResponses();
    ApiResponse response = responses.get("200");
    assertNotNull(response);
    assertTrue(response.getContent().get("*/*").getSchema() instanceof ObjectSchema);
    ObjectSchema op = (ObjectSchema) response.getContent().get("*/*").getSchema();
    assertNull(op.getProperties());
}
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) 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)

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