Skip to content

Commit

Permalink
Fix oneof java template
Browse files Browse the repository at this point in the history
  • Loading branch information
dvacca-onfido committed Oct 17, 2024
1 parent fdcac3b commit 6b63797
Showing 1 changed file with 88 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,21 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
{{#composedSchemas}}
{{#oneOf}}
{{^isArray}}
{{^isMap}}
{{^vendorExtensions.x-duplicated-data-type}}
final TypeAdapter<{{{dataType}}}> adapter{{{dataType}}} = gson.getDelegateAdapter(this, TypeToken.get({{{dataType}}}.class));
{{/vendorExtensions.x-duplicated-data-type}}
{{/isMap}}
{{/isArray}}
{{#isArray}}

final Type typeInstance = new TypeToken<{{{dataType}}}>(){}.getType();
final TypeAdapter<{{{dataType}}}> adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}} = (TypeAdapter<{{{dataType}}}>) gson.getDelegateAdapter(this, TypeToken.get(typeInstance));
final Type typeInstance{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}} = new TypeToken<{{{dataType}}}>(){}.getType();
final TypeAdapter<{{{dataType}}}> adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}} = (TypeAdapter<{{{dataType}}}>) gson.getDelegateAdapter(this, TypeToken.get(typeInstance{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}));
{{/isArray}}
{{#isMap}}
final Type typeInstance{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}} = new TypeToken<{{{dataType}}}>(){}.getType();
final TypeAdapter<{{{dataType}}}> adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}} = (TypeAdapter<{{{dataType}}}>) gson.getDelegateAdapter(this, TypeToken.get(typeInstance{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}));
{{/isMap}}
{{/oneOf}}
{{/composedSchemas}}

