Coverage for faststream/specification/asyncapi/message.py: 94%

54 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-17 21:20 +0000

1from collections.abc import Sequence 

2from inspect import isclass 

3from typing import TYPE_CHECKING, Any, Optional, cast, overload 

4 

5from pydantic import BaseModel, create_model 

6 

7from faststream._internal._compat import ( 

8 DEF_KEY, 

9 PYDANTIC_V2, 

10 get_model_fields, 

11 model_schema, 

12) 

13from faststream._internal.di.body import BodyBinding 

14 

15if TYPE_CHECKING: 

16 from fast_depends.core import CallModel 

17 

18 

19def parse_handler_params(call: "CallModel", prefix: str = "") -> dict[str, Any]: 

20 """Parses the handler parameters.""" 

21 model_container = getattr(call, "serializer", call) 

22 model = cast("type[BaseModel] | None", getattr(model_container, "model", None)) 

23 assert model 

24 

25 binding = BodyBinding(call) 

26 body_model = create_model( 

27 model.__name__, 

28 **{p.field_name: (p.field_type, p.default_value) for p in binding.params}, # type: ignore[call-overload] 

29 ) 

30 single = binding.single_parameter 

31 body = _get_model_schema( 

32 body_model, 

33 get_model_fields(body_model), 

34 single.field_name if single is not None else None, 

35 prefix=prefix, 

36 ) 

37 

38 if body is None: 

39 return {"title": "EmptyPayload", "type": "null"} 

40 

41 return body 

42 

43 

44@overload 

45def get_response_schema(call: None, prefix: str = "") -> None: ... 

46 

47 

48@overload 

49def get_response_schema(call: "CallModel", prefix: str = "") -> dict[str, Any]: ... 

50 

51 

52def get_response_schema( 

53 call: Optional["CallModel"], 

54 prefix: str = "", 

55) -> dict[str, Any] | None: 

56 """Get the response schema for a given call.""" 

57 return get_model_schema( 

58 getattr( 

59 call, 

60 "response_model", 

61 None, 

62 ), # NOTE: FastAPI Dependant object compatibility 

63 prefix=prefix, 

64 ) 

65 

66 

67@overload 

68def get_model_schema( 

69 call: None, 

70 prefix: str = "", 

71 exclude: Sequence[str] = (), 

72) -> None: ... 

73 

74 

75@overload 

76def get_model_schema( 

77 call: type[BaseModel], 

78 prefix: str = "", 

79 exclude: Sequence[str] = (), 

80) -> dict[str, Any]: ... 

81 

82 

83def get_model_schema( 

84 call: type[BaseModel] | None, 

85 prefix: str = "", 

86 exclude: Sequence[str] = (), 

87) -> dict[str, Any] | None: 

88 """Get the schema of a model.""" 

89 if call is None: 89 ↛ 90line 89 didn't jump to line 90 because the condition on line 89 was never true

90 return None 

91 

92 params = {k: v for k, v in get_model_fields(call).items() if k not in exclude} 

93 return _get_model_schema( 

94 call, 

95 params, 

96 next(iter(params)) if len(params) == 1 else None, 

97 prefix=prefix, 

98 exclude=exclude, 

99 ) 

100 

101 

102def _get_model_schema( 

103 call: type[BaseModel], 

104 params: dict[str, Any], 

105 single_parameter: str | None, 

106 *, 

107 prefix: str, 

108 exclude: Sequence[str] = (), 

109) -> dict[str, Any] | None: 

110 if not params: 

111 return None 

112 

113 model = None 

114 use_original_model = False 

115 name = single_parameter 

116 param = params[name] if name is not None else None 

117 if ( 

118 param is not None 

119 and param.annotation 

120 and isclass(param.annotation) 

121 and issubclass(param.annotation, BaseModel) # NOTE: 3.7-3.10 compatibility 

122 ): 

123 model = param.annotation 

124 use_original_model = True 

125 

126 if model is None: 

127 model = call 

128 

129 body: dict[str, Any] = model_schema(model) 

130 body["properties"] = body.get("properties", {}) 

131 for i in exclude: 131 ↛ 132line 131 didn't jump to line 132 because the loop on line 131 never started

132 body["properties"].pop(i, None) 

133 if required := body.get("required"): 

134 body["required"] = list(filter(lambda x: x not in exclude, required)) 

135 

136 if name is not None and param is not None and not use_original_model: 

137 param_body: dict[str, Any] = body.get("properties", {}) 

138 # Validation aliases can change the single emitted property's name. 

139 param_body = ( 

140 next(iter(param_body.values())) if len(param_body) == 1 else param_body[name] 

141 ) 

142 

143 if defs := body.get(DEF_KEY): 

144 # single argument with useless reference 

145 if param_body.get("$ref"): 

146 ref_obj: dict[str, Any] = next(iter(defs.values())) 

147 ref_obj[DEF_KEY] = { 

148 k: v for k, v in defs.items() if k != ref_obj.get("title") 

149 } 

150 return ref_obj 

151 param_body[DEF_KEY] = defs 

152 

153 original_title = param.title if PYDANTIC_V2 else param.field_info.title 

154 

155 if original_title: 

156 use_original_model = True 

157 param_body["title"] = original_title 

158 else: 

159 param_body["title"] = name 

160 

161 body = param_body 

162 

163 if not use_original_model: 

164 body["title"] = f"{prefix}:Payload" 

165 

166 return body