Skip to content

Commit

Permalink
311 Fix Enum Generation (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
jemacineiras committed Jan 22, 2024
1 parent 83ae1b0 commit e4922b6
Show file tree
Hide file tree
Showing 17 changed files with 609 additions and 11 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ As commented above, they both could be used at the same time, setting a double
<plugin>
<groupId>com.sngular</groupId>
<artifactId>scs-multiapi-maven-plugin</artifactId>
<version>5.1.0</version>
<version>5.3.2</version>
<executions>
<execution>
<id>asyncapi</id>
Expand Down Expand Up @@ -114,7 +114,7 @@ Apply the plugin in the `build.gradle` file and invoke the task.
```groovy
plugins {
id "java"
id "com.sngular.scs-multiapi-gradle-plugin' version '5.1.0"
id "com.sngular.scs-multiapi-gradle-plugin' version '5.3.2"
openapimodel {
Expand Down Expand Up @@ -153,7 +153,7 @@ which the plugin is designed.
<plugin>
<groupId>com.sngular</groupId>
<artifactId>scs-multiapi-maven-plugin</artifactId>
<version>5.1.0</version>
<version>5.3.2</version>
<executions>
<execution>
<phase>generate-sources</phase>
Expand Down Expand Up @@ -584,7 +584,7 @@ file. Here is an example of a basic configuration:
<plugin>
<groupId>com.sngular</groupId>
<artifactId>scs-multiapi-maven-plugin</artifactId>
<version>5.1.0</version>
<version>5.3.2</version>
<executions>
<execution>
<goals>
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>5.3.1</version>
<version>5.3.2</version>
<packaging>jar</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public class ${schema.className} {
private ${requireFinal (field)}${field.baseName?cap_first} ${calculateSafeName (field.baseName, hasConstValue(field.constValue))}
public enum ${field.baseName?cap_first} {
<#list field.enumValues as value>
${value?upper_case}("${value?no_esc}")<#sep>,
${value?upper_case?replace('[\\W]','_','r')}("${value?no_esc}")<#sep>,
</#list>;

private ${field.dataType?cap_first} value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ public class AsyncApiGeneratorFixtures {
.build()
);

final static List<SpecFile> TEST_RARE_CHARS_GENERATION = List.of(
SpecFile
.builder()
.filePath("src/test/resources/asyncapigenerator/testRareCharsGeneration/event-api.yml")
.consumer(OperationParameterObject.builder()
.ids("subscribeOperationFileGeneration")
.modelNameSuffix("DTO")
.apiPackage("com.sngular.scsplugin.rarecharsgeneration.model.event.consumer")
.modelPackage("com.sngular.scsplugin.rarecharsgeneration.model.event")
.build())
.supplier(OperationParameterObject.builder()
.ids("publishOperationFileGeneration")
.modelNameSuffix("DTO")
.apiPackage("com.sngular.scsplugin.rarecharsgeneration.model.event.producer")
.modelPackage("com.sngular.scsplugin.rarecharsgeneration.model.event")
.build())
.build()
);

final static List<SpecFile> TEST_CUSTOM_VALIDATORS = List.of(
SpecFile
.builder()
Expand Down Expand Up @@ -560,6 +579,37 @@ static Function<Path, Boolean> validateTestReservedWordsGeneration() {
modelTest(path, expectedModelSchemaFiles, DEFAULT_MODEL_SCHEMA_FOLDER);
}

static Function<Path, Boolean> validateTestRareCharsGeneration() {
final String DEFAULT_CONSUMER_FOLDER = "generated/com/sngular/scsplugin/rarecharsgeneration/model/event/consumer";

final String DEFAULT_PRODUCER_FOLDER = "generated/com/sngular/scsplugin/rarecharsgeneration/model/event/producer";

final String DEFAULT_MODEL_SCHEMA_FOLDER = "generated/com/sngular/scsplugin/rarecharsgeneration/model/event";

final String COMMON_PATH = "asyncapigenerator/testRareCharsGeneration/";

final String ASSETS_PATH = COMMON_PATH + "assets/";

final List<String> expectedConsumerFiles = List.of(
ASSETS_PATH + "ISubscribeOperationFileGeneration.java",
ASSETS_PATH + "Subscriber.java");

final List<String> expectedProducerFiles = List.of(
ASSETS_PATH + "IPublishOperationFileGeneration.java",
ASSETS_PATH + "Producer.java");

final List<String> expectedModelSchemaFiles = List.of(
ASSETS_PATH + "CreateOrderDTO.java",
ASSETS_PATH + "OrderDTO.java",
ASSETS_PATH + "WaiterDTO.java"
);

final List<String> expectedExceptionFiles = List.of();

return (path) -> commonTest(path, expectedConsumerFiles, expectedProducerFiles, DEFAULT_CONSUMER_FOLDER, DEFAULT_PRODUCER_FOLDER, expectedExceptionFiles, null) &&
modelTest(path, expectedModelSchemaFiles, DEFAULT_MODEL_SCHEMA_FOLDER);
}

static Function<Path, Boolean> validateCustomValidators(int springBootVersion) {
final String DEFAULT_CONSUMER_FOLDER = "generated/com/sngular/scsplugin/customvalidator/model/event/consumer";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ static Stream<Arguments> fileSpecToProcess() {
Arguments.of("TestFileGeneration", AsyncApiGeneratorFixtures.TEST_FILE_GENERATION, AsyncApiGeneratorFixtures.validateTestFileGeneration()),
Arguments.of("TestIssueGeneration", AsyncApiGeneratorFixtures.TEST_ISSUE_GENERATION, AsyncApiGeneratorFixtures.validateTestIssueGeneration()),
Arguments.of("TestReservedWordsGeneration", AsyncApiGeneratorFixtures.TEST_RESERVED_WORDS_GENERATION, AsyncApiGeneratorFixtures.validateTestReservedWordsGeneration()),
Arguments.of("TestRareCharsGeneration", AsyncApiGeneratorFixtures.TEST_RARE_CHARS_GENERATION, AsyncApiGeneratorFixtures.validateTestRareCharsGeneration()),
Arguments.of("TestFileGenerationIssue", AsyncApiGeneratorFixtures.TEST_FILE_GENERATION_ISSUE, AsyncApiGeneratorFixtures.validateTestFileGenerationIssue()),
Arguments.of("TestFileGenerationExternalAvro", AsyncApiGeneratorFixtures.TEST_FILE_GENERATION_EXTERNAL_AVRO,
AsyncApiGeneratorFixtures.validateTestFileGenerationExternalAvro()),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.sngular.scsplugin.rarecharsgeneration.model.event;

import java.util.Objects;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;

@JsonDeserialize(builder = CreateOrderDTO.CreateOrderDTOBuilder.class)
public class CreateOrderDTO {

@JsonProperty(value ="order")
private OrderDTO order;
@JsonProperty(value ="waiter")
private WaiterDTO waiter;

private CreateOrderDTO(CreateOrderDTOBuilder builder) {
this.order = builder.order;
this.waiter = builder.waiter;

}

public static CreateOrderDTO.CreateOrderDTOBuilder builder() {
return new CreateOrderDTO.CreateOrderDTOBuilder();
}

@JsonPOJOBuilder(buildMethodName = "build", withPrefix = "")
public static class CreateOrderDTOBuilder {

private OrderDTO order;

private WaiterDTO waiter;

public CreateOrderDTO.CreateOrderDTOBuilder order(OrderDTO order) {
this.order = order;
return this;
}

public CreateOrderDTO.CreateOrderDTOBuilder waiter(WaiterDTO waiter) {
this.waiter = waiter;
return this;
}

public CreateOrderDTO build() {
CreateOrderDTO createOrderDTO = new CreateOrderDTO(this);
return createOrderDTO;
}
}

/**
* Get order
* @return order
*/
@Schema(name = "order", required = false)
public OrderDTO getOrder() {
return order;
}
public void setOrder(OrderDTO order) {
this.order = order;
}

/**
* Get waiter
* @return waiter
*/
@Schema(name = "waiter", required = false)
public WaiterDTO getWaiter() {
return waiter;
}
public void setWaiter(WaiterDTO waiter) {
this.waiter = waiter;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
CreateOrderDTO createOrderDTO = (CreateOrderDTO) o;
return Objects.equals(this.order, createOrderDTO.order) && Objects.equals(this.waiter, createOrderDTO.waiter);
}

@Override
public int hashCode() {
return Objects.hash(order, waiter);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("CreateOrderDTO{");
sb.append(" order:").append(order).append(",");
sb.append(" waiter:").append(waiter);
sb.append("}");
return sb.toString();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.sngular.scsplugin.rarecharsgeneration.model.event.producer;

import com.sngular.scsplugin.rarecharsgeneration.model.event.OrderDTO;

public interface IPublishOperationFileGeneration {

OrderDTO publishOperationFileGeneration();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.sngular.scsplugin.rarecharsgeneration.model.event.consumer;

import com.sngular.scsplugin.rarecharsgeneration.model.event.CreateOrderDTO;

public interface ISubscribeOperationFileGeneration {

void subscribeOperationFileGeneration(final CreateOrderDTO value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.sngular.scsplugin.rarecharsgeneration.model.event.exception;

public class ModelClassException extends RuntimeException {

private static final String ERROR_MESSAGE = "There are some problems related to the entity called %s. Maybe could be caused by required fields or anyOf/oneOf restrictions";

public ModelClassException(final String modelEntity) {
super(String.format(ERROR_MESSAGE, modelEntity));
}
}
Loading

0 comments on commit e4922b6

Please sign in to comment.