Skip to content

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

@huberchrigu

Description

@huberchrigu

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.

Activity

huberchrigu

huberchrigu commented on May 1, 2025

@huberchrigu
Author

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

self-assigned this
on May 1, 2025
added
in: webIssues in web modules (web, webmvc, webflux, websocket)
and removed on May 1, 2025
added this to the 6.2.7 milestone on May 1, 2025
added a commit that references this issue on May 2, 2025
c067919
huberchrigu

huberchrigu commented on May 2, 2025

@huberchrigu
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

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @rstoyanchev@huberchrigu@spring-projects-issues

      Issue actions

        Fragment.create() requires mutable map - which is unusable when used with Kotlin · Issue #34848 · spring-projects/spring-framework