Search in sources :

Example 26 with InlineModelResolver

use of io.swagger.v3.parser.util.InlineModelResolver 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 27 with InlineModelResolver

use of io.swagger.v3.parser.util.InlineModelResolver in project swagger-parser by swagger-api.

the class InlineModelResolverTest method resolveInlineArrayModelWithoutTitle.

@Test
public void resolveInlineArrayModelWithoutTitle() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());
    Schema objectSchema = new ObjectSchema();
    objectSchema.setDefault("default");
    objectSchema.setReadOnly(false);
    objectSchema.setDescription("description");
    objectSchema.setName("name");
    objectSchema.addProperties("street", new StringSchema());
    objectSchema.addProperties("city", new StringSchema());
    ArraySchema arraySchema = new ArraySchema();
    List<String> required = new LinkedList<>();
    required.add("name");
    arraySchema.setRequired(required);
    arraySchema.setItems(objectSchema);
    openAPI.getComponents().addSchemas("User", arraySchema);
    new InlineModelResolver().flatten(openAPI);
    Schema model = openAPI.getComponents().getSchemas().get("User");
    assertTrue(model instanceof ArraySchema);
    Schema user = openAPI.getComponents().getSchemas().get("User_inner");
    assertNotNull(user);
    assertEquals("description", user.getDescription());
}
Also used : Components(io.swagger.v3.oas.models.Components) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) 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) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) LinkedList(java.util.LinkedList) Test(org.testng.annotations.Test)

Example 28 with InlineModelResolver

use of io.swagger.v3.parser.util.InlineModelResolver 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 29 with InlineModelResolver

use of io.swagger.v3.parser.util.InlineModelResolver 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 30 with InlineModelResolver

use of io.swagger.v3.parser.util.InlineModelResolver 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

OpenAPI (io.swagger.v3.oas.models.OpenAPI)35 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)35 Test (org.testng.annotations.Test)35 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)34 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)34 Schema (io.swagger.v3.oas.models.media.Schema)34 StringSchema (io.swagger.v3.oas.models.media.StringSchema)34 Operation (io.swagger.v3.oas.models.Operation)25 PathItem (io.swagger.v3.oas.models.PathItem)25 Content (io.swagger.v3.oas.models.media.Content)24 MediaType (io.swagger.v3.oas.models.media.MediaType)24 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)13 Components (io.swagger.v3.oas.models.Components)11 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)11 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)11 ArrayList (java.util.ArrayList)6 LinkedList (java.util.LinkedList)4 List (java.util.List)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Parameter (io.swagger.v3.oas.models.parameters.Parameter)1