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

@JsonKeyis ignored with parameter-names module registered #206

Closed
bertwin opened this issue Jan 28, 2021 · 4 comments
Closed

@JsonKeyis ignored with parameter-names module registered #206

bertwin opened this issue Jan 28, 2021 · 4 comments
Milestone

Comments

@bertwin
Copy link

bertwin commented Jan 28, 2021

Describe the bug
When hibernate5 or parameter-names modules are registered, the keys are serialized using toString() instead of using the @JsonKey annotated property.

Version information
2.12.1

To Reproduce

dependencies {
    implementation platform("com.fasterxml.jackson:jackson-bom:2.12.1")

    implementation 'com.fasterxml.jackson.core:jackson-databind'
    implementation 'com.fasterxml.jackson.datatype:jackson-datatype-hibernate5'
    implementation 'com.fasterxml.jackson.module:jackson-module-parameter-names'


    implementation 'org.hibernate:hibernate-core:5.4.27.Final'

    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}
class TeamTest {

    public static final Team TEAM = new Team("a", "Team A");
    public static final Map<Team, Team> TEAM_MAP = Map.of(TEAM, TEAM);
    public static final String EXPECTED = "{\"a\":{\"id\":\"a\",\"name\":\"Team A\"}}";

    @Test
    void base() throws JsonProcessingException {
        var objectMapper = new ObjectMapper();
        var json = objectMapper.writeValueAsString(TEAM_MAP);
        assertEquals(EXPECTED, json);
    }

    @Test
    void withHibernate() throws JsonProcessingException {

        var objectMapper = new ObjectMapper().registerModules(new Hibernate5Module());

        var json = objectMapper.writeValueAsString(TEAM_MAP);
        assertEquals(EXPECTED, json);
    }

    @Test
    void withParameterNames() throws JsonProcessingException {

        var objectMapper = new ObjectMapper().registerModules(new ParameterNamesModule());

        var json = objectMapper.writeValueAsString(TEAM_MAP);
        assertEquals(EXPECTED, json);
    }
}

class Team {
    @JsonKey
    private final String id;
    private final String name;

    public Team(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Team team = (Team) o;
        return id.equals(team.id);
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", Team.class.getSimpleName() + "[", "]")
                .add("id='" + id + "'")
                .add("name='" + name + "'")
                .toString();
    }
}

Expected behavior
Expected in all cases:

{
  "a": {
    "id": "a",
    "name": "Team A"
  }
}

Actual

{
  "Team[id='a', name='Team A']": {
    "id": "a",
    "name": "Team A"
  }
}
@cowtowncoder cowtowncoder transferred this issue from FasterXML/jackson-databind Feb 19, 2021
@cowtowncoder
Copy link
Member

Interesting. I can reproduce this with parameter-names module; hope to figure out how it works... cannot immediately figure out why there should be any difference.

@cowtowncoder
Copy link
Member

Ahhhhh. So the original issue forgot to augment AnnotationIntrospectorPair... hence override won't work.

@cowtowncoder cowtowncoder changed the title @JsonKey is ignored with hibernate5 or parameter-names module registered @JsonKeyis ignored with parameter-names module registered Feb 19, 2021
@cowtowncoder cowtowncoder added this to the 2.12.2 milestone Feb 19, 2021
@cowtowncoder
Copy link
Member

Fixed via FasterXML/jackson-databind#3060

@cowtowncoder
Copy link
Member

@bertwin Thank you for reporting this issue! Turns out it was a piece missing from original @JsonKey code; will be fixed in 2.12.2.

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

No branches or pull requests

2 participants