Skip to content

Commit

Permalink
Support useBeanValidation-configOption (#50)
Browse files Browse the repository at this point in the history
* Support `useBeanValidation`-configOption in Record-fields

* Update pom.xml

* Update pojo.mustache

* Annotate fields with bean validation

* Update comment in `pojo.mustache`

* Support `useBeanValidation`-configOption in Record-fields

* Annotate fields with bean validation

* Update comment in `pojo.mustache`

* Add execution for `useBeanValidation`

* Format `PluginExecution.java`

* Annotate boolean-fields

* Add new schema for testing string constraints

* Support Nullable String-fields

* Execute baseline tests of `RecordWithAllConstraints`

* Add tests for `@NotNull`-annotations

* Add inner `Builder`-class to `GenerateField`

* Remove primitive-type check

* Annotate fields with `@Pattern`

* Add `@Size`-annotation based on `minLength`/`maxLength`

* Annotate with `@Size` when `minItems`/`maxItems` is set

* Annotate `Integer`-fields with `@Min`/`@Max`

* Annotate `Long`-fields with `@Min`/`@Max`

* Annotate `BigDecimal`-fields with `@DecimalMin`/`@DecimalMax`

* Remove prototype `.mustache`-template

* Update project version 1.7.6 -> 1.8.0

* Update project version 1.7.6 -> 1.8.0

* Reset minimum package line coverage ratio to 100%
  • Loading branch information
Chrimle authored Oct 10, 2024
1 parent b7e9125 commit 14c9626
Show file tree
Hide file tree
Showing 79 changed files with 2,200 additions and 101 deletions.
44 changes: 43 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.github.chrimle</groupId>
<artifactId>openapi-to-java-records-mustache-templates</artifactId>
<version>1.7.6</version>
<version>1.8.0</version>

<!-- Project Information -->
<name>OpenAPI to Java records :: Mustache Templates</name>
Expand Down Expand Up @@ -90,6 +90,7 @@
<serializableModel.default>false</serializableModel.default>
<generateBuilders.default>false</generateBuilders.default>
<useJakartaEe.default>false</useJakartaEe.default>
<useBeanValidation.default>false</useBeanValidation.default>

<!-- Plugin Executions -->
<!-- The Plugin Execution with Standard Configurations -->
Expand All @@ -106,6 +107,8 @@
<execution.generateBuilders>generateBuilders</execution.generateBuilders>
<!-- The Plugin Execution with useJakartaEe=true -->
<execution.useJakartaEe>useJakartaEe</execution.useJakartaEe>
<!-- The Plugin Execution with useBeanValidation=true -->
<execution.useBeanValidation>useBeanValidation</execution.useBeanValidation>
</properties>


Expand Down Expand Up @@ -151,6 +154,12 @@
<artifactId>jackson-annotations</artifactId>
<version>${jackson-annotations.version}</version>
</dependency>
<dependency>
<!-- Needed when useBeanValidation=true -->
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate-validator.version}</version>
</dependency>
</dependencies>


