Add test case for InlineModelResolver (#1771)

* Add a test case of resolving inline request body

* Delete legacy test case

* Add a test case of resolve inline request body with required

* Delete legacy test case
This commit is contained in:
Akihito Nakano 2018-12-29 14:38:46 +09:00 committed by GitHub
parent 5952bec6bf
commit 06a67ce6e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 65 deletions

View File

@ -17,14 +17,14 @@
package org.openapitools.codegen;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.*;
import io.swagger.v3.oas.models.media.*;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.responses.ApiResponses;
import org.apache.commons.lang3.StringUtils;
import org.testng.Assert;
import io.swagger.v3.parser.core.models.ParseOptions;
import org.openapitools.codegen.utils.ModelUtils;
import org.testng.annotations.Test;
import java.util.HashMap;
@ -326,78 +326,38 @@ public class InlineModelResolverTest {
assertNotNull(user);
assertEquals("description", user.getDescription());
}
*/
@Test
public void resolveInlineBodyParameter() throws Exception {
OpenAPI openapi = new OpenAPI();
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);
openapi.path("/hello", new Path()
.get(new Operation()
.parameter(new BodyParameter()
.name("body")
.schema(new ObjectSchema()
.addProperties("address", new ObjectSchema()
.addProperties("street", new StringSchema()))
.addProperties("name", new StringSchema())))));
RequestBody requestBodyReference = openAPI.getPaths().get("/resolve_inline_request_body").getPost().getRequestBody();
assertNotNull(requestBodyReference.get$ref());
new InlineModelResolver().flatten(openapi);
RequestBody requestBody = ModelUtils.getReferencedRequestBody(openAPI, requestBodyReference);
MediaType mediaType = requestBody.getContent().get("application/json");
assertTrue(ModelUtils.getReferencedSchema(openAPI, mediaType.getSchema()) instanceof ObjectSchema);
Operation operation = openapi.getPaths().get("/hello").getGet();
BodyParameter bp = (BodyParameter)operation.getParameters().get(0);
assertTrue(bp.getSchema() instanceof RefModel);
ObjectSchema schema = (ObjectSchema) ModelUtils.getReferencedSchema(openAPI, mediaType.getSchema());
assertTrue(schema.getProperties().get("name") instanceof StringSchema);
assertNotNull(schema.getProperties().get("address").get$ref());
Model body = openapi.getComponents().getSchemas().get("body");
assertTrue(body instanceof ObjectSchema);
ObjectSchema impl = (ObjectSchema) body;
assertNotNull(impl.getProperties().get("address"));
Schema address = ModelUtils.getReferencedSchema(openAPI, schema.getProperties().get("address"));
assertTrue(address.getProperties().get("street") instanceof StringSchema);
}
@Test
public void resolveInlineBodyParameterWithRequired() throws Exception {
OpenAPI openapi = new OpenAPI();
public void resolveInlineRequestBodyWithRequired() {
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()
.get(new Operation()
.parameter(new BodyParameter()
.name("body")
.schema(new ObjectSchema()
.addProperties("address", new ObjectSchema()
.addProperties("street", new StringSchema()
.required(true))
.required(true))
.addProperties("name", new StringSchema())))));
new InlineModelResolver().flatten(openapi);
Operation operation = openapi.getPaths().get("/hello").getGet();
BodyParameter bp = (BodyParameter)operation.getParameters().get(0);
assertTrue(bp.getSchema() instanceof RefModel);
Model body = openapi.getComponents().getSchemas().get("body");
assertTrue(body instanceof ObjectSchema);
ObjectSchema impl = (ObjectSchema) body;
assertNotNull(impl.getProperties().get("address"));
Property addressProperty = impl.getProperties().get("address");
assertTrue(addressProperty instanceof Schema);
assertTrue(addressProperty.getRequired());
Model helloAddress = openapi.getComponents().getSchemas().get("hello_address");
assertTrue(helloAddress instanceof ObjectSchema);
ObjectSchema addressImpl = (ObjectSchema) helloAddress;
assertNotNull(addressImpl);
Property streetProperty = addressImpl.getProperties().get("street");
assertTrue(streetProperty instanceof StringSchema);
assertTrue(streetProperty.getRequired());
RequestBody requestBodyReference = openAPI.getPaths().get("/resolve_inline_request_body_with_required").getPost().getRequestBody();
assertTrue(requestBodyReference.getRequired());
}
/*
@Test
public void resolveInlineBodyParameterWithTitle() throws Exception {
OpenAPI openapi = new OpenAPI();

View File

@ -0,0 +1,45 @@
openapi: 3.0.1
info:
version: 1.0.0
title: Example
license:
name: MIT
servers:
- url: http://api.example.xyz/v1
paths:
/resolve_inline_request_body:
post:
requestBody:
content:
application/json:
schema:
type: object
properties:
name:
type: string
address:
type: object
properties:
street:
type: string
operationId: resolveInlineRequestBody
responses:
'200':
description: OK
/resolve_inline_request_body_with_required:
post:
requestBody:
content:
application/json:
schema:
type: object
properties:
name:
type: string
required: true
operationId: resolveInlineRequestBodyWithRequired
responses:
'200':
description: OK
components:
schemas: