ahx / openapi_first

openapi_first is a Ruby gem for request / response validation and contract-testing against an OpenAPI API description. It makes APIFirst easy and reliable.
MIT License
115 stars 15 forks source link

Parse error for kebab-case URL parameters #245

Closed samanthawritescode closed 6 months ago

samanthawritescode commented 6 months ago

I have a spec that prefers to use kebab-case. We have paths that have dynamic parameters that we name accordingly, i.e. /api-keys/{api-key-id}.

I'm getting a parsing error from Mustermann when using this as middleware with my spec in test:

     Mustermann::ParseError:
       unexpected - while parsing "/api-keys/{api-key-id}"
     # /usr/local/bundle/gems/mustermann-3.0.0/lib/mustermann/ast/parser.rb:231:in `unexpected'
     # /usr/local/bundle/gems/mustermann-3.0.0/lib/mustermann/ast/parser.rb:142:in `expect'
     # /usr/local/bundle/gems/mustermann-3.0.0/lib/mustermann/sinatra/parser.rb:34:in `block in <class:Parser>'
     # /usr/local/bundle/gems/mustermann-3.0.0/lib/mustermann/ast/parser.rb:91:in `read'
     # /usr/local/bundle/gems/mustermann-3.0.0/lib/mustermann/ast/parser.rb:59:in `block in parse'
     # /usr/local/bundle/gems/mustermann-3.0.0/lib/mustermann/ast/node.rb:58:in `parse'
     # /usr/local/bundle/gems/mustermann-3.0.0/lib/mustermann/ast/node.rb:171:in `parse'
     # /usr/local/bundle/gems/mustermann-3.0.0/lib/mustermann/ast/parser.rb:71:in `node'
     # /usr/local/bundle/gems/mustermann-3.0.0/lib/mustermann/ast/parser.rb:59:in `parse'
     # /usr/local/bundle/gems/mustermann-3.0.0/lib/mustermann/ast/parser.rb:17:in `parse'
     # /usr/local/bundle/gems/mustermann-3.0.0/lib/mustermann/ast/pattern.rb:91:in `block in to_ast'
     # /usr/local/bundle/gems/mustermann-3.0.0/lib/mustermann/equality_map.rb:43:in `fetch'
     # /usr/local/bundle/gems/mustermann-3.0.0/lib/mustermann/ast/pattern.rb:90:in `to_ast'
     # /usr/local/bundle/gems/mustermann-3.0.0/lib/mustermann/ast/pattern.rb:81:in `compile'
     # /usr/local/bundle/gems/mustermann-3.0.0/lib/mustermann/regexp_based.rb:19:in `initialize'
     # /usr/local/bundle/gems/mustermann-3.0.0/lib/mustermann/pattern.rb:59:in `new'
     # /usr/local/bundle/gems/mustermann-3.0.0/lib/mustermann/pattern.rb:59:in `block in new'
     # /usr/local/bundle/gems/mustermann-3.0.0/lib/mustermann/equality_map.rb:43:in `fetch'
     # /usr/local/bundle/gems/mustermann-3.0.0/lib/mustermann/pattern.rb:59:in `new'
     # /usr/local/bundle/gems/mustermann-3.0.0/lib/mustermann.rb:68:in `new'
     # /usr/local/bundle/gems/openapi_first-1.3.4/lib/openapi_first/definition.rb:103:in `block in search_for_path_item'
     # /usr/local/bundle/gems/openapi_first-1.3.4/lib/openapi_first/definition.rb:102:in `each'
     # /usr/local/bundle/gems/openapi_first-1.3.4/lib/openapi_first/definition.rb:102:in `find'
     # /usr/local/bundle/gems/openapi_first-1.3.4/lib/openapi_first/definition.rb:102:in `search_for_path_item'
     # /usr/local/bundle/gems/openapi_first-1.3.4/lib/openapi_first/definition.rb:98:in `find_path_item_and_params'
     # /usr/local/bundle/gems/openapi_first-1.3.4/lib/openapi_first/definition.rb:46:in `request'
     # /usr/local/bundle/gems/openapi_first-1.3.4/lib/openapi_first/middlewares/response_validation.rb:35:in `find_request'
     # /usr/local/bundle/gems/openapi_first-1.3.4/lib/openapi_first/middlewares/response_validation.rb:25:in `call'

If I change the URL param to not use the - character, it seems to parse correctly. I'd really like to not have to do this since it would mean either a casing change inconsistent with the rest of my spec or a clarity loss if I had to just go with id.

ahx commented 6 months ago

Thanks for the report @samanthawritescode I am looking at how to support this using the exising tools (Mustermann).

MrBananaLord commented 6 months ago

@ahx it's coming from the mustermann's Sinatra pattern

I changed the regex to /[\w\.-]+/ and then the kebab-params became supported. not sure if that's something expected in the Sinatra parser tho :D

ahx commented 6 months ago

This branch adds support for kebab-cased path parameters (and removes the mustermann dependency). Feedback is welcome.

ahx commented 6 months ago

Thanks again for the report. A fix was released in 1.3.5.