Skip to content

Commit 10743e9

Browse files
committed
Extending BoatSpringCodeGen to use existing code and updated test class
1 parent f6f879a commit 10743e9

File tree

2 files changed

+4
-393
lines changed

2 files changed

+4
-393
lines changed
Lines changed: 3 additions & 221 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,19 @@
11
package com.backbase.oss.codegen.java;
22

33
import io.swagger.v3.oas.models.Operation;
4-
import io.swagger.v3.oas.models.media.Schema;
5-
import io.swagger.v3.oas.models.parameters.Parameter;
64
import io.swagger.v3.oas.models.servers.Server;
7-
import lombok.Getter;
8-
import lombok.Setter;
95
import lombok.extern.slf4j.Slf4j;
10-
import org.openapitools.codegen.CodegenConstants;
11-
import org.openapitools.codegen.CodegenModel;
12-
import org.openapitools.codegen.CodegenOperation;
13-
import org.openapitools.codegen.CodegenParameter;
14-
import org.openapitools.codegen.CodegenProperty;
156
import org.openapitools.codegen.CliOption;
7+
import org.openapitools.codegen.CodegenOperation;
168
import org.openapitools.codegen.SupportingFile;
17-
import org.openapitools.codegen.config.GlobalSettings;
18-
import org.openapitools.codegen.languages.SpringCodegen;
19-
import org.openapitools.codegen.templating.mustache.IndentedLambda;
20-
import org.openapitools.codegen.utils.ModelUtils;
219

2210
import java.io.File;
23-
import java.util.LinkedHashMap;
2411
import java.util.List;
25-
import java.util.Locale;
26-
import java.util.Set;
27-
import java.util.regex.Matcher;
28-
import java.util.regex.Pattern;
29-
import java.util.stream.Stream;
3012

31-
import static com.backbase.oss.codegen.java.BoatCodeGenUtils.getCollectionCodegenValue;
32-
import static org.apache.commons.lang3.StringUtils.isEmpty;
3313
import static org.openapitools.codegen.utils.StringUtils.camelize;
3414

