Skip to content

Fragment.create() requires mutable map - which is unusable when used with Kotlin #34848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
huberchrigu opened this issue May 1, 2025 · 2 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@huberchrigu
Copy link

I use Fragment and FragmentsRendering in Kotlin. If I call something like that:

FragmentsRendering
    .with("myView", mapOf("modelParam" to "modelValue"))
    .build()

I get following error:

java.lang.UnsupportedOperationException: null
at java.base/java.util.Collections$SingletonMap.putIfAbsent(Collections.java:5291) ~[na:na]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
*__checkpoint ⇢ Handler MyController#getRequest(Id, Continuation) [DispatcherHandler]
Original Stack Trace:
at java.base/java.util.Collections$SingletonMap.putIfAbsent(Collections.java:5291) ~[na:na]
at org.springframework.web.reactive.result.view.Fragment.lambda$mergeAttributes$0(Fragment.java:97) ~[spring-webflux-6.2.5.jar:6.2.5]
at java.base/java.util.concurrent.ConcurrentHashMap.forEach(ConcurrentHashMap.java:1603) ~[na:na]
at org.springframework.web.reactive.result.view.Fragment.mergeAttributes(Fragment.java:97) ~[spring-webflux-6.2.5.jar:6.2.5]
at org.springframework.web.reactive.result.view.ViewResolutionResultHandler.renderFragment(ViewResolutionResultHandler.java:378) ~[spring-webflux-6.2.5.jar:6.2.5]
at org.springframework.web.reactive.result.view.ViewResolutionResultHandler.lambda$handleResult$0(ViewResolutionResultHandler.java:298) ~[spring-webflux-6.2.5.jar:6.2.5]
...

I found that Spring Webflux tries to add other parameters (in my case from a @ControllerAdvice) to the map. I have to migrate my model to a mutable map like

FragmentsRendering
    .with("myView", mapOf("modelParam" to "modelValue").toMutableMap() as Map<String, Any>)
    .build()

It would be nice if Spring Webflux could ensure that the map is mutable. Or if the builder is similar to Rendering and keeps its model map internal. Or at the very least, this contract should be documented at every function that accepts the model map.

@huberchrigu
Copy link
Author

Another option would be to provide a Kotlin extension for building fragments. There it could be handled specifically for immutable maps.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 1, 2025
@rstoyanchev rstoyanchev self-assigned this May 1, 2025
@rstoyanchev rstoyanchev added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels May 1, 2025
@rstoyanchev rstoyanchev added this to the 6.2.7 milestone May 1, 2025
@rstoyanchev rstoyanchev added type: bug A general bug and removed type: enhancement A general enhancement labels May 2, 2025
@huberchrigu
Copy link
Author

Wow, that was quick! Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants