Get the schema object for request/response body @param parameter: name of the parameter @param param_type: Any the type to be inferred @return: dict the properties object
(self, parameter: str, param_type: Any)
| 575 | return "string" |
| 576 | |
| 577 | def get_schema_object(self, parameter: str, param_type: Any) -> dict: |
| 578 | """ |
| 579 | Get the schema object for request/response body |
| 580 | |
| 581 | @param parameter: name of the parameter |
| 582 | @param param_type: Any the type to be inferred |
| 583 | @return: dict the properties object |
| 584 | """ |
| 585 | |
| 586 | properties: dict = { |
| 587 | "title": parameter.capitalize(), |
| 588 | } |
| 589 | |
| 590 | # Primitive scalars (int, str, bool, float, dict, list). |
| 591 | if param_type in _PRIMITIVE_TYPE_NAMES: |
| 592 | properties["type"] = _PRIMITIVE_TYPE_NAMES[param_type] |
| 593 | return properties |
| 594 | |
| 595 | # Special stdlib leaf types (datetime, date, UUID, Decimal, bytes, ...). |
| 596 | # Pydantic-free handlers can return these without crashing or rendering |
| 597 | # as a bare object (#1124). |
| 598 | if isinstance(param_type, type) and param_type in _LEAF_TYPE_SCHEMAS: |
| 599 | properties.update(_LEAF_TYPE_SCHEMAS[param_type]) |
| 600 | return properties |
| 601 | |
| 602 | # typing.Any -> any value, no constraints. |
| 603 | if param_type is Any: |
| 604 | return properties |
| 605 | |
| 606 | origin = get_origin(param_type) |
| 607 | args = get_args(param_type) |
| 608 | |
| 609 | # typing.Literal[...] -> an enum of literal values. |
| 610 | if origin is typing.Literal: |
| 611 | properties["enum"] = list(args) |
| 612 | inferred = type(args[0]) if args else str |
| 613 | properties["type"] = _PRIMITIVE_TYPE_NAMES.get(inferred, "string") |
| 614 | return properties |
| 615 | |
| 616 | # enum.Enum subclass -> enum of member values. |
| 617 | if isinstance(param_type, type) and issubclass(param_type, enum.Enum): |
| 618 | members = list(param_type) |
| 619 | properties["enum"] = [member.value for member in members] |
| 620 | if members: |
| 621 | properties["type"] = _PRIMITIVE_TYPE_NAMES.get(type(members[0].value), "string") |
| 622 | return properties |
| 623 | |
| 624 | # Sequence generics (list[X], tuple[X, ...], set[X]) -> array. |
| 625 | if origin in (list, tuple, set, frozenset): |
| 626 | properties["type"] = "array" |
| 627 | if args: |
| 628 | item_type = args[0] |
| 629 | properties["items"] = self.get_schema_object(f"{parameter}_item", item_type) |
| 630 | return properties |
| 631 | |
| 632 | # Mapping generics (dict[str, V]) -> object with typed additionalProperties. |
| 633 | if origin is dict: |
| 634 | properties["type"] = "object" |