Skip to content

Commit

Permalink
Improve reference (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
jemacineiras authored Nov 2, 2024
1 parent 74d836e commit b693f1f
Show file tree
Hide file tree
Showing 25 changed files with 502 additions and 89 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>6.0.2</version>
<version>6.0.3</version>
<packaging>jar</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,8 @@ private String processMessageRef(final JsonNode messageBody, final String modelP
final String messageContent = ApiTool.getRefValue(messageBody);
if (messageContent.startsWith("#")) {
namespace = processModelPackage(MapperUtil.getLongRefClass(messageBody), modelPackage);
} else if (messageContent.contains("#")) {
} else if (messageContent.contains("#") || StringUtils.endsWith(messageContent, "yml")
|| StringUtils.endsWith(messageContent, "yaml") || StringUtils.endsWith(messageContent, "json")) {
namespace = processExternalRef(modelPackage, ymlParent, messageBody);
} else {
namespace = processExternalAvro(ymlParent, messageContent);
Expand Down Expand Up @@ -538,16 +539,20 @@ private String processExternalAvro(final FileLocation ymlParent, final String me
private String processExternalRef(final String modelPackage, final FileLocation ymlParent, final JsonNode message) throws IOException {
final String[] pathToFile = message.get(REF).asText().split("#");
final String filePath = pathToFile[0];
final String componentPath = pathToFile[1];
final String component;
final String[] path = MapperUtil.splitName(componentPath);
component = path[path.length - 2] + SLASH + path[path.length - 1];

final JsonNode node = ApiTool.nodeFromFile(ymlParent, filePath, FactoryTypeEnum.YML);
if (Objects.nonNull(node.findValue(path[path.length - 2]).get(path[path.length - 1]))) {
return processModelPackage(component, modelPackage);
if (pathToFile.length > 1) {
final String componentPath = pathToFile[1];
final String component;
final String[] path = MapperUtil.splitName(componentPath);
component = path[path.length - 2] + SLASH + path[path.length - 1];

if (Objects.nonNull(node.findValue(path[path.length - 2]).get(path[path.length - 1]))) {
return processModelPackage(component, modelPackage);
} else {
throw new ExternalRefComponentNotFoundException(component, filePath);
}
} else {
throw new ExternalRefComponentNotFoundException(component, filePath);
return processModelPackage(MapperUtil.getNameFromFile(filePath), modelPackage);
}
}

Expand All @@ -570,18 +575,7 @@ private void processKafkaBindings(final ProcessBindingsResultBuilder bindingsRes
}
}

private String capitalizeWithPrefix(final String name) {
final StringBuilder response = new StringBuilder();
if (name.contains(SLASH)) {
final var splitPackage = MapperUtil.splitName(name);
for (int i = 0; i < splitPackage.length; i++) {
response.append(PACKAGE_SEPARATOR_STR).append(i < splitPackage.length - 1 ? splitPackage[i] : StringUtils.capitalize(splitPackage[i]));
}
} else {
response.append(PACKAGE_SEPARATOR_STR).append(StringUtils.capitalize(name));
}
return response.toString();
}


private String processModelPackage(final String extractedPackage, final String modelPackage) {
final String processedPackage;
Expand All @@ -591,15 +585,15 @@ private String processModelPackage(final String extractedPackage, final String m
final var className = splitPackage[splitPackage.length - 1];
processedPackage = modelPackage + PACKAGE_SEPARATOR_STR + StringUtils.capitalize(className);
} else {
processedPackage = modelPackage + capitalizeWithPrefix(extractedPackage);
processedPackage = modelPackage + MapperUtil.capitalizeWithPrefix(extractedPackage);
}
} else if (extractedPackage.contains(PACKAGE_SEPARATOR_STR)) {
final var splitPackage = MapperUtil.splitName(extractedPackage);
final var className = splitPackage[splitPackage.length - 1];
processedPackage =
StringUtils.join(PACKAGE_SEPARATOR_STR, Arrays.spliterator(splitPackage, 0, splitPackage.length)) + PACKAGE_SEPARATOR_STR + StringUtils.capitalize(className);
} else {
processedPackage = DEFAULT_ASYNCAPI_MODEL_PACKAGE + capitalizeWithPrefix(extractedPackage);
processedPackage = DEFAULT_ASYNCAPI_MODEL_PACKAGE + MapperUtil.capitalizeWithPrefix(extractedPackage);
}

return processedPackage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.sngular.api.generator.plugin.common.tools.ApiTool;
import com.sngular.api.generator.plugin.common.tools.MapperUtil;
import lombok.Builder;
import org.apache.commons.lang3.StringUtils;

public final class ReferenceProcessor {

Expand Down Expand Up @@ -78,11 +79,15 @@ private JsonNode solveRef(final FileLocation ymlParent, final String[] path, fin

if (filePath.endsWith(YML) || filePath.endsWith(JSON) || filePath.endsWith(YAML)) {
final JsonNode node = ApiTool.nodeFromFile(ymlParent, filePath, FactoryTypeEnum.YML);
if (node.findValue(path[path.length - 2]).has(path[path.length - 1])) {
returnNode = node.findValue(path[path.length - 2]).get(path[path.length - 1]);
checkReference(node, returnNode);
if (StringUtils.contains(reference, "#")) {
if (node.findValue(path[path.length - 2]).has(path[path.length - 1])) {
returnNode = node.findValue(path[path.length - 2]).get(path[path.length - 1]);
checkReference(node, returnNode);
} else {
throw new NonSupportedSchemaException(node.toPrettyString());
}
} else {
throw new NonSupportedSchemaException(node.toPrettyString());
returnNode = node;
}
} else if (filePath.endsWith(AVSC)) {
returnNode = ApiTool.nodeFromFile(ymlParent, filePath, FactoryTypeEnum.AVRO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ public class MapperUtil {

private static final String REF = "$ref";

private static final String DIVISOR = "([./])";
private static final String[] DIVISOR = {"/", "-", "_"};

private static final String SLASH = "/";

private static final String PACKAGE_SEPARATOR_STR = ".";

private MapperUtil() {}

public static String getSimpleType(final JsonNode schema, final CommonSpecFile specFile) {
Expand All @@ -29,7 +31,7 @@ public static String getSimpleType(final JsonNode schema, final CommonSpecFile s
if (checkIfNumber(nodeType)) {
type = processNumber(schema);
} else if (ApiTool.hasRef(schema)) {
type = getPojoName(getRefSchemaName(schema), specFile);
type = getPojoName(getRefSchemaName(schema, null), specFile);
} else if (TypeConstants.ARRAY.equalsIgnoreCase(nodeType)) {
type = TypeConstants.ARRAY;
} else {
Expand All @@ -39,16 +41,24 @@ public static String getSimpleType(final JsonNode schema, final CommonSpecFile s
}

public static String[] splitName(final String name) {
return ArrayUtils.removeAllOccurrences(name.split(DIVISOR), "");
return ArrayUtils.removeAllOccurrences(name.split("\\W+"), "");
}

public static String packageToFolder(final String packageName) {
return StringUtils.replace(packageName, ".", SLASH);
}

public static String getRefSchemaName(final JsonNode parameter) {
public static String getRefSchemaName(final JsonNode parameter, String defaultSchemaName) {
final String[] pathObjectRef = ApiTool.getRefValue(parameter).split("/");
return pathObjectRef[pathObjectRef.length - 1];
final String schemaName;
if (pathObjectRef[pathObjectRef.length - 1].contains(".yml")
|| pathObjectRef[pathObjectRef.length - 1].contains(".yaml")
|| pathObjectRef[pathObjectRef.length - 1].contains(".json")) {
schemaName = StringUtils.defaultIfEmpty(defaultSchemaName, "");
} else {
schemaName = pathObjectRef[pathObjectRef.length - 1];
}
return schemaName;
}

public static String getRefSchemaKey(final JsonNode parameter) {
Expand Down Expand Up @@ -109,7 +119,7 @@ public static String getTypeArray(final JsonNode array, final CommonSpecFile spe
} else if (ApiTool.isNumber(ApiTool.getItems(array))) {
typeArray = ApiTool.getNumberType(ApiTool.getItems(array));
} else if (ApiTool.hasRef(ApiTool.getItems(array))) {
typeArray = getPojoName(MapperUtil.getRefSchemaName(ApiTool.getItems(array)), specFile);
typeArray = getPojoName(MapperUtil.getRefSchemaName(ApiTool.getItems(array), null), specFile);
}
return typeArray;
}
Expand All @@ -120,10 +130,17 @@ public static String getPojoName(final String namePojo, final CommonSpecFile spe
+ (StringUtils.isNotBlank(specFile.getModelNameSuffix()) ? specFile.getModelNameSuffix() : "");
}

public static String getRef(final JsonNode schema, final CommonSpecFile specFile) {
final String typeObject;
typeObject = getPojoName(getRefSchemaName(schema), specFile);
return typeObject;
public static String calculatePrefixName(final String namePojo, final CommonSpecFile specFile) {
return (StringUtils.isNotBlank(specFile.getModelNamePrefix()) ? specFile.getModelNamePrefix() : "")
+ StringUtils.capitalize(namePojo);
}

public static String getPojoNameFromRef(final JsonNode schema, final CommonSpecFile specFile, String defaultPojoName) {
String pojoName = getRefSchemaName(schema, defaultPojoName);
if (!StringUtils.equalsIgnoreCase(pojoName, defaultPojoName)) {
pojoName = getPojoName(pojoName, specFile);
}
return pojoName;
}

public static String getDateType(final JsonNode schema, final CommonSpecFile specFile) {
Expand Down Expand Up @@ -173,4 +190,37 @@ public static String getLongRefClass(final JsonNode schema) {
final String[] pathObjectRef = getStrings(schema);
return pathObjectRef[pathObjectRef.length - 2] + "/" + pathObjectRef[pathObjectRef.length - 1];
}

public static String getNameFromFile(final String filePath) {
return capitalizeFileName(StringUtils
.removeStart(filePath, "./")
.substring(0, filePath.lastIndexOf('.') - 2)
.replace("\\/", "."));
}

public static String capitalizeWithPrefix(final String name) {
final StringBuilder response = new StringBuilder();
if (StringUtils.containsAny(name, DIVISOR)) {
final var splitPackage = MapperUtil.splitName(name);
for (int i = 0; i < splitPackage.length; i++) {
response.append(PACKAGE_SEPARATOR_STR).append(i < splitPackage.length - 1 ? splitPackage[i] : StringUtils.capitalize(splitPackage[i]));
}
} else {
response.append(PACKAGE_SEPARATOR_STR).append(StringUtils.capitalize(name));
}
return response.toString();
}

public static String capitalizeFileName(final String name) {
final StringBuilder response = new StringBuilder();
if (StringUtils.containsAny(name, DIVISOR)) {
final var splitPackage = MapperUtil.splitName(name);
for (String s : splitPackage) {
response.append(StringUtils.capitalize(s));
}
} else {
response.append(StringUtils.capitalize(name));
}
return response.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ public static SchemaObject buildSchemaObject(

antiLoopList.add(WordUtils.capitalizeFully(className));
final var schemaBuilder = SchemaObject.builder()
.schemaName(WordUtils.capitalizeFully(className))
.className(MapperUtil.getPojoName(className, specFile));
.schemaName(WordUtils.capitalizeFully(className));
final var calculatedInlinePrefix = MapperUtil.calculatePrefixName("Inline", specFile);
if (!StringUtils.startsWith(className, calculatedInlinePrefix)) {
schemaBuilder.className(MapperUtil.getPojoName(className, specFile));
} else {
schemaBuilder.className(className);
}

if (!ApiTool.isEnum(model)) {
final var listSchema = getFields(null, totalSchemas, model, specFile, compositedSchemas, antiLoopList, WordUtils.capitalizeFully(className), baseDir);
Expand Down Expand Up @@ -148,7 +153,7 @@ private static Set<SchemaFieldObject> getFields(final String buildingSchema,
baseDir));
}
} else if (TypeConstants.ARRAY.equalsIgnoreCase(ApiTool.getType(schema))) {
final String itemType = ApiTool.hasRef(ApiTool.getItems(schema)) ? MapperUtil.getRef(ApiTool.getItems(schema), specFile) : ApiTool.getType(ApiTool.getItems(schema));
final String itemType = ApiTool.hasRef(ApiTool.getItems(schema)) ? MapperUtil.getPojoNameFromRef(ApiTool.getItems(schema), specFile, null) : ApiTool.getType(ApiTool.getItems(schema));
fieldObjectArrayList.add(SchemaFieldObject.builder()
.baseName("items")
.dataType(SchemaFieldObjectType.fromTypeList(TypeConstants.ARRAY, itemType))
Expand Down Expand Up @@ -247,7 +252,7 @@ private static List<SchemaFieldObject> processObjectProperty(final String buildi
final var isRequired = ApiTool.checkIfRequired(fieldBody, fieldName);
final SchemaFieldObject field;
if (ApiTool.hasRef(fieldBody)) {
final var typeName = MapperUtil.getRefSchemaName(fieldBody);
final var typeName = MapperUtil.getRefSchemaName(fieldBody, fieldName);
final var refSchema = totalSchemas.get(MapperUtil.getRefSchemaKey(fieldBody));
if (!antiLoopList.contains(typeName) && Objects.nonNull(refSchema) && ApiTool.hasType(refSchema)
&& ApiTool.hasItems(refSchema) || ApiTool.getRefValue(fieldBody).contains(fieldName)) {
Expand Down Expand Up @@ -515,7 +520,7 @@ private static List<SchemaFieldObject> processAdditionalProperties(
.dataType(SchemaFieldObjectType.fromTypeList(TypeConstants.MAP, TypeConstants.OBJECT))
.build());
} else if (ApiTool.hasRef(addPropObj)) {
final String refSchemaName = MapperUtil.getRef(addPropObj, specFile);
final String refSchemaName = MapperUtil.getPojoNameFromRef(addPropObj, specFile, null);
fieldObjectArrayList.add(processRef(fieldName, addPropObj,
SchemaFieldObjectType.fromTypeList(TypeConstants.MAP, refSchemaName), totalSchemas, compositedSchemas,
antiLoopList, specFile, baseDir));
Expand Down Expand Up @@ -607,7 +612,7 @@ private static void setFieldType(
} else if (ApiTool.isObject(schemaProperty)) {
var typeObject = ApiTool.getType(schemaProperty);
if (ApiTool.hasRef(schemaProperty)) {
typeObject = MapperUtil.getRef(schema, specFile);
typeObject = MapperUtil.getPojoNameFromRef(schema, specFile, null);
}
field.setImportClass(getImportClass(typeObject));
field.getDataType().setDeepType(typeObject);
Expand All @@ -621,7 +626,7 @@ private static String getMapTypeObject(final JsonNode schema, final CommonSpecFi
} else {
final JsonNode additionalProperties = ApiTool.getAdditionalProperties(schema);
if (ApiTool.hasRef(additionalProperties)) {
type = MapperUtil.getRef(additionalProperties, specFile);
type = MapperUtil.getPojoNameFromRef(additionalProperties, specFile, null);
} else if (ApiTool.isObject(schema)) {
final var additionalPropertiesField = SchemaFieldObject
.builder()
Expand Down Expand Up @@ -682,7 +687,7 @@ private static Set<SchemaFieldObject> processAnyOfOneOf(final String buildingSch

for (JsonNode internalSchema : schemaList) {
if (ApiTool.hasRef(internalSchema)) {
final var schemaName = MapperUtil.getRefSchemaName(internalSchema);
final var schemaName = MapperUtil.getRefSchemaName(internalSchema, null);
if (!antiLoopList.contains(schemaName)) {
if (compositedSchemas.containsKey(schemaName)) {
antiLoopList.add(schemaName);
Expand Down Expand Up @@ -720,9 +725,9 @@ private static SchemaFieldObject processRef(
.baseName(fieldName)
.dataType(dataType)
.build();
if (!antiLoopList.contains(MapperUtil.getRefSchemaName(schema))) {
antiLoopList.add(MapperUtil.getRefSchemaName(schema));
final String refSchemaName = MapperUtil.getRef(schema, specFile);
if (!antiLoopList.contains(MapperUtil.getRefSchemaName(schema, fieldName))) {
antiLoopList.add(MapperUtil.getRefSchemaName(schema, fieldName));
final String refSchemaName = MapperUtil.getPojoNameFromRef(schema, specFile, fieldName);
setFieldType(field, schema, schema, specFile, refSchemaName);

solveRef(schema, totalSchemas, compositedSchemas, antiLoopList, specFile, baseDir);
Expand All @@ -737,11 +742,11 @@ private static SchemaObject solveRef(

final var referredSchema = SchemaUtil.solveRef(ApiTool.getRefValue(schema), totalSchemas, baseDir.resolve(specFile.getFilePath()).getParent());

final var schemaObject = buildSchemaObject(totalSchemas, MapperUtil.getRefSchemaName(schema), referredSchema,
antiLoopList, compositedSchemas, MapperUtil.getRefSchemaName(schema), specFile, baseDir);
final var schemaObject = buildSchemaObject(totalSchemas, MapperUtil.getRefSchemaName(schema, null), referredSchema,
antiLoopList, compositedSchemas, MapperUtil.getRefSchemaName(schema, null), specFile, baseDir);
schemaObject.setEnum(ApiTool.isEnum(referredSchema));

compositedSchemas.put(MapperUtil.getRefSchemaName(schema), schemaObject);
compositedSchemas.put(MapperUtil.getRefSchemaName(schema, null), schemaObject);
return schemaObject;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ public static JsonNode solveRef(final String refValue, final Map<String, JsonNod
final var refValueArr = refValue.split("#");
final var filePath = refValueArr[0];
solvedRef = OpenApiUtil.getPojoFromRef(rootFilePath.toAbsolutePath(), filePath);
schemaMap.putAll(ApiTool.getComponentSchemas(solvedRef));
solvedRef = solvedRef.findValue(MapperUtil.getKey(refValueArr[1]));
if (ApiTool.hasComponents(solvedRef)) {
schemaMap.putAll(ApiTool.getComponentSchemas(solvedRef));
solvedRef = solvedRef.findValue(MapperUtil.getKey(refValueArr[1]));
}
}
} else {
solvedRef = null;
Expand Down
Loading

0 comments on commit b693f1f

Please sign in to comment.