You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It should be possible to bind JSON to a Kotlin data class with default value(s) in the constructor without providing values (in the JSON) for the already defined constructor defaults.
Having this data class:
@Serdeable
data class Foo(
val bar: String,
var validFrom: LocalDate = LocalDate.now(),
)
And the following controller:
@Controller("/foos")
class FooController {
@Post
fun save(@Body foo: Foo): HttpResponse<Foo> {
return HttpResponse.created(foo)
}
}
This JSON should be valid to create an instance of Foo when posting to /foos:
{
"bar": "some_value"
}
Actual Behaviour
When trying to bind JSON (see example above) to a Kotlin data class containing a default value in the constructor, I will get the following error when posting a JSON without an explicit value for the non-nullable (here validFrom) property:
"Failed to convert argument [foo] for value [null] due to: Unable to deserialize type [com.example.Foo]. Required constructor parameter [LocalDate validFrom] at index [1] is not present or is null in the supplied data"
Steps To Reproduce
Clone example application
Run test using ./gradlew test
Test will fail caused by a Bad Request
Or
Clone example application
Run application using ./gradlew run
Post a JSON with validFrom value will return a JSON representation of the Foo instance:
// Response
{
"_links": {
"self": [
{
"href": "/foos",
"templated": false
}
]
},
"_embedded": {
"errors": [
{
"message": "Failed to convert argument [foo] for value [null] due to: Unable to deserialize type [com.example.Foo]. Required constructor parameter [LocalDate validFrom] at index [1] is not present or is null in the supplied data",
"path": "/foo"
}
]
},
"message": "Bad Request"
}
Expected Behavior
It should be possible to bind JSON to a Kotlin data class with default value(s) in the constructor without providing values (in the JSON) for the already defined constructor defaults.
Having this data class:
And the following controller:
This JSON should be valid to create an instance of
Foo
when posting to/foos
:Actual Behaviour
When trying to bind JSON (see example above) to a Kotlin data class containing a default value in the constructor, I will get the following error when posting a JSON without an explicit value for the non-nullable (here
validFrom
) property:Steps To Reproduce
./gradlew test
Bad Request
Or
./gradlew run
validFrom
value will return a JSON representation of theFoo
instance:validFrom
value will return a400 Bad Request
:Environment Information
Example Application
https://github.com/davidsonnabend/micronaut-serialization-kotlin-constructor-defaults-deserialization
Version
4.1.6
The text was updated successfully, but these errors were encountered: