Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -141,24 +119,28 @@ 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)");
}
} else if ("map".equals(p.containerType)) {
model.imports.add(instantiationTypes.get("map"));
}
public String toDefaultValue(CodegenProperty cp, Schema schema) {
schema = ModelUtils.getReferencedSchema(this.openAPI, schema);
if (ModelUtils.isArraySchema(schema) && (schema.getDefault() == null)) {
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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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;
Expand All @@ -17,13 +18,13 @@
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 {

Expand All @@ -33,10 +34,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 {

Expand Down Expand Up @@ -116,9 +115,6 @@ static int indentLevel(String text) {
@Getter
protected boolean useLombokAnnotations;

@Setter
@Getter
protected boolean useSetForUniqueItems;

/**
* Whether to use {@code with} prefix for pojos modifiers.
Expand All @@ -127,6 +123,10 @@ static int indentLevel(String text) {
@Getter
protected boolean useWithModifiers;

@Setter
@Getter
protected boolean useProtectedFields;

public BoatSpringCodeGen() {
super();
this.embeddedTemplateDir = this.templateDir = NAME;
Expand All @@ -141,8 +141,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,
Expand Down Expand Up @@ -171,9 +169,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:
Expand Down Expand Up @@ -204,9 +199,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);
}
Expand All @@ -220,34 +212,16 @@ 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, " "));
this.additionalProperties.put("indent8", new IndentedLambda(8, " "));
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) {
Expand All @@ -257,13 +231,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<>()";
}
}
}

Expand All @@ -288,4 +255,15 @@ 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)) {
return BoatJavaCodeGen.getArrayDefaultValue(cp, schema, containerDefaultToNull,
instantiationTypes().getOrDefault("set", "LinkedHashSet"),
instantiationTypes().getOrDefault("array", "ArrayList"));
}
return super.toDefaultValue(cp, schema);
}
}
Loading