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 Boot WebFlux validation of invalid inputs. #30960

Closed
amidukr opened this issue Jul 27, 2023 · 4 comments
Closed

Spring Boot WebFlux validation of invalid inputs. #30960

amidukr opened this issue Jul 27, 2023 · 4 comments
Assignees
Labels
status: invalid An issue that we don't feel is valid theme: kotlin An issue related to Kotlin support

Comments

@amidukr
Copy link

amidukr commented Jul 27, 2023

I am building java application using Spring Boot 2.7.12
And I have very simpler REST API controller like that:

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController


@RestController
@RequestMapping("/sample")
class SampleController {

    data class Arguments (
        val intValue: Int
    )


    @GetMapping("/echo")
    fun resource(args: Arguments) = "Return back: ${args.intValue}"
}

And the issue when I am making a regular regular request as expected, everything works fine:

$ curl http://localhost:8080/sample/echo?intValue=111
Return back: 111

However if I will enter non-numeric value, Spring Boot is crashing:

$ curl http://localhost:8080/sample/echo?intValue=111aaa
{"timestamp":"2023-07-27T16:03:34.023+00:00","path":"/sample/echo","status":500,"error":"Internal Server Error","requestId":"de11f158-6"}

If it is non-valid input, it should rather be 400 Bad Requests, than 500 Internal errors, and such things should be handled by the framework.

We using the Spring Boot framework, because we need some framework that will handle all edge case scenarios with mapping of the HTTP request parameters to controller input arguments, and that obviously should be handled properly by the framework.

Could you please advise, how to handle such HTTP requests properly?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 27, 2023
@amidukr
Copy link
Author

amidukr commented Jul 29, 2023

As a workaround, I could use Java POJO instead of kotlin data class, and with use of POJO it returns 400 HTTP response code for non-numeric inputs, so it seems that issue only with Kotlin data class. However, I am looking for a cleaner way of doing things, where I don't duplicate into POJO content to enable use of kotlin data class.

@RestController
@RequestMapping("/sample")
class SampleController {

    data class Arguments(val intValue: Int) {
        class  Pojo{
            var intValue: Int = -1

            fun toData() = Arguments(intValue =  this.intValue)
        }
    }

    @GetMapping("/echo")
    fun resource(args: Arguments.Pojo) = "Return back: ${args.toData().intValue}"
}

@sdeleuze sdeleuze added the theme: kotlin An issue related to Kotlin support label Aug 1, 2023
@sdeleuze sdeleuze self-assigned this Aug 1, 2023
@sdeleuze
Copy link
Contributor

I can't reproduce with Spring Boot 2.7.12 and Kotlin 1.8.22. Make sure you have com.fasterxml.jackson.module:jackson-module-kotlin and org.jetbrains.kotlin:kotlin-reflect dependencies on the classpath.

@sdeleuze sdeleuze closed this as not planned Won't fix, can't repro, duplicate, stale Aug 10, 2023
@sdeleuze sdeleuze added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Aug 10, 2023
@amidukr
Copy link
Author

amidukr commented Aug 13, 2023

Hi @sdeleuze ,

  • All you recommendation are there.
  • And I can assure you, that issue is reproducible.

I didn't find how to reopen this ticket, so I created a new one: #31045, please check all steps, logs outputs, commands to run, and entire source code is there.

@sdeleuze
Copy link
Contributor

sdeleuze commented Aug 14, 2023

Next time I would suggest to just comment on the existing issue, add a repro, and I would have reopen this issue, but let's continue the conversation in the more complete new issue you have created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid theme: kotlin An issue related to Kotlin support
Projects
None yet
Development

No branches or pull requests

3 participants