Skip to content
This repository was archived by the owner on Feb 9, 2021. It is now read-only.

Commit 9323cad

Browse files
authored
[Core] Minor enhancements (OpenAPITools#2863)
* fix getcontenttype, better working for skipFormModel * add tests for allof
1 parent 98afbe0 commit 9323cad

File tree

4 files changed

+111
-6
lines changed

4 files changed

+111
-6
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -2563,8 +2563,9 @@ public CodegenOperation fromOperation(String path,
25632563
CodegenParameter bodyParam = null;
25642564
RequestBody requestBody = operation.getRequestBody();
25652565
if (requestBody != null) {
2566-
if (getContentType(requestBody).toLowerCase(Locale.ROOT).startsWith("application/x-www-form-urlencoded") ||
2567-
getContentType(requestBody).toLowerCase(Locale.ROOT).startsWith("multipart/form-data")) {
2566+
if (getContentType(requestBody) != null &&
2567+
(getContentType(requestBody).toLowerCase(Locale.ROOT).startsWith("application/x-www-form-urlencoded") ||
2568+
getContentType(requestBody).toLowerCase(Locale.ROOT).startsWith("multipart/form-data"))) {
25682569
// process form parameters
25692570
formParams = fromRequestBodyToFormParameters(requestBody, imports);
25702571
for (CodegenParameter cp : formParams) {
@@ -4256,8 +4257,8 @@ public void writePropertyBack(String propertyKey, boolean value) {
42564257

42574258
protected String getContentType(RequestBody requestBody) {
42584259
if (requestBody == null || requestBody.getContent() == null || requestBody.getContent().isEmpty()) {
4259-
LOGGER.warn("Cannot determine the content type. Default to UNKNOWN_CONTENT_TYPE.");
4260-
return "UNKNOWN_CONTENT_TYPE";
4260+
LOGGER.debug("Cannot determine the content type. Returning null.");
4261+
return null;
42614262
}
42624263
return new ArrayList<>(requestBody.getContent().keySet()).get(0);
42634264
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,9 @@ private Model getParent(Model model) {
434434
if (unusedModels.contains(name)) {
435435
if (Boolean.FALSE.equals(skipFormModel)) {
436436
// if skipFormModel sets to true, still generate the model and log the result
437-
LOGGER.info("Model " + name + " (marked as unused due to form parameters) is generated due to skipFormModel=false (default)");
437+
LOGGER.info("Model " + name + " (marked as unused due to form parameters) is generated due to the system property skipFormModel=false (default)");
438438
} else {
439-
LOGGER.info("Model " + name + " not generated since it's marked as unused (due to form parameters) and skipFormModel set to true");
439+
LOGGER.info("Model " + name + " not generated since it's marked as unused (due to form parameters) and skipFormModel (system property) set to true");
440440
continue;
441441
}
442442
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/ruby/RubyClientCodegenTest.java

+41
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,47 @@ public void allOfMappingDuplicatedPropertiesTestForAdult() {
521521
Assert.assertEquals(cp1.name, "person_required");
522522
}
523523

524+
@Test(description = "test allOf composition")
525+
public void allOfCompositionTest() {
526+
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/allOf_composition.yaml");
527+
final RubyClientCodegen codegen = new RubyClientCodegen();
528+
codegen.setModuleName("OnlinePetstore");
529+
530+
final Schema schema = openAPI.getComponents().getSchemas().get("SuperMan");
531+
codegen.setOpenAPI(openAPI);
532+
CodegenModel superMan = codegen.fromModel("SuperMan", schema);
533+
Assert.assertNotNull(superMan);
534+
535+
// to test all properties
536+
Assert.assertEquals(superMan.getVars().size(), 6);
537+
538+
CodegenProperty cp0 = superMan.getVars().get(0);
539+
Assert.assertEquals(cp0.name, "id");
540+
Assert.assertTrue(cp0.required);
541+
542+
CodegenProperty cp1 = superMan.getVars().get(1);
543+
Assert.assertEquals(cp1.name, "name");
544+
Assert.assertFalse(cp1.required);
545+
546+
CodegenProperty cp2 = superMan.getVars().get(2);
547+
Assert.assertEquals(cp2.name, "reward");
548+
Assert.assertFalse(cp2.required);
549+
550+
CodegenProperty cp3 = superMan.getVars().get(3);
551+
Assert.assertEquals(cp3.name, "origin");
552+
Assert.assertTrue(cp3.required);
553+
554+
CodegenProperty cp4 = superMan.getVars().get(4);
555+
Assert.assertEquals(cp4.name, "category");
556+
Assert.assertFalse(cp4.required);
557+
558+
CodegenProperty cp5 = superMan.getVars().get(5);
559+
Assert.assertEquals(cp5.name, "level");
560+
Assert.assertTrue(cp5.required);
561+
562+
}
563+
564+
524565
@Test(description = "test example string imported from x-example parameterr (OAS2)")
525566
public void exampleStringFromExampleParameterOAS2Test() {
526567
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/2_0/petstore-nullable.yaml");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
openapi: 3.0.1
2+
info:
3+
version: 1.0.0
4+
title: Example
5+
license:
6+
name: MIT
7+
servers:
8+
- url: http://api.example.xyz/v1
9+
paths:
10+
/person/display/{personId}:
11+
get:
12+
parameters:
13+
- name: personId
14+
in: path
15+
required: true
16+
description: The id of the person to retrieve
17+
schema:
18+
type: string
19+
operationId: list
20+
responses:
21+
'200':
22+
description: OK
23+
content:
24+
application/json:
25+
schema:
26+
$ref: "#/components/schemas/SuperMan"
27+
components:
28+
schemas:
29+
SuperMan:
30+
allOf:
31+
- $ref: '#/components/schemas/Human'
32+
- $ref: '#/components/schemas/Hero'
33+
- type: object
34+
required:
35+
- level
36+
properties:
37+
category:
38+
type: string
39+
level:
40+
type: integer
41+
Hero:
42+
description: Hero
43+
type: object
44+
required:
45+
- origin
46+
properties:
47+
reward:
48+
type: integer
49+
format: int64
50+
origin:
51+
type: string
52+
Human:
53+
description: Human
54+
type: object
55+
required:
56+
- id
57+
properties:
58+
id:
59+
type: integer
60+
format: int64
61+
name:
62+
type: string
63+
example: Tom

0 commit comments

Comments
 (0)