generateShallowJSONSchema creates a schema that references definitions instead of recursing
(t reflect.Type, meta *AtomMeta)
| 187 | |
| 188 | // generateShallowJSONSchema creates a schema that references definitions instead of recursing |
| 189 | func generateShallowJSONSchema(t reflect.Type, meta *AtomMeta) map[string]any { |
| 190 | schema := make(map[string]any) |
| 191 | defer func() { |
| 192 | annotateSchemaWithAtomMeta(schema, meta) |
| 193 | }() |
| 194 | |
| 195 | // Special case for time.Time - treat as string with date-time format |
| 196 | if t == reflect.TypeOf(time.Time{}) { |
| 197 | schema["type"] = "string" |
| 198 | schema["format"] = "date-time" |
| 199 | return schema |
| 200 | } |
| 201 | |
| 202 | // Special case for []byte - treat as string with base64 encoding |
| 203 | if t.Kind() == reflect.Slice && t.Elem().Kind() == reflect.Uint8 { |
| 204 | schema["type"] = "string" |
| 205 | schema["contentEncoding"] = "base64" |
| 206 | schema["contentMediaType"] = "application/octet-stream" |
| 207 | return schema |
| 208 | } |
| 209 | |
| 210 | switch t.Kind() { |
| 211 | case reflect.String: |
| 212 | schema["type"] = "string" |
| 213 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, |
| 214 | reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 215 | schema["type"] = "integer" |
| 216 | case reflect.Float32, reflect.Float64: |
| 217 | schema["type"] = "number" |
| 218 | case reflect.Bool: |
| 219 | schema["type"] = "boolean" |
| 220 | case reflect.Slice, reflect.Array: |
| 221 | schema["type"] = "array" |
| 222 | if t.Elem() != nil { |
| 223 | schema["items"] = generateShallowJSONSchema(t.Elem(), nil) |
| 224 | } |
| 225 | case reflect.Map: |
| 226 | schema["type"] = "object" |
| 227 | if t.Elem() != nil { |
| 228 | schema["additionalProperties"] = generateShallowJSONSchema(t.Elem(), nil) |
| 229 | } |
| 230 | case reflect.Struct: |
| 231 | // Reference the definition instead of recursing |
| 232 | schema["$ref"] = fmt.Sprintf("#/$defs/%s", t.Name()) |
| 233 | case reflect.Ptr: |
| 234 | return generateShallowJSONSchema(t.Elem(), meta) |
| 235 | case reflect.Interface: |
| 236 | schema["type"] = "object" |
| 237 | default: |
| 238 | schema["type"] = "object" |
| 239 | } |
| 240 | |
| 241 | return schema |
| 242 | } |
| 243 | |
| 244 | // getAtomMeta extracts AtomMeta from the atom |
| 245 | func getAtomMeta(atom genAtom) *AtomMeta { |
no test coverage detected