Skip to content

Commit

Permalink
fix: schema array parsing with nested objects (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
abelanger5 authored Mar 4, 2024
1 parent 6a6038b commit ca56800
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,17 @@ func parseObject(obj map[string]interface{}) reflect.Type {
for key, val := range obj {
fieldType := parse(val)
defaultValue := formatDefaultValue(val)

tag := fmt.Sprintf(`json:"%s" jsonschema:"default=%s"`, key, defaultValue)

if defaultValue == "" {
tag = fmt.Sprintf(`json:"%s"`, key)
}

field := reflect.StructField{
Name: toExportedName(key),
Type: fieldType,
Tag: reflect.StructTag(fmt.Sprintf(`json:"%s" jsonschema:"default=%s"`, key, defaultValue)),
Tag: reflect.StructTag(tag),
}
fields = append(fields, field)
}
Expand All @@ -79,15 +86,14 @@ func formatDefaultValue(val interface{}) string {
case nil:
return "null"
default:
// Complex types like arrays or objects are not supported as default values in jsonschema tags.
return ""
}
}

// parseArray handles JSON arrays by determining the type of its elements.
func parseArray(arr []interface{}) reflect.Type {
if len(arr) == 0 {
return reflect.SliceOf(reflect.TypeOf(new(interface{})).Elem())
return reflect.SliceOf(reflect.TypeOf(""))
}
elemType := parse(arr[0])
return reflect.SliceOf(elemType)
Expand Down

0 comments on commit ca56800

Please sign in to comment.