-
Notifications
You must be signed in to change notification settings - Fork 146
- fixes a bug where collections would not be deserialized properly #483
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
afa91d9
- fixes a bug where collections would not be deserialized properly
baywet 1b51fe2
Merge branch 'dev' into bugfix/collection-deserialization
baywet 9a904f4
- adds comments to ease reading
baywet b343dcd
- adds support for derived types in collection deserialization
baywet 05928f8
- fixes tabing
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
95 changes: 0 additions & 95 deletions
95
src/main/java/com/microsoft/graph/serializer/AttachmentCollectionPageSerializer.java
This file was deleted.
Oops, something went wrong.
148 changes: 148 additions & 0 deletions
148
src/main/java/com/microsoft/graph/serializer/CollectionPageSerializer.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,148 @@ | ||
| // ------------------------------------------------------------------------------ | ||
| // Copyright (c) 2017 Microsoft Corporation | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| // of this software and associated documentation files (the "Software"), to deal | ||
| // in the Software without restriction, including without limitation the rights | ||
| // to use, copy, modify, merge, publish, distribute, sub-license, and/or sell | ||
| // copies of the Software, and to permit persons to whom the Software is | ||
| // furnished to do so, subject to the following conditions: | ||
| // | ||
| // The above copyright notice and this permission notice shall be included in | ||
| // all copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| // THE SOFTWARE. | ||
| // ------------------------------------------------------------------------------ | ||
|
|
||
| package com.microsoft.graph.serializer; | ||
|
|
||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.lang.reflect.Type; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import com.google.gson.JsonArray; | ||
| import com.google.gson.JsonElement; | ||
| import com.google.gson.JsonObject; | ||
| import com.google.gson.JsonParseException; | ||
| import com.google.gson.JsonParser; | ||
| import com.google.common.reflect.TypeToken; | ||
| import com.microsoft.graph.http.BaseCollectionPage; | ||
| import com.microsoft.graph.http.IRequestBuilder; | ||
| import com.microsoft.graph.logger.ILogger; | ||
|
|
||
| // keep these imports: even though they are not used, it's a good way to call | ||
| // for maintainer's attention en the event code generation naming conventions | ||
| // change. If the name change, these import will build at build time rather than | ||
| // reflection building at run time. | ||
| import com.microsoft.graph.models.extensions.Attachment; | ||
| import com.microsoft.graph.requests.extensions.AttachmentCollectionPage; | ||
| import com.microsoft.graph.requests.extensions.AttachmentCollectionResponse; | ||
| import com.microsoft.graph.requests.extensions.IAttachmentCollectionRequestBuilder; | ||
|
|
||
| public class CollectionPageSerializer { | ||
|
|
||
| private static DefaultSerializer serializer; | ||
| /** length of the word "page" */ | ||
| private final static Integer pageLength = 4; | ||
| /** length of the word "collection" */ | ||
| private final static Integer collectionLength = 10; | ||
| /** length of the work "response" */ | ||
| private final static Integer responseLength = 8; | ||
| /** the extensions segment in the package name of target classes */ | ||
| private final static String extensionsPath = "extensions."; | ||
|
|
||
| /** | ||
| * Not available for instantiation | ||
| */ | ||
| private CollectionPageSerializer() { | ||
| } | ||
|
|
||
| /** | ||
| * Serializes an CollectionPage | ||
| * | ||
| * @param src the CollectionPage variable for serialization | ||
| * @param logger the logger | ||
| * @return JsonElement of CollectionPage | ||
| */ | ||
| public static <T1, T2 extends IRequestBuilder> JsonElement serialize(final BaseCollectionPage<T1, T2> src, final ILogger logger) { | ||
| if(src == null) { | ||
| return null; | ||
| } | ||
| JsonArray jsonArray = new JsonArray(); | ||
| List<T1> items = src.getCurrentPage(); | ||
| serializer = new DefaultSerializer(logger); | ||
| for(T1 item : items) { | ||
| final String json = serializer.serializeObject(item); | ||
| final JsonElement element = JsonParser.parseString(json); | ||
| if(element != null && element.isJsonObject()) { | ||
| final JsonObject jsonObject = element.getAsJsonObject(); | ||
| jsonArray.add(jsonObject); | ||
| } | ||
| } | ||
| return jsonArray; | ||
| } | ||
|
|
||
| /** | ||
| * Deserializes the JsonElement | ||
| * | ||
| * @param json the source CollectionPage's Json | ||
| * @param typeOfT The type of the CollectionPage to deserialize to | ||
| * @param logger the logger | ||
| * @throws JsonParseException the parse exception | ||
| * @return the deserialized CollectionPage | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| public static <T1, T2 extends IRequestBuilder> BaseCollectionPage<T1, T2> deserialize(final JsonElement json, Type typeOfT, final ILogger logger) throws JsonParseException { | ||
| if (json == null) { | ||
| return null; | ||
| } | ||
| serializer = new DefaultSerializer(logger); | ||
| final JsonObject[] sourceArray = serializer.deserializeObject(json.toString(), JsonObject[].class); | ||
| final ArrayList<T1> list = new ArrayList<T1>(sourceArray.length); | ||
| /** eg: com.microsoft.graph.requests.extensions.AttachmentCollectionPage */ | ||
| final String collectionPageClassCanonicalName = typeOfT.getTypeName(); | ||
| /** eg: com.microsoft.graph.models.extensions.Attachment */ | ||
| final String baseEntityClassCanonicalName = collectionPageClassCanonicalName | ||
| .substring(0, collectionPageClassCanonicalName.length() - pageLength - collectionLength) | ||
| .replace("requests", "models"); | ||
| try { | ||
| final Class<?> baseEntityClass = Class.forName(baseEntityClassCanonicalName); | ||
| for (JsonObject sourceObject : sourceArray) { | ||
| Class<?> entityClass = serializer.getDerivedClass(sourceObject, baseEntityClass); | ||
| if(entityClass == null) | ||
| entityClass = baseEntityClass; | ||
| final T1 targetObject = (T1)serializer.deserializeObject(sourceObject.toString(), entityClass); | ||
| ((IJsonBackedObject)targetObject).setRawObject(serializer, sourceObject); | ||
| list.add(targetObject); | ||
| } | ||
| /** eg: com.microsoft.graph.requests.extensions.AttachmentCollectionResponse */ | ||
| final String responseClassCanonicalName = collectionPageClassCanonicalName | ||
| .substring(0, collectionPageClassCanonicalName.length() - pageLength) + "Response"; | ||
| final Class<?> responseClass = Class.forName(responseClassCanonicalName); | ||
| final Object response = responseClass.getConstructor().newInstance(); | ||
| responseClass.getField("value").set(response, list); | ||
| final Class<?> collectionPageClass = Class.forName(collectionPageClassCanonicalName); | ||
| /** eg: com.microsoft.graph.requests.extensions.IAttachmentCollectionRequestBuilder */ | ||
| final String responseBuilderInterfaceCanonicalName = responseClassCanonicalName | ||
| .substring(0, responseClassCanonicalName.length() - responseLength) | ||
| .replace(extensionsPath, extensionsPath + "I") + "RequestBuilder"; | ||
| final Class<?> responseBuilderInterfaceClass = Class.forName(responseBuilderInterfaceCanonicalName); | ||
| return (BaseCollectionPage<T1, T2>)collectionPageClass.getConstructor(responseClass, responseBuilderInterfaceClass).newInstance(response, null); | ||
| } catch(ClassNotFoundException ex) { | ||
| logger.logError("Could not find class during deserialization", ex); | ||
| } catch(NoSuchMethodException | InstantiationException | InvocationTargetException ex) { | ||
| logger.logError("Could not instanciate type during deserialization", ex); | ||
| } catch(NoSuchFieldException | IllegalAccessException ex) { | ||
| logger.logError("Unable to set field value during deserialization", ex); | ||
| } | ||
| 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
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.