|
| 1 | +// |
| 2 | +// Tencent is pleased to support the open source community by making trpc-agent-go available. |
| 3 | +// |
| 4 | +// Copyright (C) 2025 Tencent. All rights reserved. |
| 5 | +// |
| 6 | +// trpc-agent-go is licensed under the Apache License Version 2.0. |
| 7 | +// |
| 8 | +// |
| 9 | + |
| 10 | +package tool_test |
| 11 | + |
| 12 | +import ( |
| 13 | + "encoding/json" |
| 14 | + "reflect" |
| 15 | + "testing" |
| 16 | + |
| 17 | + itool "trpc.group/trpc-go/trpc-agent-go/internal/tool" |
| 18 | +) |
| 19 | + |
| 20 | +// TreeNode represents a recursive tree structure |
| 21 | +type TreeNode struct { |
| 22 | + Name string `json:"name"` |
| 23 | + Children []*TreeNode `json:"children,omitempty"` |
| 24 | +} |
| 25 | + |
| 26 | +// LinkedListNode represents a recursive linked list structure |
| 27 | +type LinkedListNode struct { |
| 28 | + Value int `json:"value"` |
| 29 | + Next *LinkedListNode `json:"next,omitempty"` |
| 30 | +} |
| 31 | + |
| 32 | +// MutuallyRecursiveA and MutuallyRecursiveB represent mutually recursive structures |
| 33 | +type MutuallyRecursiveA struct { |
| 34 | + Name string `json:"name"` |
| 35 | + B *MutuallyRecursiveB `json:"b,omitempty"` |
| 36 | +} |
| 37 | + |
| 38 | +type MutuallyRecursiveB struct { |
| 39 | + Value int `json:"value"` |
| 40 | + A *MutuallyRecursiveA `json:"a,omitempty"` |
| 41 | +} |
| 42 | + |
| 43 | +func TestGenerateJSONSchema_RecursiveStructure(t *testing.T) { |
| 44 | + t.Run("tree node recursive structure", func(t *testing.T) { |
| 45 | + // This should not panic and should generate a schema with $ref and $defs |
| 46 | + result := itool.GenerateJSONSchema(reflect.TypeOf(TreeNode{})) |
| 47 | + resultJson, _ := json.MarshalIndent(result, "", " ") |
| 48 | + t.Logf("%s", resultJson) |
| 49 | + |
| 50 | + if result.Type != "object" { |
| 51 | + t.Errorf("expected object type, got %s", result.Type) |
| 52 | + } |
| 53 | + |
| 54 | + // Check that we have properties |
| 55 | + if result.Properties == nil { |
| 56 | + t.Fatal("expected properties to be set") |
| 57 | + } |
| 58 | + |
| 59 | + // Check name property |
| 60 | + if result.Properties["name"] == nil || result.Properties["name"].Type != "string" { |
| 61 | + t.Errorf("expected name property of type string") |
| 62 | + } |
| 63 | + |
| 64 | + // Check children property |
| 65 | + if result.Properties["children"] == nil || result.Properties["children"].Type != "array" { |
| 66 | + t.Errorf("expected children property of type array") |
| 67 | + } |
| 68 | + |
| 69 | + // The children items should use $ref to avoid infinite recursion |
| 70 | + if result.Properties["children"].Items == nil { |
| 71 | + t.Fatal("expected children items to be defined") |
| 72 | + } |
| 73 | + |
| 74 | + // Check if we have $defs defined for recursive types |
| 75 | + if result.Defs == nil { |
| 76 | + t.Errorf("expected $defs to be defined for recursive structure") |
| 77 | + } |
| 78 | + |
| 79 | + // Check that children items use $ref |
| 80 | + if result.Properties["children"].Items.Ref != "#/$defs/treenode" { |
| 81 | + t.Errorf("expected children items to reference #/$defs/treenode, got %s", result.Properties["children"].Items.Ref) |
| 82 | + } |
| 83 | + |
| 84 | + // Check the definition in $defs |
| 85 | + treeDef := result.Defs["treenode"] |
| 86 | + if treeDef == nil { |
| 87 | + t.Fatal("expected treenode definition in $defs") |
| 88 | + } |
| 89 | + |
| 90 | + if treeDef.Type != "object" { |
| 91 | + t.Errorf("expected treenode definition type to be object, got %s", treeDef.Type) |
| 92 | + } |
| 93 | + |
| 94 | + // Check that the definition also uses $ref for children |
| 95 | + if treeDef.Properties["children"].Items == nil || treeDef.Properties["children"].Items.Ref != "#/$defs/treenode" { |
| 96 | + t.Errorf("expected treenode definition children items to reference #/$defs/treenode") |
| 97 | + } |
| 98 | + }) |
| 99 | + |
| 100 | + t.Run("linked list recursive structure", func(t *testing.T) { |
| 101 | + // This should not panic and should generate a schema with $ref and $defs |
| 102 | + result := itool.GenerateJSONSchema(reflect.TypeOf(LinkedListNode{})) |
| 103 | + resultJson, _ := json.MarshalIndent(result, "", " ") |
| 104 | + t.Logf("%s", resultJson) |
| 105 | + |
| 106 | + if result.Type != "object" { |
| 107 | + t.Errorf("expected object type, got %s", result.Type) |
| 108 | + } |
| 109 | + |
| 110 | + // Check that we have properties |
| 111 | + if result.Properties == nil { |
| 112 | + t.Fatal("expected properties to be set") |
| 113 | + } |
| 114 | + |
| 115 | + // Check value property |
| 116 | + if result.Properties["value"] == nil || result.Properties["value"].Type != "integer" { |
| 117 | + t.Errorf("expected value property of type integer") |
| 118 | + } |
| 119 | + |
| 120 | + // Check next property - should use $ref to avoid infinite recursion |
| 121 | + if result.Properties["next"] == nil { |
| 122 | + t.Fatal("expected next property to be defined") |
| 123 | + } |
| 124 | + |
| 125 | + // Check if we have $defs defined for recursive types |
| 126 | + if result.Defs == nil { |
| 127 | + t.Errorf("expected $defs to be defined for recursive structure") |
| 128 | + } |
| 129 | + }) |
| 130 | + |
| 131 | + t.Run("mutually recursive structures", func(t *testing.T) { |
| 132 | + // This should not panic and should generate a schema with $ref and $defs |
| 133 | + result := itool.GenerateJSONSchema(reflect.TypeOf(MutuallyRecursiveA{})) |
| 134 | + resultJson, _ := json.MarshalIndent(result, "", " ") |
| 135 | + t.Logf("%s", resultJson) |
| 136 | + |
| 137 | + if result.Type != "object" { |
| 138 | + t.Errorf("expected object type, got %s", result.Type) |
| 139 | + } |
| 140 | + |
| 141 | + // Check that we have $defs for both types |
| 142 | + if result.Defs == nil { |
| 143 | + t.Fatal("expected $defs to be defined for mutually recursive structures") |
| 144 | + } |
| 145 | + |
| 146 | + // Should have definitions for both types |
| 147 | + expectedDefs := 2 // MutuallyRecursiveA and MutuallyRecursiveB |
| 148 | + if len(result.Defs) < expectedDefs { |
| 149 | + t.Errorf("expected at least %d definitions in $defs, got %d", expectedDefs, len(result.Defs)) |
| 150 | + } |
| 151 | + }) |
| 152 | +} |
0 commit comments