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

Refine ModelAttributeMethodProcessor Kotlin exception handling #23846

Closed
trygveaa opened this issue Oct 22, 2019 · 2 comments
Closed

Refine ModelAttributeMethodProcessor Kotlin exception handling #23846

trygveaa opened this issue Oct 22, 2019 · 2 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) theme: kotlin An issue related to Kotlin support type: enhancement A general enhancement
Milestone

Comments

@trygveaa
Copy link

I have a kotlin class with a non-nullable property, which is not using a primitive type in the jvm, and I'm trying to use the properties of that class as request parameters. If I don't include that property as a request parameter, I would expect a bad request response. However, it instead crashes with this exception:

java.lang.IllegalArgumentException: Parameter specified as non-null is null: method no.finntech.iaap.data_import.Params.<init>, parameter a

This is the code I'm using:

class Params(
        val a: String
)

@SpringBootApplication
@RestController
class Application {
    @GetMapping("/test")
    fun test(params: Params) = ""
}

If a is of type Int instead, or if I use a non-nullable String directly in the arguments to test as a RequestParam, I get a bad request as expected.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 22, 2019
@rstoyanchev rstoyanchev added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Nov 12, 2021
@sdeleuze sdeleuze added the theme: kotlin An issue related to Kotlin support label Jan 19, 2022
@sdeleuze
Copy link
Contributor

sdeleuze commented Feb 7, 2023

First, sorry for the delay for a response.

For Int, in ModelAttributeMethodProcessor#constructAttribute we get a TypeMismatchException: Failed to convert value of type 'null' to required type 'int'; Failed to convert from type [null] to type [int] for value 'null' which makes senses since int is a primitive type so not nullable at Java/JVM level.

For String, the Java type is nullable, so that's only at BeanUtils.instantiateClass(ctor, args) level that Kotlin awareness of nullness is involved and raises this exception that is from what I can see java.lang.NullPointerException: Parameter specified as non-null is null: method com.example.demokotlinnative.Params.<init>, parameter a.

I will discuss with the team if there is something to refine here or not.

@sdeleuze sdeleuze self-assigned this Feb 7, 2023
@sdeleuze
Copy link
Contributor

sdeleuze commented Feb 7, 2023

We can potentially try to wrap it for Kotlin parameter use case, I will give it a try and see how it goes.

@sdeleuze sdeleuze added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Feb 7, 2023
@sdeleuze sdeleuze added this to the 6.0.5 milestone Feb 7, 2023
@sdeleuze sdeleuze changed the title Non-nullable non-primitive properties in kotlin classes can't be used as request parameters Refine ModelAttributeMethodProcessor Kotlin exception handling Feb 15, 2023
sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue Feb 15, 2023
This commit refines ModelAttributeMethodProcessor Kotlin exception
handling in order to throw a proper MethodArgumentNotValidException
instead of a NullPointerException when Kotlin null-safety constraints
are not fulfilled, translating to an HTTP error with 400 status code
(Bad Request) instead of 500 (Internal Server Error).

Closes spring-projectsgh-23846
sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue Feb 15, 2023
This commit refines ModelAttributeMethodProcessor Kotlin exception
handling in order to throw a proper MethodArgumentNotValidException
instead of a NullPointerException when Kotlin null-safety constraints
are not fulfilled, translating to an HTTP error with 400 status code
(Bad Request) instead of 500 (Internal Server Error).

Closes spring-projectsgh-23846
izeye added a commit to izeye/spring-framework that referenced this issue Feb 17, 2023
@izeye izeye mentioned this issue Feb 17, 2023
sbrannen pushed a commit that referenced this issue Feb 19, 2023
sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue May 30, 2023
…sult) constructor

spring-projectsgh-23846 introduced a new
MethodArgumentNotValidException(Executable, BindingResult) constructor
that can be advantageously replaced by using
MethodArgumentNotValidException(MethodParameter, BindingResult)
in ModelAttributeMethodProcessor.

This commit updates ModelAttributeMethodProcessor accordingly,
and deprecates MethodArgumentNotValidException(Executable,
BindingResult) in favor of
MethodArgumentNotValidException(MethodParameter, BindingResult).

Closes spring-projectsgh-30558
sdeleuze added a commit that referenced this issue May 30, 2023
…sult) constructor

gh-23846 introduced a new
MethodArgumentNotValidException(Executable, BindingResult)
constructor that can be advantageously replaced by using
MethodArgumentNotValidException(MethodParameter, BindingResult)
in ModelAttributeMethodProcessor.

This commit updates ModelAttributeMethodProcessor accordingly,
and deprecates MethodArgumentNotValidException(Executable,
BindingResult) in favor of
MethodArgumentNotValidException(MethodParameter, BindingResult).

Closes gh-30558
mdeinum pushed a commit to mdeinum/spring-framework that referenced this issue Jun 29, 2023
This commit refines ModelAttributeMethodProcessor Kotlin exception
handling in order to throw a proper MethodArgumentNotValidException
instead of a NullPointerException when Kotlin null-safety constraints
are not fulfilled, translating to an HTTP error with 400 status code
(Bad Request) instead of 500 (Internal Server Error).

Closes spring-projectsgh-23846
mdeinum pushed a commit to mdeinum/spring-framework that referenced this issue Jun 29, 2023
mdeinum pushed a commit to mdeinum/spring-framework that referenced this issue Jun 29, 2023
…sult) constructor

spring-projectsgh-23846 introduced a new
MethodArgumentNotValidException(Executable, BindingResult)
constructor that can be advantageously replaced by using
MethodArgumentNotValidException(MethodParameter, BindingResult)
in ModelAttributeMethodProcessor.

This commit updates ModelAttributeMethodProcessor accordingly,
and deprecates MethodArgumentNotValidException(Executable,
BindingResult) in favor of
MethodArgumentNotValidException(MethodParameter, BindingResult).

Closes spring-projectsgh-30558
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: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants