Skip to content

Commit

Permalink
fix: Schema.from_document parameter type checks
Browse files Browse the repository at this point in the history
(cherry picked from commit 7911c0b)
  • Loading branch information
flavorjones committed Jul 5, 2023
1 parent 9435007 commit 07924d9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ext/java/nokogiri/XmlSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ public class XmlSchema extends RubyObject
parseOptions = args[1];
}

if (!(rbDocument instanceof XmlNode)) {
String msg = "expected parameter to be a Nokogiri::XML::Document, received " + rbDocument.getMetaClass();
throw context.runtime.newTypeError(msg);
}
if (!(rbDocument instanceof XmlDocument)) {
// TODO: deprecate allowing Node
context.runtime.getWarnings().warn("Passing a Node as the first parameter to Schema.from_document is deprecated. Please pass a Document instead. This will become an error in a future release of Nokogiri.");
Expand Down
6 changes: 6 additions & 0 deletions ext/nokogiri/xml_schema.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ rb_xml_schema_s_from_document(int argc, VALUE *argv, VALUE klass)

rb_scan_args(argc, argv, "11", &rb_document, &rb_parse_options);

if (!rb_obj_is_kind_of(rb_document, cNokogiriXmlNode)) {
rb_raise(rb_eTypeError,
"expected parameter to be a Nokogiri::XML::Document, received %"PRIsVALUE,
rb_obj_class(rb_document));
}

if (!rb_obj_is_kind_of(rb_document, cNokogiriXmlDocument)) {
xmlNodePtr deprecated_node_type_arg;
// TODO: deprecate allowing Node
Expand Down
7 changes: 7 additions & 0 deletions test/xml/test_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ class TestNokogiriXMLSchema < Nokogiri::TestCase
assert_instance_of(Nokogiri::XML::Schema, xsd)
end

it ".from_document not accept anything other than Node or Document" do
assert_raises(TypeError) { Nokogiri::XML::Schema.from_document(1234) }
assert_raises(TypeError) { Nokogiri::XML::Schema.from_document("asdf") }
assert_raises(TypeError) { Nokogiri::XML::Schema.from_document({}) }
assert_raises(TypeError) { Nokogiri::XML::Schema.from_document(nil) }
end

it "schema_validates_with_relative_paths" do
xsd = File.join(ASSETS_DIR, "foo", "foo.xsd")
xml = File.join(ASSETS_DIR, "valid_bar.xml")
Expand Down

0 comments on commit 07924d9

Please sign in to comment.