Skip to content
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

support individual key for validation #339

Closed
ota42y opened this issue Nov 16, 2021 · 4 comments · Fixed by #349
Closed

support individual key for validation #339

ota42y opened this issue Nov 16, 2021 · 4 comments · Fixed by #349
Labels

Comments

@ota42y
Copy link
Member

ota42y commented Nov 16, 2021

Now schema validator doesn't support yet so we use merged parameter.
https://github.com/interagent/committee/blob/master/lib/committee/schema_validator/open_api_3.rb#L59

This problem caused this issue #328
So we should validation by individual key.

Add validate_by_merged_param option

To avoid breaking backward compatibility, we should add this option and do old validation.

save query hash to key

Now we save query hash to merged key

query_param = unpacker.unpack_query_params(request)
request.env[validator_option.params_key] = Committee::Utils.indifferent_hash
request.env[validator_option.params_key].merge!(Committee::Utils.deep_copy(query_param))

The last step in validation, we copy query hash to individual key

def copy_coerced_data_to_query_hash(request)
return if request.env["rack.request.query_hash"].nil? || request.env["rack.request.query_hash"].empty?
query_hash_key = @validator_option.query_hash_key
return unless query_hash_key
request.env[query_hash_key] = {} unless request.env[query_hash_key]
request.env["rack.request.query_hash"].keys.each do |k|
request.env[query_hash_key][k] = request.env[validator_option.params_key][k]
end
end

But this is bad code.
We should copy to individual key in request_unpack method.

        request.env[validator_option.params_key]= unpacker.unpack_query_params(request)

validate query parameter and request parameter

Now we check parameter by method name.
This method expect only one and merged parameter

def validate_request_params(params, headers, validator_option)
ret, err = case request_operation.http_method
when 'get'
validate_get_request_params(params, headers, validator_option)
when 'post'
validate_post_request_params(params, headers, validator_option)
when 'put'
validate_post_request_params(params, headers, validator_option)
when 'patch'
validate_post_request_params(params, headers, validator_option)
when 'delete'
validate_get_request_params(params, headers, validator_option)
else
raise "Committee OpenAPI3 not support #{request_operation.http_method} method"
end
raise err if err
ret

So we need to fix this method to get individual parameter and merge it like this.

        def validate_request_params(query_param, path_hash, request_body_hash, headers, validator_option)
          request_operation.validate_request_parameter(query_param, headers, build_openapi_parser_get_option(validator_option))
          request_operation.validate_request_body(content_type, params, schema_validator_options)   
        end
@ota42y ota42y added the ready label Nov 16, 2021
@ota42y
Copy link
Member Author

ota42y commented Nov 22, 2021

this change resolve #339

@ota42y ota42y mentioned this issue Nov 22, 2021
2 tasks
@hlascelles
Copy link

Terrific 🚀. Let me know if we can help with this.

@ota42y
Copy link
Member Author

ota42y commented Dec 6, 2021

That's very nice of you to say !!!
If you are interested, you can try to implement this feature.

@wanglian
Copy link

any plan to release this guys?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants