describeSchemaType names the JSON a schema allows, such as "string or null" or "object (simple-user)".
(ref *openapi3.SchemaRef)
| 436 | // describeSchemaType names the JSON a schema allows, such as "string or null" |
| 437 | // or "object (simple-user)". |
| 438 | func describeSchemaType(ref *openapi3.SchemaRef) string { |
| 439 | if ref == nil || ref.Value == nil { |
| 440 | return "any JSON value" |
| 441 | } |
| 442 | s := ref.Value |
| 443 | var names []string |
| 444 | nullable := s.Nullable |
| 445 | switch { |
| 446 | case len(s.OneOf) > 0 || len(s.AnyOf) > 0: |
| 447 | for _, alt := range append(append(openapi3.SchemaRefs{}, s.OneOf...), s.AnyOf...) { |
| 448 | names = append(names, describeSchemaType(alt)) |
| 449 | } |
| 450 | default: |
| 451 | types := s.Type.Slice() |
| 452 | if len(types) == 0 && (len(s.Properties) > 0 || len(s.AllOf) > 0) { |
| 453 | types = []string{"object"} |
| 454 | } |
| 455 | for _, t := range types { |
| 456 | if t == "null" { |
| 457 | nullable = true |
| 458 | continue |
| 459 | } |
| 460 | names = append(names, schemaTypeName(t, ref)) |
| 461 | } |
| 462 | } |
| 463 | if len(names) == 0 { |
| 464 | names = []string{"any JSON value"} |
| 465 | } |
| 466 | desc := strings.Join(names, " or ") |
| 467 | if nullable && !strings.HasSuffix(desc, " or null") { |
| 468 | desc += " or null" |
| 469 | } |
| 470 | if len(s.Enum) > 0 { |
| 471 | values := make([]string, len(s.Enum)) |
| 472 | for i, v := range s.Enum { |
| 473 | values[i] = jsonText(v) |
| 474 | } |
| 475 | desc += ", one of " + strings.Join(values, ", ") |
| 476 | } |
| 477 | return desc |
| 478 | } |
| 479 | |
| 480 | func schemaTypeName(t string, ref *openapi3.SchemaRef) string { |
| 481 | s := ref.Value |
no test coverage detected