Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix import for generator with lombok #1723

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import java.io.Serial;
import java.io.Serializable;
{{/serializableModel}}
{{#lombok}}
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package io.micronaut.openapi.generator;

import java.util.List;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.servers.Server;

import org.junit.jupiter.api.Test;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenConstants;

import java.util.List;

import static java.util.stream.Collectors.groupingBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand All @@ -20,9 +19,9 @@ class JavaMicronautClientCodegenTest extends AbstractMicronautCodegenTest {
void clientOptsUnicity() {
var codegen = new JavaMicronautClientCodegen();
codegen.cliOptions()
.stream()
.collect(groupingBy(CliOption::getOpt))
.forEach((k, v) -> assertEquals(1, v.size(), k + " is described multiple times"));
.stream()
.collect(groupingBy(CliOption::getOpt))
.forEach((k, v) -> assertEquals(1, v.size(), k + " is described multiple times"));
}

@Test
Expand Down Expand Up @@ -55,9 +54,9 @@ void testApiAndModelFilesPresent() {
codegen.additionalProperties().put(CodegenConstants.MODEL_PACKAGE, "org.test.test.model");
codegen.additionalProperties().put(CodegenConstants.API_PACKAGE, "org.test.test.api");
String outputPath = generateFiles(codegen, PETSTORE_PATH,
CodegenConstants.SUPPORTING_FILES,
CodegenConstants.APIS,
CodegenConstants.MODELS);
CodegenConstants.SUPPORTING_FILES,
CodegenConstants.APIS,
CodegenConstants.MODELS);

String apiFolder = outputPath + "src/main/java/org/test/test/api/";
assertFileExists(apiFolder + "PetApi.java");
Expand All @@ -78,8 +77,8 @@ void doConfigureAuthParam() {
var codegen = new JavaMicronautClientCodegen();
codegen.additionalProperties().put(JavaMicronautClientCodegen.OPT_CONFIGURE_AUTH, "true");
String outputPath = generateFiles(codegen, PETSTORE_PATH,
CodegenConstants.SUPPORTING_FILES,
CodegenConstants.APIS);
CodegenConstants.SUPPORTING_FILES,
CodegenConstants.APIS);

// Files generated
assertFileExists(outputPath + "/src/main/java/org/openapitools/auth/Authorization.java");
Expand All @@ -92,8 +91,8 @@ void doNotConfigureAuthParam() {
var codegen = new JavaMicronautClientCodegen();
codegen.additionalProperties().put(JavaMicronautClientCodegen.OPT_CONFIGURE_AUTH, "false");
String outputPath = generateFiles(codegen, PETSTORE_PATH,
CodegenConstants.SUPPORTING_FILES,
CodegenConstants.APIS);
CodegenConstants.SUPPORTING_FILES,
CodegenConstants.APIS);

// Files are not generated
assertFileNotExists(outputPath + "/src/main/java/org/openapitools/auth/");
Expand All @@ -105,7 +104,7 @@ void doUseValidationParam() {
var codegen = new JavaMicronautClientCodegen();
codegen.additionalProperties().put(JavaMicronautClientCodegen.USE_BEANVALIDATION, "true");
String outputPath = generateFiles(codegen, PETSTORE_PATH,
CodegenConstants.APIS);
CodegenConstants.APIS);

// Files are not generated
assertFileContains(outputPath + "/src/main/java/org/openapitools/api/PetApi.java", "@Valid");
Expand All @@ -117,7 +116,7 @@ void doNotUseValidationParam() {
var codegen = new JavaMicronautClientCodegen();
codegen.additionalProperties().put(JavaMicronautClientCodegen.USE_BEANVALIDATION, "false");
String outputPath = generateFiles(codegen, PETSTORE_PATH,
CodegenConstants.APIS);
CodegenConstants.APIS);

// Files are not generated
assertFileNotContains(outputPath + "/src/main/java/org/openapitools/api/PetApi.java", "@Valid");
Expand All @@ -128,10 +127,10 @@ void doNotUseValidationParam() {
void doGenerateForTestJUnit() {
var codegen = new JavaMicronautClientCodegen();
codegen.additionalProperties().put(JavaMicronautClientCodegen.OPT_TEST,
JavaMicronautClientCodegen.OPT_TEST_JUNIT);
JavaMicronautClientCodegen.OPT_TEST_JUNIT);
String outputPath = generateFiles(codegen, PETSTORE_PATH,
CodegenConstants.SUPPORTING_FILES,
CodegenConstants.API_TESTS, CodegenConstants.APIS, CodegenConstants.MODELS);
CodegenConstants.SUPPORTING_FILES,
CodegenConstants.API_TESTS, CodegenConstants.APIS, CodegenConstants.MODELS);

// Files are not generated
assertFileExists(outputPath + "src/test/java/");
Expand All @@ -143,10 +142,10 @@ void doGenerateForTestJUnit() {
void doGenerateForTestSpock() {
var codegen = new JavaMicronautClientCodegen();
codegen.additionalProperties().put(JavaMicronautClientCodegen.OPT_TEST,
JavaMicronautClientCodegen.OPT_TEST_SPOCK);
JavaMicronautClientCodegen.OPT_TEST_SPOCK);
String outputPath = generateFiles(codegen, PETSTORE_PATH,
CodegenConstants.SUPPORTING_FILES,
CodegenConstants.API_TESTS, CodegenConstants.APIS, CodegenConstants.MODELS);
CodegenConstants.SUPPORTING_FILES,
CodegenConstants.API_TESTS, CodegenConstants.APIS, CodegenConstants.MODELS);

// Files are not generated
assertFileExists(outputPath + "src/test/groovy");
Expand Down Expand Up @@ -211,7 +210,7 @@ void testAdditionalClientTypeAnnotations() {

// Micronaut declarative http client should contain custom added annotations
assertFileContains(outputPath + "/src/main/java/org/openapitools/api/PetApi.java",
"@MyAdditionalAnnotation1(1,${param1})", "@MyAdditionalAnnotation2(2,${param2})");
"@MyAdditionalAnnotation1(1,${param1})", "@MyAdditionalAnnotation2(2,${param2})");
}

@Test
Expand All @@ -222,7 +221,7 @@ void testAdditionalClientTypeAnnotationsFromSetter() {

// Micronaut declarative http client should contain custom added annotations
assertFileContains(outputPath + "/src/main/java/org/openapitools/api/PetApi.java",
"@MyAdditionalAnnotation1(1,${param1})", "@MyAdditionalAnnotation2(2,${param2})");
"@MyAdditionalAnnotation1(1,${param1})", "@MyAdditionalAnnotation2(2,${param2})");
}

@Test
Expand Down Expand Up @@ -315,10 +314,10 @@ void testAddValidAnnotations() {
String apiPath = outputPath + "src/main/java/org/openapitools/model/";

assertFileContains(apiPath + "BooksContainer.java",
"""
@JsonProperty(JSON_PROPERTY_BOOKS)
private List<@Valid Book> books;
""");
"""
@JsonProperty(JSON_PROPERTY_BOOKS)
private List<@Valid Book> books;
""");
}

