Skip to content

Commit d7d21fd

Browse files
committed
Support nullable org.springframework.web.multipart.MultipartFile in Kotlin Spring generator
- nullable is only supported for MultipartFile. However, Array<MultipartFile> could be also nullable
1 parent 1c95001 commit d7d21fd

File tree

3 files changed

+144
-1
lines changed

3 files changed

+144
-1
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{^isFile}}{{{dataType}}}{{^required}}{{^defaultValue}}?{{/defaultValue}}{{/required}}{{/isFile}}{{#isFile}}{{#isArray}}Array<{{/isArray}}org.springframework.web.multipart.MultipartFile{{#isArray}}>{{/isArray}}{{^isArray}}{{^required}}?{{/required}}{{/isArray}}{{/isFile}}
1+
{{^isFile}}{{{dataType}}}{{^required}}{{^defaultValue}}?{{/defaultValue}}{{/required}}{{/isFile}}{{#isFile}}{{#isArray}}Array<{{/isArray}}org.springframework.web.multipart.MultipartFile{{#isArray}}>{{/isArray}}{{^required}}?{{/required}}{{/isFile}}

modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/spring/KotlinSpringServerCodegenTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,40 @@ public void delegateReactiveWithTags() throws Exception {
370370
"ApiUtil");
371371
}
372372

373+
374+
@Test
375+
public void testNullableMultipartFile() throws IOException {
376+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
377+
output.deleteOnExit();
378+
String outputPath = output.getAbsolutePath().replace('\\', '/');
379+
380+
OpenAPI openAPI = new OpenAPIParser()
381+
.readLocation("src/test/resources/3_0/kotlin/feat-multipartfile_nullable.yaml", null, new ParseOptions()).getOpenAPI();
382+
383+
KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
384+
codegen.setOutputDir(output.getAbsolutePath());
385+
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");
386+
387+
ClientOptInput input = new ClientOptInput();
388+
input.openAPI(openAPI);
389+
input.config(codegen);
390+
391+
DefaultGenerator generator = new DefaultGenerator();
392+
393+
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "false");
394+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
395+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
396+
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
397+
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");
398+
399+
generator.opts(input).generate();
400+
401+
assertFileContains(Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/NullableMultipartfileApiController.kt"),
402+
"file: org.springframework.web.multipart.MultipartFile?");
403+
assertFileContains(Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/NullableMultipartfileArrayApiController.kt"),
404+
"files: Array<org.springframework.web.multipart.MultipartFile>?");
405+
}
406+
373407
@Test
374408
public void arrayItemsCanBeNullable() throws IOException {
375409
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
openapi: 3.0.0
2+
servers:
3+
- url: 'https://example.org/v1'
4+
info:
5+
description: >-
6+
Example created for nullable multipartfile issue
7+
version: 1.0.0
8+
title: OpenAPI Stuff API created to reproduce issue
9+
license:
10+
name: Apache-2.0
11+
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
12+
tags:
13+
- name: multipartfile
14+
description: All about the nullable multipartfile
15+
security:
16+
- bearerAuth: []
17+
paths:
18+
/nullable-multipartfile:
19+
post:
20+
tags:
21+
- multipartfile
22+
summary: simple nullable multipartfile
23+
operationId: testNullableMultipartfile
24+
requestBody:
25+
content:
26+
multipart/form-data:
27+
schema:
28+
type: object
29+
properties:
30+
file:
31+
type: string
32+
description: File to upload
33+
format: binary
34+
jsonPayload:
35+
type: object
36+
description: simple json payload
37+
properties:
38+
name:
39+
type: string
40+
required:
41+
- jsonPayload
42+
responses:
43+
'200':
44+
description: successful operation
45+
content:
46+
application/json:
47+
schema:
48+
type: string
49+
'400':
50+
description: Invalid status value
51+
52+
/nullable-multipartfile-array:
53+
post:
54+
tags:
55+
- multipartfile
56+
summary: simple nullable multipartfile
57+
operationId: testNullableMultipartfile
58+
requestBody:
59+
content:
60+
multipart/form-data:
61+
schema:
62+
type: object
63+
properties:
64+
files:
65+
type: array
66+
items:
67+
type: string
68+
description: File to upload
69+
format: binary
70+
jsonPayload:
71+
type: object
72+
description: simple json payload
73+
properties:
74+
name:
75+
type: string
76+
required:
77+
- jsonPayload
78+
responses:
79+
'200':
80+
description: successful operation
81+
content:
82+
application/json:
83+
schema:
84+
type: string
85+
'400':
86+
description: Invalid status value
87+
externalDocs:
88+
description: Find out more about Swagger
89+
url: 'http://swagger.io'
90+
components:
91+
securitySchemes:
92+
bearerAuth:
93+
type: http
94+
scheme: bearer
95+
bearerFormat: JWT
96+
schemas:
97+
Stuff:
98+
type: object
99+
properties:
100+
id:
101+
type: integer
102+
format: int64
103+
name:
104+
type: string
105+
tag:
106+
type: string
107+
required:
108+
- name
109+

0 commit comments

Comments
 (0)