Skip to content

Commit

Permalink
java-jaxrs: Add config options to control the output path for API int…
Browse files Browse the repository at this point in the history
…erfaces vs implementation
  • Loading branch information
karlvr committed Aug 22, 2024
1 parent 2e6e7dd commit 3436fa2
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 27 deletions.
7 changes: 7 additions & 0 deletions .changeset/big-mugs-destroy.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions packages/java-jaxrs-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 19 additions & 15 deletions packages/java-jaxrs-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}
}
Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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())
Expand Down
6 changes: 5 additions & 1 deletion packages/java-jaxrs-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand Down
3 changes: 3 additions & 0 deletions packages/java-jaxrs-common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export interface CodegenOptionsJava extends JavaLikeOptions {

maven: MavenOptions | null
relativeSourceOutputPath: string
relativeApiSourceOutputPath: string
relativeApiImplSourceOutputPath: string

relativeResourcesOutputPath?: string
relativeTestOutputPath: string
relativeTestResourcesOutputPath?: string
Expand Down
2 changes: 2 additions & 0 deletions packages/java-jaxrs-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 15 additions & 11 deletions packages/java-jaxrs-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,16 @@ export const createGenerator: CodegenGeneratorConstructor<JavaGeneratorContext>

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) {
const operations = group.operations
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)
}

Expand All @@ -66,7 +68,7 @@ export const createGenerator: CodegenGeneratorConstructor<JavaGeneratorContext>
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)
}

Expand All @@ -76,7 +78,7 @@ export const createGenerator: CodegenGeneratorConstructor<JavaGeneratorContext>
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)
}

Expand All @@ -87,7 +89,7 @@ export const createGenerator: CodegenGeneratorConstructor<JavaGeneratorContext>
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)
}
}
Expand All @@ -97,7 +99,7 @@ export const createGenerator: CodegenGeneratorConstructor<JavaGeneratorContext>
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)
}
}
Expand All @@ -107,13 +109,13 @@ export const createGenerator: CodegenGeneratorConstructor<JavaGeneratorContext>
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)
}

Expand All @@ -124,19 +126,21 @@ export const createGenerator: CodegenGeneratorConstructor<JavaGeneratorContext>

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())
Expand Down

0 comments on commit 3436fa2

Please sign in to comment.