Skip to content

Commit

Permalink
feat: Allow to use contraints from jakarta namespace for Java and Kot…
Browse files Browse the repository at this point in the history
…lin models

Description
The JAVA_CONSTRAINT_PRESET uses constraints from the javax namespace.
Since Java 17 the constraints are moved to the jakarta namespace.
Add an options to the JAVA_CONSTRAINT_PRESET to use the jakarta namespace for constraint imports.

Related issue
fixes: (#1807)[#1807]
  • Loading branch information
tomdevroomen committed Jul 19, 2024
1 parent dc6c9ac commit f742073
Show file tree
Hide file tree
Showing 18 changed files with 535 additions and 112 deletions.
17 changes: 17 additions & 0 deletions examples/java-generate-jakarta-constraint-annotation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Jakarta validation constraints annotations

A basic example that shows how Java data models having `jakarta.validation.constraints` annotations can be generated.

## How to run this example

Run this example using:

```sh
npm i && npm run start
```

If you are on Windows, use the `start:windows` script instead:

```sh
npm i && npm run start:windows
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Should be able to generate models with jakarta.validation.constraints annotations and should log expected output to console 1`] = `
Array [
"public class JakartaAnnotation {
@NotNull
@Min(0)
private double minNumberProp;
@NotNull
@Max(99)
private double maxNumberProp;
@Size(min=2, max=3)
private Object[] arrayProp;
@Pattern(regexp=\\"^I_\\")
@Size(min=3)
private String stringProp;
private Map<String, Object> additionalProperties;
public double getMinNumberProp() { return this.minNumberProp; }
public void setMinNumberProp(double minNumberProp) { this.minNumberProp = minNumberProp; }
public double getMaxNumberProp() { return this.maxNumberProp; }
public void setMaxNumberProp(double maxNumberProp) { this.maxNumberProp = maxNumberProp; }
public Object[] getArrayProp() { return this.arrayProp; }
public void setArrayProp(Object[] arrayProp) { this.arrayProp = arrayProp; }
public String getStringProp() { return this.stringProp; }
public void setStringProp(String stringProp) { this.stringProp = stringProp; }
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
}",
]
`;
15 changes: 15 additions & 0 deletions examples/java-generate-jakarta-constraint-annotation/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const spy = jest.spyOn(global.console, 'log').mockImplementation(() => {
return;
});
import { generate } from './index';

describe('Should be able to generate models with jakarta.validation.constraints annotations', () => {
afterAll(() => {
jest.restoreAllMocks();
});
test('and should log expected output to console', async () => {
await generate();
expect(spy.mock.calls.length).toEqual(1);
expect(spy.mock.calls[0]).toMatchSnapshot();
});
});
34 changes: 34 additions & 0 deletions examples/java-generate-jakarta-constraint-annotation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { JavaGenerator, JAVA_CONSTRAINTS_PRESET } from '../../src';

const generator = new JavaGenerator({
presets: [
{
preset: JAVA_CONSTRAINTS_PRESET,
options: {
importFrom: 'jakarta'
}
}
]
});
const jsonSchemaDraft7 = {
$schema: 'http://json-schema.org/draft-07/schema#',
$id: 'JakartaAnnotation',
type: 'object',
properties: {
min_number_prop: { type: 'number', minimum: 0 },
max_number_prop: { type: 'number', exclusiveMaximum: 100 },
array_prop: { type: 'array', minItems: 2, maxItems: 3 },
string_prop: { type: 'string', pattern: '^I_', minLength: 3 }
},
required: ['min_number_prop', 'max_number_prop']
};

