-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
How to use regular expressions in route path and how to make parameters optional #1866
Comments
cc @raymondfeng |
@hacksparrow , could you please help? |
@hbakhtiyor LB4 uses Express' serve-static middleware for serving static files, you can see how to use regular expression and other patterns for a path at http://expressjs.com/en/4x/api.html#path-examples. |
i can't use like this in lb4, which valid in expressjs
|
At the moment, static files can be served from a single mount point only. We don't have a controller interface yet. Having said that, you LB4 provides an Express-like method (
|
i don't need exactly for static serve, not related to my questions |
LB4 does not support regex based paths. @bajtos @raymondfeng @strongloop/sq-lb-apex how about supporting regex-based paths? |
Is this is to evaluate a regular expression against the uuid path parameter ? if (/^[0-9a-fA-F]{24}$/.test(uuidParamter)) {
} else { //error, not evaluated } |
yeah, want to add this in |
@hbakhtiyor We intentionally disabled the regular expression/wildcard support in paths to be compatible with OpenAPI and swagger-ui. See some discussions at #1765. OpenAPI spec is very vague about path templating - https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#path-templating. See more information at:
We can reevaluate the issue once the spec has a clear direction. |
+1 to what @raymondfeng wrote above.
My recommendation: Move the regular expression from the path definition into an OpenAPI spec parameter constraint. this.route(
'get',
'/asserts/:uuid',
{
parameters: [
{
name: 'uuid',
in: 'path',
schema: {
type: 'string',
pattern:
'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$',
},
},
],
responses: {
/*...*/
},
},
function(uuid: string) {
// serve the asset
},
); Please note that LB4 does not validate non-body parameters (e.g. path parameters yet) - see #1573. (Implementing these validation should be pretty easy, would you like to contribute it yourself? Let's discuss in #1573). |
Please note that in OpenAPI, path parameters are always required and all other parameters are optional by default. See OpenAPI's section Parameter Object.
As a result, it is not possible to have an optional path parameter in LB4, you have to move such parameter e.g. to a query string. Ideally, I would like LB4 to provide the following syntax for annotating parameters as required: @param.query.string('format', {required: true}) format?: string For now, you have to use @param(name: 'format', in: 'query', required: true}) format?: string I opened a new issue to keep track of this improvement, see #1940 |
@hbakhtiyor did we answer your questions? @raymondfeng @hacksparrow @dhmlau I think we should capture these Q&A in our docs. What do you think? |
Agreed. He won't be the first and the last to come up with this requirement. |
@bajtos, partially, yeah currently going with the snippet, also can't put @get('/assets/{params}')
async assets(
@param.path.string('params') params: string
): Promise<object> {
const routeAssetsMatches = /^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})(?:\.(jpeg|jpg|png))?$/i.exec(params);
if (!routeAssetsMatches || !routeAssetsMatches[1]) {
throw new HttpErrors.UnprocessableEntity('the format of route is invalid');
}
const uuid = routeAssetsMatches[1];
const format = routeAssetsMatches[2] || 'jpeg';
} |
👍
Could you please post a code snippet showing what are you trying to do? |
I am closing this issue as resolved. |
@bajtos , e.g @get('/assets/{uuid}.{format}')
async assets(
@param.path.string('uuid') uuid: string,
@param.path.string('format') format: string
): Promise<object> {
} |
@hbakhtiyor Interesting! I tried your example path in Swagger Editor (http://editor.swagger.io) and it looks like your example is a valid OpenAPI spec path template. The spec: /pet/{petId}.{format}:
get:
# ... The request made by the editor (petId = 1, format = json): curl -X GET "https://petstore.swagger.io/v2/pet/1.json" -H "accept: application/xml" Please open a new GitHub issue to discuss how LoopBack can add support for this use case. |
I thought it was supported by design. Let me check. |
@hbakhtiyor the issue with |
@bajtos works well, but |
Hmm, that's certainly not expected. I assume you have upgraded your dependencies to use Could you please take a look at the changes made in 47c24cb and try to write a new test that will reproduce your use case and fail against the current implementation? Start by forking loopback-next, then create a new feature branch, etc. |
Why do you expect |
for example like expressjs
for example
/assets/eed761cf-5418-53d3-9f67-c279f58ac875.png
/assets/eed761cf-5418-53d3-9f67-c279f58ac875
,.format
is optionalThe text was updated successfully, but these errors were encountered: