Skip to content

Commit

Permalink
feat(java): deserialize oneof (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp authored May 11, 2022
1 parent ee9c021 commit e86e870
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
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' }}
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

0 comments on commit e86e870

Please sign in to comment.