@Test
Expand All @@ -340,7 +339,7 @@ void testDiscriminatorConstructorBug() {
var codegen = new JavaMicronautClientCodegen();
codegen.additionalProperties().put(JavaMicronautClientCodegen.OPT_CONFIGURE_AUTH, "true");
String outputPath = generateFiles(codegen, "src/test/resources/3_0/discriminatorconstructorbug.yml",
CodegenConstants.MODELS
CodegenConstants.MODELS
);
String apiPath = outputPath + "src/main/java/org/openapitools/model/";

Expand All @@ -356,15 +355,15 @@ void testDifferentPropertyCase() {
String modelPath = outputPath + "src/main/java/org/openapitools/model/";

assertFileContains(
modelPath + "Book.java",
"public static final String JSON_PROPERTY_TITLE = \"tItle\";",
"public static final String JSON_PROPERTY_I_S_B_N = \"ISBN\";",
"private String title;",
"public String getTitle()",
"public void setTitle(String title)",
"private String ISBN;",
"public String getISBN()",
"public void setISBN(String ISBN)"
modelPath + "Book.java",
"public static final String JSON_PROPERTY_TITLE = \"tItle\";",
"public static final String JSON_PROPERTY_I_S_B_N = \"ISBN\";",
"private String title;",
"public String getTitle()",
"public void setTitle(String title)",
"private String ISBN;",
"public String getISBN()",
"public void setISBN(String ISBN)"
);
}

Expand All @@ -389,35 +388,35 @@ void testUnderscore() {
String modelPath = outputPath + "src/main/java/org/openapitools/model/";

assertFileContains(modelPath + "MyModel.java", "private BigDecimal _default;",
"public static final String JSON_PROPERTY_DEFAULT = \"_default\";",
"public BigDecimal get_default() {",
"public void set_default(BigDecimal _default) {");
"public static final String JSON_PROPERTY_DEFAULT = \"_default\";",
"public BigDecimal get_default() {",
"public void set_default(BigDecimal _default) {");
}

