@@ -238,6 +238,45 @@ class OutputSchemaTest < ActiveSupport::TestCase
238238 assert_raises ( OutputSchema ::ValidationError ) { schema . validate_result ( 42 ) }
239239 end
240240
241+ test "does not inject a root type into a root-level $dynamicRef schema" do
242+ # `Schema` restricts `$ref` and `$dynamicRef` alike, and the root default has to know both: with the object default merged in,
243+ # this schema advertised `type: "object"` and rejected the array it describes.
244+ schema = OutputSchema . new (
245+ "$dynamicRef" : "#vector" ,
246+ "$defs" : { vector : { "$dynamicAnchor" : "vector" , type : "array" , items : { type : "number" } } } ,
247+ )
248+
249+ refute schema . to_h . key? ( :type )
250+ assert_equal "#vector" , schema . to_h [ :"$dynamicRef" ]
251+ assert_nothing_raised { schema . validate_result ( [ 1 , 2 ] ) }
252+ assert_raises ( OutputSchema ::ValidationError ) { schema . validate_result ( [ "bad" ] ) }
253+ assert_raises ( OutputSchema ::ValidationError ) { schema . validate_result ( { } ) }
254+ assert_empty MCP ::Tool ::Schema ::REFERENCE_KEYWORDS - OutputSchema ::ROOT_SCHEMA_KEYWORDS
255+ end
256+
257+ test "keeps an explicit type beside a root-level $dynamicRef" do
258+ schema = OutputSchema . new (
259+ type : "array" ,
260+ "$dynamicRef" : "#vector" ,
261+ "$defs" : { vector : { "$dynamicAnchor" : "vector" , items : { type : "number" } } } ,
262+ )
263+
264+ assert_equal "array" , schema . to_h [ :type ]
265+ assert_nothing_raised { schema . validate_result ( [ 1 , 2 ] ) }
266+ assert_raises ( OutputSchema ::ValidationError ) { schema . validate_result ( "text" ) }
267+ end
268+
269+ test "does not inject a root type into a root-level $dynamicRef schema given with String keys" do
270+ schema = OutputSchema . new (
271+ "$dynamicRef" => "#scalar" ,
272+ "$defs" => { "scalar" => { "$dynamicAnchor" => "scalar" , "type" => "string" } } ,
273+ )
274+
275+ refute schema . to_h . key? ( :type )
276+ assert_nothing_raised { schema . validate_result ( "text" ) }
277+ assert_raises ( OutputSchema ::ValidationError ) { schema . validate_result ( 42 ) }
278+ end
279+
241280 test "allows primitive root schemas" do
242281 schema = OutputSchema . new ( type : "string" )
243282
0 commit comments