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

feat: allow to use constraints from jakarta namespace for Java and Kotlin models #2073

Merged
merged 1 commit into from
Jul 19, 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
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; }
}",
]
`;
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.

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"
}
}
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
Loading