openapi_parser icon indicating copy to clipboard operation
openapi_parser copied to clipboard

Enable handling exploded parameters and test for edge cases

Open AjaxVM opened this issue 1 year ago • 0 comments

Description

OpenAPI supports exploded array parameters, such as stuff[]=foo&stuff[]=bar. However, when operating in a Rails environment (or any other server environment which parses these exploded parameters into a single stuff field), parameter_validation fails to find the param properly, leading to erroneous missed required fields or just lack of validation.

This was mentioned in the same Committee issue that is linked in #122 which has a PR to close that does not account for this specific scenario.

I originally thought this was possibly something that should be solved on the Committee side, but it looks like they are simply passing through to the validation here, and a referenced monkeypatch to this works, but that is not nearly as defensive and it seems like this should be a core support here.

This PR introduces handling for this, looking first for the param with the [] as it normally would, in case we are in an environment where they are not stripped. It then checks non-header parameters that end with [] for a matching param that strips it.

Monkeypatch

class OpenAPIParser::ParameterValidator
  class << self
    private

    def convert_key(k, is_header)
      is_header ? k&.downcase : k.sub(/\[\]$/, '')
    end
  end
end

AjaxVM avatar Apr 03 '24 17:04 AjaxVM