0
votes

I am using Quarkus 1.11.1 with Kotlin 1.4.21 and Jackson 2.12.0.

I don't understand why when I send a POST request with a body of a data class that has a defined default parameter, this is not accepted and returns an error problem: Parameter specified as non-null is null

In the pom.xml file I have:

    <dependency>
        <groupId>com.fasterxml.jackson.module</groupId>
        <artifactId>jackson-module-kotlin</artifactId>
        <version>2.12.0</version>
    </dependency>
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-resteasy-jackson</artifactId>
    </dependency>
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-jackson</artifactId>
    </dependency>

The Quarkus Documentation says (https://quarkus.io/guides/kotlin#kotlin-and-jackson):

If the com.fasterxml.jackson.module:jackson-module-kotlin dependency and the quarkus-jackson extension (or the quarkus-resteasy-extension) have been added to project, then Quarkus automatically registers the KotlinModule to the ObjectMapper bean (see this guide for more details).

I have a data class like:

data class MyAttributes
@BsonCreator  constructor(
    @BsonProperty("myId")
    @JsonProperty("myId")
    var myId: String,
    @BsonProperty("name")
    @JsonProperty("name")
    val name: String,
    @BsonProperty("data")
    @JsonProperty("data", defaultValue = "{}")
    var data: MutableMap<String, Any> = mutableMapOf()
)

I noticed that the defaultValue in the @JsonProperty annotation is not useful, because it is used only to document expected values (https://fasterxml.github.io/jackson-annotations/javadoc/2.12/com/fasterxml/jackson/annotation/JsonProperty.html#defaultValue--)

If I send a JSON like:

{
    "myId": "AB123",
    "name": "my attribute name"
}

I get the error described previously, and the default value of the data field is ignored. If I send:

{
    "myId": "AB123",
    "name": "my attribute name",
    "data": {}
}

I don't get an error, because I send also the data field.

Can you tell me where am I doing wrong, please?

Thanks

1
do you have the jackson kotlin module defined when you create your objectmapper bean?Daniel Jacob
No, I don't create the objectmapper and I don't define the jackson kotlin module, I have only the dependency in the pom. Shouldn't Quarkus do everything automatically, as documentation says?Pietro Russo

1 Answers

0
votes

Do you have a default constructor? By default jackson requires a default constructor. This is not very common with data classes so you can either provide the constructor or you can do something like this:

@Bean
fun objectMapper(): ObjectMapper = ObjectMapper()
                    .registerKotlinModule()