Skip to content
Open
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
8 changes: 8 additions & 0 deletions go/internal/base/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,15 @@ func InferJSONSchema(x any) (s *jsonschema.Schema) {
}
if baseType.Kind() == reflect.Struct {
if seen[baseType] {
// check if the type implements json.Marshaler since it might serialize to a non-object
marshalerType := reflect.TypeOf((*json.Marshaler)(nil)).Elem()
if baseType.Implements(marshalerType) || reflect.PointerTo(baseType).Implements(marshalerType) {
return &jsonschema.Schema{
AdditionalProperties: jsonschema.TrueSchema,
}
}
return &jsonschema.Schema{
Type: "object",
AdditionalProperties: jsonschema.TrueSchema,
}
}
Expand Down
69 changes: 69 additions & 0 deletions go/internal/base/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package base

import (
"encoding/json"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -196,7 +197,75 @@ func TestSchemaAsMapRecursive(t *testing.T) {
t.Fatal("expected children to have items")
}
// The recursive Node reference should have become an "any" schema
// including "type" property to prevent recursion errors for schemas for the same type
if items["type"] != "object" {
t.Errorf("expected children.items.type to be 'object', got %v", items["type"])
}
if items["additionalProperties"] != true {
t.Errorf("expected children.items to be 'any' schema (additionalProperties: true), got %v", items)
}
}

func TestInferJSONSchema_SharedType(t *testing.T) {
type Shared struct {
Amount float64 `json:"amount"`
}
type Prizes struct {
First Shared `json:"first"`
Second Shared `json:"second"`
}

schema := SchemaAsMap(InferJSONSchema(Prizes{}))
properties, ok := schema["properties"].(map[string]any)
if !ok {
t.Fatal("expected properties in schema")
}

second, ok := properties["second"].(map[string]any)
if !ok {
t.Fatal("expected 'second' property in schema")
}

if second["type"] != "object" {
t.Errorf("expected type: object for shared type occurrence, got %v", second["type"])
}
if second["additionalProperties"] != true {
t.Errorf("expected additionalProperties: true for shared type occurrence, got %v", second["additionalProperties"])
}
}

type testStringer struct {
Value string
}

func (s testStringer) MarshalJSON() ([]byte, error) {
return json.Marshal(s.Value)
}

func TestInferJSONSchema_SharedTypeWithMarshaler(t *testing.T) {
type Container struct {
A testStringer
B testStringer
}
schema := SchemaAsMap(InferJSONSchema(Container{}))
properties, ok := schema["properties"].(map[string]any)
if !ok {
t.Fatal("expected properties in schema")
}

second, ok := properties["B"].(map[string]any)
if !ok {
t.Fatal("expected 'B' property in schema")
}

// type should be non-object because it implements json.Marshaler
if typ, hasType := second["type"]; hasType {
if typ == "object" {
t.Errorf("expected type to NOT be object for shared type with marshaler, got %v", typ)
}
}

if val, ok := second["additionalProperties"]; !ok || val != true {
t.Errorf("expected additionalProperties: true, got %v", val)
}
}
Loading