[Bug][Kotlin-Spring] Wrong parameter type for multipart-form-data file upload when using kotlin-spring with reactive #21655
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
#Closes 21548
The kotlin-spring generator allows users to generate Spring Boot controllers & service interfaces for both reactive (Spring WebFlux) or non-reactive (Spring MVC) servers.
The WebFlux or MVC variants of Spring have different mechanisms to support file upload using multipart-form-data.
The kotlin-spring generator is creating controllers & interfaces which are appropriate for Spring MVC, but not for Spring WebFlux, even when the reactive option is set to true in the generator config.
For reactive controllers & interfaces the Kotlin Spring generator should be using
org.springframework.http.codec.multipart.Part
for file parameters (And usingFlux<Part>
for arrays of files).One of the reasons why there is a bit of churn in this PR is because the template logic for determining the class to use for file parameters was put into the
optionalDataType.mustache
template. IMO, this is not a great place for the logic as the original intent of theoptionalDataType.mustache
is to manage the Kotlin null safety operator.For this PR I've pulled the logic for determining the use of
MultipartFile
/Part
into its own template file,fileParamType.mustache
. This template is then invoked when the parameter in question is not a body parameter like so:{{^isBodyParam}}{{#isFile}}{{>fileParamType}}{{/isFile}}{{^isFile}}{{>optionalDataType}}{{/isFile}}{{/isBodyParam}}
Some of the template logic in
formParams.mustache
is difficult to follow because the template uses different Swagger annotations depending on if the form parameter is for a file or not. The relevant change is in the#isFile
section where the use of{{>optionalDataType}}
is changed to{{>fileParamType}}
. There is probably future work that could be done to simplify the readability of this template by pulling out the swagger annotation logic into its own template.There is a bit of churn in the unit tests in order to simplify the generation & validation logic to remove a bunch of duplication and make more effective use of
TestUtils.assertFileContains
to test that the file contains multiple strings.Oh, and an answer to a potential question: Why not change the type mapping for "file" based on whether the generated code is reactive / not reactive?
AFAIK the use of MultipartFile / Part is just for upload parameters. Download parameters should still be
org.springframework.core.io.Resource
.PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*
.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master
(upcoming7.x.0
minor release - breaking changes with fallbacks),8.0.x
(breaking changes without fallbacks)"fixes #123"
present in the PR description)Kotlin - @dr4ke616 (2018/08) @karismann (2019/03) @Zomzog (2019/04) @andrewemery (2019/10) @4brunu (2019/11) @yutaka0m (2020/03) @stefankoppier (2022/06) @e5l (2024/10)
Prior Work - PR #20108 - @ffffionn