Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions python/copilot/generated/session_events.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions python/test_event_forward_compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ def test_data_shim_preserves_raw_mapping_values(self):
constructed = Data(arguments={"tool_call_id": "call-1"})
assert constructed.to_dict() == {"arguments": {"tool_call_id": "call-1"}}

def test_data_shim_preserves_abbreviation_json_keys_on_round_trip(self):
"""Data.from_dict(x).to_dict() should preserve JSON keys with abbreviations.

Regression test for github/copilot-sdk#1138: keys like userURL, sessionID,
and OAuthToken were rewritten on round-trip because _compat_to_json_key could
not reconstruct the original camelCase abbreviation casing.
"""
for key in ["userURL", "sessionID", "XMLPayload", "serverIP", "OAuthToken"]:
incoming = {key: 42}
assert Data.from_dict(incoming).to_dict() == incoming

def test_missing_optional_fields_remain_none_after_parsing(self):
"""Generated event models should leave missing optional fields as None.

Expand Down
15 changes: 11 additions & 4 deletions scripts/codegen/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2673,19 +2673,26 @@ export function generatePythonSessionEventsCode(schema: JSONSchema7): string {
out.push(``);
out.push(` def __init__(self, **kwargs: Any):`);
out.push(` self._values = {key: _compat_from_json_value(value) for key, value in kwargs.items()}`);
out.push(` self._json_keys: dict[str, str] = {}`);
out.push(` for key, value in self._values.items():`);
out.push(` setattr(self, key, value)`);
out.push(``);
out.push(` @staticmethod`);
out.push(` def from_dict(obj: Any) -> "Data":`);
out.push(` assert isinstance(obj, dict)`);
out.push(
` return Data(**{_compat_to_python_key(key): _compat_from_json_value(value) for key, value in obj.items()})`
);
out.push(` data = Data()`);
out.push(` data._values = {}`);
out.push(` data._json_keys = {}`);
out.push(` for key, value in obj.items():`);
out.push(` py_key = _compat_to_python_key(key)`);
out.push(` data._values[py_key] = _compat_from_json_value(value)`);
out.push(` data._json_keys[py_key] = key`);
out.push(` setattr(data, py_key, data._values[py_key])`);
out.push(` return data`);
out.push(``);
out.push(` def to_dict(self) -> dict:`);
out.push(
` return {_compat_to_json_key(key): _compat_to_json_value(value) for key, value in self._values.items() if value is not None}`
` return {(self._json_keys.get(key) or _compat_to_json_key(key)): _compat_to_json_value(value) for key, value in self._values.items() if value is not None}`
);
out.push(``);
out.push(``);
Expand Down