-
Notifications
You must be signed in to change notification settings - Fork 30
bugfix/default post content type and primitive types #167
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 3 commits into
feature/v2
from
bugfix/default-post-content-type-and-primitive-types
Mar 11, 2021
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
60 changes: 60 additions & 0 deletions
60
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,60 @@ | ||
| 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; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** Deserializer for native EDM types from the service. Used for actions and functions that return native types for example. */ | ||
| public class EdmNativeTypeSerializer { | ||
| /** | ||
| * Deserializes native EDM types from the service. Used for actions and functions that return native types for example. | ||
| * @param <T> Expected return type. | ||
| * @param json json to deserialize. | ||
| * @param type class of the expected return type. | ||
| * @param logger logger to use. | ||
| * @return the deserialized type or null. | ||
| */ | ||
| @Nullable | ||
| public static <T> T deserialize(@Nonnull final JsonElement json, @Nonnull final Class<T> type, @Nonnull final ILogger logger) { | ||
| 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"); | ||
MIchaelMainer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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) { | ||
MIchaelMainer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.UUID; | ||
|
|
||
| import com.microsoft.graph.logger.DefaultLogger; | ||
|
|
||
| import org.junit.jupiter.api.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); | ||
| } | ||
| } |
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: indent
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.
those damn tabs keep creeping on me. Thanks for noticing.
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.
GitHub makes it easy to show these because of weird tab width choice, whether it is a good or a bad thing.
I am really curious to know as to why VSCode is not applying .editorconfig on save for you.
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.
it'd be interesting to see if using VS code on this repo (just check-out this branch to the commit before I fixed that mixed indent style), hitting ctrl + S on the file, it fixes it automatically for you. I had to do an explicit ctrl + h.
Here is the extension I'm using https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig
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.
let me give it a try.
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.
It turns out space conversion is not available
on save: https://github.com/editorconfig/editorconfig-vscode#supported-properties.If all your projects are using spaces, you may want to configure
editor.insertSpacesin vscode settings.editor.renderWhitespacealso helps in visualizing space types, so that you can tell when you need to hit that extra ctrl+h.There is also this extension, but it ignores the space configuration in .editorconfig. It always applies 4 spaces: https://marketplace.visualstudio.com/items?itemName=redlin.remove-tabs-on-save
Visual Studio applies the .editorconfig spacing rules on save for C# files, so my expectation was similar with VSCode.
IntellijIDEA doesn't apply tab->space on save though it inserts new line at the end. But if you format the code, it does the conversion and respects the .editorconfig rules (tested with 8 spaces).
Maybe a little friction on Java development in VSCode to "share" in that BarcelonaJUG conference ;)
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.
An alternative to Ctrl+H for tabs to spaces is: Command Palette (F1 or Ctrl+Shift+P) -> Convert Indentation to Spaces, which respects
.editorconfig.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.
Thanks for looking into it! This is probably another case of Vincent getting confused by the tools. I do have auto final line insertion on save in TypeScript and yaml but this is probably because of other extensions. I do also have white space trim on save for all languages I believe?
I'll try to make the ctrl shift p alternative a habit and maybe if I get tired of doing that I'll go pr the editor config extension.