Closed
Description
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"
}
}
Metadata
Metadata
Assignees
Labels
No labels