You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Merge pull request #2602 from sparklemotion/flavorjones-fix-reader-node-gc_backport-v1.13.x
fix: XML::Reader XML::Attr garbage collection (backport to v1.13.x)
---
**What problem is this PR intended to solve?**
This is a proposed fix for #2598, see that issue for an extended explanation of the problem.
This PR implements "option 2" from that issue's proposed solutions:
- introduce a new `Reader#attribute_hash` that will return a `Hash<String ⇒ String>` (instead of an `Array<XML::Attr>`)
- deprecate `Reader#attribute_nodes` with a plan to remove it entirely in a future release
- re-implement `Reader#attributes` to use `#attribute_hash` (instead of `#attribute_nodes`)
After this change, only applications calling `Reader#attribute_nodes` directly will be running the unsafe code. These users will see a deprecation warning and may use `#attribute_hash` as a replacement.
I think it's very possible that `Reader#attribute_hash` won't meet the needs of people who are working with namespaced attributes and are using `#attribute_nodes` for this purpose. However, I'm intentionally deferring any attempt to solve that DX problem until someone who needs this functionality asks for it.
**Have you included adequate test coverage?**
I tried and failed to add test coverage to the suite that would reproduce the underlying GC bug.
However, existing test coverage of `Reader#attributes` is sufficient for now.
**Does this change affect the behavior of either the C or the Java implementations?**
This PR modifies both the C and Java implementations to behave the same.
Notably, the Java implementation contains a small bugfix which is that `Reader#namespaces` now returns an empty hash when there are no namespaces (it previously returned `nil`).
Copy file name to clipboardExpand all lines: ext/java/nokogiri/XmlReader.java
+8
Original file line number
Diff line number
Diff line change
@@ -141,9 +141,17 @@ public class XmlReader extends RubyObject
141
141
publicIRubyObject
142
142
attribute_nodes(ThreadContextcontext)
143
143
{
144
+
context.runtime.getWarnings().warn("Reader#attribute_nodes is deprecated and will be removed in a future version of Nokogiri. Please use Reader#attribute_hash instead.");
Copy file name to clipboardExpand all lines: ext/nokogiri/xml_node.c
+1-1
Original file line number
Diff line number
Diff line change
@@ -1806,7 +1806,7 @@ rb_xml_node_new(int argc, VALUE *argv, VALUE klass)
1806
1806
}
1807
1807
if (!rb_obj_is_kind_of(rb_document_node, cNokogiriXmlDocument)) {
1808
1808
// TODO: deprecate allowing Node
1809
-
rb_warn("Passing a Node as the second parameter to Node.new is deprecated. Please pass a Document instead, or prefer an alternative constructor like Node#add_child. This will become an error in a future release of Nokogiri.");
1809
+
NOKO_WARN_DEPRECATION("Passing a Node as the second parameter to Node.new is deprecated. Please pass a Document instead, or prefer an alternative constructor like Node#add_child. This will become an error in a future release of Nokogiri.");
// TODO: deprecated, remove in Nokogiri v1.15, see https://github.com/sparklemotion/nokogiri/issues/2598
169
+
// After removal, we can also remove all the "node_has_a_document" special handling from xml_node.c
170
+
NOKO_WARN_DEPRECATION("Reader#attribute_nodes is deprecated and will be removed in a future version of Nokogiri. Please use Reader#attribute_hash instead.");
0 commit comments