| 23 | } |
| 24 | |
| 25 | func SchemaToMap(params any) (map[string]any, error) { |
| 26 | m := map[string]any{} |
| 27 | if params != nil { |
| 28 | buf, err := json.Marshal(params) |
| 29 | if err != nil { |
| 30 | return nil, err |
| 31 | } |
| 32 | |
| 33 | if err := json.Unmarshal(buf, &m); err != nil { |
| 34 | return nil, err |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Ensure we have at least an empty object schema. |
| 39 | // That's especially important for DMR but can't hurt for others. |
| 40 | if m["type"] == nil { |
| 41 | m["type"] = "object" |
| 42 | } |
| 43 | if m["properties"] == nil { |
| 44 | m["properties"] = map[string]any{} |
| 45 | } |
| 46 | if m["required"] == nil { |
| 47 | delete(m, "required") |
| 48 | } |
| 49 | |
| 50 | // Ensure all properties have a type set, recursively. |
| 51 | ensurePropertyTypes(m) |
| 52 | |
| 53 | // Drop "null" from the type of required fields. Required + nullable |
| 54 | // is contradictory and only inflates the schema's token cost. |
| 55 | // jsonschema-go emits ["null", "array"] for any Go slice, including |
| 56 | // required ones; this normalizes those back to a plain "array". |
| 57 | stripNullFromRequiredTypes(m) |
| 58 | |
| 59 | return m, nil |
| 60 | } |
| 61 | |
| 62 | // stripNullFromRequiredTypes recursively walks a JSON Schema map and removes |
| 63 | // "null" from the type of every property listed in its parent's "required" |