get_model_schema(
call: None,
prefix: str = "",
exclude: Sequence[str] = (),
) -> None
get_model_schema(
call: type[BaseModel],
prefix: str = "",
exclude: Sequence[str] = (),
) -> dict[str, Any]
get_model_schema(
call: type[BaseModel] | None,
prefix: str = "",
exclude: Sequence[str] = (),
) -> dict[str, Any] | None
Get the schema of a model.
Source code in faststream/specification/asyncapi/message.py
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 | def get_model_schema(
call: type[BaseModel] | None,
prefix: str = "",
exclude: Sequence[str] = (),
) -> dict[str, Any] | None:
"""Get the schema of a model."""
if call is None:
return None
params = {k: v for k, v in get_model_fields(call).items() if k not in exclude}
params_number = len(params)
if params_number == 0:
return None
model = None
use_original_model = False
name, param = next(iter(params.items()))
if (
params_number == 1
and param.annotation
and isclass(param.annotation)
and issubclass(param.annotation, BaseModel) # NOTE: 3.7-3.10 compatibility
):
model = param.annotation
use_original_model = True
if model is None:
model = call
body: dict[str, Any] = model_schema(model)
body["properties"] = body.get("properties", {})
for i in exclude:
body["properties"].pop(i, None)
if required := body.get("required"):
body["required"] = list(filter(lambda x: x not in exclude, required))
if params_number == 1 and not use_original_model:
param_body: dict[str, Any] = body.get("properties", {})
param_body = param_body[name]
if defs := body.get(DEF_KEY):
# single argument with useless reference
if param_body.get("$ref"):
ref_obj: dict[str, Any] = next(iter(defs.values()))
ref_obj[DEF_KEY] = {
k: v for k, v in defs.items() if k != ref_obj.get("title")
}
return ref_obj
param_body[DEF_KEY] = defs
original_title = param.title if PYDANTIC_V2 else param.field_info.title
if original_title:
use_original_model = True
param_body["title"] = original_title
else:
param_body["title"] = name
body = param_body
if not use_original_model:
body["title"] = f"{prefix}:Payload"
return body
|