From 3436fa224b7f3bf8c5e274a010a24d9598a013b1 Mon Sep 17 00:00:00 2001 From: Karl von Randow Date: Thu, 22 Aug 2024 21:29:55 +1200 Subject: [PATCH] java-jaxrs: Add config options to control the output path for API interfaces vs implementation --- .changeset/big-mugs-destroy.md | 7 +++++ packages/java-jaxrs-client/README.md | 2 ++ packages/java-jaxrs-client/src/index.ts | 34 ++++++++++++++----------- packages/java-jaxrs-common/src/index.ts | 6 ++++- packages/java-jaxrs-common/src/types.ts | 3 +++ packages/java-jaxrs-server/README.md | 2 ++ packages/java-jaxrs-server/src/index.ts | 26 +++++++++++-------- 7 files changed, 53 insertions(+), 27 deletions(-) create mode 100644 .changeset/big-mugs-destroy.md diff --git a/.changeset/big-mugs-destroy.md b/.changeset/big-mugs-destroy.md new file mode 100644 index 00000000..58aece5b --- /dev/null +++ b/.changeset/big-mugs-destroy.md @@ -0,0 +1,7 @@ +--- +"@openapi-generator-plus/java-jaxrs-client-generator": minor +"@openapi-generator-plus/java-jaxrs-generator-common": minor +"@openapi-generator-plus/java-jaxrs-server-generator": minor +--- + +Add config options to control the output path for API interfaces vs implementation diff --git a/packages/java-jaxrs-client/README.md b/packages/java-jaxrs-client/README.md index 9d828fe8..78965b5e 100644 --- a/packages/java-jaxrs-client/README.md +++ b/packages/java-jaxrs-client/README.md @@ -42,6 +42,8 @@ constantStyle: camelCase |`apiSpiPackage`|`string`|Package for SPI interfaces for extra functionality.|`"${package}.spi`| |`modelPackage`|`string`|Package for API model classes.|`"${package}.model"`| |`relativeSourceOutputPath`|`string`|The path to output generated source code, relative to the output path.|`./` or `./src/main/java` if `maven` is specified.| +|`relativeApiSourceOutputPath`|`string`|The path to output generated API interface source code, relative to the output path.|`${relativeSourceOutputPath}`| +|`relativeApiImplSourceOutputPath`|`string`|The path to output generated API implementation source code, relative to the output path.|`${relativeSourceOutputPath}`| ### Code style diff --git a/packages/java-jaxrs-client/src/index.ts b/packages/java-jaxrs-client/src/index.ts index 2f6a5c04..717c1327 100644 --- a/packages/java-jaxrs-client/src/index.ts +++ b/packages/java-jaxrs-client/src/index.ts @@ -45,6 +45,8 @@ export default function createGenerator(config: CodegenConfig, context: JavaGene myContext.additionalExportTemplates = async(outputPath, doc, hbs, rootContext) => { const relativeSourceOutputPath = generatorOptions.relativeSourceOutputPath + const relativeApiSourceOutputPath = generatorOptions.relativeApiSourceOutputPath + const relativeApiImplSourceOutputPath = generatorOptions.relativeApiImplSourceOutputPath const apiPackagePath = packageToPath(generatorOptions.apiPackage) @@ -54,7 +56,7 @@ export default function createGenerator(config: CodegenConfig, context: JavaGene continue } - await emit('api', path.join(outputPath, relativeSourceOutputPath, apiPackagePath, `${context.generator().toClassName(group.name)}Api.java`), + await emit('api', path.join(outputPath, relativeApiSourceOutputPath, apiPackagePath, `${context.generator().toClassName(group.name)}Api.java`), { ...rootContext, ...group, operations }, true, hbs) } @@ -64,7 +66,7 @@ export default function createGenerator(config: CodegenConfig, context: JavaGene if (!operations.length) { continue } - await emit('apiImpl', path.join(outputPath, relativeSourceOutputPath, apiImplPackagePath, `${context.generator().toClassName(group.name)}ApiImpl.java`), + await emit('apiImpl', path.join(outputPath, relativeApiImplSourceOutputPath, apiImplPackagePath, `${context.generator().toClassName(group.name)}ApiImpl.java`), { ...rootContext, ...doc, ...group, operations }, true, hbs) } @@ -73,7 +75,7 @@ export default function createGenerator(config: CodegenConfig, context: JavaGene for (const group of doc.groups) { for (const operation of group.operations) { if (operation.useParamsClasses) { - await emit('apiParams', path.join(outputPath, relativeSourceOutputPath, apiParamsPackagePath, `${context.generator().toClassName(operation.uniqueName)}Params.java`), + await emit('apiParams', path.join(outputPath, relativeApiSourceOutputPath, apiParamsPackagePath, `${context.generator().toClassName(operation.uniqueName)}Params.java`), { ...rootContext, group, ...operation }, true, hbs) } } @@ -83,10 +85,10 @@ export default function createGenerator(config: CodegenConfig, context: JavaGene await emit('ApiConstants', path.join(outputPath, relativeSourceOutputPath, apiPackagePath, 'ApiConstants.java'), { ...rootContext, servers: doc.servers, server: doc.servers && doc.servers.length ? doc.servers[0] : null, }, true, hbs) - await emit('ApiInvoker', path.join(outputPath, relativeSourceOutputPath, apiPackagePath, 'ApiInvoker.java'), + await emit('ApiInvoker', path.join(outputPath, relativeApiImplSourceOutputPath, apiPackagePath, 'ApiInvoker.java'), { ...rootContext, ...doc }, true, hbs) - await emit('ApiProviders', path.join(outputPath, relativeSourceOutputPath, apiPackagePath, 'ApiProviders.java'), { + await emit('ApiProviders', path.join(outputPath, relativeApiImplSourceOutputPath, apiPackagePath, 'ApiProviders.java'), { ...rootContext, servers: doc.servers, server: doc.servers && doc.servers.length ? doc.servers[0] : undefined, }, true, hbs) @@ -96,25 +98,25 @@ export default function createGenerator(config: CodegenConfig, context: JavaGene if (!operations.length) { continue } - await emit('apiSpec', path.join(outputPath, relativeSourceOutputPath, apiSpecPackagePath, `${context.generator().toClassName(group.name)}ApiSpec.java`), + await emit('apiSpec', path.join(outputPath, relativeApiImplSourceOutputPath, apiSpecPackagePath, `${context.generator().toClassName(group.name)}ApiSpec.java`), { ...rootContext, ...group, operations }, true, hbs) } - await emit('UnexpectedApiException', path.join(outputPath, relativeSourceOutputPath, apiPackagePath, 'UnexpectedApiException.java'), { + await emit('UnexpectedApiException', path.join(outputPath, relativeApiImplSourceOutputPath, apiPackagePath, 'UnexpectedApiException.java'), { ...rootContext, }, true, hbs) - await emit('UnexpectedResponseException', path.join(outputPath, relativeSourceOutputPath, apiPackagePath, 'UnexpectedResponseException.java'), { + await emit('UnexpectedResponseException', path.join(outputPath, relativeApiImplSourceOutputPath, apiPackagePath, 'UnexpectedResponseException.java'), { ...rootContext, }, true, hbs) - await emit('UnprocessableResponseException', path.join(outputPath, relativeSourceOutputPath, apiPackagePath, 'UnprocessableResponseException.java'), { + await emit('UnprocessableResponseException', path.join(outputPath, relativeApiImplSourceOutputPath, apiPackagePath, 'UnprocessableResponseException.java'), { ...rootContext, }, true, hbs) - await emit('UnexpectedTimeoutException', path.join(outputPath, relativeSourceOutputPath, apiPackagePath, 'UnexpectedTimeoutException.java'), { + await emit('UnexpectedTimeoutException', path.join(outputPath, relativeApiImplSourceOutputPath, apiPackagePath, 'UnexpectedTimeoutException.java'), { ...rootContext, }, true, hbs) const apiSpiPackagePath = packageToPath(generatorOptions.apiSpiPackage) - await emit('spi/ApiAuthorizationProvider', path.join(outputPath, relativeSourceOutputPath, apiSpiPackagePath, 'ApiAuthorizationProvider.java'), { + await emit('spi/ApiAuthorizationProvider', path.join(outputPath, relativeApiImplSourceOutputPath, apiSpiPackagePath, 'ApiAuthorizationProvider.java'), { ...rootContext, }, true, hbs) @@ -125,18 +127,20 @@ export default function createGenerator(config: CodegenConfig, context: JavaGene myContext.additionalCleanPathPatterns = () => { const relativeSourceOutputPath = generatorOptions.relativeSourceOutputPath + const relativeApiSourceOutputPath = generatorOptions.relativeApiSourceOutputPath + const relativeApiImplSourceOutputPath = generatorOptions.relativeApiImplSourceOutputPath const apiPackagePath = packageToPath(generatorOptions.apiPackage) const apiImplPackagePath = packageToPath(generatorOptions.apiImplPackage) const apiSpecPackagePath = packageToPath(generatorOptions.apiSpecPackage) const result = [ - path.join(relativeSourceOutputPath, apiPackagePath, '*Api.java'), - path.join(relativeSourceOutputPath, apiImplPackagePath, '*ApiImpl.java'), - path.join(relativeSourceOutputPath, apiSpecPackagePath, '*ApiSpec.java'), + path.join(relativeApiSourceOutputPath, apiPackagePath, '*Api.java'), + path.join(relativeApiImplSourceOutputPath, apiImplPackagePath, '*ApiImpl.java'), + path.join(relativeApiImplSourceOutputPath, apiSpecPackagePath, '*ApiSpec.java'), ] if (generatorOptions.apiParamsPackage) { const apiParamsPackagePath = packageToPath(generatorOptions.apiParamsPackage) - result.push(path.join(relativeSourceOutputPath, apiParamsPackagePath, '*Params.java')) + result.push(path.join(relativeApiSourceOutputPath, apiParamsPackagePath, '*Params.java')) } if (context.additionalCleanPathPatterns) { result.push(...context.additionalCleanPathPatterns()) diff --git a/packages/java-jaxrs-common/src/index.ts b/packages/java-jaxrs-common/src/index.ts index c27efbb9..4a33276d 100644 --- a/packages/java-jaxrs-common/src/index.ts +++ b/packages/java-jaxrs-common/src/index.ts @@ -98,6 +98,8 @@ export function options(config: CodegenConfig, context: JavaGeneratorContext): C const apiPackage = configString(config, 'apiPackage', packageName) const maven = configObject(config, 'maven', undefined) const customTemplates = configString(config, 'customTemplates', undefined) + const relativeSourceOutputPath = computeRelativeSourceOutputPath(config) + const options: CodegenOptionsJava = { ...javaLikeOptions(config, createJavaLikeContext(context)), apiPackage, @@ -121,7 +123,9 @@ export function options(config: CodegenConfig, context: JavaGeneratorContext): C version: configString(maven, 'version', '0.0.1', 'maven.'), versions: configObject(maven, 'versions', {}, 'maven.'), } : null, - relativeSourceOutputPath: computeRelativeSourceOutputPath(config), + relativeSourceOutputPath, + relativeApiSourceOutputPath: configString(config, 'relativeApiSourceOutputPath', relativeSourceOutputPath), + relativeApiImplSourceOutputPath: configString(config, 'relativeApiImplSourceOutputPath', relativeSourceOutputPath), relativeResourcesOutputPath: computeRelativeResourcesOutputPath(config), relativeTestOutputPath: computeRelativeTestOutputPath(config), relativeTestResourcesOutputPath: computeRelativeTestResourcesOutputPath(config), diff --git a/packages/java-jaxrs-common/src/types.ts b/packages/java-jaxrs-common/src/types.ts index f21fe0de..fae3ebb7 100644 --- a/packages/java-jaxrs-common/src/types.ts +++ b/packages/java-jaxrs-common/src/types.ts @@ -25,6 +25,9 @@ export interface CodegenOptionsJava extends JavaLikeOptions { maven: MavenOptions | null relativeSourceOutputPath: string + relativeApiSourceOutputPath: string + relativeApiImplSourceOutputPath: string + relativeResourcesOutputPath?: string relativeTestOutputPath: string relativeTestResourcesOutputPath?: string diff --git a/packages/java-jaxrs-server/README.md b/packages/java-jaxrs-server/README.md index 43220d49..83ced982 100644 --- a/packages/java-jaxrs-server/README.md +++ b/packages/java-jaxrs-server/README.md @@ -33,6 +33,8 @@ The available config file properties are: |`invokerPackage`|`string` \| `null`|Package for API invoker classes.|`"${package}.app"`| |`validationPackage`|`string`|Package for API validation classes.|`"${package}.validation"`| |`relativeSourceOutputPath`|`string`|The path to output generated source code, relative to the output path.|`./` or `./src/main/java` if `maven` is specified.| +|`relativeApiSourceOutputPath`|`string`|The path to output generated API interface source code, relative to the output path.|`${relativeSourceOutputPath}`| +|`relativeApiImplSourceOutputPath`|`string`|The path to output generated API implementation source code, relative to the output path.|`${relativeSourceOutputPath}`| ### Code style diff --git a/packages/java-jaxrs-server/src/index.ts b/packages/java-jaxrs-server/src/index.ts index 47d1ebf2..b7f98e52 100644 --- a/packages/java-jaxrs-server/src/index.ts +++ b/packages/java-jaxrs-server/src/index.ts @@ -49,6 +49,8 @@ export const createGenerator: CodegenGeneratorConstructor myContext.additionalExportTemplates = async(outputPath, doc, hbs, rootContext) => { const relativeSourceOutputPath = generatorOptions.relativeSourceOutputPath + const relativeApiSourceOutputPath = generatorOptions.relativeApiSourceOutputPath + const relativeApiImplSourceOutputPath = generatorOptions.relativeApiImplSourceOutputPath const apiPackagePath = packageToPath(generatorOptions.apiPackage) for (const group of doc.groups) { @@ -56,7 +58,7 @@ export const createGenerator: CodegenGeneratorConstructor if (!operations.length) { continue } - await emit('api', path.join(outputPath, relativeSourceOutputPath, apiPackagePath, `${context.generator().toClassName(group.name)}Api.java`), + await emit('api', path.join(outputPath, relativeApiSourceOutputPath, apiPackagePath, `${context.generator().toClassName(group.name)}Api.java`), { ...rootContext, ...group, operations }, true, hbs) } @@ -66,7 +68,7 @@ export const createGenerator: CodegenGeneratorConstructor if (!operations.length) { continue } - await emit('apiImpl', path.join(outputPath, relativeSourceOutputPath, apiImplPackagePath, `${context.generator().toClassName(group.name)}ApiImpl.java`), + await emit('apiImpl', path.join(outputPath, relativeApiImplSourceOutputPath, apiImplPackagePath, `${context.generator().toClassName(group.name)}ApiImpl.java`), { ...rootContext, ...group, operations }, true, hbs) } @@ -76,7 +78,7 @@ export const createGenerator: CodegenGeneratorConstructor if (!operations.length) { continue } - await emit('apiService', path.join(outputPath, relativeSourceOutputPath, apiServicePackagePath, `${context.generator().toClassName(group.name)}ApiService.java`), + await emit('apiService', path.join(outputPath, relativeApiImplSourceOutputPath, apiServicePackagePath, `${context.generator().toClassName(group.name)}ApiService.java`), { ...rootContext, ...group, operations }, true, hbs) } @@ -87,7 +89,7 @@ export const createGenerator: CodegenGeneratorConstructor if (!operations.length) { continue } - await emit('apiServiceImpl', path.join(outputPath, relativeSourceOutputPath, apiServiceImplPackagePath, `${context.generator().toClassName(group.name)}ApiServiceImpl.java`), + await emit('apiServiceImpl', path.join(outputPath, relativeApiImplSourceOutputPath, apiServiceImplPackagePath, `${context.generator().toClassName(group.name)}ApiServiceImpl.java`), { ...rootContext, ...group }, false, hbs) } } @@ -97,7 +99,7 @@ export const createGenerator: CodegenGeneratorConstructor for (const group of doc.groups) { for (const operation of group.operations) { if (operation.useParamsClasses) { - await emit('apiParams', path.join(outputPath, relativeSourceOutputPath, apiParamsPackagePath, `${context.generator().toClassName(operation.uniqueName)}Params.java`), + await emit('apiParams', path.join(outputPath, relativeApiSourceOutputPath, apiParamsPackagePath, `${context.generator().toClassName(operation.uniqueName)}Params.java`), { ...rootContext, group, ...operation }, true, hbs) } } @@ -107,13 +109,13 @@ export const createGenerator: CodegenGeneratorConstructor const invokerPackagePath = generatorOptions.invokerPackage ? packageToPath(generatorOptions.invokerPackage) : undefined if (invokerPackagePath) { const basePath = apiBasePath(doc.servers) - await emit('invoker', path.join(outputPath, relativeSourceOutputPath, invokerPackagePath, 'RestApplication.java'), + await emit('invoker', path.join(outputPath, relativeApiImplSourceOutputPath, invokerPackagePath, 'RestApplication.java'), { ...rootContext, ...doc.info, basePath }, false, hbs) } const providerPackagePath = generatorOptions.apiProviderPackage ? packageToPath(generatorOptions.apiProviderPackage) : undefined if (providerPackagePath) { - await emit('ApiJaxbJsonProvider', path.join(outputPath, relativeSourceOutputPath, providerPackagePath, 'ApiJaxbJsonProvider.java'), + await emit('ApiJaxbJsonProvider', path.join(outputPath, relativeApiImplSourceOutputPath, providerPackagePath, 'ApiJaxbJsonProvider.java'), { ...rootContext }, true, hbs) } @@ -124,19 +126,21 @@ export const createGenerator: CodegenGeneratorConstructor myContext.additionalCleanPathPatterns = () => { const relativeSourceOutputPath = generatorOptions.relativeSourceOutputPath + const relativeApiSourceOutputPath = generatorOptions.relativeApiSourceOutputPath + const relativeApiImplSourceOutputPath = generatorOptions.relativeApiImplSourceOutputPath const apiPackagePath = packageToPath(generatorOptions.apiPackage) const apiImplPackagePath = packageToPath(generatorOptions.apiImplPackage) const apiServicePackagePath = packageToPath(generatorOptions.apiServicePackage) const result = [ - path.join(relativeSourceOutputPath, apiPackagePath, '*Api.java'), - path.join(relativeSourceOutputPath, apiImplPackagePath, '*ApiImpl.java'), - path.join(relativeSourceOutputPath, apiServicePackagePath, '*ApiService.java'), + path.join(relativeApiSourceOutputPath, apiPackagePath, '*Api.java'), + path.join(relativeApiImplSourceOutputPath, apiImplPackagePath, '*ApiImpl.java'), + path.join(relativeApiImplSourceOutputPath, apiServicePackagePath, '*ApiService.java'), ] if (generatorOptions.apiParamsPackage) { const apiParamsPackagePath = packageToPath(generatorOptions.apiParamsPackage) - result.push(path.join(relativeSourceOutputPath, apiParamsPackagePath, '*Params.java')) + result.push(path.join(relativeApiSourceOutputPath, apiParamsPackagePath, '*Params.java')) } if (context.additionalCleanPathPatterns) { result.push(...context.additionalCleanPathPatterns())