Expand Down Expand Up @@ -183,6 +192,7 @@
<serializableModel>${serializableModel.default}</serializableModel>
<generateBuilders>${generateBuilders.default}</generateBuilders>
<useJakartaEe>${useJakartaEe.default}</useJakartaEe>
<useBeanValidation>${useBeanValidation.default}</useBeanValidation>
</configOptions>
</configuration>
</execution>
Expand All @@ -208,6 +218,7 @@
<serializableModel>${serializableModel.default}</serializableModel>
<generateBuilders>${generateBuilders.default}</generateBuilders>
<useJakartaEe>${useJakartaEe.default}</useJakartaEe>
<useBeanValidation>${useBeanValidation.default}</useBeanValidation>
</configOptions>
</configuration>
</execution>
Expand Down Expand Up @@ -238,6 +249,7 @@
<serializableModel>${serializableModel.default}</serializableModel>
<generateBuilders>${generateBuilders.default}</generateBuilders>
<useJakartaEe>${useJakartaEe.default}</useJakartaEe>
<useBeanValidation>${useBeanValidation.default}</useBeanValidation>
</configOptions>
</configuration>
</execution>
Expand All @@ -264,6 +276,7 @@
<serializableModel>${serializableModel.default}</serializableModel>
<generateBuilders>${generateBuilders.default}</generateBuilders>
<useJakartaEe>${useJakartaEe.default}</useJakartaEe>
<useBeanValidation>${useBeanValidation.default}</useBeanValidation>
</configOptions>
</configuration>
</execution>
Expand All @@ -289,6 +302,7 @@
<serializableModel>true</serializableModel>
<generateBuilders>${generateBuilders.default}</generateBuilders>
<useJakartaEe>${useJakartaEe.default}</useJakartaEe>
<useBeanValidation>${useBeanValidation.default}</useBeanValidation>
</configOptions>
</configuration>
</execution>
Expand All @@ -314,6 +328,7 @@
<serializableModel>${serializableModel.default}</serializableModel>
<generateBuilders>true</generateBuilders>
<useJakartaEe>${useJakartaEe.default}</useJakartaEe>
<useBeanValidation>${useBeanValidation.default}</useBeanValidation>
</configOptions>
</configuration>
</execution>
Expand All @@ -339,6 +354,33 @@
<serializableModel>${serializableModel.default}</serializableModel>
<generateBuilders>${generateBuilders.default}</generateBuilders>
<useJakartaEe>true</useJakartaEe>
<useBeanValidation>${useBeanValidation.default}</useBeanValidation>
</configOptions>
</configuration>
</execution>
<execution>
<id>generate-${execution.useBeanValidation}</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<output>${output.default}</output>
<modelPackage>${modelPackage.default}.${execution.useBeanValidation}</modelPackage>
<generateModels>${generateModels.default}</generateModels>
<generateApis>${generateApis.default}</generateApis>
<generateSupportingFiles>${generateSupportingFiles.default}</generateSupportingFiles>
<generateModelTests>${generateModelTests.default}</generateModelTests>
<generateModelDocumentation>${generateModelDocumentation.default}</generateModelDocumentation>
<inputSpec>${inputSpec.default}</inputSpec>
<templateDirectory>${templateDirectory.default}</templateDirectory>
<generatorName>${generatorName.default}</generatorName>
<configOptions>
<sourceFolder>${sourceFolder.default}</sourceFolder>
<useEnumCaseInsensitive>${useEnumCaseInsensitive.default}</useEnumCaseInsensitive>
<serializableModel>${serializableModel.default}</serializableModel>
<generateBuilders>${generateBuilders.default}</generateBuilders>
<useJakartaEe>${useJakartaEe.default}</useJakartaEe>
<useBeanValidation>true</useBeanValidation>
</configOptions>
</configuration>
</execution>
Expand Down
83 changes: 83 additions & 0 deletions src/main/resources/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,86 @@ components:
- ENUM1
- ENUM2
- ENUM3
RecordWithAllConstraints:
type: object
description: Example of a Record which has fields with constraints
required:
- stringRequired
- stringRequiredNullable
- stringRequiredPattern
properties:
stringStandard:
type: string
stringDefault:
type: string
default: 'someDefaultValue'
stringNullable:
type: string
nullable: true
stringRequired:
type: string
stringRequiredNullable:
type: string
nullable: true
stringRequiredPattern:
type: string
pattern: '^\d{3}-\d{2}-\d{4}$'
stringMinLength:
type: string
minLength: 3
stringMaxLength:
type: string
maxLength: 7
stringMinAndMaxLength:
type: string
minLength: 3
maxLength: 7
arrayMinItems:
type: array
minItems: 1
items:
type: string
arrayMaxItems:
type: array
maxItems: 10
items:
type: string
arrayMinAndMaxItems:
type: array
minItems: 1
maxItems: 10
items:
type: string
intMinimum:
type: integer
minimum: 18
intMaximum:
type: integer
maximum: 100
intMinimumAndMaximum:
type: integer
minimum: 0
maximum: 100
longMinimum:
type: integer
format: int64
minimum: 18
longMaximum:
type: integer
format: int64
maximum: 100
longMinimumAndMaximum:
type: integer
format: int64
minimum: 0
maximum: 100
bigDecimalMinimum:
type: number
minimum: 0
bigDecimalMaximum:
type: number
maximum: 100
bigDecimalMinimumAndMaximum:
type: number
minimum: 0
maximum: 100
2 changes: 1 addition & 1 deletion src/main/resources/templates/generateBuilders.mustache
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{!
Source: openapi-to-java-records-mustache-templates
Version: 1.7.6
Version: 1.8.0
Enabled via configOptions.generateBuilders=true
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/javadoc.mustache
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{!
Source: openapi-to-java-records-mustache-templates
Version: 1.7.6
Version: 1.8.0
This template is optional, and does not override an existing template.
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/licenseInfo.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
* openapi-to-java-records-mustache-templates. For further information,
* questions, requesting features or reporting issues, please visit:
* https://github.com/Chrimle/openapi-to-java-records-mustache-templates.
* Generated with Version: 1.7.6
* Generated with Version: 1.8.0
*
*/
2 changes: 1 addition & 1 deletion src/main/resources/templates/modelEnum.mustache
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{!
Source: openapi-to-java-records-mustache-templates
Version: 1.7.6
Version: 1.8.0
Required mustache templates (generation will fail without them):
- `deprecation.mustache`
Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/templates/pojo.mustache
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{!
Source: openapi-to-java-records-mustache-templates
Version: 1.7.6
Version: 1.8.0
Required imports:
- none
Expand All @@ -10,11 +10,12 @@
- `generateBuilders.mustache`
- `javadoc.mustache`
- `serializableModel.mustache`
- `useBeanValidation.mustache`
- `additionalModelTypeAnnotations.mustache` (Official)
}}{{>javadoc}}{{>deprecation}}{{>additionalModelTypeAnnotations}}{{!
}}public record {{classname}}(
{{#vars}}@{{javaxPackage}}.annotation.{{#isNullable}}Nullable{{/isNullable}}{{^isNullable}}Nonnull{{/isNullable}} {{{datatypeWithEnum}}} {{name}}{{^-last}},
{{#vars}}@{{javaxPackage}}.annotation.{{#isNullable}}Nullable{{/isNullable}}{{^isNullable}}Nonnull{{/isNullable}}{{>useBeanValidation}} {{{datatypeWithEnum}}} {{name}}{{^-last}},
{{/-last}}{{/vars}}{{^serializableModel}}){{/serializableModel}}{{#serializableModel}}
) implements Serializable{{/serializableModel}} {
{{>serializableModel}}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/serializableModel.mustache
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{!
Source: openapi-to-java-records-mustache-templates
Version: 1.7.6
Version: 1.8.0
Enabled via configOptions.serializableModel=true
Expand Down
79 changes: 79 additions & 0 deletions src/main/resources/templates/useBeanValidation.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{{!
Source: openapi-to-java-records-mustache-templates
Version: 1.8.0
Enabled via configOption.useBeanValidation=true
}}{{#useBeanValidation}}{{!
}}{{^required}}{{!
When not required, the field *may* be nullable according to openapi-generator-maven-plugin.
}}{{/required}}{{!
}}{{#required}}{{!
}}{{#isNullable}}{{!
Jakarta Bean Validation does not have constraint annotations for nullable values,
as values are nullable by default.
}}{{/isNullable}}{{!
}}{{^isNullable}}{{!
}} @NotNull{{!
}}{{/isNullable}}{{!
}}{{/required}}{{!
}}{{#pattern}}{{!
}} @Pattern(regexp = "{{{.}}}"){{!
}}{{/pattern}}{{!
}}{{#minLength}}{{!
}}{{#maxLength}}{{!
}} @Size(min = {{minLength}}, max = {{maxLength}}){{!
}}{{/maxLength}}{{!
}}{{/minLength}}{{!
}}{{#minLength}}{{!
}}{{^maxLength}}{{!
}} @Size(min = {{minLength}}){{!
}}{{/maxLength}}{{!
}}{{/minLength}}{{!
}}{{^minLength}}{{!
}}{{#maxLength}}{{!
}} @Size(max = {{maxLength}}){{!
}}{{/maxLength}}{{!
}}{{/minLength}}{{!
}}{{#minItems}}{{!
}}{{#maxItems}}{{!
}} @Size(min = {{minItems}}, max = {{maxItems}}){{!
}}{{/maxItems}}{{!
}}{{/minItems}}{{!
}}{{#minItems}}{{!
}}{{^maxItems}}{{!
}} @Size(min = {{minItems}}){{!
}}{{/maxItems}}{{!
}}{{/minItems}}{{!
}}{{^minItems}}{{!
}}{{#maxItems}}{{!
}} @Size(max = {{maxItems}}){{!
}}{{/maxItems}}{{!
}}{{/minItems}}{{!
}}{{#isInteger}}{{!
}}{{#minimum}}{{!
}} @Min({{.}}){{!
}}{{/minimum}}{{!
}}{{#maximum}}{{!
}} @Max({{.}}){{!
}}{{/maximum}}{{!
}}{{/isInteger}}{{!
}}{{#isLong}}{{!
}}{{#minimum}}{{!
}} @Min({{.}}L){{!
}}{{/minimum}}{{!
}}{{#maximum}}{{!
}} @Max({{.}}L){{!
}}{{/maximum}}{{!
}}{{/isLong}}{{!
}}{{^isInteger}}{{!
}}{{^isLong}}{{!
}}{{#minimum}}{{!
}} @DecimalMin({{#exclusiveMinimum}}value={{/exclusiveMinimum}}"{{minimum}}"{{#exclusiveMinimum}},inclusive=false{{/exclusiveMinimum}}){{!
}}{{/minimum}}{{!
}}{{#maximum}}{{!
}} @DecimalMax({{#exclusiveMaximum}}value={{/exclusiveMaximum}}"{{maximum}}"{{#exclusiveMaximum}},inclusive=false{{/exclusiveMaximum}}){{!
}}{{/maximum}}{{!
}}{{/isLong}}{{!
}}{{/isInteger}}{{!
}}{{/useBeanValidation}}
3 changes: 2 additions & 1 deletion src/test/java/com/chrimle/example/GeneratedClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public enum GeneratedClass {
EXAMPLE_RECORD_WITH_REQUIRED_FIELDS_OF_EACH_TYPE(
"ExampleRecordWithRequiredFieldsOfEachType", false, false),
EXAMPLE_RECORD_WITH_NULLABLE_FIELDS_OF_EACH_TYPE(
"ExampleRecordWithNullableFieldsOfEachType", false, false);
"ExampleRecordWithNullableFieldsOfEachType", false, false),
RECORD_WITH_ALL_CONSTRAINTS("RecordWithAllConstraints", false, false);

public static final String PACKAGE_NAME = "io.github.chrimle.example";
public final String simpleClassName;
Expand Down
Loading

0 comments on commit 14c9626

Please sign in to comment.