Skip to content

Commit 3f8f519

Browse files
Copilotl0lawrence
andauthored
test(http-client-python): cover json overload when typeddict is disabled
Co-authored-by: l0lawrence <100643745+l0lawrence@users.noreply.github.com>
1 parent fb6b9cb commit 3f8f519

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

packages/http-client-python/generator/pygen/preprocess/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ def add_overloads_for_body_param(yaml_data: dict[str, Any]) -> None:
9494
continue
9595
if body_type.get("type") == "model" and body_type.get("base") == "json":
9696
yaml_data["overloads"].append(add_overload(yaml_data, body_type, for_flatten_params=True))
97-
# Skip single-body JSON overload; the TypedDict overload replaces it
97+
# Use the flattened JSON overload and skip the single-body JSON overload.
98+
# When TypedDict generation is disabled, this JSON overload remains.
9899
continue
99100
yaml_data["overloads"].append(add_overload(yaml_data, body_type))
100101
content_type_param = next(p for p in yaml_data["parameters"] if p["wireName"].lower() == "content-type")

packages/http-client-python/tests/unit/test_typeddict_overloads.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,32 @@ def test_dpg_mode_can_disable_typeddict_autogeneration():
119119
assert not any(t for t in code_model["types"] if t.get("base") == "typeddict")
120120

121121

122+
def test_spread_body_opt_out_keeps_json_overload():
123+
"""Spread bodies keep the flattened JSON overload when TypedDict generation is disabled."""
124+
plugin = _plugin("dpg", **{"generate-typeddict": False})
125+
spread_body = _json_spread_body_parameter("CreateRequest", "Contoso.Widget")
126+
yaml_data = {
127+
"name": "create",
128+
"bodyParameter": spread_body,
129+
"parameters": [_content_type_param()],
130+
"overloads": [],
131+
"responses": [],
132+
"exceptions": [],
133+
}
134+
code_model = {"types": [spread_body["type"]]}
135+
136+
plugin.add_body_param_type(code_model, spread_body)
137+
add_overloads_for_body_param(yaml_data)
138+
139+
assert spread_body["type"]["type"] == "combined"
140+
assert spread_body["type"]["types"][0]["base"] == "json"
141+
assert spread_body["type"]["types"][1] == {"type": "binary"}
142+
assert len(yaml_data["overloads"]) == 2
143+
json_overload = next(o for o in yaml_data["overloads"] if o["bodyParameter"]["type"].get("base") == "json")
144+
assert json_overload["bodyParameter"]["flattened"] is True
145+
assert not any(t for t in code_model["types"] if t.get("base") == "typeddict")
146+
147+
122148
def _dpg_body_parameter(name: str, cross_lang_id: str) -> dict:
123149
"""A JSON dpg-model body parameter for the given model name and cross-language id."""
124150
model_type = {

0 commit comments

Comments
 (0)