Skip to content

Commit

Permalink
[Java][Client] Fix OpenAPITools#12556 Support primitives and arrays i…
Browse files Browse the repository at this point in the history
…n oneOf (OpenAPITools#13897)

* [Java][Client] Fix OpenAPITools#12556 Support primitives and arrays in oneOf

* Regenerate petstore samples

* Regenerate petstore test samples

* Treat 'BigDecimal' as primtive datatype

* Fix integration tests
  • Loading branch information
karzang authored Jun 29, 2023
1 parent b2280e2 commit e9d9866
Show file tree
Hide file tree
Showing 282 changed files with 4,677 additions and 2,646 deletions.
1 change: 1 addition & 0 deletions docs/generators/java.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
## LANGUAGE PRIMITIVES

<ul class="column-ul">
<li>BigDecimal</li>
<li>Boolean</li>
<li>Double</li>
<li>Float</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ public CodegenProperty getNot() {
return not;
}

public void setAllOf(List<CodegenProperty> allOf) {
this.allOf = allOf;
}

public void setOneOf(List<CodegenProperty> oneOf) {
this.oneOf = oneOf;
}

public void setAnyOf(List<CodegenProperty> anyOf) {
this.anyOf = anyOf;
}

public void setNot(CodegenProperty not) {
this.not = not;
}

public String toString() {
final StringBuilder sb = new StringBuilder("CodegenComposedSchemas{");
sb.append("oneOf=").append(oneOf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

import java.io.File;
import java.util.*;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -190,6 +191,8 @@ public JavaClientCodegen() {
modelPackage = "org.openapitools.client.model";
rootJavaEEPackage = MICROPROFILE_REST_CLIENT_DEFAULT_ROOT_PACKAGE;

languageSpecificPrimitives.add("BigDecimal");

// cliOptions default redefinition need to be updated
updateOption(CodegenConstants.INVOKER_PACKAGE, this.getInvokerPackage());
updateOption(CodegenConstants.ARTIFACT_ID, this.getArtifactId());
Expand Down Expand Up @@ -1019,6 +1022,16 @@ public CodegenModel fromModel(String name, Schema model) {
return codegenModel;
}

@Override
protected boolean needToImport(String type) {
// add import for BigDecimal explicitly since it is a primitive type
if("BigDecimal".equals(type)) {
return true;
}

return super.needToImport(type) && !type.contains(".");
}

@Override
public ModelsMap postProcessModelsEnum(ModelsMap objs) {
objs = super.postProcessModelsEnum(objs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
@Override
public {{classname}} read(JsonReader in) throws IOException {
Object deserialized = null;
JsonObject jsonObject = elementAdapter.read(in).getAsJsonObject();
JsonElement jsonElement = elementAdapter.read(in);
{{#useOneOfDiscriminatorLookup}}
{{#discriminator}}
Expand All @@ -80,7 +80,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
switch (discriminatorValue) {
{{#mappedModels}}
case "{{{mappingName}}}":
deserialized = adapter{{modelName}}.fromJsonTree(jsonObject);
deserialized = adapter{{modelName}}.fromJsonTree(jsonElement);
new{{classname}}.setActualInstance(deserialized);
return new{{classname}};
{{/mappedModels}}
Expand All @@ -94,10 +94,10 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
// deserialize {{{.}}}
try {
// validate the JSON object to see if any exception is thrown
{{.}}.validateJsonObject(jsonObject);
{{.}}.validateJsonElement(jsonElement);
log.log(Level.FINER, "Input data matches schema '{{{.}}}'");
{{classname}} ret = new {{classname}}();
ret.setActualInstance(adapter{{.}}.fromJsonTree(jsonObject));
ret.setActualInstance(adapter{{.}}.fromJsonTree(jsonElement));
return ret;
} catch (Exception e) {
// deserialization failed, continue
Expand All @@ -106,7 +106,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im

{{/anyOf}}

throw new IOException(String.format("Failed deserialization for {{classname}}: no class matched. JSON: %s", jsonObject.toString()));
throw new IOException(String.format("Failed deserialization for {{classname}}: no class matched. JSON: %s", jsonElement.toString()));
}
}.nullSafe();
}
Expand Down Expand Up @@ -190,26 +190,26 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
{{/anyOf}}

/**
* Validates the JSON Object and throws an exception if issues found
* Validates the JSON Element and throws an exception if issues found
*
* @param jsonObj JSON Object
* @throws IOException if the JSON Object is invalid with respect to {{classname}}
* @param jsonElement JSON Element
* @throws IOException if the JSON Element is invalid with respect to {{classname}}
*/
public static void validateJsonObject(JsonObject jsonObj) throws IOException {
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
// validate anyOf schemas one by one
int validCount = 0;
{{#anyOf}}
// validate the json string with {{{.}}}
try {
{{{.}}}.validateJsonObject(jsonObj);
{{{.}}}.validateJsonElement(jsonElement);
return; // return earlier as at least one schema is valid with respect to the Json object
//validCount++;
} catch (Exception e) {
// continue to the next one
}
{{/anyOf}}
if (validCount == 0) {
throw new IOException(String.format("The JSON string is invalid for {{classname}} with anyOf schemas: {{#anyOf}}{{{.}}}{{^-last}}, {{/-last}}{{/anyOf}}. JSON: %s", jsonObj.toString()));
throw new IOException(String.format("The JSON string is invalid for {{classname}} with anyOf schemas: {{#anyOf}}{{{.}}}{{^-last}}, {{/-last}}{{/anyOf}}. JSON: %s", jsonElement.toString()));
}
}

Expand Down
Loading

0 comments on commit e9d9866

Please sign in to comment.