Skip to content

feat(java): deserialize oneof #492

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 2 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/actions/restore-artifacts/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ runs:

# JavaScript utils
- name: client-javascript-utils-client-common
if: ${{ inputs.type == 'all' || inputs.type == 'js_utils' }}
if: ${{ (inputs.javascript == 'true' && inputs.type == 'all') || inputs.type == 'js_utils' }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

uses: actions/download-artifact@v3
with:
name: client-javascript-utils-client-common
path: clients/algoliasearch-client-javascript/packages/client-common/

- name: client-javascript-utils-requester-browser-xhr
if: ${{ inputs.type == 'all' || inputs.type == 'js_utils' }}
if: ${{ (inputs.javascript == 'true' && inputs.type == 'all') || inputs.type == 'js_utils' }}
uses: actions/download-artifact@v3
with:
name: client-javascript-utils-requester-browser-xhr
path: clients/algoliasearch-client-javascript/packages/requester-browser-xhr/

- name: client-javascript-utils-requester-node-http
if: ${{ inputs.type == 'all' || inputs.type == 'js_utils' }}
if: ${{ (inputs.javascript == 'true' && inputs.type == 'all') || inputs.type == 'js_utils' }}
uses: actions/download-artifact@v3
with:
name: client-javascript-utils-requester-node-http
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ public static <T> T deserialize(String body, Type returnType) {
}
}
}

public static <T> T tryDeserialize(JsonReader reader, Type returnType) {
try {
return gson.fromJson(reader, returnType);
} catch (JsonParseException e) {
return null;
}
}
}

/** Gson TypeAdapter for Byte Array type */
Expand Down
6 changes: 6 additions & 0 deletions templates/java/oneof_interface.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ public abstract class {{classname}} implements CompoundType {
@Override
public {{classname}} read(final JsonReader jsonReader)
throws IOException {
{{#vendorExtensions.x-is-one-of-list}}
{{{type}}} {{#lambda.lowercase}}{{name}}{{/lambda.lowercase}} = JSON.tryDeserialize(jsonReader, new TypeToken<{{{type}}}>() {}.getType());
if({{#lambda.lowercase}}{{name}}{{/lambda.lowercase}} != null) {
return {{classname}}.of{{name}}({{#lambda.lowercase}}{{name}}{{/lambda.lowercase}});
}
{{/vendorExtensions.x-is-one-of-list}}
return null;
}
}
Expand Down