Path templating not supported
Hi,
Unfortunately path templates are not supported in this library; For example, if a PUT request is made for a Flask view which has a pattern of, say, /users/<user_id> and I have a /users/{user_id} path defined in my OpenAPI 3 schema, then it doesn't match the path because of the difference between "lesser-than/greater-than" bracers from Flask and the curly bracers from OpenAPI 3.
Please let me know if you need help with implementing this fix, in which case I can send a pull request.
Thanks, Diogo
@diogobaeder thanks for the report. Tbh I don't know how to implement this atm
I found the same problem some time ago and have been using this workaround:
# Alter request as workaround for openapi-core limitations
request.url_rule.rule = (
request.url_rule.rule.replace('<', '{').replace('>', '}')
)
The request mentioned in this code is the Flask request, not the FlaskOpenAPIRequest.
Me too, I'm doing the same replacement in my code itself. But it would be nice to have that supported by the library.
I might have some time during the weekend, if so I'll try to come up with a pull request supporting that.
class FlaskOpenAPIRequestEx(FlaskOpenAPIRequest):
@property
def full_url_pattern(self):
return re.sub('<([^>]+)>', r'{\1}', super().full_url_pattern)
The above subclass seems to work well for me, could FlaskOpenAPIRequest include that regex by default?
This is specific to the router implementation syntax. For Django things are a lot more complex, so I think it shouldn't be implemented by openapi-core, but by the url routing framework.
I've noted a potential solution to this in https://github.com/p1c2u/openapi-core/issues/93#issuecomment-439044688.
I would suggest extending @muzhig regex so that it handles cases like: <int:myid> -> {myid} with using the following re.sub:
re.sub(r'<([a-zA-Z0-9_]+ *:|) *([^>]+)>', r'{\2}', rule)
I'm not 100% if flask allows spaces around the colon, if it is not allowed, then the two * can be removed.