Skip to content

Commit

Permalink
add lombok model support on spring (OpenAPITools#17622)
Browse files Browse the repository at this point in the history
* add lombok model support on spring

* use regex to adapt annotation with parenthesis

* add ut case

* add samples

* add samples

* fix github workflow

* fix github workflow
  • Loading branch information
dabdirb committed Jan 27, 2024
1 parent a092bbd commit 66a6af5
Show file tree
Hide file tree
Showing 86 changed files with 5,737 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/samples-spring-jdk17.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ on:
pull_request:
paths:
- samples/openapi3/client/petstore/spring-cloud-3-with-optional
- samples/server/petstore/springboot-lombok-data
- samples/server/petstore/springboot-lombok-tostring
jobs:
build:
name: Build Java Spring (JDK17)
Expand All @@ -17,6 +19,9 @@ jobs:
sample:
# clients
- samples/openapi3/client/petstore/spring-cloud-3-with-optional
# servers
- samples/server/petstore/springboot-lombok-data
- samples/server/petstore/springboot-lombok-tostring
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/samples-spring.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ jobs:
- samples/server/petstore/springboot-spring-pageable-without-j8
- samples/server/petstore/springboot-spring-pageable
- samples/server/petstore/springboot-spring-provide-args
- samples/server/petstore/springboot-lombok-data
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
Expand Down
9 changes: 9 additions & 0 deletions bin/configs/spring-boot-lombok-data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
generatorName: spring
outputDir: samples/server/petstore/springboot-lombok-data
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
additionalProperties:
groupId: org.openapitools.openapi3
artifactId: springboot-lombok-data
hideGenerationTimestamp: "true"
additionalModelTypeAnnotations: "@lombok.Data;@lombok.Builder;@lombok.NoArgsConstructor;@lombok.AllArgsConstructor"
11 changes: 11 additions & 0 deletions bin/configs/spring-boot-lombok-tostring.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
generatorName: spring
outputDir: samples/server/petstore/springboot-lombok-tostring
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
additionalProperties:
groupId: org.openapitools.openapi3
artifactId: springboot-lombok-tostring
hideGenerationTimestamp: "true"
performBeanValidation: "true"
useSpringBoot3: "true"
additionalModelTypeAnnotations: "@lombok.Getter;@lombok.Setter;@lombok.ToString;@lombok.EqualsAndHashCode"
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.time.ZoneId;
import java.util.*;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -89,7 +90,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code

public static final String CAMEL_CASE_DOLLAR_SIGN = "camelCaseDollarSign";
public static final String USE_ONE_OF_INTERFACES = "useOneOfInterfaces";

public static final String LOMBOK = "lombok";
public static final String DEFAULT_TEST_FOLDER = "${project.build.directory}/generated-test-sources/openapi";

protected String dateLibrary = "java8";
Expand Down Expand Up @@ -128,6 +129,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
protected String parentVersion = "";
protected boolean parentOverridden = false;
protected List<String> additionalModelTypeAnnotations = new LinkedList<>();
protected Map<String, Boolean> lombokAnnotations = null;
protected List<String> additionalOneOfTypeAnnotations = new LinkedList<>();
protected List<String> additionalEnumTypeAnnotations = new LinkedList<>();
protected boolean openApiNullable = true;
Expand Down Expand Up @@ -1677,6 +1679,22 @@ public ModelsMap postProcessModels(ModelsMap objs) {
}
}

// parse lombok additional model type annotations
Map<String, Boolean> lombokOptions = new HashMap<>();
String regexp = "@lombok.(\\w+\\.)*(?<ClassName>\\w+)(\\(.*?\\))?";
Pattern pattern = Pattern.compile(regexp);
for (String annotation : additionalModelTypeAnnotations) {
Matcher matcher = pattern.matcher(annotation);
if (matcher.find()) {
String className = matcher.group("ClassName");
lombokOptions.put(className, true);
}
}
if (!lombokOptions.isEmpty()) {
lombokAnnotations = lombokOptions;
writePropertyBack(LOMBOK, lombokOptions);
}

