grape icon indicating copy to clipboard operation
grape copied to clipboard

param as is not work

Open ly123525 opened this issue 3 years ago • 8 comments

params do
    optional :page_size, as: :per_page, type: Integer
end

params #=> {page_size: 10} not have per_page
declared(params) #=> {per_page: 10}

i upgrade from 1.31 to 1.6.0

@brynary @tmornini @ctennis @olleolleolle @bernd How should I write? thanks

ly123525 avatar Jan 28 '22 05:01 ly123525

This seems to work as expected, what output did you expect?

dblock avatar Jan 28 '22 16:01 dblock

This seems to work as expected, what output did you expect?

@dblock Thank you for your reply

params do
    optional :page_size, as: :per_page, type: Integer
end

params #=> {per_page: 10}
declared(params) #=> {per_page: 10}

ly123525 avatar Jan 29 '22 06:01 ly123525

@dblock https://github.com/ruby-grape/grape/blob/v1.3.1/lib/grape/validations/params_scope.rb#L132

ly123525 avatar Jan 29 '22 07:01 ly123525

@ly123525 it is recommended to use declared(params) to avoid mass assignment. I wouldn't fix anything in Grape.

dnesteryuk avatar Jan 29 '22 10:01 dnesteryuk

@ly123525 it is recommended to use declared(params) to avoid mass assignment. I wouldn't fix anything in Grape.

@dnesteryuk while your suggestion is good, the current behavior still looks like a bug because we have a clear description in README for how it should work.

The value passed to as will be the key when calling params or declared(params).

And it'd worked it such way previously but not now. Looks like a breaking change.

dm1try avatar Jan 29 '22 13:01 dm1try

So did we introduce a regression, and in which version? Or should we change nothing and update documentation?

@dnesteryuk does it change anything in your comment?

dblock avatar Jan 30 '22 20:01 dblock

ok, I figured it out when this behavior was changed, actually, it is described in the upgrading doc, so it isn't a bug, it is how Grape works after 1.6.0 and the motivation of the change makes sense.

@dm1try probably, we need to update the doc :wink:

dnesteryuk avatar Jan 31 '22 08:01 dnesteryuk

@ly123525 or someone else here, care to update? thanks

dblock avatar Jan 31 '22 15:01 dblock