Skip to content

Return multiple missing kotlin parameters at once #806

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

Open
jasiustasiu opened this issue Jun 11, 2024 · 6 comments
Open

Return multiple missing kotlin parameters at once #806

jasiustasiu opened this issue Jun 11, 2024 · 6 comments

Comments

@jasiustasiu
Copy link

Use case

We expose public API and we want to to be as descriptive as possible returning bad request response. So we want to return all required null fields at once. By using KotlinValueInstantiator and MissingKotlinParameterException we are able to return just one at the time. So we use @NotNull annotation validation but this requires us to mark non-null fields as nullable and then use ugly !! in the code eg.

data class Task(
    @field:NotNull
    val taskType: TaskType?,
    @field:NotNull
    val createdAt: Instant?,
    @field:NotNull
    val updatedAt: Instant?,
)

Describe the solution you'd like

It would be much nicer if exception thrown by jackson kotlin module contained all fields that are invalid (MissingKotlinParametersException with a list of missing parameters?). Then we could simplify example above to

data class Task(
    val taskType: TaskType,
    val createdAt: Instant,
    val updatedAt: Instant,
)

It could be configurable with some kind of flag to keep backward compatibility and not iterate through all fields if not necessary.

Describe alternatives you've considered

No response

Additional context

No response

@k163377
Copy link
Contributor

k163377 commented Sep 14, 2024

At least I don't have the motivation to work on this.
I agree that it is useful, but I think most libraries (e.g. kotlin-reflect) only report one missing content.

@aboutZZ
Copy link

aboutZZ commented Sep 20, 2024

@k163377 Hi, I want to handle MismatchedInputException in spring boot, so I added a DeserializationProblemHandler, but the handler didn't handle MismatchedInputException (it only handle the unknown property problems, I want handle input null for nonnull props situation), any way to resolve this?

@k163377
Copy link
Contributor

k163377 commented Jan 2, 2025

@aboutZZ
Sorry for the late reply.

I have checked the code a bit.
When an exception occurs in KotlineValueInstantiator.createFromObjectWith, it appears to be because the exception should have been handled via DeserializationContext.handleInstantiationProblem, but was throw directly.
https://github.com/FasterXML/jackson-module-kotlin/blob/9d4ad6a5e85bc5eb58f69700199c7cee776d2f86/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt

This would cause the registered DeserializationProblemHandler to be skipped.

The databind code seems to call for this.
https://github.com/FasterXML/jackson-databind/blob/f624751d3c4c13405c8d07051b8954a691f265f7/src/main/java/com/fasterxml/jackson/databind/deser/std/StdValueInstantiator.java#L284-L295

However, changing this behavior would also destructively change the type of the thrown exception, which seems difficult to fix.

@k163377
Copy link
Contributor

k163377 commented Jan 5, 2025

@aboutZZ
I have created an Issue regarding the above comment(#875).
I checked the code again, and it seemed to me that it could be corrected without destructive changes.

@KasperWolsink99
Copy link

I am also very interested in a solution to the problem mentioned in the original post.

I tried implementing a custom deserialization modifier. The idea is that before actual deserialization, you use reflecting to check all fields missing in the target class.

I had posted about this here:
FasterXML/jackson#262

@k163377 do you know what it will take to make such an approach work?

@k163377
Copy link
Contributor

k163377 commented Jan 21, 2025

I think you need to fix KotlinValueInstantiator.
There are default values provided by Jackson, for example emptyValue, so the actual missing value needs to be handled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants