stripNullFromRequiredTypes recursively walks a JSON Schema map and removes "null" from the type of every property listed in its parent's "required" array. Optional properties are left untouched.
(schema map[string]any)
| 63 | // "null" from the type of every property listed in its parent's "required" |
| 64 | // array. Optional properties are left untouched. |
| 65 | func stripNullFromRequiredTypes(schema map[string]any) { |
| 66 | props, ok := schema["properties"].(map[string]any) |
| 67 | if !ok { |
| 68 | return |
| 69 | } |
| 70 | |
| 71 | required := requiredSet(schema) |
| 72 | |
| 73 | for name, v := range props { |
| 74 | prop, ok := v.(map[string]any) |
| 75 | if !ok { |
| 76 | continue |
| 77 | } |
| 78 | |
| 79 | if required[name] { |
| 80 | removeNullFromType(prop) |
| 81 | } |
| 82 | |
| 83 | stripNullFromRequiredTypes(prop) |
| 84 | if items, ok := prop["items"].(map[string]any); ok { |
| 85 | stripNullFromRequiredTypes(items) |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func requiredSet(schema map[string]any) map[string]bool { |
| 91 | set := map[string]bool{} |
no test coverage detected