Skip to content

Commit e19196f

Browse files
committed
add java:openapi-generator generator
1 parent 91e37a2 commit e19196f

File tree

16 files changed

+407
-119
lines changed

16 files changed

+407
-119
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# java:openapi-generator sub-generator
2+
3+
Generates openapi import structure that generates backend code using `src/main/resources/swagger/api.yml` openapi definition.
4+
5+
[openapi-generator](https://github.com/OpenAPITools/openapi-generator/tree/master)
6+
[openapi-generator-maven-plugin](https://github.com/OpenAPITools/openapi-generator/tree/master/modules/openapi-generator-maven-plugin)
7+
[openapi-generator-gradle-plugin](https://github.com/OpenAPITools/openapi-generator/tree/master/modules/openapi-generator-gradle-plugin)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`generator - java:openapi-generator gradle should call source snapshot 1`] = `
4+
{
5+
"addGradleBuildSrcDependencyCatalogLibraries": [
6+
[
7+
{
8+
"libraryName": "openapi-generator",
9+
"module": "org.openapitools:openapi-generator-gradle-plugin",
10+
"scope": "implementation",
11+
"version": "'GRADLE-OPENAPI-GENERATOR-VERSION'",
12+
},
13+
],
14+
],
15+
"addGradlePlugin": [
16+
{
17+
"id": "jhipster.openapi-generator-conventions",
18+
},
19+
],
20+
"addJavaDependencies": [
21+
[
22+
{
23+
"artifactId": "jackson-databind-nullable",
24+
"groupId": "org.openapitools",
25+
"version": "'JACKSON-DATABIND-NULLABLE-VERSION'",
26+
},
27+
],
28+
],
29+
}
30+
`;
31+
32+
exports[`generator - java:openapi-generator gradle should match files snapshot 1`] = `
33+
{
34+
".yo-rc.json": {
35+
"stateCleared": "modified",
36+
},
37+
"README.md.jhi.openapi-generator": {
38+
"stateCleared": "modified",
39+
},
40+
"buildSrc/src/main/groovy/jhipster.openapi-conventions.gradle": {
41+
"stateCleared": "modified",
42+
},
43+
"src/main/resources/swagger/api.yml": {
44+
"stateCleared": "modified",
45+
},
46+
}
47+
`;
48+
49+
exports[`generator - java:openapi-generator maven should call source snapshot 1`] = `
50+
{
51+
"addJavaDependencies": [
52+
[
53+
{
54+
"artifactId": "jackson-databind-nullable",
55+
"groupId": "org.openapitools",
56+
"version": "'JACKSON-DATABIND-NULLABLE-VERSION'",
57+
},
58+
],
59+
],
60+
"addMavenDefinition": [
61+
{
62+
"pluginManagement": [
63+
{
64+
"additionalContent": " <executions>
65+
<execution>
66+
<goals>
67+
<goal>generate</goal>
68+
</goals>
69+
<configuration>
70+
<inputSpec>\${project.basedir}/src/main/resources/swagger/api.yml</inputSpec>
71+
<generatorName>spring</generatorName>
72+
<apiPackage>com.mycompany.myapp.web.api</apiPackage>
73+
<modelPackage>com.mycompany.myapp.service.api.dto</modelPackage>
74+
<supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate>
75+
<skipValidateSpec>false</skipValidateSpec>
76+
<configOptions>
77+
<delegatePattern>true</delegatePattern>
78+
<title>jhipster</title>
79+
<useSpringBoot3>true</useSpringBoot3>
80+
</configOptions>
81+
</configuration>
82+
</execution>
83+
</executions>
84+
",
85+
"artifactId": "openapi-generator-maven-plugin",
86+
"groupId": "org.openapitools",
87+
"version": "'OPENAPI-GENERATOR-MAVEN-PLUGIN-VERSION'",
88+
},
89+
],
90+
"plugins": [
91+
{
92+
"artifactId": "openapi-generator-maven-plugin",
93+
"groupId": "org.openapitools",
94+
},
95+
],
96+
},
97+
],
98+
}
99+
`;
100+
101+
exports[`generator - java:openapi-generator maven should match files snapshot 1`] = `
102+
{
103+
".yo-rc.json": {
104+
"stateCleared": "modified",
105+
},
106+
"README.md.jhi.openapi-generator": {
107+
"stateCleared": "modified",
108+
},
109+
"src/main/resources/swagger/api.yml": {
110+
"stateCleared": "modified",
111+
},
112+
}
113+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright 2013-2024 the original author or authors from the JHipster project.
3+
*
4+
* This file is part of the JHipster project, see https://www.jhipster.tech/
5+
* for more information.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* https://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
import type { JHipsterCommandDefinition } from '../../../base/api.js';
20+
21+
const command: JHipsterCommandDefinition = {
22+
configs: {},
23+
import: [],
24+
};
25+
26+
export default command;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Copyright 2013-2024 the original author or authors from the JHipster project.
3+
*
4+
* This file is part of the JHipster project, see https://www.jhipster.tech/
5+
* for more information.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* https://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
import { basename, dirname, resolve } from 'node:path';
20+
import { fileURLToPath } from 'node:url';
21+
import { before, it, describe, expect } from 'esmocha';
22+
23+
import { shouldSupportFeatures, testBlueprintSupport } from '../../../../test/support/tests.js';
24+
import { fromMatrix, defaultHelpers as helpers, result } from '../../../../testing/index.js';
25+
import Generator from './index.js';
26+
27+
const __filename = fileURLToPath(import.meta.url);
28+
const __dirname = dirname(__filename);
29+
30+
const generator = `${basename(resolve(__dirname, '../../'))}:${basename(__dirname)}`;
31+
32+
describe(`generator - ${generator}`, () => {
33+
shouldSupportFeatures(Generator);
34+
describe('blueprint support', () => testBlueprintSupport(generator));
35+
36+
for (const [name, config] of Object.entries(fromMatrix({ buildTool: ['maven', 'gradle'] }))) {
37+
describe(name, () => {
38+
before(async () => {
39+
await helpers
40+
.runJHipster(generator)
41+
.withMockedJHipsterGenerators()
42+
.withMockedSource()
43+
.withSharedApplication({})
44+
.withJHipsterConfig(config);
45+
});
46+
47+
it('should match files snapshot', () => {
48+
expect(result.getStateSnapshot()).toMatchSnapshot();
49+
});
50+
51+
it('should call source snapshot', () => {
52+
expect(result.sourceCallsArg).toMatchSnapshot();
53+
});
54+
55+
it('should compose with generators', () => {
56+
expect(result.composedMockedGenerators).toMatchInlineSnapshot('[]');
57+
});
58+
});
59+
}
60+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/**
2+
* Copyright 2013-2024 the original author or authors from the JHipster project.
3+
*
4+
* This file is part of the JHipster project, see https://www.jhipster.tech/
5+
* for more information.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* https://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
import BaseApplicationGenerator from '../../../base-application/index.js';
20+
import { GRADLE_BUILD_SRC_MAIN_DIR } from '../../../generator-constants.js';
21+
import { javaMainResourceTemplatesBlock } from '../../support/files.js';
22+
23+
export default class OpenapiGeneratorGenerator extends BaseApplicationGenerator {
24+
async beforeQueue() {
25+
if (!this.fromBlueprint) {
26+
await this.composeWithBlueprints();
27+
}
28+
29+
if (!this.delegateToBlueprint) {
30+
await this.dependsOnBootstrapApplication();
31+
}
32+
}
33+
34+
get writing() {
35+
return this.asWritingTaskGroup({
36+
async writing({ application }) {
37+
await this.writeFiles({
38+
blocks: [
39+
{ templates: ['README.md.jhi.openapi-generator'] },
40+
javaMainResourceTemplatesBlock({ templates: ['swagger/api.yml'] }),
41+
{
42+
condition: ctx => ctx.buildToolGradle,
43+
templates: [`${GRADLE_BUILD_SRC_MAIN_DIR}/jhipster.openapi-conventions.gradle`],
44+
},
45+
],
46+
context: application,
47+
});
48+
},
49+
});
50+
}
51+
52+
get [BaseApplicationGenerator.WRITING]() {
53+
return this.delegateTasksToBlueprint(() => this.writing);
54+
}
55+
56+
get postWriting() {
57+
return this.asPostWritingTaskGroup({
58+
addDependencies({ source, application }) {
59+
const { buildToolGradle, buildToolMaven, javaDependencies } = application;
60+
source.addJavaDependencies!([
61+
{
62+
groupId: 'org.openapitools',
63+
artifactId: 'jackson-databind-nullable',
64+
version: javaDependencies!['jackson-databind-nullable'],
65+
},
66+
]);
67+
68+
if (buildToolMaven) {
69+
source.addMavenDefinition!({
70+
plugins: [{ groupId: 'org.openapitools', artifactId: 'openapi-generator-maven-plugin' }],
71+
pluginManagement: [
72+
{
73+
groupId: 'org.openapitools',
74+
artifactId: 'openapi-generator-maven-plugin',
75+
version: javaDependencies!['openapi-generator-maven-plugin'],
76+
additionalContent: ` <executions>
77+
<execution>
78+
<goals>
79+
<goal>generate</goal>
80+
</goals>
81+
<configuration>
82+
<inputSpec>\${project.basedir}/${application.srcMainResources}swagger/api.yml</inputSpec>
83+
<generatorName>spring</generatorName>
84+
<apiPackage>${application.packageName}.web.api</apiPackage>
85+
<modelPackage>${application.packageName}.service.api.dto</modelPackage>
86+
<supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate>
87+
<skipValidateSpec>false</skipValidateSpec>
88+
<configOptions>${
89+
application.reactive
90+
? `
91+
<reactive>true</reactive>
92+
`
93+
: ''
94+
}
95+
<delegatePattern>true</delegatePattern>
96+
<title>${application.dasherizedBaseName}</title>
97+
<useSpringBoot3>true</useSpringBoot3>
98+
</configOptions>
99+
</configuration>
100+
</execution>
101+
</executions>
102+
`,
103+
},
104+
],
105+
});
106+
}
107+
if (buildToolGradle) {
108+
source.addGradleBuildSrcDependencyCatalogLibraries?.([
109+
{
110+
libraryName: 'openapi-generator',
111+
module: 'org.openapitools:openapi-generator-gradle-plugin',
112+
version: javaDependencies!['gradle-openapi-generator'],
113+
scope: 'implementation',
114+
},
115+
]);
116+
source.addGradlePlugin?.({ id: 'jhipster.openapi-generator-conventions' });
117+
}
118+
},
119+
});
120+
}
121+
122+
get [BaseApplicationGenerator.POST_WRITING]() {
123+
return this.delegateTasksToBlueprint(() => this.postWriting);
124+
}
125+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright 2013-2024 the original author or authors from the JHipster project.
3+
*
4+
* This file is part of the JHipster project, see https://www.jhipster.tech/
5+
* for more information.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* https://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
export { default } from './generator.js';
20+
export { default as command } from './command.js';

0 commit comments

Comments
 (0)