Fix "undefined method `[]' for #<Nori::StringIOFile>" when adding File #495
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.
When calling
File#add
, the response XML sets atype="file"
attributeon the
baseRef
element, where "file" corresponds to the NetSuiterecord. This then causes Nori (the XML parser used by Savon, the SOAP
client) to interpret that element as representing a base64 encoded file,
so it tries to get fancy about how it parses that element into a hash by
returning an instance of it's
StringIOFile
class:https://github.com/savonrb/nori/blob/f59f8e18b0e53285c87d75a635058745e66b0bfb/lib/nori/xml_utility_node.rb#L131-L136
Either NetSuite's doing something non-standard with it's use of the
type
attribute that Nori is trying to enforce, or Nori isover-aggressive in trying to typecast to aid the developer.
The end result was that when we tried to extract the
internal_id
fromthe response, the
body
was actually an instance ofStringIOFile
, nota hash:
netsuite/lib/netsuite/actions/add.rb
Line 80 in f0e46a0
To work around this, as I don't see a way to disable such behavior in
Savon/Nori, if we detect the
baseRef
element was parsed toStringIOFile
, we'll then take the raw XML and parse out thebaseRef
ourselves, returning a hash with the
internal_id
as we'd exect fromnon-
File
responses.I'm not thrilled with this solution. If we ever needed something else
from the
baseRef
element, this effectively drops all other attributesfor file records. It also introduces explicit references to
Nori
andNokogiri
, both of which are dependencies of Savon, but I'm not sure ifthat means this gem should list them as explicit dependencies to guard
against Savon replacing them in a future update. Listing them as
dependencies would then require keeping their version constraints in
sync with Savon most likely.
I believe this answers a question from #481:
#481 (comment)
However the fix in #481 solves it by not trying to extract the
internal_id
, which would create a problem if someone wanted to add afile then save the ID in their own database for future use.