diff --git a/ext/java/nokogiri/XmlSchema.java b/ext/java/nokogiri/XmlSchema.java index d965965976..bc5dd29c06 100644 --- a/ext/java/nokogiri/XmlSchema.java +++ b/ext/java/nokogiri/XmlSchema.java @@ -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."); diff --git a/ext/nokogiri/xml_schema.c b/ext/nokogiri/xml_schema.c index c08eb11d79..a94c18d404 100644 --- a/ext/nokogiri/xml_schema.c +++ b/ext/nokogiri/xml_schema.c @@ -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 diff --git a/test/xml/test_schema.rb b/test/xml/test_schema.rb index 7500622d28..28ee212d6a 100644 --- a/test/xml/test_schema.rb +++ b/test/xml/test_schema.rb @@ -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")