Skip to content

Commit

Permalink
add java:openapi-generator generator
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Jul 11, 2024
1 parent 91e37a2 commit 4f7497b
Show file tree
Hide file tree
Showing 16 changed files with 463 additions and 119 deletions.
7 changes: 7 additions & 0 deletions generators/java/generators/openapi-generator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# java:openapi-generator sub-generator

Generates openapi import structure that generates backend code using `src/main/resources/swagger/api.yml` openapi definition.

[openapi-generator](https://github.com/OpenAPITools/openapi-generator/tree/master)
[openapi-generator-maven-plugin](https://github.com/OpenAPITools/openapi-generator/tree/master/modules/openapi-generator-maven-plugin)
[openapi-generator-gradle-plugin](https://github.com/OpenAPITools/openapi-generator/tree/master/modules/openapi-generator-gradle-plugin)
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`generator - java:openapi-generator gradle should call source snapshot 1`] = `
{
"addGradleBuildSrcDependencyCatalogLibraries": [
[
{
"libraryName": "openapi-generator",
"module": "org.openapitools:openapi-generator-gradle-plugin",
"scope": "implementation",
"version": "'GRADLE-OPENAPI-GENERATOR-VERSION'",
},
],
],
"addGradlePlugin": [
{
"id": "jhipster.openapi-generator-conventions",
},
],
"addJavaDependencies": [
[
{
"artifactId": "jackson-databind-nullable",
"groupId": "org.openapitools",
"version": "'JACKSON-DATABIND-NULLABLE-VERSION'",
},
],
],
}
`;

exports[`generator - java:openapi-generator gradle should match files snapshot 1`] = `
{
".yo-rc.json": {
"stateCleared": "modified",
},
"README.md.jhi.openapi-generator": {
"stateCleared": "modified",
},
"buildSrc/src/main/groovy/jhipster.openapi-conventions.gradle": {
"stateCleared": "modified",
},
"src/main/resources/swagger/api.yml": {
"stateCleared": "modified",
},
}
`;

exports[`generator - java:openapi-generator maven should call source snapshot 1`] = `
{
"addJavaDependencies": [
[
{
"artifactId": "jackson-databind-nullable",
"groupId": "org.openapitools",
"version": "'JACKSON-DATABIND-NULLABLE-VERSION'",
},
],
],
"addMavenDefinition": [
{
"pluginManagement": [
{
"additionalContent": " <executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>\${project.basedir}/src/main/resources/swagger/api.yml</inputSpec>
<generatorName>spring</generatorName>
<apiPackage>com.mycompany.myapp.web.api</apiPackage>
<modelPackage>com.mycompany.myapp.service.api.dto</modelPackage>
<supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate>
<skipValidateSpec>false</skipValidateSpec>
<configOptions>
<delegatePattern>true</delegatePattern>
<title>jhipster</title>
<useSpringBoot3>true</useSpringBoot3>
</configOptions>
</configuration>
</execution>
</executions>
",
"artifactId": "openapi-generator-maven-plugin",
"groupId": "org.openapitools",
"version": "'OPENAPI-GENERATOR-MAVEN-PLUGIN-VERSION'",
},
],
"plugins": [
{
"artifactId": "openapi-generator-maven-plugin",
"groupId": "org.openapitools",
},
],
},
],
}
`;

exports[`generator - java:openapi-generator maven should match files snapshot 1`] = `
{
".yo-rc.json": {
"stateCleared": "modified",
},
"README.md.jhi.openapi-generator": {
"stateCleared": "modified",
},
"src/main/resources/swagger/api.yml": {
"stateCleared": "modified",
},
}
`;
26 changes: 26 additions & 0 deletions generators/java/generators/openapi-generator/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright 2013-2024 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { JHipsterCommandDefinition } from '../../../base/api.js';

const command: JHipsterCommandDefinition = {
configs: {},
import: [],
};

export default command;
60 changes: 60 additions & 0 deletions generators/java/generators/openapi-generator/generator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright 2013-2024 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { basename, dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { before, it, describe, expect } from 'esmocha';

import { shouldSupportFeatures, testBlueprintSupport } from '../../../../test/support/tests.js';
import { fromMatrix, defaultHelpers as helpers, result } from '../../../../testing/index.js';
import Generator from './index.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const generator = `${basename(resolve(__dirname, '../../'))}:${basename(__dirname)}`;

describe(`generator - ${generator}`, () => {
shouldSupportFeatures(Generator);
describe('blueprint support', () => testBlueprintSupport(generator));

for (const [name, config] of Object.entries(fromMatrix({ buildTool: ['maven', 'gradle'] }))) {
describe(name, () => {
before(async () => {
await helpers
.runJHipster(generator)
.withMockedJHipsterGenerators()
.withMockedSource()
.withSharedApplication({})
.withJHipsterConfig(config);
});

it('should match files snapshot', () => {
expect(result.getStateSnapshot()).toMatchSnapshot();
});

it('should call source snapshot', () => {
expect(result.sourceCallsArg).toMatchSnapshot();
});

it('should compose with generators', () => {
expect(result.composedMockedGenerators).toMatchInlineSnapshot('[]');
});
});
}
});
181 changes: 181 additions & 0 deletions generators/java/generators/openapi-generator/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/**
* Copyright 2013-2024 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import BaseApplicationGenerator from '../../../base-application/index.js';
import { GRADLE_BUILD_SRC_MAIN_DIR } from '../../../generator-constants.js';
import { javaMainResourceTemplatesBlock } from '../../support/files.js';

export default class OpenapiGeneratorGenerator extends BaseApplicationGenerator {
async beforeQueue() {
if (!this.fromBlueprint) {
await this.composeWithBlueprints();
}

if (!this.delegateToBlueprint) {
await this.dependsOnBootstrapApplication();
}
}

get preparing() {
return this.asPreparingTaskGroup({});
}

get [BaseApplicationGenerator.PREPARING]() {
return this.delegateTasksToBlueprint(() => this.preparing);
}

get postPreparing() {
return this.asPostPreparingTaskGroup({});
}

get [BaseApplicationGenerator.POST_PREPARING]() {
return this.delegateTasksToBlueprint(() => this.postPreparing);
}

get preparingEachEntity() {
return this.asPreparingEachEntityTaskGroup({});
}

get [BaseApplicationGenerator.PREPARING_EACH_ENTITY]() {
return this.delegateTasksToBlueprint(() => this.preparingEachEntity);
}

get preparingEachEntityField() {
return this.asPreparingEachEntityFieldTaskGroup({});
}

get [BaseApplicationGenerator.PREPARING_EACH_ENTITY_FIELD]() {
return this.delegateTasksToBlueprint(() => this.preparingEachEntityField);
}

get preparingEachEntityRelationship() {
return this.asPreparingEachEntityRelationshipTaskGroup({});
}

get [BaseApplicationGenerator.PREPARING_EACH_ENTITY_RELATIONSHIP]() {
return this.delegateTasksToBlueprint(() => this.preparingEachEntityRelationship);
}

get postPreparingEachEntity() {
return this.asPostPreparingEachEntityTaskGroup({});
}

get [BaseApplicationGenerator.POST_PREPARING_EACH_ENTITY]() {
return this.delegateTasksToBlueprint(() => this.postPreparingEachEntity);
}

get default() {
return this.asDefaultTaskGroup({});
}

get [BaseApplicationGenerator.DEFAULT]() {
return this.delegateTasksToBlueprint(() => this.default);
}

get writing() {
return this.asWritingTaskGroup({
async writing({ application }) {
await this.writeFiles({
blocks: [
{ templates: ['README.md.jhi.openapi-generator'] },
javaMainResourceTemplatesBlock({ templates: ['swagger/api.yml'] }),
{
condition: ctx => ctx.buildToolGradle,
templates: [`${GRADLE_BUILD_SRC_MAIN_DIR}/jhipster.openapi-conventions.gradle`],
},
],
context: application,
});
},
});
}

get [BaseApplicationGenerator.WRITING]() {
return this.delegateTasksToBlueprint(() => this.writing);
}

get postWriting() {
return this.asPostWritingTaskGroup({
addDependencies({ source, application }) {
const { buildToolGradle, buildToolMaven, javaDependencies } = application;
source.addJavaDependencies!([
{
groupId: 'org.openapitools',
artifactId: 'jackson-databind-nullable',
version: javaDependencies!['jackson-databind-nullable'],
},
]);

if (buildToolMaven) {
source.addMavenDefinition!({
plugins: [{ groupId: 'org.openapitools', artifactId: 'openapi-generator-maven-plugin' }],
pluginManagement: [
{
groupId: 'org.openapitools',
artifactId: 'openapi-generator-maven-plugin',
version: javaDependencies!['openapi-generator-maven-plugin'],
additionalContent: ` <executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>\${project.basedir}/${application.srcMainResources}swagger/api.yml</inputSpec>
<generatorName>spring</generatorName>
<apiPackage>${application.packageName}.web.api</apiPackage>
<modelPackage>${application.packageName}.service.api.dto</modelPackage>
<supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate>
<skipValidateSpec>false</skipValidateSpec>
<configOptions>${
application.reactive
? `
<reactive>true</reactive>
`
: ''
}
<delegatePattern>true</delegatePattern>
<title>${application.dasherizedBaseName}</title>
<useSpringBoot3>true</useSpringBoot3>
</configOptions>
</configuration>
</execution>
</executions>
`,
},
],
});
}
if (buildToolGradle) {
source.addGradleBuildSrcDependencyCatalogLibraries?.([
{
libraryName: 'openapi-generator',
module: 'org.openapitools:openapi-generator-gradle-plugin',
version: javaDependencies!['gradle-openapi-generator'],
scope: 'implementation',
},
]);
source.addGradlePlugin?.({ id: 'jhipster.openapi-generator-conventions' });
}
},
});
}

get [BaseApplicationGenerator.POST_WRITING]() {
return this.delegateTasksToBlueprint(() => this.postWriting);
}
}
Loading

0 comments on commit 4f7497b

Please sign in to comment.