Skip to content

Commit

Permalink
feat(kotlin): add constants for endpoint paths to spring controller (#33
Browse files Browse the repository at this point in the history
)
  • Loading branch information
MaSch0212 authored Nov 20, 2024
1 parent 0ed8df2 commit 264bd28
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/kotlin/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@goast/kotlin",
"version": "0.4.4",
"version": "0.4.5",
"description": "Provides gOAst generators for generating Kotlin code from OpenAPI specifications.",
"exports": "./mod.ts",
"publish": {
Expand Down
2 changes: 2 additions & 0 deletions packages/kotlin/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type KotlinGeneratorConfig = OpenApiGeneratorConfig & {
functionNameCasing: StringCasing | StringCasingWithOptions;
propertyNameCasing: StringCasing | StringCasingWithOptions;
enumValueNameCasing: StringCasing | StringCasingWithOptions;
constantNameCasing: StringCasing | StringCasingWithOptions;

globalImports: string[];
};
Expand All @@ -23,6 +24,7 @@ export const defaultKotlinGeneratorConfig: DefaultGenerationProviderConfig<Kotli
functionNameCasing: 'camel',
propertyNameCasing: 'camel',
enumValueNameCasing: 'snake',
constantNameCasing: 'snake',

globalImports: [
'kotlin.*',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export type GetEndpointPath = { endpoint: ApiEndpoint };

export type GetDirectoryPath = { packageName: string };

export type GetPathConstantName = { endpoint: ApiEndpoint };

export type GetPackageName = object;

export type GetApiInterfaceName = object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ export class DefaultKotlinSpringControllerGenerator extends KotlinFileGenerator<
return kt.interface(interfaceName, {
annotations: this.getApiInterfaceAnnotations(ctx),
members: this.getApiInterfaceMembers(ctx),
companionObject: kt.object({
members: ctx.service.endpoints.map((endpoint) =>
kt.property(this.getPathConstantName(ctx, { endpoint }), {
const: true,
default: kt.string(this.getEndpointPath(ctx, { endpoint })),
})
),
}),
});
}

Expand Down Expand Up @@ -192,7 +200,7 @@ export class DefaultKotlinSpringControllerGenerator extends KotlinFileGenerator<
'method',
kt.collectionLiteral([kt.call([kt.refs.spring.requestMethod(), endpoint.method.toUpperCase()])]),
),
kt.argument.named('value', kt.collectionLiteral([kt.string(this.getEndpointPath(ctx, { endpoint }))])),
kt.argument.named('value', kt.collectionLiteral([this.getPathConstantName(ctx, { endpoint })])),
]);
if (endpoint.requestBody && endpoint.requestBody.content.length > 0) {
requestMapping.arguments.push(
Expand Down Expand Up @@ -543,6 +551,11 @@ export class DefaultKotlinSpringControllerGenerator extends KotlinFileGenerator<
return `${ctx.config.outputDir}/${packageName.replace(/\./g, '/')}`;
}

protected getPathConstantName(ctx: Context, args: Args.GetPathConstantName): string {
const { endpoint } = args;
return toCasing(`${endpoint.name}_path`, ctx.config.constantNameCasing);
}

protected getPackageName(ctx: Context, _args: Args.GetPackageName): string {
const packageSuffix = typeof ctx.config.packageSuffix === 'string'
? ctx.config.packageSuffix
Expand Down

0 comments on commit 264bd28

Please sign in to comment.