Skip to content
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

Spring webflux @ModelAttribute annotated methods not working with kotlin suspend methods #30894

Closed
BarracudaX opened this issue Jul 15, 2023 · 0 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) theme: kotlin An issue related to Kotlin support type: bug A general bug
Milestone

Comments

@BarracudaX
Copy link

I am using spring webflux with spring boot 3.1.0 and 1.1.0 io.spring.dependency-management gradle plugin(spring webflux 6.0.9).
According to documentation, spring should call methods annotated with @ModelAttribute before any @RequestMapping, and it should support reactive types for web flux. This works fine for Mono and Flux return types, but it doesn't work with kotlin suspend functions.


@Controller
class ViewController {

    @GetMapping("/")
    suspend fun homePage() : String {
        return "index"
    }
    @ModelAttribute(name = "roles")
    suspend fun roleAttributeInitializer(model: Model) : List<String>{
        return ReactiveSecurityContextHolder.getContext().map { sec -> sec.authentication?.authorities?.map { it.authority } ?: emptyList() }.awaitSingle()
    }

    @ModelAttribute
    suspend fun roleAttributeInitializer2(model: Model){
        model.addAttribute("roles",ReactiveSecurityContextHolder.getContext().map { sec -> sec.authentication?.authorities?.map { it.authority } ?: emptyList() })
    }

}

In the example above, methods annotated with @ModelAttribute are invoked after the @GetMapping annotated method when I debug with breakpoints.
Also, the first suspend method,even though invoked after @GetMapping, still populates the model correctly because it doesn't cause NullPointerException when rendering the view. But the second suspend method,which is also invoked after the @GetMapping, doesn't populate the model and I am getting NullPointerException when rendering the view because roles is null.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 15, 2023
@sdeleuze sdeleuze self-assigned this Aug 18, 2023
@sdeleuze sdeleuze added the theme: kotlin An issue related to Kotlin support label Aug 18, 2023
@sdeleuze sdeleuze added in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 13, 2023
@sdeleuze sdeleuze added this to the 6.0.12 milestone Sep 13, 2023
sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue Sep 13, 2023
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) theme: kotlin An issue related to Kotlin support type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants