Skip to content

Commit

Permalink
279 asyncapi message payload missing all properties except for refere…
Browse files Browse the repository at this point in the history
…nced schema property (#280)

Remove message Objects creation. Fix folder creation.
  • Loading branch information
jemacineiras committed Aug 31, 2023
1 parent 8e2c9bf commit d48f875
Show file tree
Hide file tree
Showing 174 changed files with 1,517 additions and 2,427 deletions.
4 changes: 4 additions & 0 deletions checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -417,5 +417,9 @@
<property name="files" value="[/\\].*AsyncApiGenerator.java"/>
<property name="checks" value="ClassDataAbstractionCoupling"/>
</module>
<module name="SuppressionSingleFilter">
<property name="files" value="[/\\].*.java"/>
<property name="checks" value="CyclomaticComplexity"/>
</module>

</module>
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.9.12</version>
<version>5.0.0</version>
<packaging>jar</packaging>

<properties>
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ public class InvalidAsyncAPIException extends InvalidAPIException {

private static final String ERROR_MESSAGE = "AsyncApi -> There is at least one operation without operationId";

private static final String ERROR_MESSAGE_WRONG_MESSAGE_BODY = "AsyncApi -> The following operationId : %s has the wrong body";

public InvalidAsyncAPIException() {
super(ERROR_MESSAGE);
}

public InvalidAsyncAPIException(final String operationId) {
super(String.format(ERROR_MESSAGE_WRONG_MESSAGE_BODY, operationId));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@

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

import java.util.Objects;
import com.sngular.api.generator.plugin.asyncapi.util.BindingTypeEnum;
import lombok.Builder;
import lombok.Value;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

@Value
public class MethodObject {

Expand All @@ -35,7 +34,8 @@ public class MethodObject {
String bindingType;

@Builder(toBuilder = true)
public MethodObject(final String operationId, final String classNamespace, final String type, final String channelName, final SchemaObject schemaObject,
public MethodObject(
final String operationId, final String classNamespace, final String type, final String channelName, final SchemaObject schemaObject,
final String keyClassNamespace, final String bindingType) {
this.operationId = operationId;
this.classNamespace = classNamespace.substring(0, classNamespace.lastIndexOf("."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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

import com.fasterxml.jackson.databind.JsonNode;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -21,6 +22,8 @@ public class ProcessMethodResult {

private String namespace;

private JsonNode payload;

private String bindings;

private String bindingType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,4 @@ public class SchemaFieldObject {

private List<String> enumValues;

private String parentPackage;

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class ClassTemplate {

Path filePath;

Path propertiesPath;

String modelPackage;

String className;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

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

import static com.sngular.api.generator.plugin.asyncapi.template.TemplateIndexConstants.TEMPLATE_MESSAGE_WRAPPER;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
Expand All @@ -21,7 +19,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import com.sngular.api.generator.plugin.asyncapi.exception.FileSystemException;
import com.sngular.api.generator.plugin.asyncapi.exception.NonSupportedBindingException;
import com.sngular.api.generator.plugin.asyncapi.model.MethodObject;
Expand Down Expand Up @@ -116,52 +113,31 @@ public final void fillTemplates(final boolean generateExceptionTemplate) throws

final String exceptionPackage;
if (Boolean.TRUE.equals(generateExceptionTemplate)) {
final ClassTemplate finalClassTemplate = getClassTemplate();
if (finalClassTemplate.getFilePath().endsWith("schemas")) {
fillTemplateModelClassException(finalClassTemplate.getFilePath(), finalClassTemplate.getModelPackage() + ".schemas");
exceptionPackage = finalClassTemplate.getModelPackage() + ".schemas";
} else {
fillTemplateModelClassException(finalClassTemplate.getFilePath(), finalClassTemplate.getModelPackage() + ".messages");
exceptionPackage = finalClassTemplate.getModelPackage() + ".messages";
}
exceptionPackage = getClassTemplate().getModelPackage();
} else {
exceptionPackage = null;
}

final HashSet<String> propertiesSet = new HashSet<>();
schemaObjectMap.forEach(classTemplate -> {
try {
fillTemplateSchema(classTemplate, false, propertiesSet, exceptionPackage);
propertiesSet.addAll(fillTemplateSchema(classTemplate, false, exceptionPackage));
} catch (final IOException | TemplateException exception) {
throw new FileSystemException(exception);
}
});

if (!schemaObjectMap.isEmpty()) {
try {
fillTemplates(schemaObjectMap.get(0).getFilePath(), schemaObjectMap.get(0).getModelPackage(), propertiesSet);
fillTemplates(schemaObjectMap.get(0).getPropertiesPath(), schemaObjectMap.get(0).getModelPackage(), propertiesSet);
} catch (IOException | TemplateException e) {
throw new GeneratorTemplateException("Generation Error", e);
}
}
this.generateInterfaces();
}

private ClassTemplate getClassTemplate() {
ClassTemplate ourClassTemplate = null;
for (ClassTemplate classTemplate : schemaObjectMap) {
if (classTemplate.getFilePath().endsWith("schemas")) {
ourClassTemplate = classTemplate;
break;
}
}
if (ourClassTemplate == null) {
ourClassTemplate = schemaObjectMap.get(0);
}

return ourClassTemplate;
}

@SuppressWarnings("checkstyle:CyclomaticComplexity")
private void fillTemplates(final Path filePathToSave, final String modelPackage, final Set<String> fieldProperties) throws TemplateException, IOException {
for (final String current : fieldProperties) {
switch (current) {
Expand Down Expand Up @@ -207,6 +183,21 @@ private void fillTemplates(final Path filePathToSave, final String modelPackage,
}
}

private ClassTemplate getClassTemplate() {
ClassTemplate ourClassTemplate = null;
for (ClassTemplate classTemplate : schemaObjectMap) {
if (classTemplate.getFilePath().endsWith("schemas")) {
ourClassTemplate = classTemplate;
break;
}
}
if (ourClassTemplate == null) {
ourClassTemplate = schemaObjectMap.get(0);
}

return ourClassTemplate;
}

public final void fillTemplateModelClassException(final Path filePathToSave, final String modelPackage) throws IOException, TemplateException {
final Path pathToExceptionPackage = filePathToSave.resolve("exception");
pathToExceptionPackage.toFile().mkdirs();
Expand All @@ -227,9 +218,10 @@ public final void fillTemplateCustom(
writeTemplateToFile(templateValidator, root, pathToSaveValidatorClass);
}

private void fillTemplateSchema(final ClassTemplate classTemplate, final Boolean useLombok, final Set<String> propertiesSet,
final String exceptionPackage)
@SuppressWarnings("checkstyle:CyclomaticComplexity")
private Set<String> fillTemplateSchema(final ClassTemplate classTemplate, final Boolean useLombok, final String exceptionPackage)
throws IOException, TemplateException {
final var propertiesSet = new HashSet<String>();
final var schemaObject = classTemplate.getClassSchema();
final var filePath = classTemplate.getFilePath();
if (Objects.nonNull(schemaObject) && Objects.nonNull(schemaObject.getFieldObjectList()) && !schemaObject.getFieldObjectList().isEmpty()) {
Expand All @@ -252,6 +244,7 @@ private void fillTemplateSchema(final ClassTemplate classTemplate, final Boolean
}
}
}
return propertiesSet;
}

public final void setSubscribePackageName(final String packageName) {
Expand Down Expand Up @@ -331,8 +324,9 @@ public final void addSubscribeMethod(final String operationId, final String clas
.build());
}

public final void addSchemaObject(final String modelPackage, final String keyClassName, final SchemaObject schemaObject, final Path filePath) {
final var builder = ClassTemplate.builder().filePath(filePath).modelPackage(modelPackage).className(schemaObject.getClassName()).classSchema(schemaObject);
public final void addSchemaObject(final String modelPackage, final String keyClassName, final SchemaObject schemaObject, final Path filePath, final Path propertiesPath) {
final var builder = ClassTemplate.builder().filePath(filePath).modelPackage(modelPackage).className(schemaObject.getClassName()).classSchema(schemaObject)
.propertiesPath(propertiesPath);
if (Objects.nonNull(keyClassName)) {
builder.keyClassName(keyClassName);
}
Expand Down Expand Up @@ -373,20 +367,21 @@ public final void clearData() {
streamBridgeClassName = null;
}

public final void fillTemplateWrapper(final Path filePath,
public final void fillTemplateWrapper(
final Path filePath,
final String modelPackage,
final String classFullName,
final String className,
final String keyClassFullName,
final String keyClassName
) throws TemplateException, IOException {
final Map<String, Object> context = Map.of(WRAPPER_PACKAGE, modelPackage,
"classNamespace", classFullName,
"className", className,
"keyNamespace", keyClassFullName,
"keyClassName", keyClassName);
"classNamespace", classFullName,
"className", className,
"keyNamespace", keyClassFullName,
"keyClassName", keyClassName);

writeTemplateToFile(TEMPLATE_MESSAGE_WRAPPER, context, filePath.resolve("MessageWrapper.java").toAbsolutePath().toString());
writeTemplateToFile(TemplateIndexConstants.TEMPLATE_MESSAGE_WRAPPER, context, filePath.resolve("MessageWrapper.java").toAbsolutePath().toString());
}

private void generateInterfaces() throws IOException, TemplateException {
Expand All @@ -405,7 +400,7 @@ private void generateInterfaces() throws IOException, TemplateException {

if (Objects.equals(method.getType(), "publish")) {
fillTemplate(supplierFilePath, "I" + method.getOperationId().substring(0, 1).toUpperCase() + method.getOperationId().substring(1),
checkTemplate(method.getBindingType(), TemplateIndexConstants.TEMPLATE_INTERFACE_SUPPLIERS), interfaceRoot);
checkTemplate(method.getBindingType(), TemplateIndexConstants.TEMPLATE_INTERFACE_SUPPLIERS), interfaceRoot);
} else if (Objects.equals(method.getType(), "subscribe")) {
fillTemplate(subscribeFilePath, "I" + method.getOperationId().substring(0, 1).toUpperCase() + method.getOperationId().substring(1),
checkTemplate(method.getBindingType(), TemplateIndexConstants.TEMPLATE_INTERFACE_CONSUMERS), interfaceRoot);
Expand All @@ -422,13 +417,17 @@ private void writeTemplateToFile(final String templateName, final Map<String, Ob
}

private String checkTemplate(final String bindingType, final String defaultTemplate) {
final String templateName;
switch (BindingTypeEnum.valueOf(bindingType)) {
case NONBINDING:
return defaultTemplate;
templateName = defaultTemplate;
break;
case KAFKA:
return MapperUtil.splitName(defaultTemplate)[0] + TemplateIndexConstants.KAFKA_BINDINGS_FTLH;
templateName = MapperUtil.splitName(defaultTemplate)[0] + TemplateIndexConstants.KAFKA_BINDINGS_FTLH;
break;
default:
throw new NonSupportedBindingException(bindingType);
}
return templateName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ public class TemplateIndexConstants {
public static final String TEMPLATE_UNIQUE_ITEMS_VALIDATOR_ANNOTATION = "templateUniqueItemsValidator.ftlh";

public static final String TEMPLATE_MESSAGE_WRAPPER = "templateMessageWrapper.ftlh";
public static final String KAFKA_BINDINGS_FTLH = "WithKafkaBindings.ftlh";

protected TemplateIndexConstants() {
public static final String KAFKA_BINDINGS_FTLH = "WithKafkaBindings.ftlh";

protected TemplateIndexConstants() {
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package com.sngular.api.generator.plugin.asyncapi.util;

import lombok.Getter;

@Getter
public enum BindingTypeEnum {
NONBINDING("NONBINDING"),
KAFKA("KAFKA");

private final String value;

BindingTypeEnum(String value) { this.value = value; }

public String getValue() { return value; }
BindingTypeEnum(final String value) {
this.value = value;
}

@Override
public String toString() { return String.valueOf(value); }
public String toString() {
return String.valueOf(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.sngular.api.generator.plugin.asyncapi.util;

public enum FactoryTypeEnum {
YML,
AVRO
}
Loading

0 comments on commit d48f875

Please sign in to comment.