export async function generate(): Promise<void> {
const models = await generator.generate(jsonSchemaDraft7);
for (const model of models) {
console.log(model.result);
}
}
if (require.main === module) {
generate();
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions examples/java-generate-jakarta-constraint-annotation/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"config" : { "example_name" : "java-generate-jakarta-constraint-annotation" },
"scripts": {
"install": "cd ../.. && npm i",
"start": "../../node_modules/.bin/ts-node --cwd ../../ ./examples/$npm_package_config_example_name/index.ts",
"start:windows": "..\\..\\node_modules\\.bin\\ts-node --cwd ..\\..\\ .\\examples\\%npm_package_config_example_name%\\index.ts",
"test": "../../node_modules/.bin/jest --config=../../jest.config.js ./examples/$npm_package_config_example_name/index.spec.ts",
"test:windows": "..\\..\\node_modules\\.bin\\jest --config=..\\..\\jest.config.js examples/%npm_package_config_example_name%/index.spec.ts"
}
}
17 changes: 17 additions & 0 deletions examples/kotlin-generate-jakarta-constraint-annotation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Jakarta validation constraints annotations

A basic example that shows how Kotlin data models having `jakarta.validation.constraints` annotations can be generated.

## How to run this example

Run this example using:

```sh
npm i && npm run start
```

If you are on Windows, use the `start:windows` script instead:

```sh
npm i && npm run start:windows
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Should be able to generate models with jakarta.validation.constraints annotations and should log expected output to console 1`] = `
Array [
"data class JakartaAnnotation(
@get:NotNull
@get:Min(0)
val minNumberProp: Double,
@get:NotNull
@get:Max(99)
val maxNumberProp: Double,
@get:Min(101)
val minNumberPropExclusive: Double? = null,
@get:Size(min=2, max=3)
val arrayProp: List<Any>? = null,
@get:Pattern(regexp=\\"^I_\\")
@get:Size(min=3)
val stringProp: String? = null,
val additionalProperties: Map<String, Any>? = null,
)",
]
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const spy = jest.spyOn(global.console, 'log').mockImplementation(() => {
return;
});
import { generate } from './index';

describe('Should be able to generate models with jakarta.validation.constraints annotations', () => {
afterAll(() => {
jest.restoreAllMocks();
});
test('and should log expected output to console', async () => {
await generate();
expect(spy.mock.calls.length).toEqual(1);
expect(spy.mock.calls[0]).toMatchSnapshot();
});
});
35 changes: 35 additions & 0 deletions examples/kotlin-generate-jakarta-constraint-annotation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { KotlinGenerator, KOTLIN_CONSTRAINTS_PRESET } from '../../src';

const generator = new KotlinGenerator({
presets: [
{
preset: KOTLIN_CONSTRAINTS_PRESET,
options: {
importFrom: 'jakarta'
}
}
]
});
const jsonSchemaDraft7 = {
$schema: 'http://json-schema.org/draft-07/schema#',
$id: 'JakartaAnnotation',
type: 'object',
properties: {
min_number_prop: { type: 'number', minimum: 0 },
max_number_prop: { type: 'number', exclusiveMaximum: 100 },
min_number_prop_exclusive: { type: 'number', exclusiveMinimum: 100 },
array_prop: { type: 'array', minItems: 2, maxItems: 3 },
string_prop: { type: 'string', pattern: '^I_', minLength: 3 }
},
required: ['min_number_prop', 'max_number_prop']
};

export async function generate(): Promise<void> {
const models = await generator.generate(jsonSchemaDraft7);
for (const model of models) {
console.log(model.result);
}
}
if (require.main === module) {
generate();
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"config" : { "example_name" : "kotlin-generate-jakarta-constraint-annotation" },
"scripts": {
"install": "cd ../.. && npm i",
"start": "../../node_modules/.bin/ts-node --cwd ../../ ./examples/$npm_package_config_example_name/index.ts",
"start:windows": "..\\..\\node_modules\\.bin\\ts-node --cwd ..\\..\\ .\\examples\\%npm_package_config_example_name%\\index.ts",
"test": "../../node_modules/.bin/jest --config=../../jest.config.js ./examples/$npm_package_config_example_name/index.spec.ts",
"test:windows": "..\\..\\node_modules\\.bin\\jest --config=..\\..\\jest.config.js examples/%npm_package_config_example_name%/index.spec.ts"
}
}
Loading

0 comments on commit f742073

Please sign in to comment.