Skip to content

Commit

Permalink
#160 Fix creation problems for AnyOf, OneOf
Browse files Browse the repository at this point in the history
  • Loading branch information
jemacineiras committed Jun 21, 2023
1 parent 4b0f8d9 commit 002806a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import com.sngular.api.generator.plugin.asyncapi.exception.DuplicatedOperationException;
import com.sngular.api.generator.plugin.asyncapi.exception.ExternalRefComponentNotFoundException;
import com.sngular.api.generator.plugin.asyncapi.exception.FileSystemException;
import com.sngular.api.generator.plugin.asyncapi.exception.NonSupportedSchemaException;
import com.sngular.api.generator.plugin.asyncapi.exception.InvalidAsyncAPIException;
import com.sngular.api.generator.plugin.asyncapi.exception.NonSupportedSchemaException;
import com.sngular.api.generator.plugin.asyncapi.model.SchemaObject;
import com.sngular.api.generator.plugin.asyncapi.parameter.OperationParameterObject;
import com.sngular.api.generator.plugin.asyncapi.parameter.SpecFile;
Expand Down Expand Up @@ -343,7 +343,7 @@ private void checkReference(

private void processOperation(
final SpecFile fileParameter, final FileLocation ymlParent, final Entry<String, JsonNode> entry, final JsonNode channel, final String operationId,
final JsonNode channelPayload, final Map<String, JsonNode> totalSchemas) throws IOException, TemplateException {
final JsonNode channelPayload, final Map<String, JsonNode> totalSchemas) throws IOException {
if (isValidOperation(fileParameter.getConsumer(), operationId, channel, SUBSCRIBE, true)) {
final var operationObject = fileParameter.getConsumer();
checkClassPackageDuplicate(operationObject.getClassNamePostfix(), operationObject.getApiPackage());
Expand Down Expand Up @@ -504,15 +504,15 @@ private String evaluatePackage(final OperationParameterObject operation) {

private void processSupplierMethod(
final JsonNode channel, final String modelPackage, final FileLocation ymlParent, final Map<String, JsonNode> totalSchemas, final boolean usingLombok, final String prefix,
final String suffix) throws IOException, TemplateException {
final String suffix) throws IOException {
final Pair<String, String> result = processMethod(channel, modelPackage, ymlParent, prefix, suffix);
fillTemplateFactory(result.getValue(), totalSchemas, usingLombok, suffix, modelPackage);
templateFactory.addSupplierMethod(result.getKey(), result.getValue());
}

private void processStreamBridgeMethod(
final JsonNode channel, final String modelPackage, final FileLocation ymlParent, final String channelName, final Map<String, JsonNode> totalSchemas,
final boolean usingLombok, final String prefix, final String suffix) throws IOException, TemplateException {
final boolean usingLombok, final String prefix, final String suffix) throws IOException {
final Pair<String, String> result = processMethod(channel, Objects.isNull(modelPackage) ? null : modelPackage, ymlParent, prefix, suffix);
final String regex = "[a-zA-Z0-9.\\-]*";
if (!channelName.matches(regex)) {
Expand All @@ -524,23 +524,22 @@ private void processStreamBridgeMethod(

private void processSubscribeMethod(
final JsonNode channel, final String modelPackage, final FileLocation ymlParent, final Map<String, JsonNode> totalSchemas, final boolean usingLombok, final String prefix,
final String suffix) throws IOException, TemplateException {
final String suffix) throws IOException {
final Pair<String, String> result = processMethod(channel, Objects.isNull(modelPackage) ? null : modelPackage, ymlParent, prefix, suffix);
fillTemplateFactory(result.getValue(), totalSchemas, usingLombok, suffix, modelPackage);
templateFactory.addSubscribeMethod(result.getKey(), result.getValue());
}

private void fillTemplateFactory(
final String classFullName, final Map<String, JsonNode> totalSchemas, final boolean usingLombok, final String classSuffix, final String modelPackageReceived)
throws TemplateException, IOException {
final String classFullName, final Map<String, JsonNode> totalSchemas, final boolean usingLombok, final String classSuffix, final String modelPackageReceived) {
final String modelPackage = classFullName.substring(0, classFullName.lastIndexOf("."));
final String parentPackage = modelPackage.substring(modelPackage.lastIndexOf(".") + 1);
final String className = classFullName.substring(classFullName.lastIndexOf(".") + 1);
final JsonNode schemaToBuild = totalSchemas.get((parentPackage + SLASH + className).toUpperCase());

final List<SchemaObject> schemaObjectList = MapperContentUtil.mapComponentToSchemaObject(totalSchemas, className, schemaToBuild, null, classSuffix, parentPackage);

Path filePath = null;
Path filePath;
for (SchemaObject schemaObject : schemaObjectList) {
filePath = processPath(getPath((modelPackageReceived != null ? modelPackageReceived : DEFAULT_ASYNCAPI_API_PACKAGE) + SLASH + schemaObject.getParentPackage()));
templateFactory.addSchemaObject(modelPackageReceived, className, schemaObject, filePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

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

import java.util.List;

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

import java.util.List;

@Data
@Builder
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.sngular.api.generator.plugin.asyncapi.model;

import lombok.Data;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

import lombok.Data;

@Data
public class SchemaFieldObjectProperties {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

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

import java.util.ArrayList;
import java.util.List;

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

import java.util.ArrayList;
import java.util.List;

@Data
@Builder
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@

package com.sngular.api.generator.plugin.asyncapi.template;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

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 @@ -16,13 +30,6 @@
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

public class TemplateFactory {

public static final String SUBSCRIBE_PACKAGE = "subscribePackage";
Expand All @@ -39,6 +46,8 @@ public class TemplateFactory {

public static final String FILE_TYPE_JAVA = ".java";

public static final String EXCEPTION_PACKAGE = "exceptionPackage";

private final Configuration cfg = new Configuration(Configuration.VERSION_2_3_32);

private final Map<String, Object> root = new HashMap<>();
Expand Down Expand Up @@ -81,7 +90,7 @@ private void fillTemplate(final String pathToSaveMainClass, final String templat
writeTemplateToFile(templateName, root, pathToSaveMainClass);
}

public final void fillTemplates(boolean generateExceptionTemplate) throws IOException, TemplateException {
public final void fillTemplates(final boolean generateExceptionTemplate) throws IOException, TemplateException {
root.put("publishMethods", publishMethods);
root.put("subscribeMethods", subscribeMethods);
root.put("streamBridgeMethods", streamBridgeMethods);
Expand Down Expand Up @@ -194,7 +203,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("exceptionPackage", modelPackage);
root.put(EXCEPTION_PACKAGE, modelPackage);
final String pathToSaveMainClass = pathToExceptionPackage.resolve("ModelClassException.java").toString();
writeTemplateToFile(TemplateIndexConstants.TEMPLATE_MODEL_EXCEPTION, root, pathToSaveMainClass);
}
Expand Down Expand Up @@ -226,8 +235,8 @@ private void fillTemplateSchema(
rootSchema.put("packageModel", classTemplate.getModelPackage());
}
if (Objects.nonNull(exceptionPackage)) {
rootSchema.put("exceptionPackage", exceptionPackage);
root.put("exceptionPackage", exceptionPackage);
rootSchema.put(EXCEPTION_PACKAGE, exceptionPackage);
root.put(EXCEPTION_PACKAGE, exceptionPackage);
}
fillTemplate(filePath.toString(), schemaObject.getClassName(), templateName, rootSchema);
for (SchemaFieldObject fieldObject : schemaObject.getFieldObjectList()) {
Expand Down Expand Up @@ -306,7 +315,7 @@ public final void setSubscribeEntitiesSuffix(final String suffix) {
root.put(SUBSCRIBE_ENTITIES_SUFFIX, suffix);
}

public void calculateJavaEEPackage(final Integer springBootVersion) {
public final void calculateJavaEEPackage(final Integer springBootVersion) {
if (3 <= springBootVersion) {
root.put("javaEEPackage", "jakarta");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@

public class MapperContentUtil {

public static final String OBJECT = "object";

public static final String ONE_OF = "oneOf";

public static final String PROPERTIES = "properties";
public static final String REF = "$ref";

public static final String TYPE = "type";

public static final String ALL_OF = "allOf";

public static final String ANY_OF = "anyOf";
Expand All @@ -39,15 +48,6 @@ public class MapperContentUtil {

private static final String MAP = "map";

public static final String OBJECT = "object";

public static final String ONE_OF = "oneOf";

public static final String PROPERTIES = "properties";
public static final String REF = "$ref";

public static final String TYPE = "type";

private static String schemaCombinatorType;

private MapperContentUtil() {}
Expand Down Expand Up @@ -110,7 +110,7 @@ private static void getTypeImports(final HashMap<String, List<String>> listHashM
listHashMap.computeIfAbsent(ARRAY, key -> List.of("java.util.List", "java.util.ArrayList"));
} else if (Objects.equals(fieldObject.getDataTypeSimple(), MAP)) {
listHashMap.computeIfAbsent(MAP, key -> List.of("java.util.Map", "java.util.HashMap"));
} else if (Objects.nonNull(fieldObject.getDataTypeSimple()) && fieldObject.getDataTypeSimple().equals(BIG_DECIMAL)
} else if (fieldObject.getDataTypeSimple().equals(BIG_DECIMAL)
|| Objects.nonNull(fieldObject.getDataType()) && fieldObject.getDataType().equals(BIG_DECIMAL)) {
listHashMap.computeIfAbsent(BIG_DECIMAL, key -> List.of("java.math.BigDecimal"));
}
Expand Down Expand Up @@ -239,7 +239,7 @@ private static SchemaFieldObject processFieldObjectList(
final var name = schema.has("name") ? schema.get("name").textValue() : propertyName;
if (schema.has("type")) {
final var type = schema.get("type").textValue();
if ("object".equalsIgnoreCase(type)) {
if (OBJECT.equalsIgnoreCase(type)) {
fieldObject = SchemaFieldObject.builder().restrictions(new SchemaFieldObjectProperties()).baseName(name).dataType(MapperUtil.getSimpleType(schema, prefix, suffix)).build();
setFieldType(fieldObject, schema, required, prefix, suffix);
} else if (schema.has("items")) {
Expand Down Expand Up @@ -300,7 +300,7 @@ private static void handleItems(final JsonNode schema, final Collection<String>
modelToBuildList.add(MapperUtil.getLongRefClass(items));
}
final Iterator<Map.Entry<String, JsonNode>> iterator = schema.fields();
Entry<String, JsonNode> current = null;
Entry<String, JsonNode> current;
while (iterator.hasNext()) {
current = iterator.next();
switch (current.getKey()) {
Expand Down Expand Up @@ -352,7 +352,7 @@ private static void setFieldProperties(final SchemaFieldObject fieldObject, fina
props.setMinLength(current.getValue().intValue());
break;
case "pattern":
props.setPattern(current.getValue().toString().replaceAll("\"", ""));
props.setPattern(current.getValue().toString().replace("\"", ""));
break;
case "uniqueItems":
props.setUniqueItems(current.getValue().booleanValue());
Expand Down

0 comments on commit 002806a

Please sign in to comment.