forked from loafle/openapi-generator-original
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:
parent
5952bec6bf
commit
06a67ce6e4
@ -17,14 +17,14 @@
|
|||||||
|
|
||||||
package org.openapitools.codegen;
|
package org.openapitools.codegen;
|
||||||
|
|
||||||
|
import io.swagger.parser.OpenAPIParser;
|
||||||
import io.swagger.v3.oas.models.*;
|
import io.swagger.v3.oas.models.*;
|
||||||
import io.swagger.v3.oas.models.media.*;
|
import io.swagger.v3.oas.models.media.*;
|
||||||
import io.swagger.v3.oas.models.parameters.Parameter;
|
import io.swagger.v3.oas.models.parameters.RequestBody;
|
||||||
import io.swagger.v3.core.util.Json;
|
|
||||||
import io.swagger.v3.oas.models.responses.ApiResponse;
|
import io.swagger.v3.oas.models.responses.ApiResponse;
|
||||||
import io.swagger.v3.oas.models.responses.ApiResponses;
|
import io.swagger.v3.oas.models.responses.ApiResponses;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import io.swagger.v3.parser.core.models.ParseOptions;
|
||||||
import org.testng.Assert;
|
import org.openapitools.codegen.utils.ModelUtils;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -326,78 +326,38 @@ public class InlineModelResolverTest {
|
|||||||
assertNotNull(user);
|
assertNotNull(user);
|
||||||
assertEquals("description", user.getDescription());
|
assertEquals("description", user.getDescription());
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void resolveInlineBodyParameter() throws Exception {
|
public void resolveInlineRequestBody() {
|
||||||
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()
|
RequestBody requestBodyReference = openAPI.getPaths().get("/resolve_inline_request_body").getPost().getRequestBody();
|
||||||
.get(new Operation()
|
assertNotNull(requestBodyReference.get$ref());
|
||||||
.parameter(new BodyParameter()
|
|
||||||
.name("body")
|
|
||||||
.schema(new ObjectSchema()
|
|
||||||
.addProperties("address", new ObjectSchema()
|
|
||||||
.addProperties("street", new StringSchema()))
|
|
||||||
.addProperties("name", new StringSchema())))));
|
|
||||||
|
|
||||||
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();
|
ObjectSchema schema = (ObjectSchema) ModelUtils.getReferencedSchema(openAPI, mediaType.getSchema());
|
||||||
BodyParameter bp = (BodyParameter)operation.getParameters().get(0);
|
assertTrue(schema.getProperties().get("name") instanceof StringSchema);
|
||||||
assertTrue(bp.getSchema() instanceof RefModel);
|
assertNotNull(schema.getProperties().get("address").get$ref());
|
||||||
|
|
||||||
Model body = openapi.getComponents().getSchemas().get("body");
|
Schema address = ModelUtils.getReferencedSchema(openAPI, schema.getProperties().get("address"));
|
||||||
assertTrue(body instanceof ObjectSchema);
|
assertTrue(address.getProperties().get("street") instanceof StringSchema);
|
||||||
|
|
||||||
ObjectSchema impl = (ObjectSchema) body;
|
|
||||||
assertNotNull(impl.getProperties().get("address"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void resolveInlineBodyParameterWithRequired() throws Exception {
|
public void resolveInlineRequestBodyWithRequired() {
|
||||||
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()
|
RequestBody requestBodyReference = openAPI.getPaths().get("/resolve_inline_request_body_with_required").getPost().getRequestBody();
|
||||||
.get(new Operation()
|
assertTrue(requestBodyReference.getRequired());
|
||||||
.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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
@Test
|
@Test
|
||||||
public void resolveInlineBodyParameterWithTitle() throws Exception {
|
public void resolveInlineBodyParameterWithTitle() throws Exception {
|
||||||
OpenAPI openapi = new OpenAPI();
|
OpenAPI openapi = new OpenAPI();
|
||||||
|
@ -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:
|
Loading…
x
Reference in New Issue
Block a user