-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[Jackson 3] Fix #3580 Enum (de)serialization in conjunction with JsonFormat.Shape.NUMBER_INT
#5338
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
Open
JooHyukKim
wants to merge
11
commits into
FasterXML:3.x
Choose a base branch
from
JooHyukKim:fix-3580-in-jackson-3
base: 3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+228
−9
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7bb4802
Fix
JooHyukKim be78ac9
Jackson 3 version
JooHyukKim 27cea5d
Clean up vewrsion information
JooHyukKim 783f39c
Merge branch '3.x' into fix-3580-in-jackson-3
JooHyukKim f9ba89b
Update EnumDeserializer.java
JooHyukKim 91bff73
Merge branch '3.x' into fix-3580-in-jackson-3
cowtowncoder ba2dfa7
Merge branch '3.x' into fix-3580-in-jackson-3
cowtowncoder 41bec28
Merge branch '3.x' into fix-3580-in-jackson-3
cowtowncoder 9e21605
Merge branch '3.x' into fix-3580-in-jackson-3
JooHyukKim 4850211
Merge branch 'fix-3580-in-jackson-3' of https://github.com/JooHyukKim…
JooHyukKim 2735c53
Adapt to 3.1.0 version #3580
JooHyukKim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,8 +65,30 @@ public class EnumDeserializer | |
| */ | ||
| protected final CompactStringObjectMap _lookupByEnumNaming; | ||
|
|
||
| /** | ||
| * We may also have integer-type of representation for Enum's, along with `@JsonValue`. | ||
| * | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| * @since 3.1 | ||
| */ | ||
| protected final CompactStringObjectMap _lookupByShapeNumberInt; | ||
|
|
||
| /** | ||
| * Flag to check if FormatShape of int number type would be used to deserialize | ||
| */ | ||
| protected final boolean _isShapeNumberInt; | ||
|
|
||
| @Deprecated // since 3.1.0 | ||
| public EnumDeserializer(EnumResolver byNameResolver, boolean caseInsensitive, | ||
| EnumResolver byEnumNamingResolver, EnumResolver toStringResolver) | ||
| EnumResolver byEnumNamingResolver, EnumResolver toStringResolver) | ||
| { | ||
| this(byNameResolver, caseInsensitive, byEnumNamingResolver, toStringResolver, null); | ||
| } | ||
|
|
||
| /** | ||
| * @since 3.1.0 | ||
| */ | ||
| public EnumDeserializer(EnumResolver byNameResolver, boolean caseInsensitive, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to start deprecating methods, adding overloads (since it's 3.1, not 3.0) |
||
| EnumResolver byEnumNamingResolver, EnumResolver toStringResolver, EnumResolver shapeNumberResolver) | ||
| { | ||
| super(byNameResolver.getEnumClass()); | ||
| _lookupByName = byNameResolver.constructLookup(); | ||
|
|
@@ -75,8 +97,10 @@ public EnumDeserializer(EnumResolver byNameResolver, boolean caseInsensitive, | |
| _enumDefaultValue = byNameResolver.getDefaultValue(); | ||
| _caseInsensitive = caseInsensitive; | ||
| _isFromIntValue = byNameResolver.isFromIntValue(); | ||
| _isShapeNumberInt = shapeNumberResolver != null; | ||
| _lookupByEnumNaming = byEnumNamingResolver == null ? null : byEnumNamingResolver.constructLookup(); | ||
| _lookupByToString = toStringResolver == null ? null : toStringResolver.constructLookup(); | ||
| _lookupByShapeNumberInt = shapeNumberResolver == null ? null : shapeNumberResolver.constructLookup(); | ||
| } | ||
|
|
||
| protected EnumDeserializer(EnumDeserializer base, boolean caseInsensitive, | ||
|
|
@@ -89,10 +113,13 @@ protected EnumDeserializer(EnumDeserializer base, boolean caseInsensitive, | |
| _enumDefaultValue = base._enumDefaultValue; | ||
| _caseInsensitive = caseInsensitive; | ||
| _isFromIntValue = base._isFromIntValue; | ||
| _isShapeNumberInt = base._isShapeNumberInt; | ||
| _useDefaultValueForUnknownEnum = useDefaultValueForUnknownEnum; | ||
| _useNullForUnknownEnum = useNullForUnknownEnum; | ||
| _lookupByEnumNaming = base._lookupByEnumNaming; | ||
| _lookupByToString = base._lookupByToString; | ||
| _lookupByShapeNumberInt = base._lookupByShapeNumberInt; | ||
|
|
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -194,6 +221,10 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt) | |
| // 26-Sep-2021, tatu: [databind#1850] Special case where we get "true" integer | ||
| // enumeration and should avoid use of {@code Enum.index()} | ||
| if (_isFromIntValue) { | ||
| // [databind#3580] Enum (de)serialization in conjunction with JsonFormat.Shape.NUMBER_INT | ||
| if (_isShapeNumberInt) { | ||
| return _fromInteger(p, ctxt, p.getIntValue()); | ||
| } | ||
| // ... whether to rely on "getText()" returning String, or get number, convert? | ||
| // For now assume all format backends can produce String: | ||
| return _fromString(p, ctxt, p.getString()); | ||
|
|
@@ -238,7 +269,7 @@ private CompactStringObjectMap _resolveCurrentLookup(DeserializationContext ctxt | |
| } | ||
|
|
||
| protected Object _fromInteger(JsonParser p, DeserializationContext ctxt, | ||
| int index) | ||
| int intValue) | ||
| throws JacksonException | ||
| { | ||
| final CoercionAction act = ctxt.findCoercionAction(logicalType(), handledType(), | ||
|
|
@@ -247,13 +278,13 @@ protected Object _fromInteger(JsonParser p, DeserializationContext ctxt, | |
| // First, check legacy setting for slightly different message | ||
| if (act == CoercionAction.Fail) { | ||
| if (ctxt.isEnabled(EnumFeature.FAIL_ON_NUMBERS_FOR_ENUMS)) { | ||
| return ctxt.handleWeirdNumberValue(_enumClass(), index, | ||
| return ctxt.handleWeirdNumberValue(_enumClass(), intValue, | ||
| "not allowed to deserialize Enum value out of number: disable DeserializationConfig.EnumFeature.FAIL_ON_NUMBERS_FOR_ENUMS to allow" | ||
| ); | ||
| } | ||
| // otherwise this will force failure with new setting | ||
| _checkCoercionFail(ctxt, act, handledType(), index, | ||
| "Integer value ("+index+")"); | ||
| _checkCoercionFail(ctxt, act, handledType(), intValue, | ||
| "Integer value ("+intValue+")"); | ||
| } | ||
| switch (act) { | ||
| case AsNull: | ||
|
|
@@ -263,14 +294,26 @@ protected Object _fromInteger(JsonParser p, DeserializationContext ctxt, | |
| case TryConvert: | ||
| default: | ||
| } | ||
| if (index >= 0 && index < _enumsByIndex.length) { | ||
| return _enumsByIndex[index]; | ||
|
|
||
| // [databind#3580] Enum (de)serialization in conjunction with JsonFormat.Shape.NUMBER_INT | ||
| if (_isShapeNumberInt) { | ||
| Object numberShape = _lookupByShapeNumberInt.find(String.valueOf(intValue)); | ||
| if (numberShape != null) { | ||
| return numberShape; | ||
| } else { | ||
| return ctxt.handleWeirdNumberValue(_enumClass(), intValue, | ||
| "Number Int value is not one of expected values", | ||
| _lookupByShapeNumberInt.toString()); | ||
| } | ||
| } | ||
| if (intValue >= 0 && intValue < _enumsByIndex.length) { | ||
| return _enumsByIndex[intValue]; | ||
| } | ||
| if (useDefaultValueForUnknownEnum(ctxt)) { | ||
| return _enumDefaultValue; | ||
| } | ||
| if (!useNullForUnknownEnum(ctxt)) { | ||
| return ctxt.handleWeirdNumberValue(_enumClass(), index, | ||
| return ctxt.handleWeirdNumberValue(_enumClass(), intValue, | ||
| "index value outside legal index range [0..%s]", | ||
| _enumsByIndex.length-1); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/test/java/tools/jackson/databind/format/EnumNumberFormatShape3580Test.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package tools.jackson.databind.format; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonFormat; | ||
| import com.fasterxml.jackson.annotation.JsonValue; | ||
| import tools.jackson.databind.ObjectMapper; | ||
| import tools.jackson.databind.exc.InvalidFormatException; | ||
| import tools.jackson.databind.json.JsonMapper; | ||
| import tools.jackson.databind.testutil.DatabindTestUtil; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| // [databind#3580] Enum (de)serialization in conjunction with JsonFormat.Shape.NUMBER_INT | ||
| public class EnumNumberFormatShape3580Test | ||
| extends DatabindTestUtil | ||
| { | ||
| public static class Pojo3580 { | ||
| public PojoState3580 state; | ||
| public Pojo3580() {} | ||
| public Pojo3580(PojoState3580 state) {this.state = state;} | ||
| } | ||
|
|
||
| @JsonFormat(shape = JsonFormat.Shape.NUMBER_INT) | ||
| public enum PojoState3580 { | ||
| OFF(17), | ||
| ON(31), | ||
| UNKNOWN(99); | ||
|
|
||
| private int value; | ||
|
|
||
| PojoState3580(int value) { this.value = value; } | ||
|
|
||
| @JsonValue | ||
| public int value() {return this.value;} | ||
| } | ||
|
|
||
| @Test | ||
| public void testEnumNumberFormatShape3580() | ||
| throws Exception | ||
| { | ||
| ObjectMapper mapper = JsonMapper.builder().build(); | ||
|
|
||
| // Serialize | ||
| assertEquals("{\"state\":17}", mapper.writeValueAsString(new Pojo3580(PojoState3580.OFF))); // | ||
| assertEquals("{\"state\":31}", mapper.writeValueAsString(new Pojo3580(PojoState3580.ON))); // | ||
| assertEquals("{\"state\":99}", mapper.writeValueAsString(new Pojo3580(PojoState3580.UNKNOWN))); // | ||
|
|
||
| // Pass Deserialize | ||
| assertEquals(PojoState3580.OFF, mapper.readValue("{\"state\":17}", Pojo3580.class).state); // Pojo[state=OFF] | ||
| assertEquals(PojoState3580.ON, mapper.readValue("{\"state\":31}", Pojo3580.class).state); // Pojo[state=OFF] | ||
| assertEquals(PojoState3580.UNKNOWN, mapper.readValue("{\"state\":99}", Pojo3580.class).state); // Pojo[state=OFF] | ||
|
|
||
| // Fail : Try to use ordinal number | ||
| assertThrows(InvalidFormatException.class, () -> mapper.readValue("{\"state\":0}", Pojo3580.class)); | ||
| } | ||
| } |
53 changes: 53 additions & 0 deletions
53
src/test/java/tools/jackson/databind/records/EnumNumberFormatShapeRecord3580Test.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package tools.jackson.databind.records; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonFormat; | ||
| import com.fasterxml.jackson.annotation.JsonValue; | ||
| import tools.jackson.databind.ObjectMapper; | ||
| import tools.jackson.databind.exc.InvalidFormatException; | ||
| import tools.jackson.databind.json.JsonMapper; | ||
| import tools.jackson.databind.testutil.DatabindTestUtil; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| // [databind#3580] Enum (de)serialization in conjunction with JsonFormat.Shape.NUMBER_INT | ||
| public class EnumNumberFormatShapeRecord3580Test | ||
| extends DatabindTestUtil | ||
| { | ||
| public record Record3580(@JsonFormat(shape = JsonFormat.Shape.NUMBER) RecordState3580 state) {} | ||
|
|
||
| public enum RecordState3580 { | ||
| OFF(17), | ||
| ON(31), | ||
| UNKNOWN(99); | ||
|
|
||
| private int value; | ||
|
|
||
| RecordState3580(int value) { this.value = value; } | ||
|
|
||
| @JsonValue | ||
| public int value() {return this.value;} | ||
| } | ||
|
|
||
| @Test | ||
| public void testEnumNumberFormatShapeRecord3580() | ||
| throws Exception | ||
| { | ||
| ObjectMapper mapper = JsonMapper.builder().build(); | ||
|
|
||
| // Serialize | ||
| assertEquals("{\"state\":17}", mapper.writeValueAsString(new Record3580(RecordState3580.OFF))); // | ||
| assertEquals("{\"state\":31}", mapper.writeValueAsString(new Record3580(RecordState3580.ON))); // | ||
| assertEquals("{\"state\":99}", mapper.writeValueAsString(new Record3580(RecordState3580.UNKNOWN))); // | ||
|
|
||
| // Pass Deserialize | ||
| assertEquals(RecordState3580.OFF, mapper.readValue("{\"state\":17}", Record3580.class).state); // Pojo[state=OFF] | ||
| assertEquals(RecordState3580.ON, mapper.readValue("{\"state\":31}", Record3580.class).state); // Pojo[state=OFF] | ||
| assertEquals(RecordState3580.UNKNOWN, mapper.readValue("{\"state\":99}", Record3580.class).state); // Pojo[state=OFF] | ||
|
|
||
| // Fail : Try to use ordinal number | ||
| assertThrows(InvalidFormatException.class, () -> mapper.readValue("{\"state\":0}", Record3580.class)); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.