Skip to content

fix: validators checking aliased param instead of original one #1852

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

Merged
merged 3 commits into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
change fix to not rely on the order of validators
  • Loading branch information
glaucocustodio committed Dec 20, 2018
commit 41de94ea10f14a5dc68fc59a7c891760e6f7a14e
8 changes: 4 additions & 4 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2018-12-06 21:06:59 -0500 using RuboCop version 0.51.0.
# on 2018-12-20 21:38:24 +0000 using RuboCop version 0.51.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -38,13 +38,13 @@ Metrics/BlockLength:
# Offense count: 9
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 288
Max: 295

# Offense count: 31
Metrics/CyclomaticComplexity:
Max: 14

# Offense count: 1227
# Offense count: 1234
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Expand All @@ -55,7 +55,7 @@ Metrics/LineLength:
Metrics/MethodLength:
Max: 33

# Offense count: 11
# Offense count: 12
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 220
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#### Fixes

* [#1852](https://github.com/ruby-grape/grape/pull/1852): Validators checking aliased param instead of original one - [@glaucocustodio](https://github.com/glaucocustodio).
* [#1852](https://github.com/ruby-grape/grape/pull/1852): `allow_blank` called after `as` when the original param is not blank - [@glaucocustodio](https://github.com/glaucocustodio).
* [#1844](https://github.com/ruby-grape/grape/pull/1844): Enforce `:tempfile` to be a `Tempfile` object in `File` validator - [@Nyangawa](https://github.com/Nyangawa).

### 1.2.2 (2018/12/07)
Expand Down
10 changes: 9 additions & 1 deletion lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ def run
else
run_filters before_validations, :before_validation
run_validators validations, request
remove_aliased_params
run_filters after_validations, :after_validation
response_object = @block ? @block.call(self) : nil
end
Expand Down Expand Up @@ -319,7 +320,14 @@ def build_helpers
Module.new { helpers.each { |mod_to_include| include mod_to_include } }
end

private :build_stack, :build_helpers
def remove_aliased_params
return unless route_setting(:aliased_params)
route_setting(:aliased_params).flat_map(&:keys).each do |aliased_param|
@params.delete(aliased_param)
end
end

private :build_stack, :build_helpers, :remove_aliased_params

def helpers
lazy_initialize! && @helpers
Expand Down
7 changes: 0 additions & 7 deletions lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,6 @@ def validates(attrs, validations)
# type casted values
coerce_type validations, attrs, doc_attrs, opts

move_alias_to_end(validations)

validations.each do |type, options|
validate(type, options, attrs, doc_attrs, opts)
end
Expand Down Expand Up @@ -433,11 +431,6 @@ def validate_value_coercion(coerce_type, *values_list)
end
end

def move_alias_to_end(validations)
alias_validation = validations.delete(:as)
validations[:as] = alias_validation if alias_validation
end

def extract_message_option(attrs)
return nil unless attrs.is_a?(Array)
opts = attrs.last.is_a?(Hash) ? attrs.pop : {}
Expand Down
1 change: 0 additions & 1 deletion lib/grape/validations/validators/as.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def initialize(attrs, options, required, scope, opts = {})

def validate_param!(attr_name, params)
params[@alias] = params[attr_name]
params.delete(attr_name)
end
end
end
Expand Down