-
-
Notifications
You must be signed in to change notification settings - Fork 736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Change Open API validation middleware to specify and use path parameters #8913
fix: Change Open API validation middleware to specify and use path parameters #8913
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@0417taehyun is attempting to deploy a commit to the unleash-team Team on Vercel. A member of the Team first needs to authorize it. |
src/lib/routes/controller.ts
Outdated
@@ -114,6 +125,8 @@ export default class Controller { | |||
this.useContentTypeMiddleware(options), | |||
this.useRouteErrorHandler(options.handler.bind(this)), | |||
); | |||
|
|||
this.app.use(options.path, checkOpenAPIValidationError()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can reuse this Open API validation handler for all services by using options.path
.
Moreover, it helps to add this middleware, Open API validation error handler, automatically while we add a new route by using this route
method.
Hi @0417taehyun thanks for the contribution! We'll try to add this to our next week planning and review it. |
Thanks! Feel free to inform me if I need to change something or discuss about this with your team. |
Hi, is there any progress of this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tested locally and it works fine, just adding some minor tweaks.
if (services.openApiService) { | ||
services.openApiService.useErrorHandler(app); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can easily be removed because useErrorHandler only handles errors identified as coming from openapi
src/lib/routes/controller.ts
Outdated
const checkOpenAPIValidationError = () => async (err, req, res, next) => { | ||
if (err?.status && err.validationErrors) { | ||
const apiError = fromOpenApiValidationErrors(req, err.validationErrors); | ||
|
||
res.status(apiError.statusCode).json(apiError); | ||
} else { | ||
next(err); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you see a problem on having this as a const? It's the same function all the time, we don't have to create new functions for each option path
const checkOpenAPIValidationError = () => async (err, req, res, next) => { | |
if (err?.status && err.validationErrors) { | |
const apiError = fromOpenApiValidationErrors(req, err.validationErrors); | |
res.status(apiError.statusCode).json(apiError); | |
} else { | |
next(err); | |
} | |
}; | |
const checkOpenAPIValidationError = async (err, req, res, next) => { | |
if (err?.status && err.validationErrors) { | |
const apiError = fromOpenApiValidationErrors(req, err.validationErrors); | |
res.status(apiError.statusCode).json(apiError); | |
} else { | |
next(err); | |
} | |
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it to the constant on b4702e2 commit.
I just followed the way of checkPrivateProjectPermissions
function definition like the one below but I agreed with your suggestion.
const checkPrivateProjectPermissions = () => async (req, res, next) => {
if (
!req.checkPrivateProjectPermissions ||
(await req.checkPrivateProjectPermissions())
) {
return next();
}
return res.status(404).end();
};
src/lib/routes/controller.ts
Outdated
@@ -114,6 +125,8 @@ export default class Controller { | |||
this.useContentTypeMiddleware(options), | |||
this.useRouteErrorHandler(options.handler.bind(this)), | |||
); | |||
|
|||
this.app.use(options.path, checkOpenAPIValidationError()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.app.use(options.path, checkOpenAPIValidationError()); | |
this.app.use(options.path, checkOpenAPIValidationError); |
Based on my previous comment, now just use the constant (we could rename the constant to something more meaningful like openapiErrorValidationMiddleware
or something similar)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me! (LGTM!) I'm just double checking some details with my colleagues and will merge then!
About the changes
Context
I removed
idNumberMiddleware
and changed to use Open API validation process to validate path parameters in #8734 .However, Express can't parse path parameters if they don't exist in the path, which is the first argument of the method such as
use
, hence it is impossible to parse them by usinguseErrorHandler
method like the one below.Nevertheless, we can't pass the specific path parameter to
useErrorHandler
because it is defined on the top level,app.ts
file. Consequently, we need to handle it on the controller level,/routs/controller.ts
, like the one below.I moved Open API validation handler to the controller layer to reuse on all services such as project and segments, and also removed unnecessary middleware at the top level,
app.ts
, and method,useErrorHandler
inopenapi-service.ts
.Important files
Before
Express cant' parse the path parameter because it doesn't be specified on the
use
method. Therefore, it returnsundefined
as an error message.After
Express can parse the path parameter because I change to specify it on the controller layer. Accordingly, it returns
test
.Discussion points
fromOpenApiValidatoinError
.