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

Add a non-null variant of ServerRequest.queryParam() and ServerRequest.param(). #32763

Closed

Conversation

earlgrey02
Copy link
Contributor

@earlgrey02 earlgrey02 commented May 5, 2024

Summary

ServerRequest.paramNotNull()(MVC) and ServerRequest.queryParamNotNull()(WebFlux) has been added to retrieve query parameters that are sure to be non-null without unnecessary null checking (Optional Wrapping, etc.).

Description

In general, functional endpoint allows filtering HTTP requests via RequestPredicates. For query parameters, requests can be mapped only when specific query parameters are received through RequestPredicate.queryParam() or RequestPredicate.param(). If request with a query parameter whose names do not match RequestPredicate is not received, handler mapping is not performed, so request with the corresponding query parameter must exist to reach the handler. In this case, we have determined that null checking for query parameters is unnecessary, so we are posting this PR.

Example

@Configuration
class TestRouter {
    @Bean
    fun testRoutes(handler: TestHandler): RouterFunction<ServerResponse> =
        router {
            // If there is no test query parameter, perform a handler mapping(404 NOT FOUND)
            GET("/test", queryParam("test") { true }, handler::test)
        }
}
@Component
class TestHandler {
    fun test(request: ServerRequest): Mono<ServerResponse> =
        ServerResponse.ok()
            // The test query parameter cannot be null.
            // It is an unnecessary null check(In the case of Java, there is an overhead due to Optional Wrapping)
            .bodyValue(request.queryParamOrNull("test")!!)
}

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 5, 2024
@earlgrey02 earlgrey02 changed the title Add ServerRequestExtensions.queryParamNotNull(), a non-null variant of ServerRequest.queryParam(). Add a non-null variant of ServerRequest.queryParam() and ServerRequest.param(). May 5, 2024
@snicoll snicoll added in: web Issues in web modules (web, webmvc, webflux, websocket) theme: kotlin An issue related to Kotlin support labels May 6, 2024
@sdeleuze sdeleuze self-assigned this May 6, 2024
@sdeleuze
Copy link
Contributor

sdeleuze commented May 7, 2024

Let see with @poutsma when he is available (most of us are on PTO this week) if there is interest for that on Java API side.

@sdeleuze sdeleuze added the status: waiting-for-internal-feedback An issue that needs input from a member or another Spring Team label May 7, 2024
@poutsma
Copy link
Contributor

poutsma commented May 15, 2024

Let see with @poutsma when he is available (most of us are on PTO this week) if there is interest for that on Java API side.

I fear that if we add this variant, we would have to do the same for the other SeverRequest methods that return Optional types, thus resulting in four extra methods instead of one. I don't think that adding that much noise in the API is worth it, in Java nor in Kotlin, especially considering that you can accomplish the same via request.servletRequest().getParameter() in Servlet or request.exchange().getRequest().getQueryParams().getFirst() in WebFlux.

@sdeleuze
Copy link
Contributor

I tend to agree, and am declining the proposed additional extension.

@sdeleuze sdeleuze closed this May 15, 2024
@sdeleuze sdeleuze added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on status: waiting-for-internal-feedback An issue that needs input from a member or another Spring Team labels May 15, 2024
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) status: declined A suggestion or change that we don't feel we should currently apply theme: kotlin An issue related to Kotlin support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants