-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Handle Enum
introspection of values and aliases via AnnotatedClass
instead of Class<?>
#3832
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
Merged
cowtowncoder
merged 55 commits into
FasterXML:2.16
from
JooHyukKim:2787-allow-mixins-for-enums
Jun 16, 2023
Merged
Changes from 2 commits
Commits
Show all changes
55 commits
Select commit
Hold shift + click to select a range
0ff49d2
Add test cases
JooHyukKim d9b70a9
Allow construction of EnumResolver for mixin class
JooHyukKim b11db13
Remove Pojo example tests
JooHyukKim fbcc1c3
improve JavaDoc
JooHyukKim 3ebe045
Clean up tests
JooHyukKim 4cdef6c
Fix failing test
JooHyukKim bcd9025
Add test for mix-in of Wrapper field
JooHyukKim ed21cf3
Fix documentation
JooHyukKim e33619e
Implement JacksonAnnotationIntrospector
JooHyukKim 39ff50d
Merge branch '2787-allow-mixins-for-enums' of https://github.com/JooH…
JooHyukKim 51f923b
Merge branch '2787-allow-mixins-for-enums' of https://github.com/JooH…
JooHyukKim 1f7bbc1
Remove mixin from EnumResolver
JooHyukKim 9478871
Update BasicDeserializerFactory.java
JooHyukKim 2a80825
Add MapperConfig as first parameter
JooHyukKim b9a0677
Filter self referencing properties with JsonFormat.Shape is `OBJECT`
JooHyukKim 05d77bb
Add more description
JooHyukKim ac04e46
Merge branch '2.15' into 2787-allow-mixins-for-enums
JooHyukKim caac7f5
Remove unused import
JooHyukKim b8447f7
Merge branch '2787-allow-mixins-for-enums' of https://github.com/JooH…
JooHyukKim 89d80de
Update EnumResolver.java
JooHyukKim 4440da0
Tidy up comments and control flow
JooHyukKim e132e9d
Merge remote-tracking branch 'upstream/2.15' into 2787-allow-mixins-f…
JooHyukKim 244a871
Simplify method signatures (apply reviews)
JooHyukKim baa9c8c
Merge branch '2.15' into 2787-allow-mixins-for-enums
JooHyukKim 5143724
Move removing self-referencing enum fields
JooHyukKim ad73809
Merge remote-tracking branch 'upstream/2.15' into 2787-allow-mixins-f…
JooHyukKim a20d61d
Clean up EnumResolver
JooHyukKim d3a57ee
Clean up JacksonAnnotationIntrospector
JooHyukKim 872dd10
Update EnumResolver.java
JooHyukKim 8ae8ff4
Implement JacksonAnnotationIntrospector
JooHyukKim e89a778
Merge branch '2787-allow-mixins-for-enums' of https://github.com/JooH…
JooHyukKim abbee55
Allow construction of EnumResolver for mixin class
JooHyukKim d4d56e2
improve JavaDoc
JooHyukKim c07dd2f
Fix documentation
JooHyukKim 47788f0
Remove mixin from EnumResolver
JooHyukKim 99cf1f7
Add MapperConfig as first parameter
JooHyukKim 0669e3c
Filter self referencing properties with JsonFormat.Shape is `OBJECT`
JooHyukKim 1bd982a
Add more description
JooHyukKim 833f6c4
Remove unused import
JooHyukKim dca4506
Update EnumResolver.java
JooHyukKim e3f7b1b
Tidy up comments and control flow
JooHyukKim ac2af2b
Simplify method signatures (apply reviews)
JooHyukKim aa589e5
Move removing self-referencing enum fields
JooHyukKim 50570b9
Clean up JacksonAnnotationIntrospector
JooHyukKim f91657c
Clean up changes
JooHyukKim c78a0e4
Merge branch '2787-allow-mixins-for-enums' of https://github.com/JooH…
JooHyukKim 5d98b69
Update EnumResolver.java
JooHyukKim 5a85d0b
Merge remote-tracking branch 'upstream/2.16' into 2787-allow-mixins-f…
JooHyukKim 3b89923
Modify JavaDoc
JooHyukKim db48e04
Squashed commit of the following:
JooHyukKim 6bbf646
Revert "Squashed commit of the following:"
JooHyukKim f68e42b
Merge branch 'FasterXML:2.16' into 2787-allow-mixins-for-enums
JooHyukKim 24f3f45
Merge remote-tracking branch 'upstream/2.16' into 2787-allow-mixins-f…
JooHyukKim 1f5baf6
Apply review, improve `JacksonAnnotationIntropector` implementation l…
JooHyukKim 426fef5
Improve JavaDoc wrt #2787
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
243 changes: 243 additions & 0 deletions
243
src/test/java/com/fasterxml/jackson/databind/deser/enums/EnumDeserMixin2787Test.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,243 @@ | ||
package com.fasterxml.jackson.databind.deser.enums; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAlias; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.BaseMapTest; | ||
import com.fasterxml.jackson.databind.MapperFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
import com.fasterxml.jackson.databind.exc.InvalidFormatException; | ||
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; | ||
|
||
public class EnumDeserMixin2787Test extends BaseMapTest { | ||
|
||
static enum Enum2787 { | ||
APPLE, | ||
@JsonAlias({"tax10-alias-A", "tax10-alias-B"}) | ||
@JsonProperty("orig-tax10") | ||
tax10, | ||
@JsonAlias({"overridden-alias"}) | ||
@JsonProperty("overrideen-property") | ||
tax20, | ||
taxOrig | ||
} | ||
|
||
static enum EnumMixin2787 { | ||
APPLE, | ||
@JsonProperty("mixIn-tax10") | ||
tax10, | ||
@JsonAlias({"tax20-aliasA", "tax20-aliasB"}) | ||
@JsonProperty("PytPyt") | ||
tax20, | ||
tax30; | ||
|
||
@Override | ||
public String toString() { | ||
return "-----------------------------"; | ||
} | ||
} | ||
|
||
static class Bean { | ||
@JsonProperty("orig-value") | ||
public String value; | ||
} | ||
|
||
@JsonNaming(PropertyNamingStrategies.UpperCamelCaseStrategy.class) | ||
static class MixInBean { | ||
@JsonProperty("mixin-value") | ||
public String value; | ||
} | ||
|
||
static class BeanOverloaded { | ||
@JsonAlias({"value-alias"}) | ||
@JsonProperty("value-prop") | ||
public String value; | ||
} | ||
|
||
static class BeanB { | ||
public String value; | ||
@JsonProperty("num-prop") | ||
public int num; | ||
} | ||
|
||
@JsonNaming(PropertyNamingStrategies.UpperCamelCaseStrategy.class) | ||
static class BeanMixIn { | ||
@JsonProperty("mixin-value") | ||
public String value; | ||
public int num; | ||
} | ||
|
||
static class BeanC { | ||
@JsonAlias({"value-alias-a", "value-alias-b"}) | ||
@JsonProperty("value-orig") | ||
public String value; | ||
} | ||
|
||
static class BeanMixInC { | ||
@JsonAlias({"mixin-value"}) | ||
public String value; | ||
} | ||
|
||
public void testPojoMixInR() throws JsonProcessingException { | ||
ObjectMapper mxMapper = MAPPER.addMixIn(BeanC.class, BeanMixInC.class); | ||
|
||
String jsonStr = a2q("{'mixin-value':'original'}"); | ||
BeanC obj = mxMapper.readValue(jsonStr, BeanC.class); | ||
assertEquals("original", obj.value); | ||
|
||
// Property is not overriden, so original used | ||
jsonStr = a2q("{'value-orig':'original'}"); | ||
obj = mxMapper.readValue(jsonStr, BeanC.class); | ||
assertEquals("original", obj.value); | ||
|
||
// fail for Bean property name | ||
try { | ||
mxMapper.readValue(a2q("{'value-alias-a':'original'}"), BeanC.class); | ||
fail("should not reach"); | ||
} catch (UnrecognizedPropertyException e) { | ||
verifyException(e, "BeanC"); | ||
} | ||
} | ||
|
||
public void testPojoMixInOverloaded() throws JsonProcessingException { | ||
// alias | ||
String jsonStr = a2q("{'value-alias':'original'}"); | ||
BeanOverloaded obj = MAPPER.readValue(jsonStr, BeanOverloaded.class); | ||
assertEquals("original", obj.value); | ||
|
||
// prop | ||
jsonStr = a2q("{'value-prop':'original'}"); | ||
obj = MAPPER.readValue(jsonStr, BeanOverloaded.class); | ||
assertEquals("original", obj.value); | ||
} | ||
|
||
public void testPojoMixIn() throws JsonProcessingException { | ||
ObjectMapper mxMapper = MAPPER.addMixIn(Bean.class, MixInBean.class); | ||
|
||
// success ONLY for mixin | ||
String jsonStr = a2q("{'mixin-value':'original'}"); | ||
Bean obj = mxMapper.readValue(jsonStr, Bean.class); | ||
assertEquals("original", obj.value); | ||
|
||
// fail for Bean property name | ||
jsonStr = a2q("{'value':'original'}"); | ||
try { | ||
mxMapper.readValue(jsonStr, Bean.class); | ||
fail("should not reach"); | ||
} catch (UnrecognizedPropertyException e) { | ||
verifyException(e, ""); | ||
} | ||
|
||
// fail for Bean property because overridden | ||
jsonStr = a2q("{'orig-value':'original'}"); | ||
try { | ||
mxMapper.readValue(jsonStr, Bean.class); | ||
fail("should not reach"); | ||
} catch (UnrecognizedPropertyException e) { | ||
verifyException(e, ""); | ||
} | ||
} | ||
|
||
static enum EnumSecond { | ||
@JsonAlias({"second-alias"}) | ||
SECOND | ||
} | ||
|
||
static enum EnumSecondMixIn { | ||
@JsonAlias({"second-alias"}) | ||
@JsonProperty("second-prop") | ||
SECOND | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Test methods | ||
/********************************************************************** | ||
*/ | ||
|
||
protected final ObjectMapper MAPPER = jsonMapperBuilder().build(); | ||
|
||
public void testEnumDeserSuccess() throws Exception { | ||
ObjectMapper mapper = MAPPER.addMixIn(Enum2787.class, EnumMixin2787.class); | ||
|
||
Enum2787 result = mapper.readValue(q("mixIn-tax10"), Enum2787.class); | ||
|
||
assertEquals(Enum2787.tax10, result); | ||
} | ||
|
||
public void testEnumDeserSuccessCaseInsensitive() throws Exception { | ||
ObjectMapper mapper = MAPPER.addMixIn(Enum2787.class, EnumMixin2787.class) | ||
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS); | ||
|
||
Enum2787 result = mapper.readValue(q("MiXiN-tAx10"), Enum2787.class); | ||
assertEquals(Enum2787.tax10, result); | ||
} | ||
|
||
public void testEnumMixInOverloaded() throws JsonProcessingException { | ||
ObjectMapper mapper = MAPPER.addMixIn(EnumSecond.class, EnumSecondMixIn.class); | ||
|
||
EnumSecond resultA = mapper.readValue(q("second-alias"), EnumSecond.class); | ||
assertEquals(EnumSecond.SECOND, resultA); | ||
|
||
EnumSecond resultB = mapper.readValue(q("second-prop"), EnumSecond.class); | ||
assertEquals(EnumSecond.SECOND, resultB); | ||
} | ||
|
||
public void testEnumDeserSuccessMissingFromMixIn() throws Exception { | ||
ObjectMapper mapper = MAPPER.addMixIn(Enum2787.class, EnumMixin2787.class); | ||
|
||
Enum2787 result = mapper.readValue(q("taxOrig"), Enum2787.class); | ||
|
||
assertEquals(Enum2787.taxOrig, result); | ||
} | ||
|
||
public void testEnumDeserMixinFail() throws Exception { | ||
ObjectMapper mapper = MAPPER.addMixIn(Enum2787.class, EnumMixin2787.class); | ||
|
||
// fail for Bean property name | ||
try { | ||
mapper.readValue(q("tax10"), Enum2787.class); | ||
fail("should not reach"); | ||
} catch (InvalidFormatException e) { | ||
verifyException(e, "tax10", "not one of the values accepted for Enum class"); | ||
} | ||
|
||
// fail for Bean's JsonProperty because overridden | ||
try { | ||
mapper.readValue(q("orig-tax10"), Enum2787.class); | ||
fail("should not reach"); | ||
} catch (InvalidFormatException e) { | ||
verifyException(e, "orig-tax10", "not one of the values accepted for Enum class"); | ||
} | ||
} | ||
|
||
public void testMixInItselfNonJsonProperty() throws Exception { | ||
ObjectMapper mapper = MAPPER.addMixIn(Enum2787.class, EnumMixin2787.class); | ||
|
||
EnumMixin2787 result = mapper.readValue(q("tax30"), EnumMixin2787.class); | ||
|
||
assertEquals(EnumMixin2787.tax30, result); | ||
} | ||
|
||
public void testMixInValueForTargetClass() throws Exception { | ||
ObjectMapper mapper = MAPPER.addMixIn(Enum2787.class, EnumMixin2787.class); | ||
|
||
try { | ||
mapper.readValue(q("tax30"), Enum2787.class); | ||
} catch (InvalidFormatException e) { | ||
verifyException(e, " String \"tax30\": not one of the values accepted for Enum class:"); | ||
} | ||
} | ||
|
||
public void testMixinOnEnumValuesThrowWhenUnknown() throws Exception { | ||
ObjectMapper mapper = MAPPER.addMixIn(Enum2787.class, EnumMixin2787.class); | ||
|
||
try { | ||
mapper.readValue(q("should-not-exist"), Enum2787.class); | ||
} catch (InvalidFormatException e) { | ||
verifyException(e, "should-not-exist","not one of the values accepted for Enum class"); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.