Skip to content

ParameterNamesModule fails to map Number into a class that wraps an int or long #5229

@AndreasHuber-CH

Description

@AndreasHuber-CH

Until version 2.17.3 Jackson was able to deserialize a number into wrapper classes such as this one:

class IntValue {

    private final int value;

    IntValue(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
}

Since version 2.18 and higher (I tested 2.19.2), the following error is reported:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of test.IntValue (although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (42)

This code was used to reproduce the problem, the ParameterNamesModule must be registered, and parameter names must be enabled in the compiler - both of these are done automatically in a typical spring-boot project :

        var objectMapper = new ObjectMapper();
        objectMapper.registerModule(new ParameterNamesModule());
        assertDoesNotThrow(() -> objectMapper.readValue("42", IntValue.class));

e.g. in pom.xml:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <parameters>true</parameters>
                </configuration>
            </plugin>
        </plugins>
    </build>

The problem can be fixed by annotating the IntValue constructor with @JsonCreator(mode = JsonCreator.Mode.DELEGATING) or by creating a static factory method in the class annotated with @JsonCreator.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions