Skip to content

Commit

Permalink
#160 Fix Templates and white spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
jemacineiras committed Jun 5, 2023
1 parent f6bd580 commit 158605b
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

public final class TypeConstants {

private static final Set<String> BOOLEAN_VALUES = Set.of(Boolean.TRUE.toString(), Boolean.FALSE.toString());

public static final String NUMBER = "number";

public static final String BOOLEAN = "boolean";
Expand Down Expand Up @@ -74,6 +76,10 @@ public final class TypeConstants {
OFFSETDATETIME
);

public static boolean isBoolean(final String isBoolean) {
return BOOLEAN_VALUES.contains(isBoolean.toLowerCase());
}

public enum TimeType {
LOCAL,
ZONED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;

import com.fasterxml.jackson.databind.JsonNode;
import com.sngular.api.generator.plugin.common.tools.ApiTool;
import com.sngular.api.generator.plugin.openapi.exception.BadDefinedEnumException;
Expand All @@ -18,11 +26,6 @@
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.Map.Entry;
import java.util.function.Consumer;


public class MapperContentUtil {

private static final String ADDITIONAL_PROPERTY_NAME = "AdditionalProperty";
Expand All @@ -44,9 +47,9 @@ public static Map<String, SchemaObject> mapComponentToSchemaObject(
}

private static Map<String, SchemaObject> mapComponentToSchemaObject(
final Map<String, JsonNode> totalSchemas, final Map<String, SchemaObject> compositedSchemas,
final List<String> antiLoopList, final JsonNode schema, final String nameSchema, final SpecFile specFile,
final String modelPackage) {
final Map<String, JsonNode> totalSchemas, final Map<String, SchemaObject> compositedSchemas,
final List<String> antiLoopList, final JsonNode schema, final String nameSchema, final SpecFile specFile,
final String modelPackage) {
antiLoopList.add(nameSchema);

final var listSchema = getFields(totalSchemas, schema, specFile, compositedSchemas, antiLoopList);
Expand Down Expand Up @@ -146,7 +149,7 @@ private static List<SchemaFieldObject> getFields(
.getProperties(schema)
.forEachRemaining(processProperties(totalSchemas, compositedSchemas, fieldObjectArrayList, specFile, schema, antiLoopList));
fieldObjectArrayList.addAll(
processFieldObjectList("additionalProperties", null, schema, specFile, totalSchemas, compositedSchemas, antiLoopList));
processAdditionalProperties("additionalProperties", schema, specFile, totalSchemas, compositedSchemas, antiLoopList));
} else {
ApiTool.getProperties(schema).forEachRemaining(processProperties(totalSchemas, compositedSchemas, fieldObjectArrayList, specFile, schema, antiLoopList));
}
Expand Down Expand Up @@ -206,13 +209,15 @@ private static List<SchemaFieldObject> processAnyOfOneOf(
final String[] pathObjectRef = ApiTool.getRefValue(internalSchema).split("/");
final String schemaName = pathObjectRef[pathObjectRef.length - 1];
final var schemaToProcess = totalSchemas.get(schemaName);
if (compositedSchemas.containsKey(schemaName)) {
if (compositedSchemas.containsKey(schemaName) || antiLoopList.contains(schemaName)) {
fieldObjectArrayList.add(SchemaFieldObject
.builder()
.baseName(schemaName)
.dataType(new SchemaFieldObjectType(schemaName))
.dataType(SchemaFieldObjectType.fromTypeList(MapperUtil.getSimpleType(totalSchemas.get(schemaName), specFile),
MapperUtil.getPojoName(schemaName, specFile)))
.build());
} else {
antiLoopList.add(schemaName);
ApiTool.getProperties(schemaToProcess)
.forEachRemaining(processProperties(totalSchemas, compositedSchemas, fieldObjectArrayList, specFile, schemaToProcess, antiLoopList));
}
Expand All @@ -230,28 +235,20 @@ private static List<SchemaFieldObject> processAnyOfOneOf(
private static List<SchemaFieldObject> processFieldObjectList(
final String fieldName, final String className, final JsonNode schema, final SpecFile specFile, final Map<String, JsonNode> totalSchemas,
final Map<String, SchemaObject> compositedSchemas, final List<String> antiLoopList) {
final var fieldObjectArrayList = new LinkedList<SchemaFieldObject>();
final var fieldObjectArrayList = new ArrayList<SchemaFieldObject>();

if (TypeConstants.ARRAY.equalsIgnoreCase(ApiTool.getType(schema))) {
fieldObjectArrayList.addAll(processArray(fieldName, className, schema, specFile, totalSchemas, compositedSchemas, antiLoopList));
} else if (ApiTool.hasAdditionalProperties(schema)) {
fieldObjectArrayList.addAll(processMap(fieldName, schema, specFile, totalSchemas, compositedSchemas, antiLoopList));
} else if (ApiTool.hasRef(schema)) {
final String refSchemaName = getRef(schema, specFile);
final var field = SchemaFieldObject.builder()
.baseName(fieldName)
.dataType(new SchemaFieldObjectType(MapperUtil.getSimpleType(schema, specFile)))
.build();
setFieldType(field, schema, schema, specFile, refSchemaName);
fieldObjectArrayList.add(field);
} else if (!ApiTool.isObject(schema) || !ApiTool.isComposed(schema)) {
final var field = SchemaFieldObject
.builder()
.baseName(fieldName)
.dataType(new SchemaFieldObjectType(MapperUtil.getSimpleType(schema, specFile)))
.build();
setFieldType(field, schema, schema, specFile, "");
fieldObjectArrayList.add(field);
fieldObjectArrayList.add(processRef(fieldName, schema, specFile));
} else if (ApiTool.isObject(schema) && !ApiTool.hasProperties(schema)) {
fieldObjectArrayList.add(SchemaFieldObject
.builder()
.baseName(fieldName)
.dataType(new SchemaFieldObjectType(TypeConstants.OBJECT))
.build());
} else if (!ApiTool.hasProperties(schema) && !ApiTool.isComposed(schema)) {
fieldObjectArrayList.add(SchemaFieldObject
.builder()
Expand Down Expand Up @@ -279,7 +276,17 @@ private static List<SchemaFieldObject> processFieldObjectList(
return fieldObjectArrayList;
}

private static Consumer<Entry<String, JsonNode>> processProperties(
private static SchemaFieldObject processRef(String fieldName, JsonNode schema, SpecFile specFile) {
final String refSchemaName = getRef(schema, specFile);
final var field = SchemaFieldObject.builder()
.baseName(fieldName)
.dataType(new SchemaFieldObjectType(MapperUtil.getSimpleType(schema, specFile)))
.build();
setFieldType(field, schema, schema, specFile, refSchemaName);
return field;
}

private static Consumer<Map.Entry<String, JsonNode>> processProperties(
final Map<String, JsonNode> totalSchemas, final Map<String, SchemaObject> compositedSchemas, final List<SchemaFieldObject> fieldObjectArrayList, final SpecFile specFile,
final JsonNode schema, final List<String> antiLoopList) {
return field -> {
Expand Down Expand Up @@ -499,61 +506,61 @@ private static List<SchemaFieldObject> processMap(
}

if (ApiTool.hasProperties(schema)) {
fieldObjectArrayList.addAll(processAdditionalProperties(fieldName, schema, specFile));
fieldObjectArrayList.addAll(processAdditionalProperties(fieldName, schema, specFile, totalSchemas, compositedSchemas, antiLoopList));
}

return fieldObjectArrayList;
}

private static List<SchemaFieldObject> processAdditionalProperties(final String fieldName, final JsonNode schema, final SpecFile specFile) {
private static List<SchemaFieldObject> processAdditionalProperties(final String fieldName, final JsonNode schema, final SpecFile specFile, Map<String, JsonNode> totalSchemas, Map<String, SchemaObject> compositedSchemas, List<String> antiLoopList) {
final var fieldObjectArrayList = new ArrayList<SchemaFieldObject>();

final var addPropObj = ApiTool.getAdditionalProperties(schema);
if (TypeConstants.BOOLEAN.equalsIgnoreCase(ApiTool.getType(addPropObj))) {
if (TypeConstants.isBoolean(addPropObj.asText())) {
fieldObjectArrayList
.add(SchemaFieldObject
.builder()
.baseName(fieldName)
.dataType(SchemaFieldObjectType.fromTypeList(TypeConstants.MAP, TypeConstants.OBJECT))
.build());
} else if (ApiTool.hasRef(addPropObj)) {
final String refSchemaName = getRef(addPropObj, specFile);
final var field = SchemaFieldObject.builder()
.baseName(fieldName)
.dataType(SchemaFieldObjectType.fromTypeList(TypeConstants.MAP, refSchemaName))
.build();
setFieldType(field, schema, addPropObj, specFile, refSchemaName);
fieldObjectArrayList.add(field);
} else if (ApiTool.hasItems(addPropObj)) {
fieldObjectArrayList
.add(SchemaFieldObject
.builder()
.baseName(fieldName)
.dataType(SchemaFieldObjectType.fromTypeList(TypeConstants.MAP, TypeConstants.ARRAY,
MapperUtil.getSimpleType(ApiTool.getItems(addPropObj), specFile)))
.build());
} else {
final var additionalProperties = ApiTool.getAdditionalProperties(schema);
if (ApiTool.hasRef(additionalProperties)) {
final String refSchemaName = getRef(additionalProperties, specFile);
final var field = SchemaFieldObject.builder()
.baseName(fieldName)
.dataType(SchemaFieldObjectType.fromTypeList(TypeConstants.MAP, refSchemaName))
.build();
setFieldType(field, schema, additionalProperties, specFile, refSchemaName);
fieldObjectArrayList.add(field);
} else if (ApiTool.hasItems(additionalProperties)) {
fieldObjectArrayList
.add(SchemaFieldObject
.builder()
.baseName("additionalProperties")
.dataType(SchemaFieldObjectType.fromTypeList(TypeConstants.MAP, TypeConstants.ARRAY, MapperUtil.getSimpleType(ApiTool.getItems(additionalProperties), specFile)))
.build());
} else {
final String type = isBasicType(additionalProperties) ? MapperUtil.getSimpleType(additionalProperties, specFile)
: MapperUtil.getPojoName(ApiTool.getName(schema) + ADDITIONAL_PROPERTY_NAME, specFile);
fieldObjectArrayList
.add(SchemaFieldObject
.builder()
.baseName(fieldName)
.dataType(SchemaFieldObjectType.fromTypeList(TypeConstants.MAP, type))
.build());
}
final String type = isBasicType(addPropObj) ? MapperUtil.getSimpleType(addPropObj, specFile)
: MapperUtil.getPojoName(ApiTool.getName(schema) + ADDITIONAL_PROPERTY_NAME, specFile);
fieldObjectArrayList
.add(SchemaFieldObject
.builder()
.baseName(fieldName)
.dataType(SchemaFieldObjectType.fromTypeList(TypeConstants.MAP, type))
.build());
}

return fieldObjectArrayList;
}

private static boolean isBasicType(final JsonNode value) {
return !(ApiTool.isObject(value) || ApiTool.isArray(value) || ApiTool.isComposed(value) || ApiTool.hasAdditionalProperties(value));
return !(ApiTool.isObject(value) || ApiTool.isArray(value) || ApiTool.isComposed(value)
|| ApiTool.hasAdditionalProperties(value));
}

private static SchemaObject createComposedSchema(
final String fieldName, final JsonNode schema, final SpecFile specFile, final Map<String, JsonNode> totalSchemas, final Map<String, SchemaObject> compositedSchemas,
final String fieldName, final JsonNode schema, final SpecFile specFile, final Map<String, JsonNode> totalSchemas,
final Map<String, SchemaObject> compositedSchemas,
final List<String> antiLoopList) {
final var fieldObjectArrayList = new ArrayList<SchemaFieldObject>();
String schemaCombinatorType = "";
Expand All @@ -580,7 +587,8 @@ private static SchemaObject createComposedSchema(
.build();
}

private static void setFieldType(final SchemaFieldObject field, final JsonNode value, final JsonNode schema, final SpecFile specFile, final String key) {
private static void setFieldType(final SchemaFieldObject field, final JsonNode value, final JsonNode schema,
final SpecFile specFile, final String key) {
field.setRequired(ApiTool.hasRequired(schema) && ApiTool.checkIfRequired(schema, key));
if (ApiTool.isArray(value)) {
final String typeArray;
Expand All @@ -596,10 +604,6 @@ private static void setFieldType(final SchemaFieldObject field, final JsonNode v
field.setDataType(SchemaFieldObjectType.fromTypeList(TypeConstants.MAP, typeObject));
field.setImportClass(getImportClass(typeObject));

/* final var typeMap = MapperUtil.getTypeMap((MapSchema) value, specFile);
field.setDataType(SchemaFieldObjectType.fromTypeList(TypeConstants.MAP, typeMap));
field.setImportClass(getImportClass(typeMap));*/

} else if (ApiTool.isObject(value)) {
var typeObject = ApiTool.getType(value);
if (ApiTool.hasRef(value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

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

import com.sngular.api.generator.plugin.openapi.model.TypeConstants.TimeType;
import com.sngular.api.generator.plugin.openapi.parameter.SpecFile;
import com.sngular.api.generator.test.utils.TestUtils;
import org.apache.commons.collections4.CollectionUtils;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.io.IOException;
Expand All @@ -18,7 +15,10 @@
import java.util.List;
import java.util.function.Function;

import static org.assertj.core.api.Assertions.assertThat;
import com.sngular.api.generator.plugin.openapi.model.TypeConstants.TimeType;
import com.sngular.api.generator.plugin.openapi.parameter.SpecFile;
import com.sngular.api.generator.test.utils.TestUtils;
import org.apache.commons.collections4.CollectionUtils;

public final class OpenApiGeneratorFixtures {

Expand Down Expand Up @@ -518,7 +518,7 @@ static Function<Path, Boolean> validateComplexAnyOf() {
ASSETS_PATH + "ApiArrayFieldDTO.java",
ASSETS_PATH + "ApiBooleanFieldDTO.java",
ASSETS_PATH + "ApiDateFieldDTO.java",
// ASSETS_PATH + "ApiDefaultItemDTO.java",
ASSETS_PATH + "ApiDefaultItemDTO.java",
ASSETS_PATH + "ApiEnumFieldDTO.java",
ASSETS_PATH + "ApiMapFieldDTO.java",
ASSETS_PATH + "ApiNumberFieldDTO.java",
Expand Down
Loading

0 comments on commit 158605b

Please sign in to comment.