return postProcessModelsEnum(objs);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,19 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
{{#classifier}}
<configuration>
{{#classifier}}
<classifier>{{{classifier}}}</classifier>
{{/classifier}}
{{#lombok}}
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
{{/lombok}}
</configuration>
{{/classifier}}
</plugin>
{{#apiFirst}}
<plugin>
Expand Down Expand Up @@ -217,6 +225,13 @@
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
{{/hateoas}}
{{#lombok}}
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
{{/lombok}}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,19 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
{{#classifier}}
<configuration>
{{#classifier}}
<classifier>{{{classifier}}}</classifier>
</configuration>
{{/classifier}}
{{#lombok}}
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
{{/lombok}}
</configuration>
</plugin>
{{#apiFirst}}
<plugin>
Expand Down Expand Up @@ -227,6 +235,13 @@
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
{{/hateoas}}
{{#lombok}}
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
{{/lombok}}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
{{/hateoas}}
{{#lombok}}
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
{{/lombok}}
{{#useBeanValidation}}
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
{{/hateoas}}
{{#lombok}}
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
{{/lombok}}
{{#useBeanValidation}}
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@
<version>0.2.6</version>
</dependency>
{{/openApiNullable}}
{{#lombok}}
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
{{/lombok}}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ public class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}{{^parent}}
{{#gson}}
@SerializedName("{{baseName}}")
{{/gson}}
{{#lombok}}
{{#required}}
@lombok.NonNull
{{/required}}
{{#isPassword}}
@lombok.ToString.Exclude
{{/isPassword}}
{{/lombok}}
{{#vendorExtensions.x-field-extra-annotation}}
{{{vendorExtensions.x-field-extra-annotation}}}
{{/vendorExtensions.x-field-extra-annotation}}
Expand Down Expand Up @@ -81,13 +89,17 @@ public class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}{{^parent}}
{{/openApiNullable}}
{{/isContainer}}
{{/vars}}
{{^lombok.Data}}
{{^lombok.RequiredArgsConstructor}}
{{#generatedConstructorWithRequiredArgs}}
{{#hasRequired}}

{{^lombok.NoArgsConstructor}}
public {{classname}}() {
super();
}

{{/lombok.NoArgsConstructor}}
/**
* Constructor with only required parameters
*/
Expand All @@ -108,7 +120,10 @@ public class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}{{^parent}}
}
{{/hasRequired}}
{{/generatedConstructorWithRequiredArgs}}
{{/lombok.RequiredArgsConstructor}}
{{/lombok.Data}}
{{#vars}}
{{^lombok.Data}}

{{! begin feature: fluent setter methods }}
public {{classname}} {{name}}({{{datatypeWithEnum}}} {{name}}) {
Expand Down Expand Up @@ -158,6 +173,7 @@ public class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}{{^parent}}
{{/isMap}}
{{! end feature: fluent setter methods }}
{{! begin feature: getter and setter }}
{{^lombok.Getter}}

/**
{{#description}}
Expand Down Expand Up @@ -204,7 +220,9 @@ public class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}{{^parent}}
public {{>nullableDataTypeBeanValidation}} {{getter}}() {
return {{name}};
}
{{/lombok.Getter}}

{{^lombok.Setter}}
{{#deprecated}}
/**
* @deprecated
Expand All @@ -219,11 +237,15 @@ public class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}{{^parent}}
public void {{setter}}({{>nullableDataType}} {{name}}) {
this.{{name}} = {{name}};
}
{{/lombok.Setter}}
{{/lombok.Data}}
{{! end feature: getter and setter }}
{{/vars}}
{{>additionalProperties}}
{{^lombok.Data}}
{{#parentVars}}

{{^lombok.Setter}}
{{! begin feature: fluent setter methods for inherited properties }}
public {{classname}} {{name}}({{{datatypeWithEnum}}} {{name}}) {
super.{{name}}({{name}});
Expand All @@ -243,8 +265,10 @@ public class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}{{^parent}}
return this;
}
{{/isMap}}
{{/lombok.Setter}}
{{! end feature: fluent setter methods for inherited properties }}
{{/parentVars}}
{{^lombok.EqualsAndHashCode}}
@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down Expand Up @@ -276,7 +300,9 @@ public class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}{{^parent}}
}
return a.isPresent() ? Arrays.deepHashCode(new Object[]{a.get()}) : 31;
}{{/vendorExtensions.x-jackson-optional-nullable-helpers}}
{{/lombok.EqualsAndHashCode}}

{{^lombok.ToString}}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Expand All @@ -301,4 +327,6 @@ public class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}{{^parent}}
}
return o.toString().replace("\n", "\n ");
}
{{/lombok.ToString}}
{{/lombok.Data}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4427,4 +4427,48 @@ public void testMultiInheritanceParentRequiredParams_issue15796() throws IOExcep
.hasParameter("race").toConstructor()
;
}

@Test
public void testLombokAnnotations() throws IOException {
final Map<String, Object> additionalProperties = new HashMap<>();
additionalProperties.put(AbstractJavaCodegen.ADDITIONAL_MODEL_TYPE_ANNOTATIONS, "@lombok.Data;@lombok.NoArgsConstructor;@lombok.AllArgsConstructor");
Map<String, File> output = generateFromContract("src/test/resources/3_0/petstore.yaml", SPRING_BOOT, additionalProperties);
JavaFileAssert.assertThat(output.get("Pet.java"))
.assertNoConstructor()
.assertNoMethod("toString")
.assertNoMethod("hashCode")
.assertNoMethod("equals")
.assertNoMethod("getId")
.assertNoMethod("setId")
.assertNoMethod("getName")
.assertNoMethod("setName")
;
additionalProperties.put(AbstractJavaCodegen.ADDITIONAL_MODEL_TYPE_ANNOTATIONS, "@lombok.ToString");
output = generateFromContract("src/test/resources/3_0/petstore.yaml", SPRING_BOOT, additionalProperties);
JavaFileAssert.assertThat(output.get("Pet.java"))
.assertConstructor().toFileAssert()
.assertNoMethod("toString")
.assertMethod("hashCode")
.toFileAssert()
.assertMethod("equals")
.toFileAssert()
.assertMethod("getId")
.toFileAssert()
.assertMethod("setId")
.toFileAssert()
.assertMethod("getName")
.toFileAssert()
.assertMethod("setName")
;
additionalProperties.put(AbstractJavaCodegen.ADDITIONAL_MODEL_TYPE_ANNOTATIONS, "@lombok.Getter;@lombok.Setter");
output = generateFromContract("src/test/resources/3_0/petstore.yaml", SPRING_BOOT, additionalProperties);
JavaFileAssert.assertThat(output.get("Pet.java"))
.assertConstructor().toFileAssert()
.assertMethod("toString")
.toFileAssert()
.assertMethod("hashCode")
.toFileAssert()
.assertMethod("equals")
;
}
}
2 changes: 2 additions & 0 deletions samples/openapi3/server/petstore/spring-boot-oneof/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
2 changes: 2 additions & 0 deletions samples/openapi3/server/petstore/springboot-3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
Loading

0 comments on commit 66a6af5

Please sign in to comment.