3515
@Slf4j
36-
public class BoatWebhooksCodeGen extends SpringCodegen {
16+
public class BoatWebhooksCodeGen extends BoatSpringCodeGen {
3717
public static final String NAME = "boat-webhooks";
3818

3919
public static final String USE_CLASS_LEVEL_BEAN_VALIDATION = "useClassLevelBeanValidation";
@@ -46,46 +26,6 @@ public class BoatWebhooksCodeGen extends SpringCodegen {
4626
public static final String JAVA_EXTENSION =".java";
4727

4828

49-
/**
50-
* Add @Validated to class-level Api interfaces. Defaults to false
51-
*/
52-
@Setter
53-
@Getter
54-
protected boolean useClassLevelBeanValidation;
55-
56-
/**
57-
* Adds a HttpServletRequest object to the API definition method.
58-
*/
59-
@Setter
60-
@Getter
61-
protected boolean addServletRequest;
62-
63-
/**
64-
* Adds BindingResult to API interface method if @validate is used
65-
*/
66-
@Setter
67-
@Getter
68-
protected boolean addBindingResult;
69-
70-
/**
71-
* Add Lombok to class-level Api models. Defaults to false
72-
*/
73-
@Setter
74-
@Getter
75-
protected boolean useLombokAnnotations;
76-
77-
78-
/**
79-
* Whether to use {@code with} prefix for pojos modifiers.
80-
*/
81-
@Setter
82-
@Getter
83-
protected boolean useWithModifiers;
84-
85-
@Setter
86-
@Getter
87-
protected boolean useProtectedFields;
88-
8929
public BoatWebhooksCodeGen() {
9030
super();
9131
log.info("BoatWebhooksCodeGen constructor called. NAME: {}", NAME);
@@ -130,27 +70,6 @@ public String toApiName(String name) {
13070
public void processOpts() {
13171
super.processOpts();
13272
log.info("BoatWebhooksCodeGen processOpts called. Adding supporting files and properties.");
133-
134-
// Whether it's using ApiUtil or not.
135-
// cases:
136-
// <supportingFilesToGenerate>ApiUtil.java present or not</supportingFilesToGenerate>
137-
// <generateSupportingFiles>true or false</generateSupportingFiles>
138-
final String supFiles = GlobalSettings.getProperty(CodegenConstants.SUPPORTING_FILES);
139-
final boolean useApiUtil = supFiles != null && (supFiles.isEmpty()
140-
? needApiUtil() // set to empty by <generateSuportingFiles>true</generateSuportingFiles>
141-
: supFiles.contains("ApiUtil.java")); // set by <supportingFilesToGenerate/>
142-
143-
if (!useApiUtil) {
144-
this.supportingFiles
145-
.removeIf(sf -> "apiUtil.mustache".equals(sf.getTemplateFile()));
146-
}
147-
writePropertyBack("useApiUtil", useApiUtil);
148-
final var serializerTemplate = "BigDecimalCustomSerializer";
149-
this.supportingFiles.add(new SupportingFile(
150-
serializerTemplate + MUSTACHE_EXTENSION,
151-
(sourceFolder + File.separator + modelPackage).replace(".", File.separator),
152-
serializerTemplate + JAVA_EXTENSION
153-
));
15473
final var webhookResponseTemplate = "WebhookResponse";
15574
this.supportingFiles.add(new SupportingFile(webhookResponseTemplate + MUSTACHE_EXTENSION,
15675
(sourceFolder + File.separator + modelPackage).replace(".", File.separator),
@@ -167,153 +86,16 @@ public void processOpts() {
16786
this.supportingFiles.add(new SupportingFile(prehookRequestTemplate + MUSTACHE_EXTENSION,
16887
(sourceFolder + File.separator + modelPackage).replace(".", File.separator),
16988
prehookRequestTemplate + JAVA_EXTENSION));
170-
this.additionalProperties.put("indent4", new IndentedLambda(4, " ", true, true));
171-
this.additionalProperties.put("newLine4", new BoatSpringCodeGen.NewLineIndent(4, " "));
172-
this.additionalProperties.put("indent8", new IndentedLambda(8, " ", true, true));
173-
this.additionalProperties.put("newLine8", new BoatSpringCodeGen.NewLineIndent(8, " "));
174-
this.additionalProperties.put("toOneLine", new BoatSpringCodeGen.FormatToOneLine());
175-
this.additionalProperties.put("trimAndIndent4", new BoatSpringCodeGen.TrimAndIndent(4, " "));
176-
}
177-
178-
private boolean needApiUtil() {
179-
return this.apiTemplateFiles.containsKey("api.mustache")
180-
&& this.apiTemplateFiles.containsKey("apiDelegate.mustache");
18189
}
18290

183-
/*
184-
* Overridden to be able to override the private <code>replaceBeanValidationCollectionType</code> method.
185-
*/
186-
@Override
187-
public CodegenParameter fromParameter(Parameter parameter, Set<String> imports) {
188-
CodegenParameter codegenParameter = super.fromParameter(parameter, imports);
189-
if (!isListOrSet(codegenParameter)) {
190-
return new BoatSpringCodegenParameter(codegenParameter);
191-
} else {
192-
codegenParameter.datatypeWithEnum = replaceBeanValidationCollectionType(codegenParameter.items, codegenParameter.datatypeWithEnum);
193-
codegenParameter.dataType = replaceBeanValidationCollectionType(codegenParameter.items, codegenParameter.dataType);
194-
return new BoatSpringCodegenParameter(codegenParameter);
195-
}
196-
}
19791

198-
/*
199-
* Overridden to be able to override the private <code>replaceBeanValidationCollectionType</code> method.
200-
*/
201-
@Override
202-
public CodegenProperty fromProperty(String name, Schema p, boolean required, boolean schemaIsFromAdditionalProperties) {
203-
CodegenProperty codegenProperty = super.fromProperty(name, p, required, schemaIsFromAdditionalProperties);
204-
if (!isListOrSet(codegenProperty)) {
205-
return new BoatSpringCodegenProperty(codegenProperty);
206-
} else {
207-
codegenProperty.datatypeWithEnum = replaceBeanValidationCollectionType(codegenProperty.items, codegenProperty.datatypeWithEnum);
208-
codegenProperty.dataType = replaceBeanValidationCollectionType(codegenProperty.items, codegenProperty.dataType);
209-
return new BoatSpringCodegenProperty(codegenProperty);
210-
}
211-
}
212-
213-
/**
214-
* "overridden" to fix invalid code when the data type is a collection of a fully qualified classname.
215-
* eg. <code>Set<@Valid com.backbase.dbs.arrangement.commons.model.TranslationItemDto></code>
216-
*
217-
* @param codegenProperty
218-
* @param dataType
219-
* @return
220-
*/
221-
String replaceBeanValidationCollectionType(CodegenProperty codegenProperty, String dataType) {
222-
if (!useBeanValidation || isEmpty(dataType) || !codegenProperty.isModel || isResponseType(codegenProperty)) {
223-
return dataType;
224-
}
225-
String result = dataType;
226-
if (!dataType.contains("@Valid")) {
227-
result = dataType.replace("<", "<@Valid ");
228-
}
229-
// Use a safer regex to avoid catastrophic backtracking
230-
Matcher m = Pattern.compile("^([^<]+<)(@Valid) ([a-z\\.]+)([A-Z].*)(>)$").matcher(dataType);
231-
if (m.matches()) {
232-
// Set<@Valid com.backbase.dbs.arrangement.commons.model.TranslationItemDto>
233-
result = m.group(1) + m.group(3) + m.group(2) + " " + m.group(4) + m.group(5);
234-
}
235-
return result;
236-
}
237-
238-
// Copied, but not modified
239-
private static boolean isListOrSet(CodegenProperty codegenProperty) {
240-
return codegenProperty.isContainer && !codegenProperty.isMap;
241-
}
242-
243-
// Copied, but not modified
244-
private static boolean isListOrSet(CodegenParameter codegenParameter) {
245-
return codegenParameter.isContainer && !codegenParameter.isMap;
246-
}
247-
248-
// Copied, but not modified
249-
private static boolean isResponseType(CodegenProperty codegenProperty) {
250-
return codegenProperty.baseName.toLowerCase(Locale.ROOT).contains("response");
251-
}
252-
253-
/**
254-
This method has been overridden in order to add a parameter to codegen operation for adding HttpServletRequest to
255-
the service interface. There is a relevant httpServletParam.mustache file.
256-
*/
25792
@Override
25893
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, List<Server> servers) {
259-
if (operation.getExtensions() == null) {
260-
operation.setExtensions(new LinkedHashMap<>());
261-
}
262-
final CodegenOperation codegenOperation = super.fromOperation(path, httpMethod, operation, servers);
94+
CodegenOperation codegenOperation = super.fromOperation(path, httpMethod, operation, servers);
26395
// Remove the standard body parameter (if it exists) ---
26496
// This prevents the generator's default logic from inserting its own request body.
26597
codegenOperation.allParams.removeIf(p -> p.isBodyParam);
266-
if (this.addServletRequest) {
267-
final CodegenParameter codegenParameter = new CodegenParameter();
268-
codegenParameter.paramName = "httpServletRequest";
269-
codegenOperation.allParams.add(codegenParameter);
270-
}
271-
if (codegenOperation.returnType != null) {
272-
codegenOperation.returnType = codegenOperation.returnType.replace("@Valid", "");
273-
}
27498
return codegenOperation;
27599
}
276100

277-
@Override
278-
public String toDefaultValue(CodegenProperty cp, Schema schema) {
279-
final Schema referencedSchema = ModelUtils.getReferencedSchema(this.openAPI, schema);
280-
return getCollectionCodegenValue(cp, referencedSchema, containerDefaultToNull, instantiationTypes())
281-
.map(BoatCodeGenUtils.CodegenValueType::getValue)
282-
.orElseGet(() -> super.toDefaultValue(cp, referencedSchema));
283-
}
284-
285-
@Override
286-
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
287-
288-
super.postProcessModelProperty(model, property);
289-
290-
if (shouldSerializeBigDecimalAsString(property)) {
291-
property.vendorExtensions.put("x-extra-annotation", "@JsonSerialize(using = BigDecimalCustomSerializer.class)");
292-
model.imports.add("BigDecimalCustomSerializer");
293-
model.imports.add("JsonSerialize");
294-
}
295-
}
296-
297-
private boolean shouldSerializeBigDecimalAsString(CodegenProperty property) {
298-
return (serializeBigDecimalAsString && ("decimal".equalsIgnoreCase(property.baseType) || "bigdecimal".equalsIgnoreCase(property.baseType)))
299-
|| (isApiStringFormattedAsNumber(property) && !isDataTypeString(property));
300-
}
301-
302-
private boolean isApiStringFormattedAsNumber(CodegenProperty property) {
303-
return "string".equalsIgnoreCase(property.openApiType) && "number".equalsIgnoreCase(property.dataFormat);
304-
}
305-
306-
private boolean isDataTypeString(CodegenProperty property) {
307-
return Stream.of(property.baseType, property.dataType, property.datatypeWithEnum)
308-
.anyMatch("string"::equalsIgnoreCase);
309-
}
310-
311-
@Override
312-
public void postProcessParameter(CodegenParameter p) {
313-
super.postProcessParameter(p);
314-
if (p.isContainer && !this.reactive) {
315-
p.baseType = p.dataType.replaceAll("^([^<]+)<.+>$", "$1");
316-
}
317-
}
318-
319101
}

0 commit comments

Comments
 (0)