[Core] Minor enhancements (#2863)

* fix getcontenttype, better working for skipFormModel

* add tests for allof
This commit is contained in:
William Cheng 2019-05-11 11:11:06 +08:00 committed by GitHub
parent 98afbe062b
commit 9323cad06b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 111 additions and 6 deletions

View File

@ -2563,8 +2563,9 @@ public class DefaultCodegen implements CodegenConfig {
CodegenParameter bodyParam = null; CodegenParameter bodyParam = null;
RequestBody requestBody = operation.getRequestBody(); RequestBody requestBody = operation.getRequestBody();
if (requestBody != null) { if (requestBody != null) {
if (getContentType(requestBody).toLowerCase(Locale.ROOT).startsWith("application/x-www-form-urlencoded") || if (getContentType(requestBody) != null &&
getContentType(requestBody).toLowerCase(Locale.ROOT).startsWith("multipart/form-data")) { (getContentType(requestBody).toLowerCase(Locale.ROOT).startsWith("application/x-www-form-urlencoded") ||
getContentType(requestBody).toLowerCase(Locale.ROOT).startsWith("multipart/form-data"))) {
// process form parameters // process form parameters
formParams = fromRequestBodyToFormParameters(requestBody, imports); formParams = fromRequestBodyToFormParameters(requestBody, imports);
for (CodegenParameter cp : formParams) { for (CodegenParameter cp : formParams) {
@ -4256,8 +4257,8 @@ public class DefaultCodegen implements CodegenConfig {
protected String getContentType(RequestBody requestBody) { protected String getContentType(RequestBody requestBody) {
if (requestBody == null || requestBody.getContent() == null || requestBody.getContent().isEmpty()) { if (requestBody == null || requestBody.getContent() == null || requestBody.getContent().isEmpty()) {
LOGGER.warn("Cannot determine the content type. Default to UNKNOWN_CONTENT_TYPE."); LOGGER.debug("Cannot determine the content type. Returning null.");
return "UNKNOWN_CONTENT_TYPE"; return null;
} }
return new ArrayList<>(requestBody.getContent().keySet()).get(0); return new ArrayList<>(requestBody.getContent().keySet()).get(0);
} }

View File

@ -434,9 +434,9 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
if (unusedModels.contains(name)) { if (unusedModels.contains(name)) {
if (Boolean.FALSE.equals(skipFormModel)) { if (Boolean.FALSE.equals(skipFormModel)) {
// if skipFormModel sets to true, still generate the model and log the result // if skipFormModel sets to true, still generate the model and log the result
LOGGER.info("Model " + name + " (marked as unused due to form parameters) is generated due to skipFormModel=false (default)"); LOGGER.info("Model " + name + " (marked as unused due to form parameters) is generated due to the system property skipFormModel=false (default)");
} else { } else {
LOGGER.info("Model " + name + " not generated since it's marked as unused (due to form parameters) and skipFormModel set to true"); LOGGER.info("Model " + name + " not generated since it's marked as unused (due to form parameters) and skipFormModel (system property) set to true");
continue; continue;
} }
} }

View File

@ -521,6 +521,47 @@ public class RubyClientCodegenTest {
Assert.assertEquals(cp1.name, "person_required"); Assert.assertEquals(cp1.name, "person_required");
} }
@Test(description = "test allOf composition")
public void allOfCompositionTest() {
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/allOf_composition.yaml");
final RubyClientCodegen codegen = new RubyClientCodegen();
codegen.setModuleName("OnlinePetstore");
final Schema schema = openAPI.getComponents().getSchemas().get("SuperMan");
codegen.setOpenAPI(openAPI);
CodegenModel superMan = codegen.fromModel("SuperMan", schema);
Assert.assertNotNull(superMan);
// to test all properties
Assert.assertEquals(superMan.getVars().size(), 6);
CodegenProperty cp0 = superMan.getVars().get(0);
Assert.assertEquals(cp0.name, "id");
Assert.assertTrue(cp0.required);
CodegenProperty cp1 = superMan.getVars().get(1);
Assert.assertEquals(cp1.name, "name");
Assert.assertFalse(cp1.required);
CodegenProperty cp2 = superMan.getVars().get(2);
Assert.assertEquals(cp2.name, "reward");
Assert.assertFalse(cp2.required);
CodegenProperty cp3 = superMan.getVars().get(3);
Assert.assertEquals(cp3.name, "origin");
Assert.assertTrue(cp3.required);
CodegenProperty cp4 = superMan.getVars().get(4);
Assert.assertEquals(cp4.name, "category");
Assert.assertFalse(cp4.required);
CodegenProperty cp5 = superMan.getVars().get(5);
Assert.assertEquals(cp5.name, "level");
Assert.assertTrue(cp5.required);
}
@Test(description = "test example string imported from x-example parameterr (OAS2)") @Test(description = "test example string imported from x-example parameterr (OAS2)")
public void exampleStringFromExampleParameterOAS2Test() { public void exampleStringFromExampleParameterOAS2Test() {
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/2_0/petstore-nullable.yaml"); final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/2_0/petstore-nullable.yaml");

View File

@ -0,0 +1,63 @@
openapi: 3.0.1
info:
version: 1.0.0
title: Example
license:
name: MIT
servers:
- url: http://api.example.xyz/v1
paths:
/person/display/{personId}:
get:
parameters:
- name: personId
in: path
required: true
description: The id of the person to retrieve
schema:
type: string
operationId: list
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/SuperMan"
components:
schemas:
SuperMan:
allOf:
- $ref: '#/components/schemas/Human'
- $ref: '#/components/schemas/Hero'
- type: object
required:
- level
properties:
category:
type: string
level:
type: integer
Hero:
description: Hero
type: object
required:
- origin
properties:
reward:
type: integer
format: int64
origin:
type: string
Human:
description: Human
type: object
required:
- id
properties:
id:
type: integer
format: int64
name:
type: string
example: Tom