|
| 1 | +package jsonschema_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/sashabaranov/go-openai/jsonschema" |
| 7 | +) |
| 8 | + |
| 9 | +// SelfRef struct used to produce a self-referential schema. |
| 10 | +type SelfRef struct { |
| 11 | + Friends []SelfRef `json:"friends"` |
| 12 | +} |
| 13 | + |
| 14 | +// Address struct referenced by Person without self-reference. |
| 15 | +type Address struct { |
| 16 | + Street string `json:"street"` |
| 17 | +} |
| 18 | + |
| 19 | +type Person struct { |
| 20 | + Address Address `json:"address"` |
| 21 | +} |
| 22 | + |
| 23 | +// TestGenerateSchemaForType_SelfRef ensures that self-referential types are not |
| 24 | +// flattened during schema generation. |
| 25 | +func TestGenerateSchemaForType_SelfRef(t *testing.T) { |
| 26 | + schema, err := jsonschema.GenerateSchemaForType(SelfRef{}) |
| 27 | + if err != nil { |
| 28 | + t.Fatalf("unexpected error: %v", err) |
| 29 | + } |
| 30 | + if _, ok := schema.Defs["SelfRef"]; !ok { |
| 31 | + t.Fatal("expected defs to contain SelfRef for self reference") |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// TestGenerateSchemaForType_NoSelfRef ensures that non-self-referential types |
| 36 | +// are flattened and do not reappear in $defs. |
| 37 | +func TestGenerateSchemaForType_NoSelfRef(t *testing.T) { |
| 38 | + schema, err := jsonschema.GenerateSchemaForType(Person{}) |
| 39 | + if err != nil { |
| 40 | + t.Fatalf("unexpected error: %v", err) |
| 41 | + } |
| 42 | + if _, ok := schema.Defs["Person"]; ok { |
| 43 | + t.Fatal("unexpected Person definition in defs") |
| 44 | + } |
| 45 | + if _, ok := schema.Defs["Address"]; !ok { |
| 46 | + t.Fatal("expected Address definition in defs") |
| 47 | + } |
| 48 | +} |
0 commit comments