@Test
void testReservedWords() {

var codegen = new JavaMicronautClientCodegen();
String outputPath = generateFiles(codegen, "src/test/resources/3_0/javaReservedWords.yml",
CodegenConstants.APIS,
CodegenConstants.MODELS,
CodegenConstants.SUPPORTING_FILES,
CodegenConstants.MODEL_TESTS,
CodegenConstants.MODEL_DOCS,
CodegenConstants.API_TESTS,
CodegenConstants.API_DOCS
CodegenConstants.APIS,
CodegenConstants.MODELS,
CodegenConstants.SUPPORTING_FILES,
CodegenConstants.MODEL_TESTS,
CodegenConstants.MODEL_DOCS,
CodegenConstants.API_TESTS,
CodegenConstants.API_DOCS
);
String path = outputPath + "src/main/java/org/openapitools/";

assertFileContains(path + "api/ParametersApi.java", "Mono<Void> callInterface(",
"@QueryValue(\"class\") @NotNull @Valid Package propertyClass,",
"@QueryValue(\"while\") @NotNull String _while");
"@QueryValue(\"class\") @NotNull @Valid Package propertyClass,",
"@QueryValue(\"while\") @NotNull String _while");
assertFileContains(path + "model/Package.java",
"public static final String JSON_PROPERTY_FOR = \"for\";",
"@JsonProperty(JSON_PROPERTY_FOR)",
"private String _for;",
"public String get_for() {",
"public void set_for(String _for) {");
"public static final String JSON_PROPERTY_FOR = \"for\";",
"@JsonProperty(JSON_PROPERTY_FOR)",
"private String _for;",
"public String get_for() {",
"public void set_for(String _for) {");
}

@Test
Expand All @@ -438,13 +437,13 @@ void testCommonPathParametersWithRef() {
String path = outputPath + "src/main/java/org/openapitools/";

assertFileContains(path + "api/WeatherForecastApisApi.java", "@Get(\"/v1/forecast/{id}\")",
"@PathVariable(\"id\") @NotNull String id,",
"@QueryValue(\"hourly\") @Nullable List<V1ForecastIdGetHourlyParameterInner> hourly,");
"@PathVariable(\"id\") @NotNull String id,",
"@QueryValue(\"hourly\") @Nullable List<V1ForecastIdGetHourlyParameterInner> hourly,");

assertFileContains(path + "model/V1ForecastIdGetHourlyParameterInner.java",
"public enum V1ForecastIdGetHourlyParameterInner {",
"@JsonProperty(\"temperature_2m\")",
"TEMPERATURE_2M(\"temperature_2m\"),");
"public enum V1ForecastIdGetHourlyParameterInner {",
"@JsonProperty(\"temperature_2m\")",
"TEMPERATURE_2M(\"temperature_2m\"),");
}

@Test
Expand All @@ -455,30 +454,30 @@ void testExtraAnnotations() {
String path = outputPath + "src/main/java/org/openapitools/";

assertFileContains(path + "api/BooksApi.java",
"""
@Post("/add-book")
@NotBlank
Mono<@Valid Book> addBook(
""");
"""
@Post("/add-book")
@NotBlank
Mono<@Valid Book> addBook(
""");

assertFileContains(path + "model/Book.java",
"""
@Serializable
public class Book {
""",
"""
@NotNull
@Size(max = 10)
@JsonProperty(JSON_PROPERTY_TITLE)
@jakarta.validation.constraints.NotBlank
private String title;
""",
"""
@NotEmpty
public void setTitle(String title) {
this.title = title;
}
"""
@Serializable
public class Book {
""",
"""
@NotNull
@Size(max = 10)
@JsonProperty(JSON_PROPERTY_TITLE)
@jakarta.validation.constraints.NotBlank
private String title;
""",
"""
@NotEmpty
public void setTitle(String title) {
this.title = title;
}
"""
);
}

Expand Down Expand Up @@ -564,4 +563,28 @@ void testSingleProduceContentType() {

assertFileContains(path + "api/FilesApi.java", "@Produces(\"application/octet-stream\")");
}

@Test
void testLombok() {

var codegen = new JavaMicronautClientCodegen();
codegen.setLombok(true);
String outputPath = generateFiles(codegen, "src/test/resources/3_0/openmeteo.yml", CodegenConstants.MODELS);
String path = outputPath + "src/main/java/org/openapitools/";

assertFileContains(path + "model/V1ForecastIdGet400Response.java",
"import lombok.AllArgsConstructor;",
"import lombok.NoArgsConstructor;",
"import lombok.RequiredArgsConstructor;",
"import lombok.Data;",
"import lombok.EqualsAndHashCode;",
"import lombok.Getter;",
"import lombok.Setter;",
"import lombok.ToString;",
"import lombok.experimental.Accessors;",
"@Accessors(chain = true)",
"@NoArgsConstructor",
"@AllArgsConstructor",
"@Data");
}
}
Loading