-
Notifications
You must be signed in to change notification settings - Fork 145
bugfix/default content type and boolean deserialization #693
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
baywet
merged 5 commits into
dev
from
bugfix/default-content-type-and-boolean-deserialization
Mar 9, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f3ca542
- adds a serializer to handle actions & functions with primitive retu…
baywet b9c9126
- fixes a bug where null content on post request would have a wrong a…
baywet 1d37003
- updates types summary
1c35771
- minor defensive programming improvement
baywet 38b99aa
Merge branch 'bugfix/default-content-type-and-boolean-deserialization…
baywet 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
47 changes: 47 additions & 0 deletions
47
src/main/java/com/microsoft/graph/serializer/EdmNativeTypeSerializer.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,47 @@ | ||
| package com.microsoft.graph.serializer; | ||
|
|
||
| import java.lang.reflect.Type; | ||
| import java.math.BigDecimal; | ||
| import java.util.UUID; | ||
|
|
||
| import com.google.gson.JsonElement; | ||
| import com.google.gson.JsonParseException; | ||
| import com.microsoft.graph.logger.ILogger; | ||
|
|
||
| public class EdmNativeTypeSerializer { | ||
| public static <T> T deserialize(final JsonElement json, final Class<T> type, final ILogger logger) throws JsonParseException { | ||
| if (json == null || type == null) { | ||
| return null; | ||
| } else if(json.isJsonPrimitive()) { | ||
| return getPrimitiveValue(json, type); | ||
| } else if(json.isJsonObject()) { | ||
| final JsonElement element = json.getAsJsonObject().get("@odata.null"); | ||
| if(element != null && element.isJsonPrimitive()) { | ||
| return getPrimitiveValue(element, type); | ||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
| @SuppressWarnings("unchecked") | ||
| private static <T> T getPrimitiveValue(final JsonElement json, final Class<T> type) { | ||
| if(type == Boolean.class) { | ||
| return (T) Boolean.valueOf(json.getAsBoolean()); | ||
| } else if(type == String.class) { | ||
| return (T)json.getAsString(); | ||
| } else if(type == Integer.class) { | ||
| return (T) Integer.valueOf(json.getAsInt()); | ||
| } else if(type == UUID.class) { | ||
| return (T) UUID.fromString(json.getAsString()); | ||
| } else if(type == Long.class) { | ||
| return (T) Long.valueOf(json.getAsLong()); | ||
| } else if (type == Float.class) { | ||
| return (T) Float.valueOf(json.getAsFloat()); | ||
| } else if (type == BigDecimal.class) { | ||
| return (T) json.getAsBigDecimal(); | ||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
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
76 changes: 76 additions & 0 deletions
76
src/test/java/com/microsoft/graph/serializer/EdmNativeTypeSerializerTests.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,76 @@ | ||
| package com.microsoft.graph.serializer; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.UUID; | ||
|
|
||
| import com.microsoft.graph.logger.DefaultLogger; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| public class EdmNativeTypeSerializerTests { | ||
| @Test | ||
| public void testBoolean() throws Exception { | ||
| final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger()); | ||
|
|
||
| final String source = "{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#Edm.Null\",\"@odata.null\":true}"; | ||
| final Boolean result = serializer.deserializeObject(source, Boolean.class); | ||
|
|
||
| assertEquals(Boolean.valueOf(true), result); | ||
| } | ||
| @Test | ||
| public void testInteger() throws Exception { | ||
| final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger()); | ||
|
|
||
| final String source = "{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#Edm.Null\",\"@odata.null\":12}"; | ||
| final Integer result = serializer.deserializeObject(source, Integer.class); | ||
|
|
||
| assertEquals(Integer.valueOf(12), result); | ||
| } | ||
| @Test | ||
| public void testString() throws Exception { | ||
| final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger()); | ||
|
|
||
| final String source = "{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#Edm.Null\",\"@odata.null\":\"toto\"}"; | ||
| final String result = serializer.deserializeObject(source, String.class); | ||
|
|
||
| assertEquals("toto", result); | ||
| } | ||
| @Test | ||
| public void testFloat() throws Exception { | ||
| final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger()); | ||
|
|
||
| final String source = "{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#Edm.Null\",\"@odata.null\":12.5}"; | ||
| final Float result = serializer.deserializeObject(source, Float.class); | ||
|
|
||
| assertEquals(Float.valueOf("12.5"), result); | ||
| } | ||
| @Test | ||
| public void testLong() throws Exception { | ||
| final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger()); | ||
|
|
||
| final String source = "{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#Edm.Null\",\"@odata.null\":12}"; | ||
| final Long result = serializer.deserializeObject(source, Long.class); | ||
|
|
||
| assertEquals(Long.valueOf(12), result); | ||
| } | ||
| @Test | ||
| public void testBigDecimal() throws Exception { | ||
| final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger()); | ||
|
|
||
| final String source = "{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#Edm.Null\",\"@odata.null\":12}"; | ||
| final BigDecimal result = serializer.deserializeObject(source, BigDecimal.class); | ||
|
|
||
| assertEquals(BigDecimal.valueOf(12), result); | ||
| } | ||
| @Test | ||
| public void testUUID() throws Exception { | ||
| final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger()); | ||
|
|
||
| final String source = "{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#Edm.Null\",\"@odata.null\":\"0E6558C3-9640-4385-860A-2A894AC5C246\"}"; | ||
| final UUID result = serializer.deserializeObject(source, UUID.class); | ||
|
|
||
| assertEquals(UUID.fromString("0E6558C3-9640-4385-860A-2A894AC5C246"), result); | ||
| } | ||
| } |
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
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.
nit: else block can be removed