Skip to content

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

Closed
@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.

Metadata

Metadata

Assignees

Labels

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

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions