Skip to content

Commit d840c49

Browse files
[FEAT] Support nullable Array<org.springframework.web.multipart.MultipartFile> in Kotlin-Spring generator (#21994)
* Support nullable `org.springframework.web.multipart.MultipartFile` in Kotlin Spring generator - nullable is only supported for MultipartFile. However, Array<MultipartFile> could be also nullable * Support nullable `org.springframework.web.multipart.MultipartFile` in Kotlin Spring generator * Support nullable `org.springframework.web.multipart.MultipartFile` in Kotlin Spring generator Update kotlin-spring-additionalproperties samples
1 parent 7c00068 commit d840c49

File tree

38 files changed

+256
-36
lines changed

38 files changed

+256
-36
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}}{{#isNullable}}?{{/isNullable}}{{/isFile}}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,44 @@ 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+
assertFileContains(Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/NonNullableMultipartfileApiController.kt"),
406+
"file: org.springframework.web.multipart.MultipartFile)");
407+
assertFileContains(Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/NonNullableMultipartfileArrayApiController.kt"),
408+
"files: Array<org.springframework.web.multipart.MultipartFile>)");
409+
}
410+
373411
@Test
374412
public void arrayItemsCanBeNullable() throws IOException {
375413
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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+
nullable: true
35+
jsonPayload:
36+
type: object
37+
description: simple json payload
38+
properties:
39+
name:
40+
type: string
41+
required:
42+
- jsonPayload
43+
responses:
44+
'200':
45+
description: successful operation
46+
content:
47+
application/json:
48+
schema:
49+
type: string
50+
'400':
51+
description: Invalid status value
52+
53+
/nullable-multipartfile-array:
54+
post:
55+
tags:
56+
- multipartfile
57+
summary: simple nullable multipartfile
58+
operationId: testNullableMultipartfile
59+
requestBody:
60+
content:
61+
multipart/form-data:
62+
schema:
63+
type: object
64+
properties:
65+
files:
66+
type: array
67+
items:
68+
type: string
69+
description: File to upload
70+
format: binary
71+
nullable: true
72+
jsonPayload:
73+
type: object
74+
description: simple json payload
75+
properties:
76+
name:
77+
type: string
78+
required:
79+
- jsonPayload
80+
responses:
81+
'200':
82+
description: successful operation
83+
content:
84+
application/json:
85+
schema:
86+
type: string
87+
'400':
88+
description: Invalid status value
89+
/non-nullable-multipartfile:
90+
post:
91+
tags:
92+
- multipartfile
93+
summary: simple non nullable multipartfile
94+
operationId: testNonNullableMultipartfile
95+
requestBody:
96+
content:
97+
multipart/form-data:
98+
schema:
99+
type: object
100+
properties:
101+
file:
102+
type: string
103+
description: File to upload
104+
format: binary
105+
nullable: false
106+
jsonPayload:
107+
type: object
108+
description: simple json payload
109+
properties:
110+
name:
111+
type: string
112+
required:
113+
- jsonPayload
114+
responses:
115+
'200':
116+
description: successful operation
117+
content:
118+
application/json:
119+
schema:
120+
type: string
121+
'400':
122+
description: Invalid status value
123+
124+
/non-nullable-multipartfile-array:
125+
post:
126+
tags:
127+
- multipartfile
128+
summary: simple non nullable multipartfile
129+
operationId: testNonNullableMultipartfile
130+
requestBody:
131+
content:
132+
multipart/form-data:
133+
schema:
134+
type: object
135+
properties:
136+
files:
137+
type: array
138+
items:
139+
type: string
140+
description: File to upload
141+
format: binary
142+
nullable: false
143+
jsonPayload:
144+
type: object
145+
description: simple json payload
146+
properties:
147+
name:
148+
type: string
149+
required:
150+
- jsonPayload
151+
responses:
152+
'200':
153+
description: successful operation
154+
content:
155+
application/json:
156+
schema:
157+
type: string
158+
'400':
159+
description: Invalid status value
160+
externalDocs:
161+
description: Find out more about Swagger
162+
url: 'http://swagger.io'
163+
components:
164+
securitySchemes:
165+
bearerAuth:
166+
type: http
167+
scheme: bearer
168+
bearerFormat: JWT
169+
schemas:
170+
Stuff:
171+
type: object
172+
properties:
173+
id:
174+
type: integer
175+
format: int64
176+
name:
177+
type: string
178+
tag:
179+
type: string
180+
required:
181+
- name
182+

samples/server/petstore/kotlin-spring-cloud/src/main/kotlin/org/openapitools/api/PetApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ interface PetApi {
110110
produces = ["application/json"],
111111
consumes = ["multipart/form-data"]
112112
)
113-
fun uploadFile( @PathVariable("petId") petId: kotlin.Long, @Valid @RequestParam(value = "additionalMetadata", required = false) additionalMetadata: kotlin.String? , @Valid @RequestPart("file", required = false) file: org.springframework.web.multipart.MultipartFile?): ResponseEntity<ModelApiResponse> {
113+
fun uploadFile( @PathVariable("petId") petId: kotlin.Long, @Valid @RequestParam(value = "additionalMetadata", required = false) additionalMetadata: kotlin.String? , @Valid @RequestPart("file", required = false) file: org.springframework.web.multipart.MultipartFile): ResponseEntity<ModelApiResponse> {
114114
return ResponseEntity(HttpStatus.NOT_IMPLEMENTED)
115115
}
116116
}

samples/server/petstore/kotlin-spring-default/src/main/kotlin/org/openapitools/api/PetApiController.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class PetApiController() {
176176
produces = ["application/json"],
177177
consumes = ["multipart/form-data"]
178178
)
179-
fun uploadFile(@Parameter(description = "ID of pet to update", required = true) @PathVariable("petId") petId: kotlin.Long,@Parameter(description = "Additional data to pass to server") @Valid @RequestParam(value = "additionalMetadata", required = false) additionalMetadata: kotlin.String? ,@Parameter(description = "file to upload") @Valid @RequestPart("file", required = false) file: org.springframework.web.multipart.MultipartFile?): ResponseEntity<ModelApiResponse> {
179+
fun uploadFile(@Parameter(description = "ID of pet to update", required = true) @PathVariable("petId") petId: kotlin.Long,@Parameter(description = "Additional data to pass to server") @Valid @RequestParam(value = "additionalMetadata", required = false) additionalMetadata: kotlin.String? ,@Parameter(description = "file to upload") @Valid @RequestPart("file", required = false) file: org.springframework.web.multipart.MultipartFile): ResponseEntity<ModelApiResponse> {
180180
return ResponseEntity(HttpStatus.NOT_IMPLEMENTED)
181181
}
182182
}

samples/server/petstore/kotlin-springboot-3/src/main/kotlin/org/openapitools/api/PetApiController.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class PetApiController(@Autowired(required = true) val service: PetApiService) {
106106
produces = ["application/json"],
107107
consumes = ["multipart/form-data"]
108108
)
109-
fun uploadFile( @PathVariable("petId") petId: kotlin.Long, @Valid @RequestParam(value = "additionalMetadata", required = false) additionalMetadata: kotlin.String? , @Valid @RequestPart("file", required = false) file: org.springframework.web.multipart.MultipartFile?): ResponseEntity<ModelApiResponse> {
109+
fun uploadFile( @PathVariable("petId") petId: kotlin.Long, @Valid @RequestParam(value = "additionalMetadata", required = false) additionalMetadata: kotlin.String? , @Valid @RequestPart("file", required = false) file: org.springframework.web.multipart.MultipartFile): ResponseEntity<ModelApiResponse> {
110110
return ResponseEntity(service.uploadFile(petId, additionalMetadata, file), HttpStatus.valueOf(200))
111111
}
112112
}

samples/server/petstore/kotlin-springboot-3/src/main/kotlin/org/openapitools/api/PetApiService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,5 @@ interface PetApiService {
9999
* @return successful operation (status code 200)
100100
* @see PetApi#uploadFile
101101
*/
102-
fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: org.springframework.web.multipart.MultipartFile?): ModelApiResponse
102+
fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: org.springframework.web.multipart.MultipartFile): ModelApiResponse
103103
}

