Add test case for InlineModelResolver: inline array request body (#1777)

* Add test case : inline array request body

* Delete legacy test case

* Tweak code format

* Delete unused import
This commit is contained in:
Akihito Nakano 2018-12-30 12:12:50 +09:00 committed by GitHub
parent 77b5cea518
commit 7cee999543
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 47 deletions

View File

@ -295,7 +295,11 @@ public class InlineModelResolverTest {
OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/inline_model_resolver.yaml", null, new ParseOptions()).getOpenAPI(); OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/inline_model_resolver.yaml", null, new ParseOptions()).getOpenAPI();
new InlineModelResolver().flatten(openAPI); 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()); assertNotNull(requestBodyReference.get$ref());
RequestBody requestBody = ModelUtils.getReferencedRequestBody(openAPI, requestBodyReference); RequestBody requestBody = ModelUtils.getReferencedRequestBody(openAPI, requestBodyReference);
@ -355,65 +359,62 @@ public class InlineModelResolverTest {
OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/inline_model_resolver.yaml", null, new ParseOptions()).getOpenAPI(); OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/inline_model_resolver.yaml", null, new ParseOptions()).getOpenAPI();
new InlineModelResolver().flatten(openAPI); 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); assertTrue(mediaType.getSchema() instanceof BinarySchema);
assertEquals("string", mediaType.getSchema().getType()); assertEquals("string", mediaType.getSchema().getType());
assertEquals("binary", mediaType.getSchema().getFormat()); assertEquals("binary", mediaType.getSchema().getFormat());
} }
/*
@Test @Test
public void resolveInlineArrayBodyParameter() throws Exception { public void resolveInlineArrayRequestBody() {
OpenAPI openapi = new OpenAPI(); OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/inline_model_resolver.yaml", null, new ParseOptions()).getOpenAPI();
new InlineModelResolver().flatten(openAPI);
openapi.path("/hello", new Path() MediaType mediaType = openAPI
.get(new Operation() .getPaths()
.parameter(new BodyParameter() .get("/resolve_inline_array_request_body")
.name("body") .getPost()
.schema(new ArraySchema() .getRequestBody()
.items(new ObjectSchema() .getContent()
.addProperties("address", new ObjectSchema() .get("application/json");
.addProperties("street", new StringSchema())))))));
new InlineModelResolver().flatten(openapi); assertTrue(mediaType.getSchema() instanceof ArraySchema);
Parameter param = openapi.getPaths().get("/hello").getGet().getParameters().get(0); ArraySchema requestBody = (ArraySchema) mediaType.getSchema();
assertTrue(param instanceof BodyParameter); assertNotNull(requestBody.getItems().get$ref());
assertEquals("#/components/schemas/NULL_UNIQUE_NAME", requestBody.getItems().get$ref());
BodyParameter bp = (BodyParameter) param; Schema items = ModelUtils.getReferencedSchema(openAPI, ((ArraySchema) mediaType.getSchema()).getItems());
Model schema = bp.getSchema(); assertTrue(items.getProperties().get("street") instanceof StringSchema);
assertTrue(items.getProperties().get("city") instanceof StringSchema);
assertTrue(schema instanceof ArraySchema);
ArraySchema am = (ArraySchema) schema;
Property inner = am.getItems();
assertTrue(inner instanceof Schema);
Schema rp = (Schema) inner;
assertEquals(rp.getType(), "ref");
assertEquals(rp.get$ref(), "#/definitions/body");
assertEquals(rp.getSimpleRef(), "body");
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");
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);
} }
@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);
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 @Test
public void resolveInlineArrayResponse() throws Exception { public void resolveInlineArrayResponse() throws Exception {
OpenAPI openapi = new OpenAPI(); OpenAPI openapi = new OpenAPI();

View File

@ -53,6 +53,43 @@ paths:
responses: responses:
'200': '200':
description: OK 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: components:
schemas: schemas:
Users: Users: