Skip to content

Allow for strings in 'only' and 'except' options #136

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 1 commit into from
Jun 26, 2015
Merged

Allow for strings in 'only' and 'except' options #136

merged 1 commit into from
Jun 26, 2015

Conversation

bswinnerton
Copy link
Contributor

With the addition of the only and except options, possibilities such as the partial response design pattern (where the consumer of the API can specifically request only the fields that it desires) become available. A common pattern to request these fields would be via params, which will arrive as strings. Currently only an array of symbols (or hash containing symbols) is supported to define the desired fields.

As discussed here, it certainly should be the responsibility of another gem to do parameter validation in a partial response scenario, but such a gem would also have to do a [perhaps unnecessary] recursive symbol coercion as well when it could be handled by grape-entity, as proposed by this pull request.

This commit updates Grape::Entity to support an array of symbols or strings to be passed as the only or except options of represent.


@only_fields ||= options[:only].each_with_object({}) do |attribute, allowed_fields|
if attribute.is_a?(Hash)
attribute.each do |attr, nested_attrs|
attr = attr.to_sym
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should convert those to strings. Until Ruby 2.2 symbols are not garbage collected so you're creating an opportunity for some DDoS if this is used with user-supplied data.

@dblock
Copy link
Member

dblock commented Jun 24, 2015

See my comment around symbols vs. strings to be converted/used internally. Otherwise this makes total sense.

@bswinnerton
Copy link
Contributor Author

@dblock what are your thoughts on this latest approach (and trailing-method-call-on-an-end style)?

Rather than coercing the keys to strings inside the loop, we can use ActiveSupport's symbolize_keys after the appropriate hash of keys has been built. I think this should solve the garbage collection issue you brought up.

@dblock
Copy link
Member

dblock commented Jun 25, 2015

I think this is fine.

I'd like to have explicit tests for this rather than updating existing tests with mixed keys. Write one that says something like "it works for strings", another that "it works for symbols" and another that "it works for mixed strings and symbols".

@bswinnerton
Copy link
Contributor Author

Absolutely. Just added explicit tests for the symbol and string code paths of only and except, each with nested attributes.

@dblock
Copy link
Member

dblock commented Jun 25, 2015

Good. Could you please squash these commits? I'll merge.

With the addition of the `only` and `except` options, it opens up the
possiblities to the [partial response](http://bit.ly/1fxWuXJ) design
pattern where the consumer of the API can specifically request only the
fields that it desires. A common pattern to request these fields would
be via `params`, which will arrive as strings. Currently only an array
of symbols (or hash containing symbols) is supported to define the
desired fields.

This commit updates Grape::Entity to support an array of symbols _or_
strings to be passed as the `only` or `except` options of `represent`.
@bswinnerton
Copy link
Contributor Author

Much thanks @dblock! Done.

dblock added a commit that referenced this pull request Jun 26, 2015
Allow for strings in 'only' and 'except' options
@dblock dblock merged commit 20298ea into ruby-grape:master Jun 26, 2015
@dblock
Copy link
Member

dblock commented Jun 26, 2015

Merged.

@bswinnerton bswinnerton deleted the symbolize-only-and-except branch June 26, 2015 15:14
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.

2 participants