From 3880a402903742c1c7e908009ec270a460b5748d Mon Sep 17 00:00:00 2001 From: aramissennyeydd Date: Tue, 16 Dec 2025 20:07:44 -0700 Subject: [PATCH 1/3] chore: use native feature for base path determination Signed-off-by: aramissennyeydd --- packages/backend-openapi-utils/src/stub.ts | 45 +--------------------- 1 file changed, 1 insertion(+), 44 deletions(-) diff --git a/packages/backend-openapi-utils/src/stub.ts b/packages/backend-openapi-utils/src/stub.ts index 8ab00c4df4..717e2ae19b 100644 --- a/packages/backend-openapi-utils/src/stub.ts +++ b/packages/backend-openapi-utils/src/stub.ts @@ -31,13 +31,6 @@ import { middleware as OpenApiValidator } from 'express-openapi-validator'; import { OPENAPI_SPEC_ROUTE } from './constants'; import { isErrorResult, merge } from 'openapi-merge'; -type PropertyOverrideRequest = Request & { - [key: symbol]: string; -}; - -const baseUrlSymbol = Symbol(); -const originalUrlSymbol = Symbol(); - function validatorErrorTransformer(): ErrorRequestHandler { return (error: Error, _: Request, _2: Response, next: NextFunction) => { next(new InputError(error.message)); @@ -75,30 +68,6 @@ function createRouterWithValidation( const router = PromiseRouter(); router.use(options?.middleware || getDefaultRouterMiddleware()); - /** - * Middleware to setup the routing for OpenApiValidator. OpenApiValidator expects `req.originalUrl` - * and `req.baseUrl` to be the full path. We adjust them here to basically be nothing and then - * revive the old values in the last function in this method. We could instead update `req.path` - * but that might affect the routing and I'd rather not. - * - * TODO: I opened https://github.com/cdimascio/express-openapi-validator/issues/843 - * to track this on the middleware side, but there was a similar ticket, https://github.com/cdimascio/express-openapi-validator/issues/113 - * that has had minimal activity. If that changes, update this to use a new option on their side. - */ - router.use((req: Request, _, next) => { - /** - * Express typings are weird. They don't recognize PropertyOverrideRequest as a valid - * Request child and try to overload as PathParams. Just cast it here, since we know - * what we're doing. - */ - const customRequest = req as PropertyOverrideRequest; - customRequest[baseUrlSymbol] = customRequest.baseUrl; - customRequest.baseUrl = ''; - customRequest[originalUrlSymbol] = customRequest.originalUrl; - customRequest.originalUrl = customRequest.url; - next(); - }); - // TODO: Handle errors by converting from OpenApiValidator errors to known @backstage/errors errors. router.use( OpenApiValidator({ @@ -106,6 +75,7 @@ function createRouterWithValidation( coerceTypes: false, allowUnknownQueryParameters: false, }, + useRequestUrl: true, ignoreUndocumented: true, validateResponses: false, ...options?.validatorOptions, @@ -113,19 +83,6 @@ function createRouterWithValidation( }), ); - /** - * Revert `req.baseUrl` and `req.originalUrl` changes. This ensures that any further usage - * of these variables will be unchanged. - */ - router.use((req: Request, _, next) => { - const customRequest = req as PropertyOverrideRequest; - customRequest.baseUrl = customRequest[baseUrlSymbol]; - customRequest.originalUrl = customRequest[originalUrlSymbol]; - delete customRequest[baseUrlSymbol]; - delete customRequest[originalUrlSymbol]; - next(); - }); - // Any errors from the middleware get through here. router.use(validatorErrorTransformer()); From 6678b7881363363af7707ae8290e26d2eda57735 Mon Sep 17 00:00:00 2001 From: aramissennyeydd Date: Tue, 16 Dec 2025 20:09:02 -0700 Subject: [PATCH 2/3] add changeset Signed-off-by: aramissennyeydd --- .changeset/major-mangos-shine.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/major-mangos-shine.md diff --git a/.changeset/major-mangos-shine.md b/.changeset/major-mangos-shine.md new file mode 100644 index 0000000000..ebbd132ce6 --- /dev/null +++ b/.changeset/major-mangos-shine.md @@ -0,0 +1,5 @@ +--- +'@backstage/backend-openapi-utils': minor +--- + +Internal update to use native feature from our request validation library for handling base path determination. From af3a4b1fe0d9e5e96eea8877ccae9281521d3d33 Mon Sep 17 00:00:00 2001 From: Aramis Sennyey <159921952+aramissennyeydd@users.noreply.github.com> Date: Wed, 17 Dec 2025 08:12:15 -0700 Subject: [PATCH 3/3] Update .changeset/major-mangos-shine.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Fredrik Adelöw Signed-off-by: Aramis Sennyey <159921952+aramissennyeydd@users.noreply.github.com> --- .changeset/major-mangos-shine.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/major-mangos-shine.md b/.changeset/major-mangos-shine.md index ebbd132ce6..f42fcf5d0e 100644 --- a/.changeset/major-mangos-shine.md +++ b/.changeset/major-mangos-shine.md @@ -1,5 +1,5 @@ --- -'@backstage/backend-openapi-utils': minor +'@backstage/backend-openapi-utils': patch --- Internal update to use native feature from our request validation library for handling base path determination.