Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions jsonschema/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@ func GenerateSchemaForType(v any) (*Definition, error) {
if err != nil {
return nil, err
}
// If the schema has a root $ref, resolve it by:
// 1. Extracting the key from the $ref.
// 2. Detaching the referenced definition from $defs.
// 3. Checking for self-references in the detached definition.
// - If a self-reference is found, restore the original $defs structure.
// 4. Flattening the referenced definition into the root schema.
// 5. Clearing the $ref field in the root schema.
if def.Ref != "" {
origRef := def.Ref
key := strings.TrimPrefix(origRef, "#/$defs/")
if root, ok := defs[key]; ok {
delete(defs, key)
root.Defs = defs
if containsRef(root, origRef) {
root.Defs = nil
defs[key] = root
}
*def = root
}
def.Ref = ""
}
def.Defs = defs
return def, nil
}
Expand Down Expand Up @@ -189,3 +210,26 @@ func reflectSchemaObject(t reflect.Type, defs map[string]Definition) (*Definitio
d.Properties = properties
return &d, nil
}

func containsRef(def Definition, targetRef string) bool {
if def.Ref == targetRef {
return true
}

for _, d := range def.Defs {
if containsRef(d, targetRef) {
return true
}
}

for _, prop := range def.Properties {
if containsRef(prop, targetRef) {
return true
}
}

if def.Items != nil && containsRef(*def.Items, targetRef) {
return true
}
return false
}
174 changes: 174 additions & 0 deletions jsonschema/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,18 @@ func TestDefinition_MarshalJSON(t *testing.T) {
}
}

type User struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Orders []*Order `json:"orders,omitempty"`
}

type Order struct {
ID int `json:"id,omitempty"`
Amount float64 `json:"amount,omitempty"`
Buyer *User `json:"buyer,omitempty"`
}

func TestStructToSchema(t *testing.T) {
type Tweet struct {
Text string `json:"text"`
Expand All @@ -194,6 +206,13 @@ func TestStructToSchema(t *testing.T) {
Tweets []Tweet `json:"tweets,omitempty"`
}

type MyStructuredResponse struct {
PascalCase string `json:"pascal_case" required:"true" description:"PascalCase"`
CamelCase string `json:"camel_case" required:"true" description:"CamelCase"`
KebabCase string `json:"kebab_case" required:"true" description:"KebabCase"`
SnakeCase string `json:"snake_case" required:"true" description:"SnakeCase"`
}

tests := []struct {
name string
in any
Expand Down Expand Up @@ -444,6 +463,161 @@ func TestStructToSchema(t *testing.T) {
"additionalProperties" : false
}
}
}`,
},
{
name: "Test Person",
in: Person{},
want: `{
"type": "object",
"properties": {
"age": {
"type": "integer"
},
"friends": {
"type": "array",
"items": {
"$ref": "#/$defs/Person"
}
},
"name": {
"type": "string"
},
"tweets": {
"type": "array",
"items": {
"$ref": "#/$defs/Tweet"
}
}
},
"additionalProperties": false,
"$defs": {
"Person": {
"type": "object",
"properties": {
"age": {
"type": "integer"
},
"friends": {
"type": "array",
"items": {
"$ref": "#/$defs/Person"
}
},
"name": {
"type": "string"
},
"tweets": {
"type": "array",
"items": {
"$ref": "#/$defs/Tweet"
}
}
},
"additionalProperties": false
},
"Tweet": {
"type": "object",
"properties": {
"text": {
"type": "string"
}
},
"required": [
"text"
],
"additionalProperties": false
}
}
}`,
},
{
name: "Test MyStructuredResponse",
in: MyStructuredResponse{},
want: `{
"type": "object",
"properties": {
"camel_case": {
"type": "string",
"description": "CamelCase"
},
"kebab_case": {
"type": "string",
"description": "KebabCase"
},
"pascal_case": {
"type": "string",
"description": "PascalCase"
},
"snake_case": {
"type": "string",
"description": "SnakeCase"
}
},
"required": [
"pascal_case",
"camel_case",
"kebab_case",
"snake_case"
],
"additionalProperties": false
}`,
},
{
name: "Test User",
in: User{},
want: `{
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"orders": {
"type": "array",
"items": {
"$ref": "#/$defs/Order"
}
}
},
"additionalProperties": false,
"$defs": {
"Order": {
"type": "object",
"properties": {
"amount": {
"type": "number"
},
"buyer": {
"$ref": "#/$defs/User"
},
"id": {
"type": "integer"
}
},
"additionalProperties": false
},
"User": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"orders": {
"type": "array",
"items": {
"$ref": "#/$defs/Order"
}
}
},
"additionalProperties": false
}
}
}`,
},
}
Expand Down
Loading