added comment for what to do about cycles in json schema

This commit is contained in:
Shreyas Goenka 2023-01-17 11:26:46 +01:00
parent 7385f37df6
commit 2a66a2fc74
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
1 changed files with 19 additions and 14 deletions

View File

@ -7,31 +7,36 @@ import (
"strings" "strings"
) )
const MaxHistoryOccurances = 3 // TODO: Add tests for the error cases, forcefully triggering them
// TODO: Add ignore for -
// TODO: add tests for the error cases, forcefully triggering them // TODO: Add required validation for omitempty
// TODO: Add support for refs in case of a cycle // TODO: Add example documentation
// TODO: handle case of self referential pointers in structs // TODO: Do final checks for more validation that can be added to json schema
// TODO: add ignore for -
type Schema struct { type Schema struct {
Type JavascriptType `json:"type"` Type JavascriptType `json:"type"`
Properties map[string]*Property `json:"properties,omitempty"` Properties map[string]*Property `json:"properties,omitempty"`
AdditionalProperties *Property `json:"additionalProperties,omitempty"` AdditionalProperties *Property `json:"additionalProperties,omitempty"`
} }
type Property struct { type Property struct {
Type JavascriptType `json:"type"` Type JavascriptType `json:"type"`
Items *Item `json:"items,omitempty"` Items *Item `json:"items,omitempty"`
Properties map[string]*Property `json:"properties,omitempty"` Properties map[string]*Property `json:"properties,omitempty"`
AdditionalProperties *Property `json:"additionalProperties,omitempty"` AdditionalProperties *Property `json:"additionalProperties,omitempty"`
} }
type Item struct { type Item struct {
Type JavascriptType `json:"type"` Type JavascriptType `json:"type"`
Properties map[string]*Property `json:"properties,omitempty"` Properties map[string]*Property `json:"properties,omitempty"`
} }
// NOTE about loops in golangType: Right now we error out if there is a loop
// in the types traversed when generating the json schema. This can be solved
// using $refs but there is complexity around making sure we do not create json
// schema's where properties indirectly refer to each other, which would be an
// invalid json schema. See https://json-schema.org/understanding-json-schema/structuring.html#recursion
// for more details
func NewSchema(golangType reflect.Type) (*Schema, error) { func NewSchema(golangType reflect.Type) (*Schema, error) {
seenTypes := map[reflect.Type]struct{}{} seenTypes := map[reflect.Type]struct{}{}
debugTrace := list.New() debugTrace := list.New()
@ -40,7 +45,7 @@ func NewSchema(golangType reflect.Type) (*Schema, error) {
return nil, errWithTrace(err.Error(), debugTrace) return nil, errWithTrace(err.Error(), debugTrace)
} }
return &Schema{ return &Schema{
Type: rootProp.Type, Type: rootProp.Type,
Properties: rootProp.Properties, Properties: rootProp.Properties,
AdditionalProperties: rootProp.AdditionalProperties, AdditionalProperties: rootProp.AdditionalProperties,
}, nil }, nil
@ -184,7 +189,7 @@ func toProperty(golangType reflect.Type, seenTypes map[reflect.Type]struct{}, de
} }
items = &Item{ items = &Item{
// TODO: Add a test for slice of object // TODO: Add a test for slice of object
Type: elemJavascriptType, Type: elemJavascriptType,
Properties: elemProps.Properties, Properties: elemProps.Properties,
} }
} }
@ -233,8 +238,8 @@ func toProperty(golangType reflect.Type, seenTypes map[reflect.Type]struct{}, de
} }
return &Property{ return &Property{
Type: rootJavascriptType, Type: rootJavascriptType,
Items: items, Items: items,
Properties: properties, Properties: properties,
AdditionalProperties: additionalProperties, AdditionalProperties: additionalProperties,
}, nil }, nil