Skip to content

Commit 5e1cf4e

Browse files
committed
Assert at least one $ref is found in rewrite tests
Address Copilot review feedback: AssertAllRefsStartWith and AssertAllRefsResolvable now return a count of $ref nodes encountered, and both tests assert the count is > 0 so they cannot vacuously pass if JsonSchemaExporter stops emitting $ref for these shapes.
1 parent 9c68a58 commit 5e1cf4e

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,10 @@ public async Task StructuredOutput_WithDuplicateTypeRefs_RewritesRefPointers()
593593
// (confirming the rewrite happened).
594594
string schemaJson = tool.ProtocolTool.OutputSchema.Value.GetRawText();
595595
var schemaNode = JsonNode.Parse(schemaJson)!;
596-
AssertAllRefsStartWith(schemaNode, "#/properties/result");
597-
AssertAllRefsResolvable(schemaNode, schemaNode);
596+
int refCount = AssertAllRefsStartWith(schemaNode, "#/properties/result");
597+
Assert.True(refCount > 0, "Expected at least one $ref in the schema to validate the rewrite, but none were found.");
598+
int resolvableCount = AssertAllRefsResolvable(schemaNode, schemaNode);
599+
Assert.True(resolvableCount > 0, "Expected at least one resolvable $ref in the schema, but none were found.");
598600
}
599601

600602
[Fact]
@@ -631,39 +633,46 @@ public async Task StructuredOutput_WithRecursiveTypeRefs_RewritesRefPointers()
631633

632634
string schemaJson = tool.ProtocolTool.OutputSchema.Value.GetRawText();
633635
var schemaNode = JsonNode.Parse(schemaJson)!;
634-
AssertAllRefsStartWith(schemaNode, "#/properties/result");
635-
AssertAllRefsResolvable(schemaNode, schemaNode);
636+
int refCount = AssertAllRefsStartWith(schemaNode, "#/properties/result");
637+
Assert.True(refCount > 0, "Expected at least one $ref in the schema to validate the rewrite, but none were found.");
638+
int resolvableCount = AssertAllRefsResolvable(schemaNode, schemaNode);
639+
Assert.True(resolvableCount > 0, "Expected at least one resolvable $ref in the schema, but none were found.");
636640
}
637641

638-
private static void AssertAllRefsStartWith(JsonNode? node, string expectedPrefix)
642+
private static int AssertAllRefsStartWith(JsonNode? node, string expectedPrefix)
639643
{
644+
int count = 0;
640645
if (node is JsonObject obj)
641646
{
642647
if (obj.TryGetPropertyValue("$ref", out JsonNode? refNode) &&
643648
refNode?.GetValue<string>() is string refValue)
644649
{
645650
Assert.StartsWith(expectedPrefix, refValue);
651+
count++;
646652
}
647653

648654
foreach (var property in obj)
649655
{
650-
AssertAllRefsStartWith(property.Value, expectedPrefix);
656+
count += AssertAllRefsStartWith(property.Value, expectedPrefix);
651657
}
652658
}
653659
else if (node is JsonArray arr)
654660
{
655661
foreach (var item in arr)
656662
{
657-
AssertAllRefsStartWith(item, expectedPrefix);
663+
count += AssertAllRefsStartWith(item, expectedPrefix);
658664
}
659665
}
666+
667+
return count;
660668
}
661669

662670
/// <summary>
663671
/// Walks the JSON tree and verifies that every <c>$ref</c> pointer resolves to a valid node.
664672
/// </summary>
665-
private static void AssertAllRefsResolvable(JsonNode root, JsonNode? node)
673+
private static int AssertAllRefsResolvable(JsonNode root, JsonNode? node)
666674
{
675+
int count = 0;
667676
if (node is JsonObject obj)
668677
{
669678
if (obj.TryGetPropertyValue("$ref", out JsonNode? refNode) &&
@@ -672,20 +681,23 @@ private static void AssertAllRefsResolvable(JsonNode root, JsonNode? node)
672681
{
673682
var resolved = ResolveJsonPointer(root, refValue);
674683
Assert.True(resolved is not null, $"$ref \"{refValue}\" does not resolve to a valid node in the schema.");
684+
count++;
675685
}
676686

677687
foreach (var property in obj)
678688
{
679-
AssertAllRefsResolvable(root, property.Value);
689+
count += AssertAllRefsResolvable(root, property.Value);
680690
}
681691
}
682692
else if (node is JsonArray arr)
683693
{
684694
foreach (var item in arr)
685695
{
686-
AssertAllRefsResolvable(root, item);
696+
count += AssertAllRefsResolvable(root, item);
687697
}
688698
}
699+
700+
return count;
689701
}
690702

691703
/// <summary>

0 commit comments

Comments
 (0)