MCPcopy Index your code
hub / github.com/rilldata/rill / rewriteJSONSchemaRefs

Function rewriteJSONSchemaRefs

runtime/pkg/openapiutil/openapiutil.go:89–124  ·  view source on GitHub ↗

rewriteJSONSchemaRefs recursively rewrites $ref paths from "#/$defs/Name" to "#/components/schemas/ Name". This is necessary to convert a JSON schema to an OpenAPI schema. See the docstring for ParseJSONSchema for more details. This is an in-place operation that modifies the data structur

(namePrefix string, data any)

Source from the content-addressed store, hash-verified

87// This is necessary to convert a JSON schema to an OpenAPI schema. See the docstring for ParseJSONSchema for more details.
88// This is an in-place operation that modifies the data structure directly.
89func rewriteJSONSchemaRefs(namePrefix string, data any) error {
90 switch v := data.(type) {
91 case map[string]any:
92 for key, value := range v {
93 // If it's not a $ref, recursively process the value.
94 if key != "$ref" {
95 if err := rewriteJSONSchemaRefs(namePrefix, value); err != nil {
96 return err
97 }
98 continue
99 }
100
101 refStr, ok := value.(string)
102 if !ok {
103 return fmt.Errorf("expected string value for $ref, got %T", value)
104 }
105
106 // Convert #/$defs/Name to #/components/schemas/<namePrefix>Name
107 if strings.HasPrefix(refStr, "#/$defs/") {
108 defName := strings.TrimPrefix(refStr, "#/$defs/")
109 v[key] = fmt.Sprintf("#/components/schemas/%s%s", namePrefix, defName)
110 }
111 }
112 case []any:
113 // Recursively process each item
114 for _, item := range v {
115 if err := rewriteJSONSchemaRefs(namePrefix, item); err != nil {
116 return err
117 }
118 }
119 default:
120 // For primitive types (string, number, bool, nil), nothing to do
121 // Since this is in-place, we don't need to return anything
122 }
123 return nil
124}
125
126func mapToSchema(schema map[string]any) (*openapi3.Schema, error) {
127 specSchema := openapi3.Schema{}

Callers 1

ParseJSONSchemaFunction · 0.85

Calls 1

ErrorfMethod · 0.65

Tested by

no test coverage detected