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

Simplify logic for defining declared params #2077

Merged
merged 1 commit into from
Jun 28, 2020
Merged

Conversation

dnesteryuk
Copy link
Member

Now, route settings get declared params when an endpoint gets initialized, the Grape::Validations::ParamsScope doesn't need to worry about that anymore.

Since the endpoint takes care of finalizing declared params, the declared_params method works with a structure which doesn't require any processing.

Now, route settings get declared params when an endpoint
gets initialized, the `Grape::Validations::ParamsScope` doesn't
need to worry about that anymore.

Since the endpoint takes care of finalizing declared params,
the `declared_params` method works with a structure which
doesn't require any processing.
@dblock dblock merged commit 0d5cf40 into master Jun 28, 2020
@dblock
Copy link
Member

dblock commented Jun 28, 2020

👍

@dnesteryuk dnesteryuk deleted the crush_desclared_params branch July 7, 2020 06:33
@dblock
Copy link
Member

dblock commented Jul 14, 2020

This caused ruby-grape/grape-swagger#801, at the very least this is not expected.

Got some time to look at it, @dnesteryuk? I don't understand what changed or why grape-swagger was doing all this work here. Looks like the internal setting :declared_params is no longer behaving as before, but the grape-swagger code shouldn't be relying on that anyway.

@dnesteryuk
Copy link
Member Author

@dblock yeah, I will take a look this week

@LeFnord
Copy link
Member

LeFnord commented Jul 14, 2020

@dnesteryuk … think it's not realy needed, cause declared_params will not be used anymore in grape-swagger

@dnesteryuk
Copy link
Member Author

@LeFnord thanks for you fix 👍

@mobilutz
Copy link

mobilutz commented Nov 8, 2021

In a project I am working on, we used the @declared_params attribute to get data for a StrictHash Validator.

Is there an alternative way now to get declared_params within a Validator?

This is the validator pseudo code:

        def validate_param!(attr_name, params)
          actual_keys = params[attr_name].keys.map(&:to_sym)
          errors = (actual_keys - allowed_keys).map { |key| error(key, attr_name) }
          
          raise ::Grape::Exceptions::ValidationArrayErrors, errors if errors.any?
        end

        def allowed_keys
          # NOTE: This of course does not return the real keys, just shows that we used the now `nil` @declared_params
          @allowed_keys ||= @scope.instance_variable_get(:@declared_params)
        end

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

Successfully merging this pull request may close these issues.

4 participants