-
Notifications
You must be signed in to change notification settings - Fork 0
Raise Booqable::MissingAttribute for reads of absent attributes #46
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Booqable | ||
| # Sawyer::Agent used for every resource this gem creates | ||
| # | ||
| # Behaves exactly like Sawyer::Agent. It exists as a marker so that | ||
| # {Booqable::StrictAttributes} can tell resources created by this gem | ||
| # apart from resources created by other Sawyer-based gems | ||
| # loaded in the same process. | ||
| class SawyerAgent < Sawyer::Agent | ||
| end | ||
|
|
||
| # Strict attribute reads for resources created by this gem | ||
| # | ||
| # Sawyer::Resource#method_missing silently answers reads of absent | ||
| # attributes with nil. That turns typos and renamed API fields | ||
| # (e.g. `time_zone` vs `default_timezone`) into silent data bugs. | ||
| # For resources created by a {Booqable::SawyerAgent}, this module raises | ||
| # {Booqable::MissingAttribute} instead — both for plain reads | ||
| # (`resource.foo`) and predicate reads (`resource.foo?`). | ||
| # | ||
| # Attributes that ARE present in the payload with a null value still | ||
| # return nil — only reads of keys that are absent from the payload raise. | ||
| # | ||
| # Everything else about Sawyer::Resource is unchanged: attribute writes | ||
| # (`resource.foo = 1`) still define new attributes, hash-style access | ||
| # (`resource[:foo]`) stays a lenient probe returning nil for absent keys, | ||
| # and `to_h`/`to_attrs`, `key?`, `dig`, `fetch`, enumeration, marshaling, | ||
| # and respond_to? semantics all behave as before. Resources created by | ||
| # other gems' Sawyer agents are unaffected. | ||
| module StrictAttributes | ||
| # Matches plain attribute reads (`foo`) and predicate reads (`foo?`). | ||
| # Setters (`foo=`) stay permitted since Sawyer allows adding attributes. | ||
| ATTRIBUTE_READ_PATTERN = /\A([a-z0-9_]+)(\?)?\z/i | ||
|
|
||
| # Raise {Booqable::MissingAttribute} for reads of absent attributes | ||
| # on strict resources; defer to Sawyer's behavior for everything else. | ||
| def method_missing(method, *) | ||
| attr_name = booqable_missing_attribute_read(method) | ||
| raise Booqable::MissingAttribute.new(attr_name, self) if attr_name | ||
|
|
||
| super | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # Returns the attribute name when +method+ is a read of an attribute | ||
| # that is absent from the payload of a strict resource, nil otherwise | ||
| # | ||
| # @param method [Symbol] the method name passed to #method_missing | ||
| # @return [Symbol, nil] | ||
| def booqable_missing_attribute_read(method) | ||
| # _agent/_fields are Sawyer::Resource's public attr_readers — the bare `agent`/`fields` | ||
| # spellings are SPECIAL_METHODS resolved inside method_missing, so calling those from | ||
| # this hook (which method_missing invokes) would recurse infinitely. | ||
| return nil unless _agent.is_a?(Booqable::SawyerAgent) | ||
|
|
||
| match = ATTRIBUTE_READ_PATTERN.match(method.to_s) | ||
| return nil unless match | ||
|
|
||
| attr_name = match[1].to_sym | ||
| return nil if _fields.include?(attr_name) | ||
| return nil if match[2].nil? && Sawyer::Resource::SPECIAL_METHODS.include?(match[1]) | ||
|
|
||
| attr_name | ||
| end | ||
| end | ||
| end | ||
|
|
||
| Sawyer::Resource.prepend(Booqable::StrictAttributes) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Predicate checks on built-in resource accessors incorrectly raise an error instead of returning a value
The predicate form of Sawyer's built-in accessors (e.g.
.agent?,.fields?,.rels?) is not exempted from the strict-read guard (booqable_missing_attribute_readatlib/booqable/strict_attributes.rb:63) because the special-methods bypass only fires for non-predicate calls, so these valid queries raise an error instead of returning a boolean.Impact: Calling
.agent?,.fields?, or.rels?on any API resource crashes withMissingAttributeinstead of returningtrue.Mechanism: the SPECIAL_METHODS guard is gated on match[2].nil?
At
lib/booqable/strict_attributes.rb:63:When the method is
agent?,match[2]is"?"(not nil), somatch[2].nil?is false and the entire guard is skipped. The code then falls through to returnattr_name(:agent), which triggersMissingAttribute. Sawyer's ownmethod_missingnever gets a chance to handle the predicate form of the special method.The fix is to drop the
match[2].nil? &&condition so both plain and predicate forms of special methods are passed through to Sawyer:Was this helpful? React with 👍 or 👎 to provide feedback.