Pass request to validators along with params.#1242
Conversation
c41897f to
94e8d52
Compare
94e8d52 to
c545377
Compare
|
I think we want to build |
|
@dblock the validator is per Endpoint, not per request, so making it a method of the Validator class seems odd to me. |
|
I see your point, but I politely disagree :) The API is all singletons, and there're methods all over the place such as Give it a try, you'll find the code much easier to grok. |
|
Either we change the method args, the constructor args or add a setter. All the same to me. Is there no need for thread safety here? I wrote a test to check thread safety with |
|
I think you shouldn't memoize anything or it should be thread-safe. I vote for the former to start. |
|
Huh, I'm not sure how to do this without attaching something to the instance - memoizing or whatever. This is one way I got it to work that passed my thread safe test - duplicating the validator objects: route_setting(:saved_validations).map(&:dup).each do |validator|
req = @request
validator.define_singleton_method(:request) { req }
begin
validator.validate!(params)
rescue Grape::Exceptions::Validation => e
validation_errors << e
end
end
It's not clear to me how |
|
Not sure why/what/etc., I am speaking without looking at the code in depth, so I'll take a look soon. I think all I would want here is to be able to have |
d10602c to
a319d96
Compare
|
@dblock updated. |
There was a problem hiding this comment.
This change is to make sure @converter is only created once, rather than in each validator dup.
coerce_spec.rb is expecting this behavior.
|
Calling Is https://github.com/ruby-grape/grape/blob/master/lib/grape/validations/validators/multiple_params_base.rb#L7 saying that we shouldn't worry about thread-safety in any of these and therefore don't need a I am proposing dblock@7815ad0 basically on top of your change. Either way we don't need to pass both the |
|
If I use threads to make requests to the same app, that I haven't tested it in a common threaded app server, but I could if that would help. |
No magic. Just @glennpratt has the good point: all validators for a specific endpoint share the same state; they are saved in
This line seems unsafe (multiple requests for the same endpoint with shared state 😟) In short, don't provide any "per-request" state to validators because the state is shared across all endpoint instances(add to README) or copy them. I'll have a deeper look at this later. |
@dm1try doh, thank you. When I was poking at it I could have sworn multiple requests were in the same Endpoint object, but I must have been wrong. I'm thinking If |
|
Ok so we're saying that https://github.com/ruby-grape/grape/blob/master/lib/grape/validations/validators/multiple_params_base.rb#L7 is a bug. @dm1try Should we merge this PR as is giving validators a |
As mentioned before, we might do some benchmarks.
Sounds good to me! |
|
Super Scientific Benchmarks |
|
Looks like To summarize:
This will let you override We still need to fix the existing problem in |
|
@glennpratt Want to give above a try? I am interested in getting this merged, I think we're close. |
|
@dblock sure, I have a moment now. |
a319d96 to
b15c410
Compare
|
@dblock The cleanest, backwards compatible fix for def validate(request)
dup.validate!(request.params)
endAssuming external users could be depending on the |
|
@dblock changes pushed. Do you want me to push that fix for |
|
Ok, this change looks good, merging. Make a separate PR for the |
Pass request to validators along with params.
|
#986 mentioned being able to "use helpers such as How do the changes from this PR let us do that? |
|
This PR passes |
Addresses #986