Skip to content

Commit

Permalink
#160 Fix Enum and Lombok generation
Browse files Browse the repository at this point in the history
  • Loading branch information
jemacineiras committed Jun 5, 2023
1 parent 158605b commit 77ef6ab
Show file tree
Hide file tree
Showing 68 changed files with 294 additions and 454 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.5</version>
<version>4.9.0</version>
<packaging>jar</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,42 @@

package com.sngular.api.generator.plugin.openapi;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.sngular.api.generator.plugin.PluginConstants;
import com.sngular.api.generator.plugin.common.tools.ApiTool;
import com.sngular.api.generator.plugin.exception.GeneratedSourcesException;
import com.sngular.api.generator.plugin.exception.GeneratorTemplateException;
import com.sngular.api.generator.plugin.openapi.exception.CodeGenerationException;
import com.sngular.api.generator.plugin.openapi.exception.DuplicateModelClassException;
import com.sngular.api.generator.plugin.openapi.model.*;
import com.sngular.api.generator.plugin.openapi.model.AuthObject;
import com.sngular.api.generator.plugin.openapi.model.GlobalObject;
import com.sngular.api.generator.plugin.openapi.model.PathObject;
import com.sngular.api.generator.plugin.openapi.model.SchemaObject;
import com.sngular.api.generator.plugin.openapi.model.TypeConstants;
import com.sngular.api.generator.plugin.openapi.parameter.SpecFile;
import com.sngular.api.generator.plugin.openapi.template.TemplateFactory;
import com.sngular.api.generator.plugin.openapi.template.TemplateIndexConstants;
import com.sngular.api.generator.plugin.openapi.utils.*;
import com.sngular.api.generator.plugin.openapi.utils.MapperAuthUtil;
import com.sngular.api.generator.plugin.openapi.utils.MapperContentUtil;
import com.sngular.api.generator.plugin.openapi.utils.MapperPathUtil;
import com.sngular.api.generator.plugin.openapi.utils.MapperUtil;
import com.sngular.api.generator.plugin.openapi.utils.OpenApiUtil;
import freemarker.template.TemplateException;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.Path;
import java.util.*;
import java.util.function.Consumer;
import java.util.regex.Pattern;

public class OpenApiGenerator {

private static final String DEFAULT_OPENAPI_API_PACKAGE = PluginConstants.DEFAULT_API_PACKAGE + ".openapi";
Expand All @@ -42,8 +52,6 @@ public class OpenApiGenerator {

private static final Pattern PACKAGE_SEPARATOR = Pattern.compile("\\.");

private static final String ADDITIONAL_PROPERTY_NAME = "AdditionalProperty";

private final Boolean overwriteModel;

private boolean generateExceptionTemplate;
Expand Down Expand Up @@ -244,7 +252,6 @@ private String convertPackageToTargetPath(final String fileSpecPackage, final bo
private void processModels(
final SpecFile specFile, final JsonNode openAPI, final String fileModelToSave, final String modelPackage, final Map<String, JsonNode> basicSchemaMap,
final boolean overwrite) {
final Map<String, JsonNode> additionalPropertiesSchemas = new HashMap<>();

basicSchemaMap.forEach((schemaName, basicSchema) -> {
if (!overwrite && !overwriteModelList.add(schemaName + modelPackage)) {
Expand All @@ -254,39 +261,11 @@ private void processModels(
if (ApiTool.hasRef(basicSchema)) {
final var refSchema = MapperUtil.getRefSchemaName(basicSchema);
writeModel(specFile, openAPI, fileModelToSave, refSchema, basicSchemaMap.get(refSchema));
} else if (ApiTool.hasAdditionalProperties(basicSchema)) {
writeModelWithAdditionalProperties(specFile, openAPI, fileModelToSave, schemaName, basicSchema,
schema -> additionalPropertiesSchemas.put(schemaName + ADDITIONAL_PROPERTY_NAME, schema));
} else if (!ApiTool.isArray(basicSchema) && !TypeConstants.STRING.equalsIgnoreCase(ApiTool.getType(basicSchema))) {
writeModel(specFile, openAPI, fileModelToSave, schemaName, basicSchema);
}
});

if (!additionalPropertiesSchemas.isEmpty()) {
processModels(specFile, openAPI, fileModelToSave, modelPackage, additionalPropertiesSchemas, overwrite);
}
}

private void writeModelRefSchema(final SpecFile specFile, final JsonNode openAPI, final String fileModelToSave, final JsonNode basicSchema) {
final Map<String, JsonNode> properties = new HashMap<>();
final String[] refSplit = ApiTool.getRefValue(basicSchema).split("/");
final String refSchemaName = refSplit[refSplit.length - 1];
properties.put("properties", basicSchema);
final ObjectNode additionalPropertiesSchema = new ObjectNode(JsonNodeFactory.instance, properties);
additionalPropertiesSchema.put("name", StringUtils.uncapitalize(refSchemaName));
additionalPropertiesSchema.put("type", TypeConstants.OBJECT);
writeModel(specFile, openAPI, fileModelToSave, refSchemaName, additionalPropertiesSchema);
}

private void writeModelWithAdditionalProperties(
final SpecFile specFile, final JsonNode openAPI, final String fileModelToSave, final String schemaName, final JsonNode basicSchema,
final Consumer<JsonNode> addAdditionalSchema) {

final var embeddedNode = ApiTool.getAdditionalProperties(basicSchema);
if (ApiTool.isObject(embeddedNode) && !ApiTool.isArray(basicSchema) && (ApiTool.hasRef(embeddedNode))) {
addAdditionalSchema.accept(embeddedNode);
}
writeModel(specFile, openAPI, fileModelToSave, schemaName, basicSchema);
}

private void writeModel(final SpecFile specFile, final JsonNode openAPI, final String fileModelToSave, final String schemaName, final JsonNode basicSchema) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@

package com.sngular.api.generator.plugin.openapi.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SchemaObject {

private boolean isEnum;

private String schemaName;

private String className;
Expand All @@ -34,6 +36,8 @@ public class SchemaObject {

public static final class SchemaObjectBuilder {

private boolean isEnum = false;

private final List<String> importList = new ArrayList<>();

private final Set<SchemaFieldObject> fieldObjectList = new HashSet<>();
Expand All @@ -57,5 +61,10 @@ public SchemaObjectBuilder fieldObject(final SchemaFieldObject fieldObject) {
this.fieldObjectList.add(fieldObject);
return this;
}

public SchemaObjectBuilder isEnum(final boolean isEnum) {
this.isEnum = isEnum;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ClasspathTemplateLoader implements TemplateLoader {
TemplateIndexConstants.TEMPLATE_CONTENT_SCHEMA_LOMBOK, TemplateIndexConstants.TEMPLATE_CALL_WEB_API,
TemplateIndexConstants.TEMPLATE_WEB_CLIENT, TemplateIndexConstants.TEMPLATE_CALL_REST_API,
TemplateIndexConstants.TEMPLATE_REST_CLIENT, TemplateIndexConstants.TEMPLATE_REACTIVE_API,
TemplateIndexConstants.TEMPLATE_MODEL_EXCEPTION);
TemplateIndexConstants.TEMPLATE_MODEL_EXCEPTION, TemplateIndexConstants.TEMPLATE_CONTENT_ENUM_SCHEMA);

private static final List<String> TEMPLATE_AUTH_FILES = List.of(TemplateIndexConstants.TEMPLATE_API_KEY, TemplateIndexConstants.TEMPLATE_AUTHENTICATION,
TemplateIndexConstants.TEMPLATE_HTTP_BASIC, TemplateIndexConstants.TEMPLATE_HTTP_BEARER,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public final void fillTemplateSchema(final String filePathToSave, final Boolean
if (Objects.nonNull(schemaObject.getFieldObjectList()) && !schemaObject.getFieldObjectList().isEmpty()) {
root.put("schema", schemaObject);
final String pathToSaveMainClass = fileToSave.toPath().resolve(schemaObject.getClassName() + JAVA_EXTENSION).toString();
writeTemplateToFile(null != useLombok && useLombok ? TemplateIndexConstants.TEMPLATE_CONTENT_SCHEMA_LOMBOK : TemplateIndexConstants.TEMPLATE_CONTENT_SCHEMA, root,
pathToSaveMainClass);
writeTemplateToFile(templateSelector(useLombok, schemaObject), root, pathToSaveMainClass);
for (SchemaFieldObject fieldObject : schemaObject.getFieldObjectList()) {
propertiesSet.addAll(fieldObject.getRestrictionProperties().getProperties());
if (fieldObject.isRequired() && Boolean.FALSE.equals(useLombok)) {
Expand All @@ -64,6 +63,22 @@ public final void fillTemplateSchema(final String filePathToSave, final Boolean
}
}

private static String templateSelector(final Boolean useLombok, final SchemaObject schemaObject) {
final var shouldUseLombok = Objects.requireNonNullElse(useLombok, false);
final var shouldUseEnum = schemaObject.isEnum();
final String template;

if (shouldUseEnum) {
template = TemplateIndexConstants.TEMPLATE_CONTENT_ENUM_SCHEMA;
} else if (Boolean.TRUE.equals(shouldUseLombok)) {
template = TemplateIndexConstants.TEMPLATE_CONTENT_SCHEMA_LOMBOK;
} else {
template = TemplateIndexConstants.TEMPLATE_CONTENT_SCHEMA;
}

return template;
}

public final void fillTemplateModelClassException(final String filePathToSave, final boolean overwriteEnabled) throws IOException, TemplateException {
final File fileToSave = new File(filePathToSave);
final Path pathToExceptionPackage = fileToSave.toPath().resolve("exception");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public class TemplateIndexConstants {
public static final String TEMPLATE_UNIQUE_ITEMS_ANNOTATION = "templateUniqueItems.ftlh";

public static final String TEMPLATE_UNIQUE_ITEMS_VALIDATOR_ANNOTATION = "templateUniqueItemsValidator.ftlh";
public static final String TEMPLATE_CONTENT_ENUM_SCHEMA = "templateEnumSchema.ftlh" ;

private TemplateIndexConstants() {}
private TemplateIndexConstants() {}

}
Loading

0 comments on commit 77ef6ab

Please sign in to comment.