samples/server/petstore/kotlin-springboot-3/src/main/kotlin/org/openapitools/api/PetApiServiceImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class PetApiServiceImpl : PetApiService {
3434
TODO("Implement me")
3535
}
3636

37-
override fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: org.springframework.web.multipart.MultipartFile?): ModelApiResponse {
37+
override fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: org.springframework.web.multipart.MultipartFile): ModelApiResponse {
3838
TODO("Implement me")
3939
}
4040
}

samples/server/petstore/kotlin-springboot-additionalproperties/src/main/kotlin/org/openapitools/api/PetApiController.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class PetApiController(@Autowired(required = true) val service: PetApiService) {
106106
produces = ["application/json"],
107107
consumes = ["multipart/form-data"]
108108
)
109-
fun uploadFile( @PathVariable("petId") petId: kotlin.Long, @Valid @RequestParam(value = "additionalMetadata", required = false) additionalMetadata: kotlin.String? , @Valid @RequestPart("file", required = false) file: org.springframework.web.multipart.MultipartFile?): ResponseEntity<ModelApiResponse> {
109+
fun uploadFile( @PathVariable("petId") petId: kotlin.Long, @Valid @RequestParam(value = "additionalMetadata", required = false) additionalMetadata: kotlin.String? , @Valid @RequestPart("file", required = false) file: org.springframework.web.multipart.MultipartFile): ResponseEntity<ModelApiResponse> {
110110
return ResponseEntity(service.uploadFile(petId, additionalMetadata, file), HttpStatus.valueOf(200))
111111
}
112112
}

samples/server/petstore/kotlin-springboot-additionalproperties/src/main/kotlin/org/openapitools/api/PetApiService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,5 @@ interface PetApiService {
9999
* @return successful operation (status code 200)
100100
* @see PetApi#uploadFile
101101
*/
102-
fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: org.springframework.web.multipart.MultipartFile?): ModelApiResponse
102+
fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: org.springframework.web.multipart.MultipartFile): ModelApiResponse
103103
}

0 commit comments

Comments
 (0)