2025-01-10 17:33:58 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/libs/jsonschema"
|
|
|
|
)
|
|
|
|
|
2025-01-10 18:26:45 +00:00
|
|
|
func isReferenceType(v *jsonschema.Schema, refs map[string]jsonschema.Schema, customFields map[string]bool) bool {
|
|
|
|
if v.Type != "object" && v.Type != "array" {
|
|
|
|
return false
|
|
|
|
}
|
2025-01-10 17:33:58 +00:00
|
|
|
if len(v.Properties) > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if v.Items != nil {
|
|
|
|
items := resolveRefs(v.Items, refs)
|
|
|
|
if items != nil && items.Type == "object" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2025-01-10 18:26:45 +00:00
|
|
|
props := resolveAdditionaProperties(v)
|
|
|
|
if !isInOwnFields(props, customFields) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if props != nil {
|
|
|
|
propsResolved := resolveRefs(props, refs)
|
|
|
|
return propsResolved.Type == "object"
|
2025-01-10 17:33:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2025-01-10 18:26:45 +00:00
|
|
|
func isInOwnFields(node *jsonschema.Schema, customFields map[string]bool) bool {
|
|
|
|
if node != nil && node.Reference != nil {
|
|
|
|
return customFields[getRefType(node)]
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func resolveAdditionaProperties(v *jsonschema.Schema) *jsonschema.Schema {
|
2025-01-10 17:33:58 +00:00
|
|
|
if v.AdditionalProperties == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
additionalProps, ok := v.AdditionalProperties.(*jsonschema.Schema)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2025-01-10 18:26:45 +00:00
|
|
|
return additionalProps
|
2025-01-10 17:33:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func resolveRefs(s *jsonschema.Schema, schemas map[string]jsonschema.Schema) *jsonschema.Schema {
|
|
|
|
node := s
|
|
|
|
|
|
|
|
description := s.Description
|
|
|
|
markdownDescription := s.MarkdownDescription
|
|
|
|
examples := s.Examples
|
|
|
|
|
|
|
|
for node.Reference != nil {
|
2025-01-10 18:26:45 +00:00
|
|
|
ref := getRefType(node)
|
2025-01-10 17:33:58 +00:00
|
|
|
newNode, ok := schemas[ref]
|
|
|
|
if !ok {
|
|
|
|
log.Printf("schema %s not found", ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
if description == "" {
|
|
|
|
description = newNode.Description
|
|
|
|
}
|
|
|
|
if markdownDescription == "" {
|
|
|
|
markdownDescription = newNode.MarkdownDescription
|
|
|
|
}
|
|
|
|
if len(examples) == 0 {
|
|
|
|
examples = newNode.Examples
|
|
|
|
}
|
|
|
|
|
|
|
|
node = &newNode
|
|
|
|
}
|
|
|
|
|
|
|
|
node.Description = description
|
|
|
|
node.MarkdownDescription = markdownDescription
|
|
|
|
node.Examples = examples
|
|
|
|
|
|
|
|
return node
|
|
|
|
}
|
2025-01-10 18:26:45 +00:00
|
|
|
|
|
|
|
func getRefType(node *jsonschema.Schema) string {
|
|
|
|
if node.Reference == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return strings.TrimPrefix(*node.Reference, "#/$defs/")
|
|
|
|
}
|