Skip to content

Commit

Permalink
feat: Support for Jakarta Validation 3.0.0 added (#156, #157)
Browse files Browse the repository at this point in the history
* feat: Support for Jakarta Validation 3.0.0 added (#156)

* chore(docs): mention new jakarta.validation module
  • Loading branch information
CarstenWickner committed Dec 23, 2020
1 parent 995b181 commit f2b0c82
Show file tree
Hide file tree
Showing 20 changed files with 1,849 additions and 3 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### `jsonschema-module-jakarta-validation`
#### Added
- Initial implementation (initial features are equivalent to `jsonschema-module-javax-validation`)

### `jsonschema-maven-plugin`
#### Added
- Support for new `jakarta.validation` module

## [4.16.0] - 2020-09-25
### `jsonschema-generator`
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This project consists of:
- the [victools/jsonschema-generator](jsonschema-generator) (the only thing you need to get started)
- a few modules bundling standard configurations for your convenience:
- [victools/jsonschema-module-jackson](jsonschema-module-jackson) – deriving JSON Schema attributes from `jackson` annotations (e.g. "description", property name overrides, what properties to ignore) as well as looking up appropriate (annotated) subtypes
- [victools/jsonschema-module-jakarta-validation](jsonschema-module-jakarta-validation) – deriving JSON Schema attributes from `jakarta.validation.constraints` annotations (e.g. which properties are nullable or not, their "minimum"/"maximum", "minItems"/"maxItems", "minLength"/"maxLength")
- [victools/jsonschema-module-javax-validation](jsonschema-module-javax-validation) – deriving JSON Schema attributes from `javax.validation` annotations (e.g. which properties are nullable or not, their "minimum"/"maximum", "minItems"/"maxItems", "minLength"/"maxLength")
- [victools/jsonschema-module-swagger-1.5](jsonschema-module-swagger-1.5) – deriving JSON Schema attributes from `swagger` (1.5.x) annotations (e.g. "description", property name overrides, what properties to ignore, their "minimum"/"maximum", "const"/"enum")
- [victools/jsonschema-module-swagger-2](jsonschema-module-swagger-2) – deriving JSON Schema attributes from `swagger` (2.x) `@Schema` annotations
Expand Down
3 changes: 2 additions & 1 deletion jsonschema-maven-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ When you want to have more control over the modules that are to be used during g
```
This configuration will generate the schema using the Jackson module.

There are three standard modules that can be used:
There are five standard modules that can be used:
- `Jackson`
- `JakartaValidation`
- `JavaxValidation`
- `Swagger15`
- `Swagger2`
Expand Down
9 changes: 9 additions & 0 deletions jsonschema-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<!-- JakartaValidation Module dependencies -->
<dependency>
<groupId>com.github.victools</groupId>
<artifactId>jsonschema-module-jakarta-validation</artifactId>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
</dependency>
<!-- Swagger 1.5 Module dependencies -->
<dependency>
<groupId>com.github.victools</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.github.victools.jsonschema.generator.SchemaVersion;
import com.github.victools.jsonschema.module.jackson.JacksonModule;
import com.github.victools.jsonschema.module.jackson.JacksonOption;
import com.github.victools.jsonschema.module.jakarta.validation.JakartaValidationModule;
import com.github.victools.jsonschema.module.jakarta.validation.JakartaValidationOption;
import com.github.victools.jsonschema.module.javax.validation.JavaxValidationModule;
import com.github.victools.jsonschema.module.javax.validation.JavaxValidationOption;
import com.github.victools.jsonschema.module.swagger15.SwaggerModule;
Expand Down Expand Up @@ -380,6 +382,10 @@ private void setModules(SchemaGeneratorConfigBuilder configBuilder) throws MojoE
this.getLog().debug("- Adding Jackson Module");
addJacksonModule(configBuilder, module);
break;
case "JakartaValidation":
this.getLog().debug("- Adding Jakarta Validation Module");
addJakartaValidationModule(configBuilder, module);
break;
case "JavaxValidation":
this.getLog().debug("- Adding Javax Validation Module");
addJavaxValidationModule(configBuilder, module);
Expand All @@ -394,7 +400,7 @@ private void setModules(SchemaGeneratorConfigBuilder configBuilder) throws MojoE
break;
default:
throw new MojoExecutionException("Error: Module does not have a name in "
+ "['Jackson', 'JavaxValidation', 'Swagger15', 'Swagger2'] or does not have a custom classname.");
+ "['Jackson', 'JakartaValidation', 'JavaxValidation', 'Swagger15', 'Swagger2'] or does not have a custom classname.");
}
}
}
Expand Down Expand Up @@ -504,6 +510,29 @@ private void addJavaxValidationModule(SchemaGeneratorConfigBuilder configBuilder
}
}

/**
* Add the Jakarta Validation module to the generator config.
*
* @param configBuilder The builder on which the config is added
* @param module The modules section form the pom
* @throws MojoExecutionException in case of problems
*/
private void addJakartaValidationModule(SchemaGeneratorConfigBuilder configBuilder, GeneratorModule module) throws MojoExecutionException {
if (module.options == null || module.options.length == 0) {
configBuilder.with(new JakartaValidationModule());
} else {
JakartaValidationOption[] jakartaValidationOptions = new JakartaValidationOption[module.options.length];
for (int i = 0; i < module.options.length; i++) {
try {
jakartaValidationOptions[i] = JakartaValidationOption.valueOf(module.options[i]);
} catch (IllegalArgumentException e) {
throw new MojoExecutionException("Error: Unknown JakartaValidation option " + module.options[i], e);
}
}
configBuilder.with(new JakartaValidationModule(jakartaValidationOptions));
}
}

/**
* Add the Jackson module to the generator config.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public Object[] parametersForTestGeneration() {
{"SchemaVersion"},
{"JacksonModule"},
{"JavaxValidationModule"},
{"JakartaValidationModule"},
{"Swagger15Module"},
{"Swagger2Module"},
{"Complete"}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<project>
<build>
<plugins>
<plugin>
<groupId>com.github.victools</groupId>
<artifactId>jsonschema-maven-plugin</artifactId>
<configuration>
<classNames>com.github.victools.jsonschema.plugin.maven.TestClass</classNames>
<schemaFilePath>target/generated-test-sources/JakartaValidationModule</schemaFilePath>
<modules>
<module>
<name>JakartaValidation</name>
</module>
</modules>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema" : "http://json-schema.org/draft-07/schema#",
"type" : "object",
"properties" : {
"anInt" : {
"type" : "integer"
}
}
}
74 changes: 74 additions & 0 deletions jsonschema-module-jakarta-validation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Java JSON Schema Generator – Module jakarta.validation
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.victools/jsonschema-module-jakarta-validation/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.victools/jsonschema-module-jakarta-validation)

Module for the [jsonschema-generator](../jsonschema-generator) – deriving JSON Schema attributes from `jakarta.validation.constraints` annotations.

## Features
1. Determine whether a member is not nullable, base assumption being that all fields and method return values are nullable if not annotated. Based on `@NotNull`/`@Null`/`@NotEmpty`/`@NotBlank`
2. Populate list of "required" fields/methods for objects if `JakartaValidationOption.NOT_NULLABLE_FIELD_IS_REQUIRED` and/or `JakartaValidationOption.NOT_NULLABLE_METHOD_IS_REQUIRED` is/are being provided in constructor.
3. Populate "minItems" and "maxItems" for containers (i.e. arrays and collections). Based on `@Size`/`@NotEmpty`
4. Populate "minLength" and "maxLength" for strings. Based on `@Size`/`@NotEmpty`/`@NotBlank`
5. Populate "format" for strings. Based on `@Email`, can be "email" or "idn-email" depending on whether `JakartaValidationOption.PREFER_IDN_EMAIL_FORMAT` is being provided in constructor.
6. Populate "pattern" for strings. Based on `@Pattern`/`@Email`, when corresponding `JakartaValidationOption.INCLUDE_PATTERN_EXPRESSIONS` is being provided in constructor.
7. Populate "minimum"/"exclusiveMinimum" for numbers. Based on `@Min`/`@DecimalMin`/`@Positive`/`@PositiveOrZero`
8. Populate "maximum"/"exclusiveMaximum" for numbers. Based on `@Max`/`@DecimalMax`/`@Negative`/`@NegativeOrZero`

Schema attributes derived from validation annotations on fields are also applied to their respective getter methods.
Schema attributes derived from validation annotations on getter methods are also applied to their associated fields.

----

## Documentation
JavaDoc is being used throughout the codebase, offering contextual information in your respective IDE or being available online through services like [javadoc.io](https://www.javadoc.io/doc/com.github.victools/jsonschema-module-jakarta-validation).

Additional documentation can be found in the [Project Wiki](https://github.com/victools/jsonschema-generator/wiki).

----

## Usage
### Dependency (Maven)
```xml
<dependency>
<groupId>com.github.victools</groupId>
<artifactId>jsonschema-module-jakarta-validation</artifactId>
<version>[4.17.0,5.0.0)</version>
</dependency>
```

Since version `4.7`, the release versions of the main generator library and this module are aligned.
It is recommended to use identical versions for both dependencies to ensure compatibility.

### Code
#### Passing into SchemaGeneratorConfigBuilder.with(Module)
```java
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder;
import com.github.victools.jsonschema.generator.SchemaVersion;
import com.github.victools.jsonschema.module.jakarta.validation.JakartaValidationModule;
```
```java
JakartaValidationModule module = new JakartaValidationModule();
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09)
.with(module);
```

#### Complete Example
```java
import com.fasterxml.jackson.databind.JsonNode;
import com.github.victools.jsonschema.generator.OptionPreset;
import com.github.victools.jsonschema.generator.SchemaGenerator;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfig;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder;
import com.github.victools.jsonschema.generator.SchemaVersion;
import com.github.victools.jsonschema.module.jakarta.validation.JakartaValidationModule;
import com.github.victools.jsonschema.module.jakarta.validation.JakartaValidationOption;
```
```java
JakartaValidationModule module = new JakartaValidationModule(JakartaValidationOption.INCLUDE_PATTERN_EXPRESSIONS);
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON)
.with(module);
SchemaGeneratorConfig config = configBuilder.build();
SchemaGenerator generator = new SchemaGenerator(config);
JsonNode jsonSchema = generator.generateSchema(YourClass.class);

System.out.println(jsonSchema.toString());
```
45 changes: 45 additions & 0 deletions jsonschema-module-jakarta-validation/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.github.victools</groupId>
<artifactId>jsonschema-generator-parent</artifactId>
<version>4.17.0-SNAPSHOT</version>
</parent>
<artifactId>jsonschema-module-jakarta-validation</artifactId>

<name>Java JSON Schema Generator Module – jakarta.validation</name>
<description>Module for the jsonschema-generator – deriving JSON Schema attributes from jakarta.validation annotations</description>

<dependencies>
<dependency>
<groupId>com.github.victools</groupId>
<artifactId>jsonschema-generator</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Loading

0 comments on commit f2b0c82

Please sign in to comment.