Skip to content

Commit

Permalink
fix ModelClassException package (#246)
Browse files Browse the repository at this point in the history
* #229 added test to check correctness of exception

* #229 fixed exception generation

* #229 updated project version

* #229 fixed syntax error
  • Loading branch information
davidgayoso authored May 15, 2023
1 parent 0497bd0 commit 04ef68c
Show file tree
Hide file tree
Showing 37 changed files with 1,565 additions and 284 deletions.
2 changes: 1 addition & 1 deletion multiapi-engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.sngular</groupId>
<artifactId>multiapi-engine</artifactId>
<version>4.8.3</version>
<version>4.8.4</version>
<packaging>jar</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public final void processFileSpec(final List<SpecFile> specsListFile) {
}

setUpTemplate(fileParameter, springBootVersion);
templateFactory.fillTemplates();
templateFactory.fillTemplates(generateExceptionTemplate);
templateFactory.clearData();
} catch (final TemplateException | IOException e) {
e.printStackTrace();
Expand Down Expand Up @@ -546,10 +546,6 @@ private void fillTemplateFactory(
templateFactory.addSchemaObject(modelPackageReceived, className, schemaObject, filePath);
checkRequiredOrCombinatorExists(schemaObject, usingLombok);
}

if (filePath != null && Boolean.TRUE.equals(generateExceptionTemplate)) {
templateFactory.fillTemplateModelClassException(filePath, modelPackage);
}
}

private Pair<String, String> processMethod(final JsonNode channel, final String modelPackage, final FileLocation ymlParent, final String prefix, final String suffix)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import java.util.stream.Collectors;
import com.sngular.api.generator.plugin.asyncapi.MethodObject;
import com.sngular.api.generator.plugin.asyncapi.exception.FileSystemException;
import com.sngular.api.generator.plugin.asyncapi.model.SchemaFieldObject;
Expand All @@ -29,6 +29,7 @@
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import org.apache.commons.collections4.CollectionUtils;

public class TemplateFactory {

Expand Down Expand Up @@ -88,7 +89,7 @@ private void fillTemplate(final String pathToSaveMainClass, final String templat
writeTemplateToFile(templateName, root, pathToSaveMainClass);
}

public final void fillTemplates() throws IOException, TemplateException {
public final void fillTemplates(boolean generateExceptionTemplate) throws IOException, TemplateException {
root.put("publishMethods", publishMethods);
root.put("subscribeMethods", subscribeMethods);
root.put("streamBridgeMethods", streamBridgeMethods);
Expand All @@ -105,10 +106,24 @@ public final void fillTemplates() throws IOException, TemplateException {
fillTemplate(streamBridgeFilePath, streamBridgeClassName, TemplateIndexConstants.TEMPLATE_API_STREAM_BRIDGE, root);
}

final String exceptionPackage;
if (Boolean.TRUE.equals(generateExceptionTemplate)) {
final ClassTemplate finalClassTemplate = getClassTemplate();
if (finalClassTemplate.getFilePath().endsWith("schemas")) {
fillTemplateModelClassException(finalClassTemplate.getFilePath(), finalClassTemplate.getModelPackage() + ".schemas");
exceptionPackage = finalClassTemplate.getModelPackage() + ".schemas";
} else {
fillTemplateModelClassException(finalClassTemplate.getFilePath(), finalClassTemplate.getModelPackage() + ".messages");
exceptionPackage = finalClassTemplate.getModelPackage() + ".messages";
}
} else {
exceptionPackage = null;
}

final HashSet<String> propertiesSet = new HashSet<>();
schemaObjectMap.forEach(classTemplate -> {
try {
fillTemplateSchema(classTemplate, false, propertiesSet);
fillTemplateSchema(classTemplate, false, propertiesSet, exceptionPackage);
} catch (final IOException | TemplateException exception) {
throw new FileSystemException(exception);
}
Expand All @@ -124,6 +139,21 @@ public final void fillTemplates() throws IOException, TemplateException {
this.generateInterfaces();
}

private ClassTemplate getClassTemplate(){
ClassTemplate ourClassTemplate = null;
for (ClassTemplate classTemplate : schemaObjectMap) {
if (classTemplate.getFilePath().endsWith("schemas")) {
ourClassTemplate = classTemplate;
break;
}
}
if (ourClassTemplate == null) {
ourClassTemplate = schemaObjectMap.get(0);
}

return ourClassTemplate;
}

private void fillTemplates(final Path filePathToSave, final String modelPackage, final Set<String> fieldProperties) throws TemplateException, IOException {
for (final String current : fieldProperties) {
switch (current) {
Expand Down Expand Up @@ -172,7 +202,7 @@ private void fillTemplates(final Path filePathToSave, final String modelPackage,
public final void fillTemplateModelClassException(final Path filePathToSave, final String modelPackage) throws IOException, TemplateException {
final Path pathToExceptionPackage = filePathToSave.resolve("exception");
pathToExceptionPackage.toFile().mkdirs();
root.put("packageModel", modelPackage);
root.put("exceptionPackage", modelPackage);
final String pathToSaveMainClass = pathToExceptionPackage.resolve("ModelClassException.java").toString();
writeTemplateToFile(TemplateIndexConstants.TEMPLATE_MODEL_EXCEPTION, root, pathToSaveMainClass);
}
Expand All @@ -189,7 +219,9 @@ public final void fillTemplateCustom(
writeTemplateToFile(templateValidator, root, pathToSaveValidatorClass);
}

private void fillTemplateSchema(final ClassTemplate classTemplate, final Boolean useLombok, final Set<String> propertiesSet) throws IOException, TemplateException {
private void fillTemplateSchema(final ClassTemplate classTemplate, final Boolean useLombok, final Set<String> propertiesSet,
final String exceptionPackage)
throws IOException, TemplateException {
final var schemaObject = classTemplate.getClassSchema();
final var filePath = classTemplate.getFilePath();
if (Objects.nonNull(schemaObject) && Objects.nonNull(schemaObject.getFieldObjectList()) && !schemaObject.getFieldObjectList().isEmpty()) {
Expand All @@ -200,6 +232,10 @@ private void fillTemplateSchema(final ClassTemplate classTemplate, final Boolean
if (Objects.nonNull(classTemplate.getModelPackage())) {
rootSchema.put("packageModel", classTemplate.getModelPackage());
}
if (Objects.nonNull(exceptionPackage)){
rootSchema.put("exceptionPackage", exceptionPackage);
root.put("exceptionPackage", exceptionPackage);
}
fillTemplate(filePath.toString(), schemaObject.getClassName(), templateName, rootSchema);
for (SchemaFieldObject fieldObject : schemaObject.getFieldObjectList()) {
propertiesSet.addAll(fieldObject.getRestrictions().getProperties());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ${packageModel}.exception;
package ${exceptionPackage}.exception;

public class ModelClassException extends RuntimeException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import ${packageModel}.customvalidator.MinItems;
</#list>
<#list schema.fieldObjectList as field>
<#if field.required || schema.schemaCombinator == "anyOf" || schema.schemaCombinator == "oneOf">
import ${packageModel}.exception.ModelClassException;
import ${exceptionPackage}.exception.ModelClassException;
<#break>
</#if>
</#list>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ import lombok.NonNull;
</#if>
</#list>
<#if schema.schemaCombinator == "anyOf" || schema.schemaCombinator == "oneOf">
import ${packageModel}.exception.ModelClassException;
import ${exceptionPackage}.exception.ModelClassException;
</#if>

@Data
Expand Down
Loading

0 comments on commit 04ef68c

Please sign in to comment.