Expand All @@ -76,8 +82,9 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
{{#oneOf}}
{{^vendorExtensions.x-duplicated-data-type}}
// check if the actual instance is of the type `{{{dataType}}}`
if (value.getActualInstance() instanceof {{#isArray}}List<?>{{/isArray}}{{^isArray}}{{{dataType}}}{{/isArray}}) {
if (value.getActualInstance() instanceof {{#isArray}}List<?>{{/isArray}}{{#isMap}}Map<?, ?>{{/isMap}}{{^isMap}}{{^isArray}}{{{dataType}}}{{/isArray}}{{/isMap}}) {
{{#isPrimitiveType}}
{{^isMap}}
{{! Updated code - BEGIN }}
{{#isArray}}
JsonElement element = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}.toJsonTree(({{{dataType}}})value.getActualInstance());
Expand All @@ -88,6 +95,12 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
elementAdapter.write(out, element);
{{! Updated code - END }}
return;
{{/isMap}}
{{#isMap}}
JsonObject object = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}.toJsonTree(({{{dataType}}})value.getActualInstance()).getAsJsonObject();
elementAdapter.write(out, object);
return;
{{/isMap}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}
{{#isArray}}
Expand All @@ -99,13 +112,15 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
}
{{/isArray}}
{{/isPrimitiveType}}
{{^isMap}}
{{^isArray}}
{{^isPrimitiveType}}
JsonElement element = adapter{{{dataType}}}.toJsonTree(({{{dataType}}})value.getActualInstance());
elementAdapter.write(out, element);
return;
{{/isPrimitiveType}}
{{/isArray}}
{{/isMap}}
}
{{/vendorExtensions.x-duplicated-data-type}}
{{/oneOf}}
Expand Down Expand Up @@ -157,6 +172,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
try {
// validate the JSON object to see if any exception is thrown
{{^isArray}}
{{^isMap}}
{{#isNumber}}
if (!jsonElement.getAsJsonPrimitive().isNumber()) {
throw new IllegalArgumentException(String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()));
Expand All @@ -177,6 +193,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
actualAdapter = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}};
{{/isPrimitiveType}}
{{/isNumber}}
{{/isMap}}
{{/isArray}}
{{#isArray}}
if (!jsonElement.isJsonArray()) {
Expand Down Expand Up @@ -208,6 +225,38 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
}
actualAdapter = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}};
{{/isArray}}
{{#isMap}}
if (!jsonElement.isJsonObject()) {
throw new IllegalArgumentException(String.format("Expected json element to be a object type in the JSON string but got `%s`", jsonElement.toString()));
}

{{^isFreeFormObject}}
Map<String, JsonElement> map = jsonElement.getAsJsonObject().asMap();
// validate map items
for(JsonElement element : map.values()) {
{{#items}}
{{#isNumber}}
if (!jsonElement.getAsJsonPrimitive().isNumber()) {
throw new IllegalArgumentException(String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()));
}
{{/isNumber}}
{{^isNumber}}
{{#isPrimitiveType}}
if (!element.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}()) {
throw new IllegalArgumentException(String.format("Expected array items to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString()));
}
{{/isPrimitiveType}}
{{/isNumber}}
{{^isNumber}}
{{^isPrimitiveType}}
{{{dataType}}}.validateJsonElement(element);
{{/isPrimitiveType}}
{{/isNumber}}
{{/items}}
}
{{/isFreeFormObject}}
actualAdapter = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}};
{{/isMap}}
match++;
log.log(Level.FINER, "Input data matches schema '{{{dataType}}}'");
} catch (Exception e) {
Expand Down Expand Up @@ -256,13 +305,11 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
super("oneOf", {{#isNullable}}Boolean.TRUE{{/isNullable}}{{^isNullable}}Boolean.FALSE{{/isNullable}});
}

{{#oneOf}}
public {{classname}}({{{.}}} o) {
public {{classname}}(Object o) {
super("oneOf", {{#isNullable}}Boolean.TRUE{{/isNullable}}{{^isNullable}}Boolean.FALSE{{/isNullable}});
setActualInstance(o);
}

{{/oneOf}}
static {
{{#composedSchemas}}
{{#oneOf}}
Expand Down Expand Up @@ -297,7 +344,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
{{#composedSchemas}}
{{#oneOf}}
{{^vendorExtensions.x-duplicated-data-type}}
if (instance instanceof {{#isArray}}List<?>{{/isArray}}{{^isArray}}{{{dataType}}}{{/isArray}}) {
if (instance instanceof {{#isArray}}List<?>{{/isArray}}{{#isMap}}Map<?, ?>{{/isMap}}{{^isMap}}{{^isArray}}{{{dataType}}}{{/isArray}}{{/isMap}}) {
{{#isArray}}
List<?> list = (List<?>) instance;
if (list.get(0) instanceof {{{items.dataType}}}) {
Expand Down Expand Up @@ -341,7 +388,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
*/
{{! Suppress unchecked cast warning }}
@SuppressWarnings("unchecked")
public {{{dataType}}} get{{#isArray}}{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}{{/isArray}}{{^isArray}}{{{dataType}}}{{/isArray}}() throws ClassCastException {
public {{{dataType}}} get{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}() throws ClassCastException {
return ({{{dataType}}})super.getActualInstance();
}
{{/vendorExtensions.x-duplicated-data-type}}
Expand All @@ -364,6 +411,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
// validate the json string with {{{dataType}}}
try {
{{^hasVars}}
{{^isMap}}
{{^isArray}}
{{#isNumber}}
if (!jsonElement.getAsJsonPrimitive().isNumber()) {
Expand All @@ -383,6 +431,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
{{/isPrimitiveType}}
{{/isNumber}}
{{/isArray}}
{{/isMap}}
{{#isArray}}
if (!jsonElement.isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString()));
Expand Down Expand Up @@ -411,6 +460,37 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
{{/items}}
}
{{/isArray}}
{{#isMap}}
if (!jsonElement.isJsonObject()) {
throw new IllegalArgumentException(String.format("Expected json element to be a object type in the JSON string but got `%s`", jsonElement.toString()));
}

{{^isFreeFormObject}}
Map<String, JsonElement> map = jsonElement.getAsJsonObject().asMap();
// validate map items
for(JsonElement element : map.values()) {
{{#items}}
{{#isNumber}}
if (!jsonElement.getAsJsonPrimitive().isNumber()) {
throw new IllegalArgumentException(String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()));
}
{{/isNumber}}
{{^isNumber}}
{{#isPrimitiveType}}
if (!element.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}()) {
throw new IllegalArgumentException(String.format("Expected array items to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString()));
}
{{/isPrimitiveType}}
{{/isNumber}}
{{^isNumber}}
{{^isPrimitiveType}}
{{{dataType}}}.validateJsonElement(element);
{{/isPrimitiveType}}
{{/isNumber}}
{{/items}}
}
{{/isFreeFormObject}}
{{/isMap}}
{{/hasVars}}
{{#hasVars}}
{{{.}}}.validateJsonElement(jsonElement);
Expand Down

0 comments on commit 6b63797

Please sign in to comment.