From 9ad0ed173a09fc23179d978aa528d0104bea6597 Mon Sep 17 00:00:00 2001 From: Milad Khalilian Date: Thu, 6 Jul 2023 16:35:18 +0200 Subject: [PATCH 1/2] add a workaround for default value instantiation --- .../oss/codegen/java/BoatJavaCodeGen.java | 59 +++------ .../oss/codegen/java/BoatSpringCodeGen.java | 68 +++++------ .../codegen/java/BoatJavaCodeGenTests.java | 113 ------------------ .../codegen/java/BoatSpringCodeGenTests.java | 38 ------ .../java/BoatSpringTemplatesTests.java | 33 +---- 5 files changed, 49 insertions(+), 262 deletions(-) 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 ccb5119c6..798bb5b76 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,36 +1,30 @@ package com.backbase.oss.codegen.java; +import io.swagger.v3.oas.models.media.Schema; +import java.util.Locale; import lombok.Getter; import lombok.Setter; import org.openapitools.codegen.CliOption; -import org.openapitools.codegen.CodegenModel; import org.openapitools.codegen.CodegenProperty; import org.openapitools.codegen.languages.JavaClientCodegen; +import org.openapitools.codegen.utils.ModelUtils; public class BoatJavaCodeGen extends JavaClientCodegen { public static final String NAME = "boat-java"; public static final String USE_WITH_MODIFIERS = "useWithModifiers"; - public static final String USE_SET_FOR_UNIQUE_ITEMS = "useSetForUniqueItems"; - public static final String USE_CLASS_LEVEL_BEAN_VALIDATION = "useClassLevelBeanValidation"; public static final String USE_JACKSON_CONVERSION = "useJacksonConversion"; - public static final String USE_DEFAULT_API_CLIENT = "useDefaultApiClient"; public static final String REST_TEMPLATE_BEAN_NAME = "restTemplateBeanName"; public static final String CREATE_API_COMPONENT = "createApiComponent"; public static final String USE_PROTECTED_FIELDS = "useProtectedFields"; - private static final String JAVA_UTIL_SET = "java.util.Set"; - @Setter @Getter protected boolean useWithModifiers; @Setter @Getter - protected boolean useSetForUniqueItems; - @Setter - @Getter protected boolean useClassLevelBeanValidation; @Setter @Getter @@ -76,27 +70,11 @@ public String getName() { public void processOpts() { super.processOpts(); - if (WEBCLIENT.equals(getLibrary())) { - this.useSetForUniqueItems = false; - } - if (this.additionalProperties.containsKey(USE_WITH_MODIFIERS)) { this.useWithModifiers = convertPropertyToBoolean(USE_WITH_MODIFIERS); } writePropertyBack(USE_WITH_MODIFIERS, this.useWithModifiers); - if (this.additionalProperties.containsKey(USE_SET_FOR_UNIQUE_ITEMS)) { - this.useSetForUniqueItems = convertPropertyToBoolean(USE_SET_FOR_UNIQUE_ITEMS); - } - writePropertyBack(USE_SET_FOR_UNIQUE_ITEMS, this.useSetForUniqueItems); - - if (this.useSetForUniqueItems) { - this.typeMapping.put("set", JAVA_UTIL_SET); - - this.importMapping.put("Set", JAVA_UTIL_SET); - this.importMapping.put("LinkedHashSet", "java.util.LinkedHashSet"); - } - if (RESTTEMPLATE.equals(getLibrary())) { processRestTemplateOpts(); } @@ -141,24 +119,23 @@ private void processRestTemplateOpts() { } @Override - public void postProcessModelProperty(CodegenModel model, CodegenProperty p) { - super.postProcessModelProperty(model, p); - - if (!fullJavaUtil) { - if ("array".equals(p.containerType)) { - model.imports.add(instantiationTypes.get("array")); - } else if ("set".equals(p.containerType)) { - model.imports.add(instantiationTypes.get("set")); - boolean canNotBeWrappedToNullable = !openApiNullable || !p.isNullable; - if (canNotBeWrappedToNullable) { - model.imports.add("JsonDeserialize"); - p.vendorExtensions.put("x-setter-extra-annotation", "@JsonDeserialize(as = " + instantiationTypes.get("set") + ".class)"); + public String toDefaultValue(CodegenProperty cp, Schema schema) { + schema = ModelUtils.getReferencedSchema(this.openAPI, schema); + if (ModelUtils.isArraySchema(schema) && (schema.getDefault() == null)) { + if (cp.isNullable || containerDefaultToNull) { + return null; + } else { + if (ModelUtils.isSet(schema)) { + return String.format(Locale.ROOT, "new %s<>()", + instantiationTypes().getOrDefault("set", "LinkedHashSet")); + } else { + return String.format(Locale.ROOT, "new %s<>()", + instantiationTypes().getOrDefault("array", "ArrayList")); + } } - } else if ("map".equals(p.containerType)) { - model.imports.add(instantiationTypes.get("map")); - } - } + } + return super.toDefaultValue(cp, schema); } } 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 079680dc1..db2f8c5b7 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 @@ -7,23 +7,25 @@ import com.samskivert.mustache.Mustache; import com.samskivert.mustache.Template.Fragment; import io.swagger.v3.oas.models.Operation; +import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.servers.Server; import java.io.IOException; import java.io.Writer; import java.util.List; +import java.util.Locale; import java.util.stream.IntStream; import lombok.Getter; import lombok.Setter; import org.apache.commons.lang3.StringUtils; import org.openapitools.codegen.CliOption; import org.openapitools.codegen.CodegenConstants; -import org.openapitools.codegen.CodegenModel; import org.openapitools.codegen.CodegenOperation; import org.openapitools.codegen.CodegenParameter; import org.openapitools.codegen.CodegenProperty; import org.openapitools.codegen.config.GlobalSettings; import org.openapitools.codegen.languages.SpringCodegen; import org.openapitools.codegen.templating.mustache.IndentedLambda; +import org.openapitools.codegen.utils.ModelUtils; public class BoatSpringCodeGen extends SpringCodegen { @@ -33,10 +35,8 @@ public class BoatSpringCodeGen extends SpringCodegen { public static final String ADD_SERVLET_REQUEST = "addServletRequest"; public static final String ADD_BINDING_RESULT = "addBindingResult"; public static final String USE_LOMBOK_ANNOTATIONS = "useLombokAnnotations"; - public static final String USE_SET_FOR_UNIQUE_ITEMS = "useSetForUniqueItems"; public static final String USE_WITH_MODIFIERS = "useWithModifiers"; public static final String USE_PROTECTED_FIELDS = "useProtectedFields"; - public static final String UNIQUE_BASE_TYPE = "java.util.Set"; static class NewLineIndent implements Mustache.Lambda { @@ -116,9 +116,6 @@ static int indentLevel(String text) { @Getter protected boolean useLombokAnnotations; - @Setter - @Getter - protected boolean useSetForUniqueItems; /** * Whether to use {@code with} prefix for pojos modifiers. @@ -127,6 +124,10 @@ static int indentLevel(String text) { @Getter protected boolean useWithModifiers; + @Setter + @Getter + protected boolean useProtectedFields; + public BoatSpringCodeGen() { super(); this.embeddedTemplateDir = this.templateDir = NAME; @@ -141,8 +142,6 @@ public BoatSpringCodeGen() { this.addBindingResult)); this.cliOptions.add(CliOption.newBoolean(USE_LOMBOK_ANNOTATIONS, "Add Lombok to class-level Api models. Defaults to false.", this.useLombokAnnotations)); - this.cliOptions.add(CliOption.newBoolean(USE_SET_FOR_UNIQUE_ITEMS, - "Use java.util.Set for arrays that have uniqueItems set to true.", this.useSetForUniqueItems)); this.cliOptions.add(CliOption.newBoolean(USE_WITH_MODIFIERS, "Whether to use \"with\" prefix for POJO modifiers.", this.useWithModifiers)); this.cliOptions.add(CliOption.newString(USE_PROTECTED_FIELDS, @@ -171,9 +170,6 @@ public String toApiName(String name) { public void processOpts() { super.processOpts(); - if (this.reactive) { - this.useSetForUniqueItems = false; - } // Whether it's using ApiUtil or not. // cases: @@ -204,9 +200,6 @@ public void processOpts() { if (this.additionalProperties.containsKey(USE_LOMBOK_ANNOTATIONS)) { this.useLombokAnnotations = convertPropertyToBoolean(USE_LOMBOK_ANNOTATIONS); } - if (this.additionalProperties.containsKey(USE_SET_FOR_UNIQUE_ITEMS)) { - this.useSetForUniqueItems = convertPropertyToBoolean(USE_SET_FOR_UNIQUE_ITEMS); - } if (this.additionalProperties.containsKey(USE_WITH_MODIFIERS)) { this.useWithModifiers = convertPropertyToBoolean(USE_WITH_MODIFIERS); } @@ -220,15 +213,9 @@ public void processOpts() { writePropertyBack(ADD_SERVLET_REQUEST, this.addServletRequest); writePropertyBack(ADD_BINDING_RESULT, this.addBindingResult); writePropertyBack(USE_LOMBOK_ANNOTATIONS, this.useLombokAnnotations); - writePropertyBack(USE_SET_FOR_UNIQUE_ITEMS, this.useSetForUniqueItems); writePropertyBack(USE_WITH_MODIFIERS, this.useWithModifiers); + writePropertyBack(USE_PROTECTED_FIELDS, this.useProtectedFields); - if (this.useSetForUniqueItems) { - this.typeMapping.put("set", UNIQUE_BASE_TYPE); - - this.importMapping.put("Set", UNIQUE_BASE_TYPE); - this.importMapping.put("LinkedHashSet", "java.util.LinkedHashSet"); - } this.additionalProperties.put("indent4", new IndentedLambda(4, " ")); this.additionalProperties.put("newLine4", new NewLineIndent(4, " ")); @@ -236,18 +223,6 @@ public void processOpts() { this.additionalProperties.put("newLine8", new NewLineIndent(8, " ")); } - @Override - public void postProcessModelProperty(CodegenModel model, CodegenProperty p) { - super.postProcessModelProperty(model, p); - - if (p.isContainer && this.useSetForUniqueItems && p.getUniqueItems()) { - p.containerType = "set"; - p.baseType = UNIQUE_BASE_TYPE; - p.dataType = UNIQUE_BASE_TYPE + "<" + p.items.dataType + ">"; - p.datatypeWithEnum = UNIQUE_BASE_TYPE + "<" + p.items.datatypeWithEnum + ">"; - p.defaultValue = "new " + "java.util.LinkedHashSet<>()"; - } - } @Override public void postProcessParameter(CodegenParameter p) { @@ -257,13 +232,6 @@ public void postProcessParameter(CodegenParameter p) { if (!this.reactive) { p.baseType = p.dataType.replaceAll("^([^<]+)<.+>$", "$1"); } - - if (this.useSetForUniqueItems && p.getUniqueItems()) { - p.baseType = UNIQUE_BASE_TYPE; - p.dataType = UNIQUE_BASE_TYPE + "<" + p.items.dataType + ">"; - p.datatypeWithEnum = UNIQUE_BASE_TYPE + "<" + p.items.datatypeWithEnum + ">"; - p.defaultValue = "new " + "java.util.LinkedHashSet<>()"; - } } } @@ -288,4 +256,24 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation } return codegenOperation; } + + @Override + public String toDefaultValue(CodegenProperty cp, Schema schema) { + schema = ModelUtils.getReferencedSchema(this.openAPI, schema); + if (ModelUtils.isArraySchema(schema) && (schema.getDefault() == null)) { + if (cp.isNullable || containerDefaultToNull) { + return null; + } else { + if (ModelUtils.isSet(schema)) { + return String.format(Locale.ROOT, "new %s<>()", + instantiationTypes().getOrDefault("set", "LinkedHashSet")); + } else { + return String.format(Locale.ROOT, "new %s<>()", + instantiationTypes().getOrDefault("array", "ArrayList")); + } + } + + } + return super.toDefaultValue(cp, schema); + } } 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 6ba055b81..9679c6aa4 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 @@ -38,7 +38,6 @@ void processOptsWithRestTemplateDefaults() { gen.processOpts(); assertThat(gen.useWithModifiers, is(false)); - assertThat(gen.useSetForUniqueItems, is(false)); assertThat(gen.useClassLevelBeanValidation, is(false)); assertThat(gen.useJacksonConversion, is(false)); @@ -53,7 +52,6 @@ void processOptsWithRestTemplate() { gen.setLibrary("resttemplate"); options.put(USE_WITH_MODIFIERS, "true"); - options.put(USE_SET_FOR_UNIQUE_ITEMS, "true"); options.put(USE_CLASS_LEVEL_BEAN_VALIDATION, "true"); options.put(USE_JACKSON_CONVERSION, "true"); @@ -63,7 +61,6 @@ void processOptsWithRestTemplate() { gen.processOpts(); assertThat(gen.useWithModifiers, is(true)); - assertThat(gen.useSetForUniqueItems, is(true)); assertThat(gen.useClassLevelBeanValidation, is(true)); assertThat(gen.useJacksonConversion, is(true)); @@ -77,7 +74,6 @@ void processOptsWithoutRestTemplate() { final Map options = gen.additionalProperties(); options.put(USE_WITH_MODIFIERS, "true"); - options.put(USE_SET_FOR_UNIQUE_ITEMS, "true"); options.put(USE_CLASS_LEVEL_BEAN_VALIDATION, "true"); options.put(USE_JACKSON_CONVERSION, "true"); @@ -87,7 +83,6 @@ void processOptsWithoutRestTemplate() { gen.processOpts(); assertThat(gen.useWithModifiers, is(true)); - assertThat(gen.useSetForUniqueItems, is(true)); assertThat(gen.useClassLevelBeanValidation, is(false)); assertThat(gen.useJacksonConversion, is(false)); @@ -120,112 +115,4 @@ void processOptsUseProtectedFields() { assertThat(gen.additionalProperties(), hasEntry("modelFieldsVisibility", "protected")); } - @Test - @Disabled("Since useSetForUniqueItems is not supported and it is enabled by default") - void uniquePropertyToSet() { - final BoatJavaCodeGen gen = new BoatJavaCodeGen(); - final CodegenProperty prop = new CodegenProperty(); - - gen.useSetForUniqueItems = true; - prop.isContainer = true; - prop.setUniqueItems(true); - prop.items = new CodegenProperty(); - prop.items.dataType = "String"; - prop.baseType = "java.util.List"; - prop.dataType = "java.util.List"; - - gen.postProcessModelProperty(new CodegenModel(), prop); - - assertThat(prop.containerType, is("set")); - assertThat(prop.baseType, is("java.util.Set")); - assertThat(prop.dataType, is("java.util.Set")); - } - - @Test - @Disabled("Since useSetForUniqueItems is not supported and it is enabled by default") - void uniqueParameterToSet() { - final BoatJavaCodeGen gen = new BoatJavaCodeGen(); - final CodegenParameter param = new CodegenParameter(); - - gen.useSetForUniqueItems = true; - param.isContainer = true; - param.setUniqueItems(true); - param.items = new CodegenProperty(); - param.items.dataType = "String"; - param.baseType = "java.util.List"; - param.dataType = "java.util.List"; - - gen.postProcessParameter(param); - - assertThat(param.baseType, is("java.util.Set")); - assertThat(param.dataType, is("java.util.Set")); - } - - @Test - void setTypeMappingForList() { - final BoatJavaCodeGen gen = new BoatJavaCodeGen(); - gen.setFullJavaUtil(false); - gen.instantiationTypes().put("set", "ArrayList"); - gen.typeMapping().put("set", "List"); - - final CodegenProperty prop = new CodegenProperty(); - prop.isContainer = true; - prop.containerType = "set"; - prop.setUniqueItems(true); - prop.items = new CodegenProperty(); - prop.items.dataType = "String"; - prop.baseType = "java.util.Set"; - prop.dataType = "java.util.HashSet"; - - final var model = new CodegenModel(); - - gen.postProcessModelProperty(model, prop); - - assertTrue(model.getImports().contains("ArrayList")); - } - - @Test - void arrayTypeMappingForList() { - final BoatJavaCodeGen gen = new BoatJavaCodeGen(); - gen.setFullJavaUtil(false); - gen.instantiationTypes().put("array", "ArrayList"); - gen.typeMapping().put("array", "List"); - - final CodegenProperty prop = new CodegenProperty(); - prop.isContainer = true; - prop.containerType = "array"; - prop.setUniqueItems(true); - prop.items = new CodegenProperty(); - prop.items.dataType = "String"; - prop.baseType = "java.util.Set"; - prop.dataType = "java.util.HashSet"; - - final var model = new CodegenModel(); - - gen.postProcessModelProperty(model, prop); - - assertTrue(model.getImports().contains("ArrayList")); - } - - @Test - void mapTypeMappingForMap() { - final BoatJavaCodeGen gen = new BoatJavaCodeGen(); - gen.setFullJavaUtil(false); - gen.instantiationTypes().put("map", "TreeMap"); - - final CodegenProperty prop = new CodegenProperty(); - prop.isContainer = true; - prop.containerType = "map"; - prop.setUniqueItems(true); - prop.items = new CodegenProperty(); - prop.items.dataType = "String"; - prop.baseType = "java.util.Set"; - prop.dataType = "java.util.HashSet"; - - final var model = new CodegenModel(); - - gen.postProcessModelProperty(model, prop); - - assertTrue(model.getImports().contains("TreeMap")); - } } diff --git a/boat-scaffold/src/test/java/com/backbase/oss/codegen/java/BoatSpringCodeGenTests.java b/boat-scaffold/src/test/java/com/backbase/oss/codegen/java/BoatSpringCodeGenTests.java index 61b0d9d5b..1cf3b7bc3 100644 --- a/boat-scaffold/src/test/java/com/backbase/oss/codegen/java/BoatSpringCodeGenTests.java +++ b/boat-scaffold/src/test/java/com/backbase/oss/codegen/java/BoatSpringCodeGenTests.java @@ -38,44 +38,6 @@ void clientOptsUnicity() { .forEach((k, v) -> assertEquals(1, v.size(), k + " is described multiple times")); } - @Test - void uniquePropertyToSet() { - final BoatSpringCodeGen gen = new BoatSpringCodeGen(); - final CodegenProperty prop = new CodegenProperty(); - - gen.useSetForUniqueItems = true; - prop.isContainer = true; - prop.setUniqueItems(true); - prop.items = new CodegenProperty(); - prop.items.dataType = "String"; - prop.baseType = "java.util.List"; - prop.dataType = "java.util.List"; - - gen.postProcessModelProperty(new CodegenModel(), prop); - - assertThat(prop.containerType, is("set")); - assertThat(prop.baseType, is("java.util.Set")); - assertThat(prop.dataType, is("java.util.Set")); - } - - @Test - void uniqueParameterToSet() { - final BoatSpringCodeGen gen = new BoatSpringCodeGen(); - final CodegenParameter param = new CodegenParameter(); - - gen.useSetForUniqueItems = true; - param.isContainer = true; - param.setUniqueItems(true); - param.items = new CodegenProperty(); - param.items.dataType = "String"; - param.baseType = "java.util.List"; - param.dataType = "java.util.List"; - - gen.postProcessParameter(param); - - assertThat(param.baseType, is("java.util.Set")); - assertThat(param.dataType, is("java.util.Set")); - } @Test void processOptsUseProtectedFields() { diff --git a/boat-scaffold/src/test/java/com/backbase/oss/codegen/java/BoatSpringTemplatesTests.java b/boat-scaffold/src/test/java/com/backbase/oss/codegen/java/BoatSpringTemplatesTests.java index bfae5a0cd..78024acd2 100644 --- a/boat-scaffold/src/test/java/com/backbase/oss/codegen/java/BoatSpringTemplatesTests.java +++ b/boat-scaffold/src/test/java/com/backbase/oss/codegen/java/BoatSpringTemplatesTests.java @@ -48,7 +48,6 @@ /** * These tests verify that the code generation works for various combinations of configuration * parameters; the projects that are generated are later compiled in the integration test phase. - * * The suite is generated dynamically, read below. *

* With Junit4, the test hierarchy was {@code root-> combination -> method}. The code relied on that @@ -72,7 +71,7 @@ static public void setUpClass() throws IOException { } static class Combination { - static final List CASES = asList("flx", "unq", "val", "opt", "req", "bin", "lmb", "nbl", "wth", "utl"); + static final List CASES = asList("flx", "val", "opt", "req", "bin", "lmb", "wth", "utl"); final String name; @@ -82,8 +81,6 @@ static class Combination { final boolean addServletRequest; final boolean addBindingResult; final boolean useLombokAnnotations; - final boolean openApiNullable; - final boolean useSetForUniqueItems; final boolean useWithModifiers; final boolean reactive; @@ -102,8 +99,6 @@ static class Combination { this.useOptional = (mask & 1 << CASES.indexOf("opt")) != 0; this.addServletRequest = (mask & 1 << CASES.indexOf("req")) != 0; this.useLombokAnnotations = (mask & 1 << CASES.indexOf("lmb")) != 0; - this.openApiNullable = (mask & 1 << CASES.indexOf("nbl")) != 0; - this.useSetForUniqueItems = (mask & 1 << CASES.indexOf("unq")) != 0; this.useWithModifiers = (mask & 1 << CASES.indexOf("wth")) != 0; this.reactive = (mask & 1 << CASES.indexOf("flx")) != 0; this.apiUtil = (mask & 1 << CASES.indexOf("utl")) != 0; @@ -219,22 +214,6 @@ void useLombokAnnotations() { equalTo(this.param.useLombokAnnotations)); } - @Check - void openApiNullable() { - assertThat(findPattern("/api/.+\\.java$", "JsonNullable<[^>]+>"), - is(false)); - assertThat(findPattern("/model/.+\\.java$", "JsonNullable<[^>]+>"), - equalTo(this.param.openApiNullable)); - } - - @Check - void useSetForUniqueItems() { - assertThat(findPattern("/api/.+\\.java$", "java\\.util\\.Set<.+>"), - equalTo(this.param.useSetForUniqueItems)); - assertThat(findPattern("/model/.+\\.java$", "java\\.util\\.Set<.+>"), - equalTo(this.param.useSetForUniqueItems)); - } - @Check void useWithModifiers() { assertThat(findPattern("/api/.+\\.java$", "\\s+with\\p{Upper}"), @@ -280,12 +259,7 @@ private List generateFrom(String templates, String combination) { GlobalSettings.setProperty(CodegenConstants.MODELS, ""); GlobalSettings.setProperty(CodegenConstants.MODEL_TESTS, "true"); GlobalSettings.setProperty(CodegenConstants.MODEL_DOCS, "true"); - -// if (this.param.apiUtil) { - GlobalSettings.setProperty(CodegenConstants.SUPPORTING_FILES, "ApiUtil.java,pom.xml,OpenApiGeneratorApplication.java"); -// } else { -// GlobalSettings.setProperty(CodegenConstants.SUPPORTING_FILES, "pom.xml"); -// } + GlobalSettings.setProperty(CodegenConstants.SUPPORTING_FILES, "ApiUtil.java,pom.xml,OpenApiGeneratorApplication.java"); gcf.setApiNameSuffix("-api"); @@ -303,8 +277,7 @@ private List generateFrom(String templates, String combination) { gcf.addAdditionalProperty(BeanValidationFeatures.USE_BEANVALIDATION, this.param.useBeanValidation); } gcf.addAdditionalProperty(BoatSpringCodeGen.USE_LOMBOK_ANNOTATIONS, this.param.useLombokAnnotations); - gcf.addAdditionalProperty(BoatSpringCodeGen.USE_SET_FOR_UNIQUE_ITEMS, this.param.useSetForUniqueItems); - gcf.addAdditionalProperty(BoatSpringCodeGen.OPENAPI_NULLABLE, this.param.openApiNullable); + gcf.addAdditionalProperty(BoatSpringCodeGen.OPENAPI_NULLABLE, false); gcf.addAdditionalProperty(BoatSpringCodeGen.USE_WITH_MODIFIERS, this.param.useWithModifiers); gcf.addAdditionalProperty(SpringCodegen.REACTIVE, this.param.reactive); From 1bac1f396c5eab103143a203ef8fe812e238597e Mon Sep 17 00:00:00 2001 From: Milad Khalilian Date: Fri, 7 Jul 2023 09:25:37 +0200 Subject: [PATCH 2/2] remove duplicate code --- .../oss/codegen/java/BoatJavaCodeGen.java | 29 +++++++++++-------- .../oss/codegen/java/BoatSpringCodeGen.java | 16 ++-------- 2 files changed, 20 insertions(+), 25 deletions(-) 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 798bb5b76..7cedd5478 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 @@ -122,20 +122,25 @@ private void processRestTemplateOpts() { public String toDefaultValue(CodegenProperty cp, Schema schema) { schema = ModelUtils.getReferencedSchema(this.openAPI, schema); if (ModelUtils.isArraySchema(schema) && (schema.getDefault() == null)) { - if (cp.isNullable || containerDefaultToNull) { - return null; - } else { - if (ModelUtils.isSet(schema)) { - return String.format(Locale.ROOT, "new %s<>()", - instantiationTypes().getOrDefault("set", "LinkedHashSet")); - } else { - return String.format(Locale.ROOT, "new %s<>()", - instantiationTypes().getOrDefault("array", "ArrayList")); - } - } - + return getArrayDefaultValue(cp, schema, containerDefaultToNull, + instantiationTypes().getOrDefault("set", "LinkedHashSet"), + instantiationTypes().getOrDefault("array", "ArrayList")); } return super.toDefaultValue(cp, schema); } + public static String getArrayDefaultValue(CodegenProperty cp, Schema schema, + boolean containerDefaultToNull, String setType, String arrayType) { + if (cp.isNullable || containerDefaultToNull) { + return null; + } else { + if (ModelUtils.isSet(schema)) { + return String.format(Locale.ROOT, "new %s<>()", + setType); + } else { + return String.format(Locale.ROOT, "new %s<>()", + arrayType); + } + } + } } 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 db2f8c5b7..e54728b2c 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 @@ -12,7 +12,6 @@ import java.io.IOException; import java.io.Writer; import java.util.List; -import java.util.Locale; import java.util.stream.IntStream; import lombok.Getter; import lombok.Setter; @@ -261,18 +260,9 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation public String toDefaultValue(CodegenProperty cp, Schema schema) { schema = ModelUtils.getReferencedSchema(this.openAPI, schema); if (ModelUtils.isArraySchema(schema) && (schema.getDefault() == null)) { - if (cp.isNullable || containerDefaultToNull) { - return null; - } else { - if (ModelUtils.isSet(schema)) { - return String.format(Locale.ROOT, "new %s<>()", - instantiationTypes().getOrDefault("set", "LinkedHashSet")); - } else { - return String.format(Locale.ROOT, "new %s<>()", - instantiationTypes().getOrDefault("array", "ArrayList")); - } - } - + return BoatJavaCodeGen.getArrayDefaultValue(cp, schema, containerDefaultToNull, + instantiationTypes().getOrDefault("set", "LinkedHashSet"), + instantiationTypes().getOrDefault("array", "ArrayList")); } return super.toDefaultValue(cp, schema); }