openapi-core icon indicating copy to clipboard operation
openapi-core copied to clipboard

Path templating not supported

Open diogobaeder opened this issue 7 years ago • 8 comments

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 avatar May 15 '18 06:05 diogobaeder

@diogobaeder thanks for the report. Tbh I don't know how to implement this atm

p1c2u avatar May 30 '18 13:05 p1c2u

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.

erpheus avatar Jun 01 '18 08:06 erpheus

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.

diogobaeder avatar Jun 01 '18 08:06 diogobaeder

class FlaskOpenAPIRequestEx(FlaskOpenAPIRequest):
    @property
    def full_url_pattern(self):
        return re.sub('<([^>]+)>', r'{\1}', super().full_url_pattern)

muzhig avatar Jul 06 '18 21:07 muzhig

The above subclass seems to work well for me, could FlaskOpenAPIRequest include that regex by default?

jamesrwhite avatar Sep 04 '18 14:09 jamesrwhite

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.

ghost avatar Sep 10 '18 20:09 ghost

I've noted a potential solution to this in https://github.com/p1c2u/openapi-core/issues/93#issuecomment-439044688.

stephenfin avatar Nov 15 '18 13:11 stephenfin

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.

local-minimum avatar Feb 01 '19 14:02 local-minimum