Skip to content

Commit

Permalink
265 supplier producer code is generated exclusively (#266)
Browse files Browse the repository at this point in the history
* #262 Fix to String generation.

* #262 Fix to String generation.

* #265 Fix Wrong folder generation
  • Loading branch information
jemacineiras committed Jul 28, 2023
1 parent 6ce3fdd commit d70f7cd
Show file tree
Hide file tree
Showing 163 changed files with 222 additions and 1,714 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ configure the tasks.
In the case that you only want to run one of the goals of the plugin, you only
need to remove the *execution* section that you don't need.

In case no configuration is provided but only the file to generate an exception will be raised and an error will occur.
In the [AsyncApi Generator](#asyncapi-generator) and the
[OpenApi Generator](#openapi-generator) sections, you can find more information
about how they work, and the parameters and configuration options they offer.
Expand Down
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.5</version>
<version>4.9.6</version>
<packaging>jar</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.sngular.api.generator.plugin.common.files.DirectoryFileLocation;
import com.sngular.api.generator.plugin.common.files.FileLocation;
import com.sngular.api.generator.plugin.common.tools.ApiTool;
import com.sngular.api.generator.plugin.exception.InvalidAPIException;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
Expand Down Expand Up @@ -168,8 +169,6 @@ public final void processFileSpec(final List<SpecFile> specsListFile) {
final String operationId = getOperationId(channel);
final JsonNode channelPayload = getChannelPayload(channel);

handleMissingPublisherConsumer(fileParameter, channel, operationId);

processOperation(fileParameter, ymlParent, entry, channel, operationId, channelPayload, totalSchemas);
}

Expand Down Expand Up @@ -198,32 +197,6 @@ public static Pair<InputStream, FileLocation> resolveYmlLocation(final String ym
return new ImmutablePair<>(ymlFile, ymlParentPath);
}

private void handleMissingPublisherConsumer(final SpecFile fileParameter, final JsonNode channel, final String operationId) {
final OperationParameterObject operationParameter = OperationParameterObject
.builder()
.ids(operationId)
.apiPackage(DEFAULT_ASYNCAPI_API_PACKAGE)
.modelPackage(DEFAULT_ASYNCAPI_MODEL_PACKAGE)
.build();
if (channel.has(SUBSCRIBE) && ObjectUtils.allNull(fileParameter.getConsumer(), fileParameter.getStreamBridge())) {
try {
checkClassPackageDuplicate(CONSUMER_CLASS_NAME, DEFAULT_ASYNCAPI_API_PACKAGE);
fileParameter.setConsumer(operationParameter);
} catch (final DuplicateClassException ignored) {
// Don't set consumer
}
}

if (channel.has(PUBLISH) && ObjectUtils.allNull(fileParameter.getSupplier(), fileParameter.getStreamBridge())) {
try {
checkClassPackageDuplicate(SUPPLIER_CLASS_NAME, DEFAULT_ASYNCAPI_API_PACKAGE);
fileParameter.setSupplier(operationParameter);
} catch (final DuplicateClassException ignored) {
// Don't set supplier
}
}
}

private void checkRequiredOrCombinatorExists(final SchemaObject schema, final boolean useLombok) {
if ("anyOf".equals(schema.getSchemaCombinator()) || "oneOf".equals(schema.getSchemaCombinator())) {
generateExceptionTemplate = true;
Expand Down Expand Up @@ -421,9 +394,18 @@ private void setUpTemplate(final SpecFile fileParameter, final Integer springBoo
}

private void processFilePaths(final SpecFile fileParameter) {
templateFactory.setSupplierFilePath(processPath(convertPackageToTargetPath(fileParameter.getSupplier())));
templateFactory.setStreamBridgeFilePath(processPath(convertPackageToTargetPath(fileParameter.getStreamBridge())));
templateFactory.setSubscribeFilePath(processPath(convertPackageToTargetPath(fileParameter.getConsumer())));
var pathToCreate = convertPackageToTargetPath(fileParameter.getSupplier());
if (Objects.nonNull(pathToCreate)) {
templateFactory.setSupplierFilePath(processPath(pathToCreate));
}
pathToCreate = convertPackageToTargetPath(fileParameter.getStreamBridge());
if (Objects.nonNull(pathToCreate)) {
templateFactory.setStreamBridgeFilePath(processPath(pathToCreate));
}
pathToCreate = convertPackageToTargetPath(fileParameter.getConsumer());
if (Objects.nonNull(pathToCreate)) {
templateFactory.setSubscribeFilePath(processPath(pathToCreate));
}
}

private void processEntitiesSuffix(final SpecFile fileParameter) {
Expand Down Expand Up @@ -476,12 +458,13 @@ private Path processPath(final String packagePath) {
}

private String convertPackageToTargetPath(final OperationParameterObject operationParameter) {
final String apiPackage = operationParameter != null ? operationParameter.getApiPackage() : null;
final String path;
if (Objects.nonNull(apiPackage)) {
path = getPath(apiPackage);
} else {
path = getPath(DEFAULT_ASYNCAPI_API_PACKAGE);
String path = null;
if (Objects.nonNull(operationParameter)) {
if (Objects.nonNull(operationParameter.getApiPackage())) {
path = getPath(operationParameter.getApiPackage());
} else {
path = getPath(DEFAULT_ASYNCAPI_API_PACKAGE);
}
}
return path;
}
Expand All @@ -495,9 +478,13 @@ private void processJavaEEPackage(final Integer springBootVersion) {
}

private void processPackage(final SpecFile fileParameter) {
templateFactory.setSupplierPackageName(evaluatePackage(fileParameter.getSupplier()));
templateFactory.setStreamBridgePackageName(evaluatePackage(fileParameter.getStreamBridge()));
templateFactory.setSubscribePackageName(evaluatePackage(fileParameter.getConsumer()));
if (ObjectUtils.anyNotNull(fileParameter.getSupplier(), fileParameter.getStreamBridge(), fileParameter.getConsumer())) {
templateFactory.setSupplierPackageName(evaluatePackage(fileParameter.getSupplier()));
templateFactory.setStreamBridgePackageName(evaluatePackage(fileParameter.getStreamBridge()));
templateFactory.setSubscribePackageName(evaluatePackage(fileParameter.getConsumer()));
} else {
throw new InvalidAPIException("No Configuration provided, nothing will be generated.");
}
}

private String evaluatePackage(final OperationParameterObject operation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public static boolean hasType(final JsonNode schema) {
}

public static boolean hasRef(final JsonNode schema) {
return hasNode(schema, "$ref");
return (hasNode(schema, "$ref") || schema.fieldNames().hasNext()) && schema.fieldNames().next().equals("$ref");
}

public static boolean hasProperties(final JsonNode schema) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class InvalidAPIException extends RuntimeException {

public InvalidAPIException(String message) {
public InvalidAPIException(final String message) {
super(message);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.sngular.api.generator.plugin.openapi.model.SchemaObject;
import com.sngular.api.generator.plugin.openapi.model.TypeConstants;
import com.sngular.api.generator.plugin.openapi.parameter.SpecFile;
import org.apache.commons.collections4.IteratorUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

Expand Down Expand Up @@ -265,6 +264,8 @@ private static List<SchemaFieldObject> processFieldObjectList(
.baseName(fieldName)
.dataType(new SchemaFieldObjectType(TypeConstants.OBJECT))
.build());
} else if (ApiTool.isEnum(schema)) {
fieldObjectArrayList.add(processEnumField(fieldName, schema, specFile, ApiTool.getEnumValues(schema), schema));
} else if (!ApiTool.hasProperties(schema) && !ApiTool.isComposed(schema)) {
fieldObjectArrayList.add(SchemaFieldObject
.builder()
Expand Down Expand Up @@ -347,20 +348,24 @@ private static List<SchemaFieldObject> processObjectProperty(
final SchemaFieldObject field;
if (ApiTool.hasRef(fieldBody)) {
final var typeName = MapperUtil.getRefSchemaName(fieldBody);

if (!antiLoopList.contains(typeName) && totalSchemas.containsKey(typeName) && ApiTool.hasType(totalSchemas.get(typeName))
&& ApiTool.hasItems(totalSchemas.get(typeName)) || ApiTool.getRefValue(fieldBody).contains(fieldName)) {
final var refSchema = totalSchemas.get(typeName);
if (!antiLoopList.contains(typeName) && totalSchemas.containsKey(typeName) && ApiTool.hasType(refSchema)
&& ApiTool.hasItems(refSchema) || ApiTool.getRefValue(fieldBody).contains(fieldName)) {
antiLoopList.add(typeName);
fieldObjectArrayList.addAll(processFieldObjectList(fieldName, typeName, totalSchemas.get(typeName), specFile, totalSchemas, compositedSchemas, antiLoopList, baseDir));
} else if (ApiTool.isEnum(refSchema)) {
fieldObjectArrayList.add(processEnumField(fieldName, refSchema, specFile, ApiTool.getEnumValues(refSchema), schema));
} else {
fieldObjectArrayList.add(SchemaFieldObject
.builder()
.baseName(fieldName)
.required(isRequired)
.dataType(SchemaFieldObjectType.fromTypeList(MapperUtil.getSimpleType(totalSchemas.get(typeName), specFile),
.required(ApiTool.checkIfRequired(schema, fieldName))
.dataType(SchemaFieldObjectType.fromTypeList(MapperUtil.getSimpleType(refSchema, specFile),
MapperUtil.getPojoName(typeName, specFile)))
.build());
}
} else if (ApiTool.isEnum(fieldBody)) {
fieldObjectArrayList.add(processEnumField(fieldName, fieldBody, specFile, ApiTool.getEnumValues(fieldBody), fieldBody));
} else if (TypeConstants.STRING.equalsIgnoreCase(ApiTool.getType(fieldBody))) {
field = processStringProperty(fieldName, fieldBody, specFile);
setFieldType(field, fieldBody, schema, specFile, fieldName);
Expand Down Expand Up @@ -709,8 +714,7 @@ private static SchemaFieldObject processEnumField(
.baseName(key)
.dataType(new SchemaFieldObjectType(TypeConstants.ENUM))
.build();
field.setRequired(schema.has("required")
&& Objects.nonNull(IteratorUtils.find(schema.get("required").elements(), required -> required.textValue().equalsIgnoreCase(key))));
field.setRequired(ApiTool.checkIfRequired(schema, key));
final var dataType = MapperUtil.getSimpleType(value, specFile);
field.getDataType().setDeepType(dataType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ private static BiConsumer<String, JsonNode> createResponseObject(final SpecFile
realResponse = globalObject.getResponseNode(MapperUtil.getRefSchemaName(response)).orElseThrow();
}
final String operationIdWithCap = operationId.substring(0, 1).toUpperCase() + operationId.substring(1);
var content = ApiTool.getNode(realResponse, CONTENT);
final var content = ApiTool.getNode(realResponse, CONTENT);
responseObjects.add(ResponseObject
.builder()
.responseName(responseCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,13 @@ public class ${schema.className} {
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("${schema.className}{");
<#list schema.fieldObjectList as field>
<#list schema.fieldObjectList as field>
<#if field_has_next>
sb.append(" ${field.baseName?uncap_first}:").append(${field.baseName?uncap_first}).append(",");
</#list>
<#else>
sb.append(" ${field.baseName?uncap_first}:").append(${field.baseName?uncap_first});
</#if>
</#list>
sb.append("}");
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,13 @@ public class ${schema.className} {
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("${schema.className}{");
<#list schema.fieldObjectList as field>
<#list schema.fieldObjectList as field>
<#if field_has_next>
sb.append(" ${field.baseName?uncap_first}:").append(${field.baseName?uncap_first}).append(",");
</#list>
<#else>
sb.append(" ${field.baseName?uncap_first}:").append(${field.baseName?uncap_first});
</#if>
</#list>
sb.append("}");
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;

import com.sngular.api.generator.plugin.asyncapi.parameter.SpecFile;
import com.sngular.api.generator.plugin.exception.InvalidAPIException;
import org.assertj.core.api.Assertions;
Expand Down Expand Up @@ -41,7 +42,6 @@ static void setup() {
static Stream<Arguments> fileSpecToProcess() {
return Stream.of(
Arguments.of("TestFileGeneration", AsyncApiGeneratorFixtures.TEST_FILE_GENERATION, AsyncApiGeneratorFixtures.validateTestFileGeneration()),
Arguments.of("TestFileGenerationNoConfig", AsyncApiGeneratorFixtures.TEST_FILE_GENERATION_NO_CONFIG, AsyncApiGeneratorFixtures.validateTestFileGenerationNoConf()),
Arguments.of("TestIssueGeneration", AsyncApiGeneratorFixtures.TEST_ISSUE_GENERATION, AsyncApiGeneratorFixtures.validateTestIssueGeneration()),
Arguments.of("TestFileGenerationIssue", AsyncApiGeneratorFixtures.TEST_FILE_GENERATION_ISSUE, AsyncApiGeneratorFixtures.validateTestFileGenerationIssue()),
Arguments.of("TestFileGenerationExternalAvro", AsyncApiGeneratorFixtures.TEST_FILE_GENERATION_EXTERNAL_AVRO,
Expand Down Expand Up @@ -79,4 +79,10 @@ void testExceptionForTestGenerationWithNoOperationId() {
.isInstanceOf(InvalidAPIException.class);
}

@Test
void testExceptionForTestGenerationWithNoOperationConfiguration() {
Assertions.assertThatThrownBy(() -> asyncApiGenerator.processFileSpec(AsyncApiGeneratorFixtures.TEST_FILE_GENERATION_NO_CONFIG))
.isInstanceOf(InvalidAPIException.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public int hashCode() {
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("DataClientDTO{");
sb.append(" payload:").append(payload).append(",");
sb.append(" payload:").append(payload);
sb.append("}");
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public String toString() {
sb.append(" clientId:").append(clientId).append(",");
sb.append(" clientName:").append(clientName).append(",");
sb.append(" flightNumber:").append(flightNumber).append(",");
sb.append(" test:").append(test).append(",");
sb.append(" test:").append(test);
sb.append("}");
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public int hashCode() {
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("StatusDTO{");
sb.append(" payload:").append(payload).append(",");
sb.append(" payload:").append(payload);
sb.append("}");
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("StatusMsgDTO{");
sb.append(" clientId:").append(clientId).append(",");
sb.append(" status:").append(status).append(",");
sb.append(" status:").append(status);
sb.append("}");
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("CreateOrderMapper{");
sb.append(" order:").append(order).append(",");
sb.append(" waiter:").append(waiter).append(",");
sb.append(" waiter:").append(waiter);
sb.append("}");
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public int hashCode() {
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("OrderCreatedDTO{");
sb.append(" payload:").append(payload).append(",");
sb.append(" payload:").append(payload);
sb.append("}");
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public String toString() {
sb.append(" ref:").append(ref).append(",");
sb.append(" clientRef:").append(clientRef).append(",");
sb.append(" amount:").append(amount).append(",");
sb.append(" lines:").append(lines).append(",");
sb.append(" lines:").append(lines);
sb.append("}");
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("OrderLineDTO{");
sb.append(" ref:").append(ref).append(",");
sb.append(" products:").append(products).append(",");
sb.append(" products:").append(products);
sb.append("}");
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("OrderLineMapper{");
sb.append(" ref:").append(ref).append(",");
sb.append(" products:").append(products).append(",");
sb.append(" products:").append(products);
sb.append("}");
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public String toString() {
sb.append(" ref:").append(ref).append(",");
sb.append(" clientRef:").append(clientRef).append(",");
sb.append(" amount:").append(amount).append(",");
sb.append(" lines:").append(lines).append(",");
sb.append(" lines:").append(lines);
sb.append("}");
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public String toString() {
sb.append(" ref:").append(ref).append(",");
sb.append(" productRef:").append(productRef).append(",");
sb.append(" price:").append(price).append(",");
sb.append(" quantity:").append(quantity).append(",");
sb.append(" quantity:").append(quantity);
sb.append("}");
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public String toString() {
sb.append(" ref:").append(ref).append(",");
sb.append(" productRef:").append(productRef).append(",");
sb.append(" price:").append(price).append(",");
sb.append(" quantity:").append(quantity).append(",");
sb.append(" quantity:").append(quantity);
sb.append("}");
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public String toString() {
sb.append("Waiter{");
sb.append(" ref:").append(ref).append(",");
sb.append(" timestamp:").append(timestamp).append(",");
sb.append(" table:").append(table).append(",");
sb.append(" table:").append(table);
sb.append("}");
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public String toString() {
sb.append("WaiterMapper{");
sb.append(" ref:").append(ref).append(",");
sb.append(" timestamp:").append(timestamp).append(",");
sb.append(" table:").append(table).append(",");
sb.append(" table:").append(table);
sb.append("}");
return sb.toString();
}
Expand Down
Loading

0 comments on commit d70f7cd

Please sign in to comment.