Skip to content

Commit

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

* Add test case : inline array response with title

* Delete legacy test cases
  • Loading branch information
ackintosh authored Dec 30, 2018
1 parent 2e727f6 commit 321416e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -414,100 +414,47 @@ public void resolveInlineArrayRequestBodyWithTitle() {
assertEquals("#/components/schemas/resolveInlineArrayRequestBodyWithTitleItems", requestBodySchema.getItems().get$ref());
}

/*
@Test
public void resolveInlineArrayResponse() throws Exception {
OpenAPI openapi = new OpenAPI();
ArrayProperty schema = new ArrayProperty()
.items(new ObjectSchema()
.addProperties("name", new StringSchema())
.vendorExtension("x-ext", "ext-items"))
.vendorExtension("x-ext", "ext-prop");
openapi.path("/foo/baz", new Path()
.get(new Operation()
.response(200, new Response()
.vendorExtension("x-foo", "bar")
.description("it works!")
.schema(schema))));
new InlineModelResolver().flatten(openapi);
Response response = openapi.getPaths().get("/foo/baz").getGet().getResponses().get("200");
assertNotNull(response);
assertNotNull(response.getSchema());
Property responseProperty = response.getSchema();
// no need to flatten more
assertTrue(responseProperty instanceof ArrayProperty);
public void resolveInlineArrayResponse() {
OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/inline_model_resolver.yaml", null, new ParseOptions()).getOpenAPI();
new InlineModelResolver().flatten(openAPI);

ArrayProperty ap = (ArrayProperty) responseProperty;
assertEquals(1, ap.getVendorExtensions().size());
assertEquals("ext-prop", ap.getVendorExtensions().get("x-ext"));
Property p = ap.getItems();
MediaType mediaType = openAPI
.getPaths()
.get("/resolve_inline_array_response")
.getGet()
.getResponses()
.get("200")
.getContent()
.get("application/json");

assertNotNull(p);
assertTrue(mediaType.getSchema() instanceof ArraySchema);

Schema rp = (Schema) p;
assertEquals(rp.getType(), "ref");
assertEquals(rp.get$ref(), "#/definitions/inline_response_200");
assertEquals(rp.getSimpleRef(), "inline_response_200");
assertEquals(1, rp.getVendorExtensions().size());
assertEquals("ext-items", rp.getVendorExtensions().get("x-ext"));
ArraySchema responseSchema = (ArraySchema) mediaType.getSchema();
assertEquals("#/components/schemas/inline_response_200", responseSchema.getItems().get$ref());

Model inline = openapi.getComponents().getSchemas().get("inline_response_200");
assertNotNull(inline);
assertTrue(inline instanceof ObjectSchema);
ObjectSchema impl = (ObjectSchema) inline;
assertNotNull(impl.getProperties().get("name"));
assertTrue(impl.getProperties().get("name") instanceof StringSchema);
Schema items = ModelUtils.getReferencedSchema(openAPI, responseSchema.getItems());
assertTrue(items.getProperties().get("array_response_property") instanceof StringSchema);
}

@Test
public void resolveInlineArrayResponseWithTitle() throws Exception {
OpenAPI openapi = new OpenAPI();
openapi.path("/foo/baz", new Path()
.get(new Operation()
.response(200, new Response()
.vendorExtension("x-foo", "bar")
.description("it works!")
.schema(new ArrayProperty()
.items(new ObjectSchema()
.title("FooBar")
.addProperties("name", new StringSchema()))))));
new InlineModelResolver().flatten(openapi);
Response response = openapi.getPaths().get("/foo/baz").getGet().getResponses().get("200");
assertNotNull(response);
assertNotNull(response.getSchema());
Property responseProperty = response.getSchema();
// no need to flatten more
assertTrue(responseProperty instanceof ArrayProperty);
ArrayProperty ap = (ArrayProperty) responseProperty;
Property p = ap.getItems();
assertNotNull(p);
public void resolveInlineArrayResponseWithTitle() {
OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/inline_model_resolver.yaml", null, new ParseOptions()).getOpenAPI();
new InlineModelResolver().flatten(openAPI);

Schema rp = (Schema) p;
assertEquals(rp.getType(), "ref");
assertEquals(rp.get$ref(), "#/definitions/"+ "FooBar");
assertEquals(rp.getSimpleRef(), "FooBar");
MediaType mediaType = openAPI
.getPaths()
.get("/resolve_inline_array_response_with_title")
.getGet()
.getResponses()
.get("200")
.getContent()
.get("application/json");

Model inline = openapi.getComponents().getSchemas().get("FooBar");
assertNotNull(inline);
assertTrue(inline instanceof ObjectSchema);
ObjectSchema impl = (ObjectSchema) inline;
assertNotNull(impl.getProperties().get("name"));
assertTrue(impl.getProperties().get("name") instanceof StringSchema);
ArraySchema responseSchema = (ArraySchema) mediaType.getSchema();
assertEquals("#/components/schemas/resolveInlineArrayResponseWithTitleItems", responseSchema.getItems().get$ref());
}
/*
@Test
public void testInlineMapResponse() throws Exception {
OpenAPI openapi = new OpenAPI();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,37 @@ paths:
responses:
'200':
description: OK
/resolve_inline_array_response:
get:
operationId: resolveInlineArrayResponse
responses:
'200':
description: OK
content:
application/json:
schema:
type: array
items:
type: object
properties:
array_response_property:
type: string
/resolve_inline_array_response_with_title:
get:
operationId: resolveInlineArrayResponseWithTitle
responses:
'200':
description: OK
content:
application/json:
schema:
type: array
items:
title: resolveInlineArrayResponseWithTitleItems
type: object
properties:
array_response_with_title_property:
type: string
components:
schemas:
Users:
Expand Down

0 comments on commit 321416e

Please sign in to comment.