-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Issue/2563 #2571
Open
msevcenko
wants to merge
11
commits into
google:main
Choose a base branch
from
msevcenko:issue/2563
base: main
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.
Open
Issue/2563 #2571
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1b9c565
Approximate raw type with bound of type variable
msevcenko e0628ef
spotless update
msevcenko 266043a
Unit test for type variable inference test.
msevcenko 7ba5de1
Unit test method updated
msevcenko 3bde53e
HandleRawEnumTest - test serialization of raw enum type
msevcenko afd1e74
ObjectTypeAdapter now handles raw enum type
msevcenko 0f1f141
spotless update
msevcenko 13d2e49
Overridable binding of raw Enum to ObjectTypeAdapter
msevcenko 9b69d1b
fixed warnings
msevcenko 3cad07c
remove extra comment
msevcenko b9f96f5
spotless
msevcenko 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 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 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 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 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 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,96 @@ | ||
package com.google.gson.functional; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.junit.Assert.fail; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.reflect.TypeToken; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Test processing raw enum types. | ||
* | ||
* @author sevcenko | ||
*/ | ||
public class HandleRawEnumTest { | ||
private Gson gson; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
gson = new Gson(); | ||
} | ||
|
||
public enum SomeEnum { | ||
ONE | ||
} | ||
|
||
public static class ClassWithRawEnum { | ||
private final Enum<?> anyEnum; | ||
|
||
public ClassWithRawEnum(Enum<?> anyEnum) { | ||
this.anyEnum = anyEnum; | ||
} | ||
|
||
public Enum<?> getAnyEnum() { | ||
return anyEnum; | ||
} | ||
} | ||
|
||
public static class ClassWithTypedEnum<T extends Enum<T>> { | ||
private final T someEnum; | ||
|
||
public ClassWithTypedEnum(T someEnum) { | ||
this.someEnum = someEnum; | ||
} | ||
|
||
public T getSomeEnum() { | ||
return someEnum; | ||
} | ||
} | ||
|
||
public static class GroupClass { | ||
|
||
private final ClassWithTypedEnum<SomeEnum> field; | ||
|
||
public GroupClass(ClassWithTypedEnum<SomeEnum> field) { | ||
this.field = field; | ||
} | ||
|
||
public ClassWithTypedEnum<SomeEnum> getField() { | ||
return field; | ||
} | ||
} | ||
|
||
@Test | ||
public void handleRawEnumClass() { | ||
// serializing raw enum is fine, but note that Adapters.ENUM_FACTORY cannot handle raw enums | ||
// even for serialization! before #2563, this just failed because raw enum falled through | ||
// ReflectiveTypeAdapterFactory, which fails to even search enum for fields | ||
assertThat(gson.toJson(new ClassWithRawEnum(SomeEnum.ONE))).isEqualTo("{\"anyEnum\":\"ONE\"}"); | ||
|
||
// we can deserialize if the enum type is known | ||
assertThat( | ||
gson.fromJson( | ||
"{\"someEnum\":\"ONE\"}", new TypeToken<ClassWithTypedEnum<SomeEnum>>() {}) | ||
.getSomeEnum()) | ||
.isEqualTo(SomeEnum.ONE); | ||
|
||
assertThat(gson.toJson(new GroupClass(new ClassWithTypedEnum<>(SomeEnum.ONE)))) | ||
.isEqualTo("{\"field\":{\"someEnum\":\"ONE\"}}"); | ||
|
||
assertThat( | ||
gson.fromJson("{\"field\":{\"someEnum\":\"ONE\"}}", GroupClass.class) | ||
.getField() | ||
.getSomeEnum()) | ||
.isEqualTo(SomeEnum.ONE); | ||
; | ||
try { | ||
// but raw type cannot be deserialized | ||
gson.fromJson("{\"anyEnum\":\"ONE\"}", new TypeToken<ClassWithRawEnum>() {}); | ||
fail("Expected exception"); | ||
} catch (IllegalArgumentException e) { | ||
assertThat(e).hasMessageThat().contains("Can not set final java.lang.Enum field"); | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
gson/src/test/java/com/google/gson/functional/InferenceFromTypeVariableTest.java
This file contains 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,77 @@ | ||
package com.google.gson.functional; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.TypeAdapterFactory; | ||
import com.google.gson.reflect.TypeToken; | ||
import java.lang.reflect.Type; | ||
import java.lang.reflect.TypeVariable; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Test deserialization of generic wrapper with type bound. | ||
* | ||
* @author sevcenko | ||
*/ | ||
public class InferenceFromTypeVariableTest { | ||
private Gson gson; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
gson = new GsonBuilder().registerTypeAdapterFactory(new ResolveGenericBoundFactory()).create(); | ||
} | ||
|
||
public static class Foo { | ||
private final String text; | ||
|
||
public Foo(String text) { | ||
this.text = text; | ||
} | ||
|
||
public String getText() { | ||
return text; | ||
} | ||
} | ||
|
||
public static class BarDynamic<T extends Foo> { | ||
private final T foo; | ||
|
||
public BarDynamic(T foo) { | ||
this.foo = foo; | ||
} | ||
|
||
public T getFoo() { | ||
return foo; | ||
} | ||
} | ||
|
||
static class ResolveGenericBoundFactory implements TypeAdapterFactory { | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { | ||
if (type.getType() instanceof TypeVariable<?>) { | ||
TypeVariable<?> tv = (TypeVariable<?>) type.getType(); | ||
Type[] bounds = tv.getBounds(); | ||
if (bounds.length == 1 && bounds[0] != Object.class) { | ||
Type bound = bounds[0]; | ||
return (TypeAdapter<T>) gson.getAdapter(TypeToken.get(bound)); | ||
} | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
@Test | ||
public void testSubClassSerialization() { | ||
BarDynamic<Foo> bar = new BarDynamic<>(new Foo("foo!")); | ||
assertThat(gson.toJson(bar)).isEqualTo("{\"foo\":{\"text\":\"foo!\"}}"); | ||
// without #2563 fix, this would deserialize foo as Object and fails to assign it to foo field | ||
BarDynamic<?> deserialized = gson.fromJson(gson.toJson(bar), BarDynamic.class); | ||
assertThat(deserialized.getFoo().getText()).isEqualTo("foo!"); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this might be functionally equivalent to the previous behavior, I think if we are going to add an adapter for
java.lang.Enum
anyway, then we should implement it properly and have it behave explicitly as desired (instead of implicitly relying onObjectTypeAdapter
's behavior).Something like this should work: