Skip to content

Commit

Permalink
271 asyncapi nullpointerexception in mappercontentutiljava (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
jemacineiras committed Aug 23, 2023
1 parent f30f692 commit e829981
Show file tree
Hide file tree
Showing 12 changed files with 557 additions and 95 deletions.
7 changes: 6 additions & 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.9.8</version>
<version>4.9.9</version>
<packaging>jar</packaging>

<properties>
Expand All @@ -29,6 +29,11 @@
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.10.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ 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 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 {
if (isValidOperation(fileParameter.getConsumer(), operationId, channel, SUBSCRIBE, true)) {
final var operationObject = fileParameter.getConsumer();
checkClassPackageDuplicate(operationObject.getClassNamePostfix(), operationObject.getApiPackage());
Expand All @@ -349,12 +349,12 @@ private void processOperation(
}

private boolean isValidOperation(
final OperationParameterObject operation, final String operationId, final JsonNode channel, final String channelType, final boolean excludingOperationExists) {
final OperationParameterObject operation, final String operationId, final JsonNode channel, final String channelType,
final boolean excludingOperationExists) {
final boolean result;
if (operation != null) {
final List<String> operationIds = operation.getOperationIds();
result = operationIds.contains(operationId)
|| operationIds.isEmpty() && channel.has(channelType) && excludingOperationExists;
result = operationIds.contains(operationId) || operationIds.isEmpty() && channel.has(channelType) && excludingOperationExists;
} else {
result = false;
}
Expand Down Expand Up @@ -646,7 +646,7 @@ private String processExternalAvro(final String modelPackage, final FileLocation
final String fullNamespace = fileTree.get("namespace").asText() + PACKAGE_SEPARATOR + fileTree.get("name").asText();
namespace = processModelPackage(fullNamespace, modelPackage);
} catch (final IOException e) {
e.printStackTrace();
throw new FileSystemException(e);
}
return namespace;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import com.sngular.api.generator.plugin.asyncapi.model.SchemaFieldObject;
import com.sngular.api.generator.plugin.asyncapi.model.SchemaFieldObjectProperties;
import com.sngular.api.generator.plugin.asyncapi.model.SchemaObject;
import com.sngular.api.generator.plugin.common.tools.ApiTool;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.WordUtils;

public class MapperContentUtil {

Expand Down Expand Up @@ -63,8 +65,8 @@ public static List<SchemaObject> mapComponentToSchemaObject(
while (!modelToBuildList.isEmpty()) {
final var modelToBuild = modelToBuildList.remove();
final var path = MapperUtil.splitName(modelToBuild);
final var nexElement = buildSchemaObject(totalSchemas, modelToBuild, totalSchemas.get((path[path.length - 2] + "/" + path[path.length - 1]).toUpperCase()),
prefix, suffix, modelToBuildList, path[path.length - 2]);
final var nexElement = buildSchemaObject(totalSchemas, modelToBuild, totalSchemas.get(getComponent(path)),
prefix, suffix, modelToBuildList, getParentName(path));
if (schemasList.contains(nexElement)) {
modelToBuildList.poll();
} else {
Expand All @@ -75,20 +77,40 @@ public static List<SchemaObject> mapComponentToSchemaObject(
return schemasList;
}

private static String getParentName(final String[] path) {
final String parenName;
if (path.length > 1) {
parenName = path[path.length - 2];
} else {
parenName = "";
}
return parenName;
}

private static String getComponent(final String[] path) {
final String componentName;
if (path.length > 1) {
componentName = path[path.length - 2] + "/" + path[path.length - 1];
} else {
componentName = path[0];
}
return componentName.toUpperCase();
}

private static SchemaObject buildSchemaObject(
final Map<String, JsonNode> totalSchemas, final String component, final JsonNode model,
final String prefix, final String suffix, final Collection<String> modelToBuildList, final String parentPackage) {

final var listSchema = getFields(totalSchemas, model, true, prefix, suffix, modelToBuildList);
final var listSchema = getFields(totalSchemas, model, true, prefix, suffix, modelToBuildList, parentPackage);
final var splitPackage = MapperUtil.splitName(component);
final String className = splitPackage[splitPackage.length - 1];
return SchemaObject.builder()
.schemaName(StringUtils.capitalize(className))
.schemaName(WordUtils.capitalizeFully(className))
.className(MapperUtil.getPojoName(className, prefix, suffix))
.importList(getImportList(listSchema))
.schemaCombinator(StringUtils.isNotBlank(schemaCombinatorType) ? schemaCombinatorType : "")
.fieldObjectList(listSchema)
.parentPackage(parentPackage)
.parentPackage(parentPackage.toLowerCase())
.build();
}

Expand Down Expand Up @@ -119,17 +141,17 @@ private static void getTypeImports(final HashMap<String, List<String>> listHashM
}

private static List<SchemaFieldObject> getFields(
final Map<String, JsonNode> totalSchemas, final JsonNode model, final boolean required, final String prefix,
final String suffix, final Collection<String> modelToBuildList) {
final Map<String, JsonNode> totalSchemas, final JsonNode model, final boolean required, final String prefix,
final String suffix, final Collection<String> modelToBuildList, final String parentPackage) {
final var fieldObjectArrayList = new ArrayList<SchemaFieldObject>();
schemaCombinatorType = null;
if (model.has(TYPE)) {
if (ApiTool.hasType(model)) {
if (OBJECT.equalsIgnoreCase(model.get(TYPE).textValue())) {
processFieldObject(totalSchemas, model, prefix, suffix, modelToBuildList, fieldObjectArrayList);
processFieldObject(totalSchemas, model, prefix, suffix, modelToBuildList, fieldObjectArrayList, parentPackage);
} else if (ARRAY.equalsIgnoreCase(model.get(TYPE).textValue())) {
fieldObjectArrayList.add(processFieldObjectList("", model, required, prefix, suffix, modelToBuildList));
fieldObjectArrayList.add(processFieldObjectList(totalSchemas, "", model, required, prefix, suffix, modelToBuildList, parentPackage));
} else if ("enum".equalsIgnoreCase(model.get(TYPE).textValue())) {
fieldObjectArrayList.add(processFieldObjectList("", model, required, prefix, suffix, modelToBuildList));
fieldObjectArrayList.add(processFieldObjectList(totalSchemas, "", model, required, prefix, suffix, modelToBuildList, parentPackage));
}
} else if (model.elements().hasNext()) {
final var fieldsIt = model.fields();
Expand All @@ -139,8 +161,8 @@ private static List<SchemaFieldObject> getFields(
}

private static void processFieldObject(
final Map<String, JsonNode> totalSchemas, final JsonNode model, final String prefix, final String suffix, final Collection<String> modelToBuildList,
final ArrayList<SchemaFieldObject> fieldObjectArrayList) {
final Map<String, JsonNode> totalSchemas, final JsonNode model, final String prefix, final String suffix, final Collection<String> modelToBuildList,
final ArrayList<SchemaFieldObject> fieldObjectArrayList, final String parentPackage) {
final Set<String> requiredSet = new HashSet<>();
if (model.has("required")) {
final JsonNode arrayNode = model.get("required");
Expand All @@ -152,8 +174,8 @@ private static void processFieldObject(
final var propertiesIt = model.get(PROPERTIES).fieldNames();
while (propertiesIt.hasNext()) {
final var property = propertiesIt.next();
fieldObjectArrayList.add(
processFieldObjectList(property, model.get(PROPERTIES).path(property), requiredSet.contains(property), prefix, suffix, modelToBuildList));
fieldObjectArrayList.add(processFieldObjectList(totalSchemas, property, model.get(PROPERTIES).path(property), requiredSet.contains(property), prefix, suffix,
modelToBuildList, parentPackage));
if (model.get(PROPERTIES).path(property).has(REF)) {
modelToBuildList.add(MapperUtil.getLongRefClass(model.get(PROPERTIES).path(property)));
}
Expand Down Expand Up @@ -225,24 +247,34 @@ private static SchemaFieldObject solveElement(
if (element.has(REF)) {
final String schemaName = MapperUtil.getLongRefClass(element);
final var schemaToProcess = totalSchemas.get(schemaName.toUpperCase());
result = processFieldObjectList(schemaName, schemaToProcess, required, prefix, suffix, modelToBuildList);
result = processFieldObjectList(totalSchemas, schemaName, schemaToProcess, required, prefix, suffix, modelToBuildList, null);
result.setRequired(false);
} else {
result = processFieldObjectList("", element, required, prefix, suffix, modelToBuildList);
result = processFieldObjectList(totalSchemas, "", element, required, prefix, suffix, modelToBuildList, null);
}
return result;
}

private static SchemaFieldObject processFieldObjectList(
final String propertyName, final JsonNode schema, final boolean required, final String prefix, final String suffix,
final Collection<String> modelToBuildList) {
final Map<String, JsonNode> totalSchemas, final String propertyName, final JsonNode schema, final boolean required,
final String prefix, final String suffix, final Collection<String> modelToBuildList, final String modelPackage) {
final SchemaFieldObject fieldObject;
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)) {
fieldObject = SchemaFieldObject.builder().restrictions(new SchemaFieldObjectProperties()).baseName(name).dataType(MapperUtil.getSimpleType(schema, prefix, suffix)).build();
fieldObject =
SchemaFieldObject
.builder()
.restrictions(new SchemaFieldObjectProperties())
.baseName(name)
.dataType(MapperUtil.getSimpleType(schema, prefix, suffix))
.build();
setFieldType(fieldObject, schema, required, prefix, suffix);
if (StringUtils.isNotEmpty(propertyName) && !totalSchemas.containsKey(propertyName)) {
totalSchemas.put(createKey(modelPackage, propertyName.toUpperCase(), "/"), schema);
modelToBuildList.add(createKey(modelPackage.toLowerCase(), propertyName, "."));
}
} else if (schema.has("items")) {
final var items = schema.get("items");
final var arrayType = MapperUtil.getSimpleType(items, prefix, suffix);
Expand All @@ -268,7 +300,8 @@ private static SchemaFieldObject processFieldObjectList(
fieldObject = processEnumField(name, required, schema, prefix, suffix);
} else {
fieldObject = SchemaFieldObject
.builder().restrictions(new SchemaFieldObjectProperties()).baseName(name).dataType(MapperUtil.getSimpleType(schema, prefix, suffix))
.builder()
.restrictions(new SchemaFieldObjectProperties()).baseName(name).dataType(MapperUtil.getSimpleType(schema, prefix, suffix))
.dataTypeSimple(MapperUtil.getSimpleType(schema, prefix, suffix))
.build();
setFieldProperties(fieldObject, schema);
Expand All @@ -295,6 +328,10 @@ private static SchemaFieldObject processFieldObjectList(
return fieldObject;
}

private static String createKey(final String modelPackage, final String className, final String separator) {
return Objects.nonNull(modelPackage) ? modelPackage.toUpperCase() + separator + className : className;
}

private static void handleItems(final JsonNode schema, final Collection<String> modelToBuildList, final SchemaFieldObject fieldObject, final boolean required,
final JsonNode items) {
if (items.has("$ref")) {
Expand Down Expand Up @@ -369,26 +406,31 @@ private static void setFieldProperties(final SchemaFieldObject fieldObject, fina

private static void setFieldType(final SchemaFieldObject field, final JsonNode value, final boolean required, final String prefix, final String suffix) {
field.setRequired(required);
if (value.has(TYPE)) {
if (ARRAY.equalsIgnoreCase(value.get(TYPE).textValue())) {
if (ApiTool.hasType(value)) {
if (ARRAY.equalsIgnoreCase(ApiTool.getType(value))) {
final var typeArray = MapperUtil.getTypeArray(value, prefix, suffix);
field.setDataType(typeArray);
field.setImportClass(getImportClass(typeArray));
} else if (value.get(TYPE).textValue().equalsIgnoreCase(OBJECT)) {
} else if (ApiTool.getType(value).equalsIgnoreCase(OBJECT)) {
if (value.has("additionalProperties")) {
final var typeMap = MapperUtil.getTypeMap(value, prefix, suffix);
field.setDataTypeSimple(MAP);
field.setDataType(typeMap);
field.setImportClass(getImportClass(typeMap));
} else {
var typeObject = "";
if (value.has(REF)) {
if (ApiTool.hasRef(value)) {
typeObject = MapperUtil.getRef(value, prefix, suffix);
} else {
typeObject = MapperUtil.getPojoName(field.getBaseName(), prefix, suffix);
}
field.setImportClass(getImportClass(typeObject));
field.setDataType(typeObject);
final String[] path = MapperUtil.splitName(value.get(REF).textValue());
field.setParentPackage(path[path.length - 2]);
field.setDataTypeSimple(typeObject);
if (ApiTool.hasRef(value)) {
final String[] path = MapperUtil.splitName(ApiTool.getRefValue(value));
field.setParentPackage(path[path.length - 2]);
}
}
} else {
throw new NonSupportedSchemaException(value.toPrettyString());
Expand Down
Loading

0 comments on commit e829981

Please sign in to comment.