From 73c5a25e03bd7b600451d38a78e06612f228b0d2 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sun, 13 Apr 2025 21:14:56 +0000 Subject: [PATCH 1/4] Bump org.openapitools:openapi-generator from 7.4.0 to 7.12.0 Bumps [org.openapitools:openapi-generator](https://github.com/openapitools/openapi-generator) from 7.4.0 to 7.12.0. - [Release notes](https://github.com/openapitools/openapi-generator/releases) - [Changelog](https://github.com/OpenAPITools/openapi-generator/blob/master/docs/3.0.0-release-note.md) - [Commits](https://github.com/openapitools/openapi-generator/compare/v7.4.0...v7.12.0) --- updated-dependencies: - dependency-name: org.openapitools:openapi-generator dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 4f1bff4b6..f6f84b235 100644 --- a/pom.xml +++ b/pom.xml @@ -44,7 +44,7 @@ backbase https://sonarcloud.io - 7.4.0 + 7.12.0 tests/target/site/jacoco-aggregate/jacoco.xml ${aggregate.report.dir} From 46c0b642bd4b3833c619936687b4c7ed0e6253c1 Mon Sep 17 00:00:00 2001 From: jasper linschoten Date: Mon, 14 Apr 2025 14:28:02 +0200 Subject: [PATCH 2/4] Fix openapi-generator update. --- .../oss/codegen/java/BoatJavaCodeGen.java | 6 +- .../oss/codegen/java/BoatSpringCodeGen.java | 1 - .../java/BoatSpringCodegenProperty.java | 1 + .../apache-httpclient/BaseApi.mustache | 110 ++++++++++++++++++ .../libraries/resttemplate/BaseApi.mustache | 76 ++++++++++++ .../main/templates/boat-java/model.mustache | 1 + .../main/templates/boat-spring/model.mustache | 1 + 7 files changed, 194 insertions(+), 2 deletions(-) create mode 100644 boat-scaffold/src/main/templates/boat-java/libraries/apache-httpclient/BaseApi.mustache create mode 100644 boat-scaffold/src/main/templates/boat-java/libraries/resttemplate/BaseApi.mustache diff --git a/boat-scaffold/src/main/java/com/backbase/oss/codegen/java/BoatJavaCodeGen.java b/boat-scaffold/src/main/java/com/backbase/oss/codegen/java/BoatJavaCodeGen.java index b1fc08324..f7f55d1fd 100644 --- a/boat-scaffold/src/main/java/com/backbase/oss/codegen/java/BoatJavaCodeGen.java +++ b/boat-scaffold/src/main/java/com/backbase/oss/codegen/java/BoatJavaCodeGen.java @@ -157,7 +157,11 @@ private boolean isArrayTypeOfEnum(Schema s) { if (!ModelUtils.isArraySchema(target)) { return false; } - Schema items = getSchemaItems((ArraySchema) target); + Schema items = target.getItems(); + if (items == null) { + // Will default to strings schema, which is not an enum + return false; + } if (items.get$ref() != null) { items = openAPI.getComponents().getSchemas().get(ModelUtils.getSimpleRef(items.get$ref())); } diff --git a/boat-scaffold/src/main/java/com/backbase/oss/codegen/java/BoatSpringCodeGen.java b/boat-scaffold/src/main/java/com/backbase/oss/codegen/java/BoatSpringCodeGen.java index 89e23ee2e..f67fa1f54 100644 --- a/boat-scaffold/src/main/java/com/backbase/oss/codegen/java/BoatSpringCodeGen.java +++ b/boat-scaffold/src/main/java/com/backbase/oss/codegen/java/BoatSpringCodeGen.java @@ -414,7 +414,6 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert if (shouldSerializeBigDecimalAsString(property)) { property.vendorExtensions.put("x-extra-annotation", "@JsonSerialize(using = BigDecimalCustomSerializer.class)"); model.imports.add("BigDecimalCustomSerializer"); - model.imports.add("JsonSerialize"); } } diff --git a/boat-scaffold/src/main/java/com/backbase/oss/codegen/java/BoatSpringCodegenProperty.java b/boat-scaffold/src/main/java/com/backbase/oss/codegen/java/BoatSpringCodegenProperty.java index 60f0a750c..7f6239fbf 100644 --- a/boat-scaffold/src/main/java/com/backbase/oss/codegen/java/BoatSpringCodegenProperty.java +++ b/boat-scaffold/src/main/java/com/backbase/oss/codegen/java/BoatSpringCodegenProperty.java @@ -137,6 +137,7 @@ public BoatSpringCodegenProperty(CodegenProperty codegenProperty) { defaultValue = "new HashMap<>()"; isMap = true; } + this.nameInCamelCase = StringUtils.capitalize(this.nameInCamelCase); } @Override public String toString() { diff --git a/boat-scaffold/src/main/templates/boat-java/libraries/apache-httpclient/BaseApi.mustache b/boat-scaffold/src/main/templates/boat-java/libraries/apache-httpclient/BaseApi.mustache new file mode 100644 index 000000000..8b48de086 --- /dev/null +++ b/boat-scaffold/src/main/templates/boat-java/libraries/apache-httpclient/BaseApi.mustache @@ -0,0 +1,110 @@ +{{>licenseInfo}} +package {{invokerPackage}}; + +import com.fasterxml.jackson.core.type.TypeReference; + +import java.util.Collections; +import java.util.Map; + +{{>generatedAnnotation}} +public abstract class BaseApi { + + protected ApiClient apiClient; + + public BaseApi() { + this(Configuration.getDefaultApiClient()); + } + + public BaseApi(ApiClient apiClient) { + this.apiClient = apiClient; + } + + public ApiClient getApiClient() { + return apiClient; + } + + public void setApiClient(ApiClient apiClient) { + this.apiClient = apiClient; + } + + /** + * Directly invoke the API for the given URL. Useful if the API returns direct links/URLs for subsequent requests. + * @param url The URL for the request, either full URL or only the path. + * @param method The HTTP method for the request. + * @throws ApiException if fails to make API call. + */ + public void invokeAPI(String url, String method) throws ApiException { + invokeAPI(url, method, null, null, Collections.emptyMap()); + } + + /** + * Directly invoke the API for the given URL. Useful if the API returns direct links/URLs for subsequent requests. + * @param url The URL for the request, either full URL or only the path. + * @param method The HTTP method for the request. + * @param additionalHeaders Additional headers for the request. + * @throws ApiException if fails to make API call. + */ + public void invokeAPI(String url, String method, Map additionalHeaders) throws ApiException { + invokeAPI(url, method, null, null, additionalHeaders); + } + + /** + * Directly invoke the API for the given URL. Useful if the API returns direct links/URLs for subsequent requests. + * @param url The URL for the request, either full URL or only the path. + * @param method The HTTP method for the request. + * @param request The request object. + * @throws ApiException if fails to make API call. + */ + public void invokeAPI(String url, String method, Object request) throws ApiException { + invokeAPI(url, method, request, null, Collections.emptyMap()); + } + + /** + * Directly invoke the API for the given URL. Useful if the API returns direct links/URLs for subsequent requests. + * @param url The URL for the request, either full URL or only the path. + * @param method The HTTP method for the request. + * @param request The request object. + * @param additionalHeaders Additional headers for the request. + * @throws ApiException if fails to make API call. + */ + public void invokeAPI(String url, String method, Object request, Map additionalHeaders) throws ApiException { + invokeAPI(url, method, request, null, additionalHeaders); + } + + /** + * Directly invoke the API for the given URL. Useful if the API returns direct links/URLs for subsequent requests. + * @param url The URL for the request, either full URL or only the path. + * @param method The HTTP method for the request. + * @param returnType The return type. + * @return The API response in the specified type. + * @throws ApiException if fails to make API call. + */ + public T invokeAPI(String url, String method, TypeReference returnType) throws ApiException { + return invokeAPI(url, method, null, returnType, Collections.emptyMap()); + } + + /** + * Directly invoke the API for the given URL. Useful if the API returns direct links/URLs for subsequent requests. + * @param url The URL for the request, either full URL or only the path. + * @param method The HTTP method for the request. + * @param request The request object. + * @param returnType The return type. + * @return The API response in the specified type. + * @throws ApiException if fails to make API call. + */ + public T invokeAPI(String url, String method, Object request, TypeReference returnType) throws ApiException { + return invokeAPI(url, method, request, returnType, Collections.emptyMap()); + } + + /** + * Directly invoke the API for the given URL. Useful if the API returns direct links/URLs for subsequent requests. + * @param url The URL for the request, either full URL or only the path. + * @param method The HTTP method for the request. + * @param request The request object. + * @param returnType The return type. + * @param additionalHeaders Additional headers for the request. + * @return The API response in the specified type. + * @throws ApiException if fails to make API call. + */ + public abstract T invokeAPI(String url, String method, Object request, TypeReference returnType, Map additionalHeaders) throws ApiException; +} diff --git a/boat-scaffold/src/main/templates/boat-java/libraries/resttemplate/BaseApi.mustache b/boat-scaffold/src/main/templates/boat-java/libraries/resttemplate/BaseApi.mustache new file mode 100644 index 000000000..3b47979ed --- /dev/null +++ b/boat-scaffold/src/main/templates/boat-java/libraries/resttemplate/BaseApi.mustache @@ -0,0 +1,76 @@ +{{>licenseInfo}} + +package {{invokerPackage}}; + +import org.springframework.web.client.RestClientException; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; + +{{>generatedAnnotation}} +public abstract class BaseApi { + + protected ApiClient apiClient; + + public BaseApi() { + this(new ApiClient()); + } + + public BaseApi(ApiClient apiClient) { + this.apiClient = apiClient; + } + + public ApiClient getApiClient() { + return apiClient; + } + + public void setApiClient(ApiClient apiClient) { + this.apiClient = apiClient; + } + + /** + * Directly invoke the API for the given URL. Useful if the API returns direct links/URLs for subsequent requests. + * @param url The URL for the request, either full URL or only the path. + * @param method The HTTP method for the request. + * @return ResponseEntity<Void> + * @throws RestClientException if an error occurs while attempting to invoke the API + */ + public ResponseEntity invokeAPI(String url, HttpMethod method) throws RestClientException { + return invokeAPI(url, method, null, new ParameterizedTypeReference() {}); + } + + /** + * Directly invoke the API for the given URL. Useful if the API returns direct links/URLs for subsequent requests. + * @param url The URL for the request, either full URL or only the path. + * @param method The HTTP method for the request. + * @param request The request object. + * @return ResponseEntity<Void> + * @throws RestClientException if an error occurs while attempting to invoke the API + */ + public ResponseEntity invokeAPI(String url, HttpMethod method, Object request) throws RestClientException { + return invokeAPI(url, method, request, new ParameterizedTypeReference() {}); + } + + /** + * Directly invoke the API for the given URL. Useful if the API returns direct links/URLs for subsequent requests. + * @param url The URL for the request, either full URL or only the path. + * @param method The HTTP method for the request. + * @param returnType The return type. + * @return ResponseEntity in the specified type. + * @throws RestClientException if an error occurs while attempting to invoke the API + */ + public ResponseEntity invokeAPI(String url, HttpMethod method, ParameterizedTypeReference returnType) throws RestClientException { + return invokeAPI(url, method, null, returnType); + } + + /** + * Directly invoke the API for the given URL. Useful if the API returns direct links/URLs for subsequent requests. + * @param url The URL for the request, either full URL or only the path. + * @param method The HTTP method for the request. + * @param request The request object. + * @param returnType The return type. + * @return ResponseEntity in the specified type. + * @throws RestClientException if an error occurs while attempting to invoke the API + */ + public abstract ResponseEntity invokeAPI(String url, HttpMethod method, Object request, ParameterizedTypeReference returnType) throws RestClientException; +} diff --git a/boat-scaffold/src/main/templates/boat-java/model.mustache b/boat-scaffold/src/main/templates/boat-java/model.mustache index 140d35fc3..06c6ff72f 100644 --- a/boat-scaffold/src/main/templates/boat-java/model.mustache +++ b/boat-scaffold/src/main/templates/boat-java/model.mustache @@ -17,6 +17,7 @@ import java.io.Serializable; {{#jackson}} import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.annotation.JsonTypeName; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; {{#withXml}} import com.fasterxml.jackson.dataformat.xml.annotation.*; {{/withXml}} diff --git a/boat-scaffold/src/main/templates/boat-spring/model.mustache b/boat-scaffold/src/main/templates/boat-spring/model.mustache index 896d93104..5cc67ab79 100644 --- a/boat-scaffold/src/main/templates/boat-spring/model.mustache +++ b/boat-scaffold/src/main/templates/boat-spring/model.mustache @@ -45,6 +45,7 @@ import javax.validation.constraints.*; import org.hibernate.validator.constraints.*; {{/performBeanValidation}} {{#jackson}} +import com.fasterxml.jackson.databind.annotation.JsonSerialize; {{#withXml}} import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; From a34a74c5b248530bdc4d346d1ba4b83b28e77403 Mon Sep 17 00:00:00 2001 From: jasper linschoten Date: Mon, 14 Apr 2025 15:46:17 +0200 Subject: [PATCH 3/4] Adjust maven plugin to updated openapi-generator --- .../boat-generate/java-server/models/pom.xml | 5 ++++ boat-maven-plugin/src/it/example/pom.xml | 1 - .../com/backbase/oss/boat/GenerateMojo.java | 25 ++++++++++++------- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/boat-maven-plugin/src/it/example/boat-generate/java-server/models/pom.xml b/boat-maven-plugin/src/it/example/boat-generate/java-server/models/pom.xml index d642577db..3b14a7f7e 100644 --- a/boat-maven-plugin/src/it/example/boat-generate/java-server/models/pom.xml +++ b/boat-maven-plugin/src/it/example/boat-generate/java-server/models/pom.xml @@ -73,6 +73,11 @@ swagger-annotations 2.2.7 + + + org.springframework + spring-core + jar diff --git a/boat-maven-plugin/src/it/example/pom.xml b/boat-maven-plugin/src/it/example/pom.xml index 650561ff3..9bd39dd2a 100644 --- a/boat-maven-plugin/src/it/example/pom.xml +++ b/boat-maven-plugin/src/it/example/pom.xml @@ -11,7 +11,6 @@ @pom.version@ - 0.2.3 2.13.3 diff --git a/boat-maven-plugin/src/main/java/com/backbase/oss/boat/GenerateMojo.java b/boat-maven-plugin/src/main/java/com/backbase/oss/boat/GenerateMojo.java index 233cdabe1..7b7cb1abf 100644 --- a/boat-maven-plugin/src/main/java/com/backbase/oss/boat/GenerateMojo.java +++ b/boat-maven-plugin/src/main/java/com/backbase/oss/boat/GenerateMojo.java @@ -13,7 +13,6 @@ import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyInstantiationTypesKvpList; import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyLanguageSpecificPrimitivesCsv; import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyLanguageSpecificPrimitivesCsvList; -import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyOpenAPINormalizerKvpList; import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyReservedWordsMappingsKvp; import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyReservedWordsMappingsKvpList; import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applySchemaMappingsKvp; @@ -74,6 +73,7 @@ import org.openapitools.codegen.auth.AuthParser; import org.openapitools.codegen.config.CodegenConfigurator; import org.openapitools.codegen.config.GlobalSettings; +import org.openapitools.codegen.utils.OptionUtils; import org.sonatype.plexus.build.incremental.BuildContext; import org.sonatype.plexus.build.incremental.DefaultBuildContext; @@ -573,13 +573,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { java.nio.file.Files.copy(inputSpecFile.toPath(), copyTo.toPath(), StandardCopyOption.REPLACE_EXISTING); } - // attempt to read from config file - CodegenConfigurator configurator = CodegenConfigurator.fromFile(configurationFile); - - // if a config file wasn't specified or we were unable to read it - if (configurator == null) { - configurator = new CodegenConfigurator(); - } + final CodegenConfigurator configurator = loadCodegenConfigurator(); configurator.setVerbose(verbose); @@ -857,7 +851,10 @@ public void execute() throws MojoExecutionException, MojoFailureException { } if (openapiNormalizer != null && (configOptions == null || !configOptions.containsKey("openapi-normalizer"))) { - applyOpenAPINormalizerKvpList(openapiNormalizer, configurator); + for (String propString: openapiNormalizer) { + OptionUtils.parseCommaSeparatedTuples(propString) + .forEach(p -> {configurator.addOpenapiNormalizer(p.getLeft(), p.getRight());}); + } } // Apply Schema Mappings @@ -953,6 +950,16 @@ public void execute() throws MojoExecutionException, MojoFailureException { } } + /** + * Attempt to read from config file, return default otherwise. + * + * @return The CodegenConfigurator loaded from file or a default. + */ + private CodegenConfigurator loadCodegenConfigurator() { + CodegenConfigurator configurator = CodegenConfigurator.fromFile(configurationFile); + return configurator != null ? configurator : new CodegenConfigurator(); + } + protected Collection getGeneratorSpecificSupportingFiles() { return Collections.emptySet(); } From ce49476f056f14182ffd1040bcb6223a75d46dbf Mon Sep 17 00:00:00 2001 From: jasper linschoten Date: Tue, 15 Apr 2025 12:59:35 +0200 Subject: [PATCH 4/4] Address sonar complaints and remove dead, unreachable, code --- .../com/backbase/oss/boat/GenerateMojo.java | 77 ++++++------------- .../oss/codegen/java/BoatJavaCodeGen.java | 5 -- .../codegen/java/BoatJavaCodeGenTests.java | 72 ++++++++++++----- 3 files changed, 77 insertions(+), 77 deletions(-) diff --git a/boat-maven-plugin/src/main/java/com/backbase/oss/boat/GenerateMojo.java b/boat-maven-plugin/src/main/java/com/backbase/oss/boat/GenerateMojo.java index 7b7cb1abf..4ea7d3d85 100644 --- a/boat-maven-plugin/src/main/java/com/backbase/oss/boat/GenerateMojo.java +++ b/boat-maven-plugin/src/main/java/com/backbase/oss/boat/GenerateMojo.java @@ -1,27 +1,5 @@ package com.backbase.oss.boat; -import static java.lang.String.format; -import static java.util.Arrays.stream; -import static java.util.Collections.emptyMap; -import static java.util.stream.Collectors.joining; -import static org.apache.commons.lang3.StringUtils.isNotEmpty; -import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyAdditionalPropertiesKvp; -import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyAdditionalPropertiesKvpList; -import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyImportMappingsKvp; -import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyImportMappingsKvpList; -import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyInstantiationTypesKvp; -import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyInstantiationTypesKvpList; -import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyLanguageSpecificPrimitivesCsv; -import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyLanguageSpecificPrimitivesCsvList; -import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyReservedWordsMappingsKvp; -import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyReservedWordsMappingsKvpList; -import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applySchemaMappingsKvp; -import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applySchemaMappingsKvpList; -import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyServerVariablesKvp; -import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyServerVariablesKvpList; -import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyTypeMappingsKvp; -import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyTypeMappingsKvpList; - import com.backbase.oss.boat.transformers.Bundler; import com.backbase.oss.boat.transformers.DereferenceComponentsPropertiesTransformer; import com.backbase.oss.boat.transformers.UnAliasTransformer; @@ -32,31 +10,6 @@ import io.swagger.v3.core.util.Yaml; import io.swagger.v3.parser.core.models.AuthorizationValue; import io.swagger.v3.parser.util.ClasspathHelper; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; -import java.net.URLConnection; -import java.nio.channels.Channels; -import java.nio.channels.FileChannel; -import java.nio.channels.ReadableByteChannel; -import java.nio.charset.StandardCharsets; -import java.nio.file.StandardCopyOption; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; -import java.util.stream.Collectors; -import java.util.stream.Stream; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.apache.maven.plugin.MojoExecutionException; @@ -65,11 +18,7 @@ import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; -import org.openapitools.codegen.CliOption; -import org.openapitools.codegen.ClientOptInput; -import org.openapitools.codegen.CodegenConfig; -import org.openapitools.codegen.CodegenConstants; -import org.openapitools.codegen.DefaultGenerator; +import org.openapitools.codegen.*; import org.openapitools.codegen.auth.AuthParser; import org.openapitools.codegen.config.CodegenConfigurator; import org.openapitools.codegen.config.GlobalSettings; @@ -77,6 +26,28 @@ import org.sonatype.plexus.build.incremental.BuildContext; import org.sonatype.plexus.build.incremental.DefaultBuildContext; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.*; +import java.nio.channels.Channels; +import java.nio.channels.FileChannel; +import java.nio.channels.ReadableByteChannel; +import java.nio.charset.StandardCharsets; +import java.nio.file.StandardCopyOption; +import java.util.*; +import java.util.Map.Entry; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static java.lang.String.format; +import static java.util.Arrays.stream; +import static java.util.Collections.emptyMap; +import static java.util.stream.Collectors.joining; +import static org.apache.commons.lang3.StringUtils.isNotEmpty; +import static org.openapitools.codegen.config.CodegenConfiguratorUtils.*; + /** * Generates client/server code from an OpenAPI json/yaml definition. */ @@ -853,7 +824,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { if (openapiNormalizer != null && (configOptions == null || !configOptions.containsKey("openapi-normalizer"))) { for (String propString: openapiNormalizer) { OptionUtils.parseCommaSeparatedTuples(propString) - .forEach(p -> {configurator.addOpenapiNormalizer(p.getLeft(), p.getRight());}); + .forEach(p -> configurator.addOpenapiNormalizer(p.getLeft(), p.getRight())); } } diff --git a/boat-scaffold/src/main/java/com/backbase/oss/codegen/java/BoatJavaCodeGen.java b/boat-scaffold/src/main/java/com/backbase/oss/codegen/java/BoatJavaCodeGen.java index f7f55d1fd..2d8750007 100644 --- a/boat-scaffold/src/main/java/com/backbase/oss/codegen/java/BoatJavaCodeGen.java +++ b/boat-scaffold/src/main/java/com/backbase/oss/codegen/java/BoatJavaCodeGen.java @@ -1,7 +1,6 @@ package com.backbase.oss.codegen.java; import com.backbase.oss.codegen.java.BoatCodeGenUtils.CodegenValueType; -import io.swagger.v3.oas.models.media.ArraySchema; import io.swagger.v3.oas.models.media.Schema; import lombok.Getter; import lombok.Setter; @@ -158,10 +157,6 @@ private boolean isArrayTypeOfEnum(Schema s) { return false; } Schema items = target.getItems(); - if (items == null) { - // Will default to strings schema, which is not an enum - return false; - } if (items.get$ref() != null) { items = openAPI.getComponents().getSchemas().get(ModelUtils.getSimpleRef(items.get$ref())); } diff --git a/boat-scaffold/src/test/java/com/backbase/oss/codegen/java/BoatJavaCodeGenTests.java b/boat-scaffold/src/test/java/com/backbase/oss/codegen/java/BoatJavaCodeGenTests.java index 1dd8c6215..fbc8d3b70 100644 --- a/boat-scaffold/src/test/java/com/backbase/oss/codegen/java/BoatJavaCodeGenTests.java +++ b/boat-scaffold/src/test/java/com/backbase/oss/codegen/java/BoatJavaCodeGenTests.java @@ -1,36 +1,34 @@ package com.backbase.oss.codegen.java; -import static com.backbase.oss.codegen.java.BoatJavaCodeGen.CREATE_API_COMPONENT; -import static com.backbase.oss.codegen.java.BoatJavaCodeGen.REST_TEMPLATE_BEAN_NAME; -import static com.backbase.oss.codegen.java.BoatJavaCodeGen.USE_CLASS_LEVEL_BEAN_VALIDATION; -import static com.backbase.oss.codegen.java.BoatJavaCodeGen.USE_DEFAULT_API_CLIENT; -import static com.backbase.oss.codegen.java.BoatJavaCodeGen.USE_JACKSON_CONVERSION; -import static com.backbase.oss.codegen.java.BoatJavaCodeGen.USE_PROTECTED_FIELDS; -import static com.backbase.oss.codegen.java.BoatJavaCodeGen.USE_WITH_MODIFIERS; -import static java.util.stream.Collectors.groupingBy; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.hasEntry; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.nullValue; -import static org.junit.jupiter.api.Assertions.assertEquals; - import com.github.javaparser.StaticJavaParser; import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.body.MethodDeclaration; import com.github.javaparser.ast.body.TypeDeclaration; import io.swagger.parser.OpenAPIParser; +import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.parser.core.models.ParseOptions; -import java.io.File; -import java.io.FileNotFoundException; -import java.util.List; -import java.util.Map; -import java.util.function.Function; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.ValueSource; import org.openapitools.codegen.CliOption; import org.openapitools.codegen.ClientOptInput; import org.openapitools.codegen.DefaultGenerator; +import org.openapitools.codegen.config.GlobalSettings; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Stream; + +import static com.backbase.oss.codegen.java.BoatJavaCodeGen.*; +import static java.util.stream.Collectors.groupingBy; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; +import static org.junit.jupiter.api.Assertions.assertEquals; class BoatJavaCodeGenTests { @@ -208,4 +206,40 @@ void shouldHonourBeanValidationOption(boolean useBeanValidation) throws FileNotF assertThat("Expect jakarta Valid import", compilationUnit.getImports().stream().anyMatch( id -> id.getNameAsString().equals("jakarta.validation.Valid")), is(useBeanValidation)); } + + @ParameterizedTest + @MethodSource("testTypeDeclarationValues") + public void testTypeDeclaration(boolean useBeanValidation, String schemaType, + String itemType, boolean itemEnum, String expected) { + + // Setup + BoatJavaCodeGen gen = new BoatJavaCodeGen(); + gen.setUseBeanValidation(useBeanValidation); + GlobalSettings.setProperty("generateAliasAsModel", "true"); + + Schema schema = new Schema(); + schema.type(schemaType); + if (itemType != null) { + Schema itemSchema = new Schema(); + itemSchema.type(itemType); + if (itemEnum) { + itemSchema.setEnum(List.of("1", "2")); + } + schema.setItems(itemSchema); + } + + String s = gen.getTypeDeclaration(schema); + assertEquals(expected, s); + } + + static Stream testTypeDeclarationValues() { + return Stream.of( + Arguments.of(true, "string", null, false, "String"), + Arguments.of(false, "string", null, false, "String"), + Arguments.of(true, "array", "string", false, "List"), + Arguments.of(true, "array", "string", true, "List<@Valid String>"), + Arguments.of(true, "array", "object", false, "List"), + Arguments.of(true, "array", null, false, "List") // this case logs an ERROR + ); + } }