Skip to content

Commit

Permalink
Add test case for InlineModelResolver: inline array request body (#1777)
Browse files Browse the repository at this point in the history
* Add test case : inline array request body

* Delete legacy test case

* Tweak code format

* Delete unused import
  • Loading branch information
ackintosh authored Dec 30, 2018
1 parent 77b5cea commit 7cee999
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,11 @@ public void resolveInlineRequestBody() {
OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/inline_model_resolver.yaml", null, new ParseOptions()).getOpenAPI();
new InlineModelResolver().flatten(openAPI);

RequestBody requestBodyReference = openAPI.getPaths().get("/resolve_inline_request_body").getPost().getRequestBody();
RequestBody requestBodyReference = openAPI
.getPaths()
.get("/resolve_inline_request_body")
.getPost()
.getRequestBody();
assertNotNull(requestBodyReference.get$ref());

RequestBody requestBody = ModelUtils.getReferencedRequestBody(openAPI, requestBodyReference);
Expand Down Expand Up @@ -355,65 +359,62 @@ public void nonModelRequestBody() {
OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/inline_model_resolver.yaml", null, new ParseOptions()).getOpenAPI();
new InlineModelResolver().flatten(openAPI);

MediaType mediaType = openAPI.getPaths().get("/non_model_request_body").getPost().getRequestBody().getContent().get("multipart/form-data");
MediaType mediaType = openAPI
.getPaths()
.get("/non_model_request_body")
.getPost()
.getRequestBody()
.getContent()
.get("multipart/form-data");

assertTrue(mediaType.getSchema() instanceof BinarySchema);
assertEquals("string", mediaType.getSchema().getType());
assertEquals("binary", mediaType.getSchema().getFormat());
}

/*
@Test
public void resolveInlineArrayBodyParameter() throws Exception {
OpenAPI openapi = new OpenAPI();
openapi.path("/hello", new Path()
.get(new Operation()
.parameter(new BodyParameter()
.name("body")
.schema(new ArraySchema()
.items(new ObjectSchema()
.addProperties("address", new ObjectSchema()
.addProperties("street", new StringSchema())))))));
new InlineModelResolver().flatten(openapi);
Parameter param = openapi.getPaths().get("/hello").getGet().getParameters().get(0);
assertTrue(param instanceof BodyParameter);
BodyParameter bp = (BodyParameter) param;
Model schema = bp.getSchema();
public void resolveInlineArrayRequestBody() {
OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/inline_model_resolver.yaml", null, new ParseOptions()).getOpenAPI();
new InlineModelResolver().flatten(openAPI);

assertTrue(schema instanceof ArraySchema);
MediaType mediaType = openAPI
.getPaths()
.get("/resolve_inline_array_request_body")
.getPost()
.getRequestBody()
.getContent()
.get("application/json");

ArraySchema am = (ArraySchema) schema;
Property inner = am.getItems();
assertTrue(inner instanceof Schema);
assertTrue(mediaType.getSchema() instanceof ArraySchema);

Schema rp = (Schema) inner;
ArraySchema requestBody = (ArraySchema) mediaType.getSchema();
assertNotNull(requestBody.getItems().get$ref());
assertEquals("#/components/schemas/NULL_UNIQUE_NAME", requestBody.getItems().get$ref());

assertEquals(rp.getType(), "ref");
assertEquals(rp.get$ref(), "#/definitions/body");
assertEquals(rp.getSimpleRef(), "body");
Schema items = ModelUtils.getReferencedSchema(openAPI, ((ArraySchema) mediaType.getSchema()).getItems());
assertTrue(items.getProperties().get("street") instanceof StringSchema);
assertTrue(items.getProperties().get("city") instanceof StringSchema);
}

Model inline = openapi.getComponents().getSchemas().get("body");
assertNotNull(inline);
assertTrue(inline instanceof ObjectSchema);
ObjectSchema impl = (ObjectSchema) inline;
Schema rpAddress = (Schema) impl.getProperties().get("address");
assertNotNull(rpAddress);
assertEquals(rpAddress.getType(), "ref");
assertEquals(rpAddress.get$ref(), "#/definitions/hello_address");
assertEquals(rpAddress.getSimpleRef(), "hello_address");
@Test
public void resolveInlineArrayRequestBodyWithTitle() {
OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/inline_model_resolver.yaml", null, new ParseOptions()).getOpenAPI();
new InlineModelResolver().flatten(openAPI);

Model inlineProp = openapi.getComponents().getSchemas().get("hello_address");
assertNotNull(inlineProp);
assertTrue(inlineProp instanceof ObjectSchema);
ObjectSchema implProp = (ObjectSchema) inlineProp;
assertNotNull(implProp.getProperties().get("street"));
assertTrue(implProp.getProperties().get("street") instanceof StringSchema);
ArraySchema requestBodySchema = (ArraySchema) openAPI
.getPaths()
.get("/resolve_inline_array_request_body_with_title")
.getPost()
.getRequestBody()
.getContent()
.get("application/json")
.getSchema();

assertNotNull(requestBodySchema.getItems().get$ref());
assertEquals("#/components/schemas/resolveInlineArrayRequestBodyWithTitleItems", requestBodySchema.getItems().get$ref());
}

/*
@Test
public void resolveInlineArrayResponse() throws Exception {
OpenAPI openapi = new OpenAPI();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,43 @@ paths:
responses:
'200':
description: OK
/resolve_inline_array_request_body:
post:
requestBody:
content:
application/json:
schema:
type: array
items:
type: object
properties:
street:
type: string
city:
type: string
operationId: resolveInlineArrayRequestBody
responses:
'200':
description: OK
/resolve_inline_array_request_body_with_title:
post:
requestBody:
content:
application/json:
schema:
type: array
items:
title: resolveInlineArrayRequestBodyWithTitleItems
type: object
properties:
street_2:
type: string
city_2:
type: string
operationId: resolveInlineArrayRequestBodyWithTitle
responses:
'200':
description: OK
components:
schemas:
Users:
Expand Down

0 comments on commit 7cee999

Please sign in to comment.