Skip to content

Commit 2e8fbc5

Browse files
Merge pull request #12 from apvee/dev
Route normalization improvements and refines the OpenAPI handler response formatting for JSON
2 parents ba4db10 + 167e033 commit 2e8fbc5

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

packages/azure-functions-openapi/core/registerFunction.ts

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,42 @@ import { app, HttpMethod } from "@azure/functions";
33
import { registry } from "./registry";
44
import { FunctionRouteConfig } from "./types";
55

6+
/**
7+
* Normalizes a path for OpenAPI documentation.
8+
* Ensures the path starts with a single leading slash and removes multiple consecutive slashes.
9+
*
10+
* @param azureFunctionRoutePrefix - Optional route prefix for the Azure Function
11+
* @param route - The route path
12+
* @returns Normalized path string for OpenAPI (always starts with /)
13+
*/
14+
function normalizeOpenAPIPath(azureFunctionRoutePrefix: string | undefined, route: string): string {
15+
let fullPath = azureFunctionRoutePrefix
16+
? `/${azureFunctionRoutePrefix}/${route}`
17+
: route;
18+
19+
// Replace multiple slashes with a single slash, and ensure leading slash
20+
fullPath = '/' + fullPath.replace(/^\/+/, '');
21+
fullPath = fullPath.replace(/\/+/g, '/');
22+
23+
return fullPath;
24+
}
25+
26+
/**
27+
* Normalizes a route for Azure Functions registration.
28+
* Azure Functions expects routes without leading slashes and handles prefixes via configuration.
29+
* This function cleans the route by removing leading slashes, trailing slashes, and multiple consecutive slashes.
30+
*
31+
* @param route - The route path
32+
* @returns Normalized route for Azure Functions (no leading slash, no trailing slash)
33+
*/
34+
function normalizeAzureFunctionRoute(route: string): string {
35+
// Remove leading slashes, trailing slashes, and replace multiple slashes with single slash
36+
let normalizedRoute = route.replace(/^\/+/, '').replace(/\/+$/, '').replace(/\/+/g, '/');
37+
38+
// If the route becomes empty (was just slashes), return empty string (root route)
39+
return normalizedRoute || '';
40+
}
41+
642
/**
743
* Registers a function with the specified configuration.
844
*
@@ -100,19 +136,24 @@ function registerPath(
100136
isWebHook: boolean,
101137
options: FunctionRouteConfig) {
102138

139+
// Normalize the route for Azure Functions registration (without leading slash and prefix)
140+
const normalizedRoute = normalizeAzureFunctionRoute(options.route);
141+
103142
app.http(name, {
104143
methods: options.methods,
105144
authLevel: options.authLevel,
106145
handler: options.handler,
107-
route: options.route
146+
route: normalizedRoute
108147
});
109148

110149
options.methods.forEach(method => {
150+
// Normalize the path for OpenAPI (with prefix and leading slash)
151+
const fullPath = normalizeOpenAPIPath(options.azureFunctionRoutePrefix, options.route);
152+
111153
const routeConfig: RouteConfig = {
112154
summary: summary,
113155
method: mapHttpMethod(method),
114-
// Add the route to the OpenAPI registry, with the route prefix if it exists
115-
path: (options.azureFunctionRoutePrefix) ? `/${options.azureFunctionRoutePrefix}/${options.route}` : options.route,
156+
path: fullPath,
116157
security: options.security,
117158
request: options.request,
118159
responses: options.responses,

packages/azure-functions-openapi/handlers/openAPI.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ export function registerOpenAPIHandler(
5050
return {
5151
status: 200,
5252
headers: { 'Content-Type': format === 'json' ? 'application/json' : 'application/x-yaml' },
53-
body: (format === 'yaml') ? yamlStringify(openAPIDefinition) : undefined,
54-
jsonBody: (format === 'json') ? openAPIDefinition : undefined,
53+
body: (format === 'yaml')
54+
? yamlStringify(openAPIDefinition)
55+
: JSON.stringify(openAPIDefinition, null, 2),
5556
};
5657
},
5758
route: finalRoute

packages/azure-functions-openapi/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@apvee/azure-functions-openapi",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"repository": {
55
"type": "git",
66
"url": "https://github.com/apvee/azure-functions-nodejs-monorepo.git"

0 commit comments

Comments
 (0)