@JsonProperty on Enum not working as key in Map
See original GitHub issueI’m using Jackson-Databind 2.8.1 and it seems like Enums that are keys in a Map aren’t respecting their @JsonProperty
settings.
Here’s my Enum:
public enum EventType {
@JsonProperty(value = "user.delete")
UserDelete,
@JsonProperty(value = "user.create")
UserCreate,
@JsonProperty(value = "user.update")
UserUpdate,
@JsonProperty(value = "user.deactivate")
UserDeactivate,
@JsonProperty(value = "user.bulk.create")
UserBulkCreate,
@JsonProperty(value = "user.reactivate")
UserReactivate,
@JsonProperty(value = "user.action")
UserAction
}
Here’s my object:
public static class EventConfiguration implements Buildable<EventConfiguration> {
public Map<EventType, EventConfigurationData> events = new HashMap<>();
public static class EventConfigurationData {
public boolean enabled;
public TransactionType transactionType;
public EventConfigurationData() {
}
public EventConfigurationData(boolean enabled, TransactionType transactionType) {
this.enabled = enabled;
this.transactionType = transactionType;
}
}
}
Here’s the ACTUAL and INCORRECT JSON:
{
"eventConfiguration": {
"events": {
"UserDelete": {
"enabled": true,
"transactionType": "None"
},
"UserCreate": {
"enabled": true,
"transactionType": "None"
},
"UserUpdate": {
"enabled": true,
"transactionType": "None"
},
"UserDeactivate": {
"enabled": true,
"transactionType": "None"
},
"UserBulkCreate": {
"enabled": true,
"transactionType": "None"
},
"UserReactivate": {
"enabled": true,
"transactionType": "None"
}
}
}
}
This is the JSON that comes from this call:
try {
body = objectMapper.writeValueAsBytes(request);
} catch (IOException e) {
throw new JSONException(e);
}
This is the EXPECTED and CORRECT JSON that Jackson is NOT generating:
{
"eventConfiguration": {
"events": {
"user.delete": {
"enabled": true,
"transactionType": "None"
},
"user.create": {
"enabled": true,
"transactionType": "None"
},
"user.update": {
"enabled": true,
"transactionType": "None"
},
"user.deactivate": {
"enabled": true,
"transactionType": "None"
},
"user.bulk.create": {
"enabled": true,
"transactionType": "None"
},
"user.reactivate": {
"enabled": true,
"transactionType": "None"
}
}
}
}
As you can see, when the JSON is generated by Jackson, it is not respecting the @JsonProperty
enum values but is instead using the EventType.name()
method. Additionally, if I feed the JSON that Jackson generates right back into Jackson, it fails. It should never fail as Jackson should always produce JSON that it can consume back (at least with my code here - I can definitely create code where this isn’t true).
Then when I turn around and parse this JSON using this code it explodes:
objectMapper.reader(EventConfiguration.class).readValue(request.getInputStream());
I get this stack trace:
com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize Map key of type com.inversoft.passport.domain.event.EventType from String "UserDelete": not a valid representation, problem: Can not deserialize Map key of type com.inversoft.passport.domain.event.EventType from String "UserDelete": not one of values excepted for Enum class: [user.delete, user.create, user.update, user.reactivate, user.bulk.create, user.deactivate, user.action]
at [Source: org.primeframework.mock.servlet.MockServletInputStream@5a634b91; line: 1, column: 507]
at [Source: org.primeframework.mock.servlet.MockServletInputStream@5a634b91; line: 1, column: 507] (through reference chain: com.inversoft.passport.domain.api.SystemConfigurationRequest["systemConfiguration"]->com.inversoft.passport.domain.SystemConfiguration["eventConfiguration"]->com.inversoft.passport.domain.EventConfiguration["events"])
Am I missing a configuration option or something that would make this work?
Issue Analytics
- State:
- Created 7 years ago
- Comments:13 (7 by maintainers)
Top GitHub Comments
I’ll create a new issue and paste the java version. As I said, this is not a kotlin issue, I was able to reproduce it on both sides. I just provided the kotlin test because it’s easier to read.
Cool thanks!