Commit b9d3962
Fix Python from_dict() round-trip for optional fields with schema defaults (#1313)
* Fix Python from_dict() round-trip for optional fields with schema defaults
Fixes #1139, #1140, #1141.
The Python codegen was embedding JSON-Schema `default` values into
`obj.get(key, default)` for optional fields. But the generated
dataclass field is always `T | None = None` and `to_dict()` omits
the field when `None`, so `from_dict(to_dict(x))` silently mutated
unset fields into the schema default:
- `SessionTaskCompleteData.summary`: `None` -> `""`
- `PermissionPromptRequest.action`: `None` -> `MemoryAction.STORE`
- `PermissionRequest.action`: `None` -> `MemoryAction.STORE`
Drop `defaultLiteral` from both `emitPyClass` and
`emitPyFlatDiscriminatedUnion` so `from_dict()` always uses
`obj.get(key)` (matching the dataclass default). Regenerate
`session_events.py`. Flip the two codegen-level test assertions that
previously locked in the buggy output and add negative assertions.
Replace `test_schema_defaults_are_applied_for_missing_optional_fields`
(which asserted the bug as expected behavior) with regression tests
covering missing-key parsing, explicit-null parsing, and full
`from_dict(to_dict(x))` round-trips for all three affected classes.
Other languages (Go, .NET, Rust, TypeScript) are unaffected; their
generators never read `propSchema.default` for deserialization
fallbacks.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Fix prettier formatting in python-codegen.test.ts
The CI prettier check failed on test/python-codegen.test.ts after the assertion update. Apply prettier --write to bring the file back into compliance.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Regenerate Python session events
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent 971ef11 commit b9d3962
4 files changed
Lines changed: 55 additions & 39 deletions
File tree
- nodejs/test
- python
- copilot/generated
- scripts/codegen
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
| 77 | + | |
81 | 78 | | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
| |||
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
| 21 | + | |
20 | 22 | | |
| 23 | + | |
21 | 24 | | |
22 | 25 | | |
23 | 26 | | |
| |||
137 | 140 | | |
138 | 141 | | |
139 | 142 | | |
140 | | - | |
141 | | - | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
142 | 153 | | |
143 | | - | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
144 | 160 | | |
| 161 | + | |
145 | 162 | | |
146 | | - | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
918 | 918 | | |
919 | 919 | | |
920 | 920 | | |
921 | | - | |
922 | | - | |
923 | | - | |
924 | | - | |
925 | | - | |
926 | | - | |
927 | | - | |
928 | | - | |
929 | | - | |
930 | | - | |
931 | | - | |
932 | | - | |
933 | | - | |
934 | | - | |
935 | | - | |
936 | | - | |
937 | 921 | | |
938 | 922 | | |
939 | 923 | | |
| |||
1492 | 1476 | | |
1493 | 1477 | | |
1494 | 1478 | | |
1495 | | - | |
1496 | | - | |
1497 | | - | |
1498 | 1479 | | |
1499 | 1480 | | |
1500 | 1481 | | |
| |||
1536 | 1517 | | |
1537 | 1518 | | |
1538 | 1519 | | |
1539 | | - | |
1540 | | - | |
1541 | | - | |
| 1520 | + | |
1542 | 1521 | | |
1543 | 1522 | | |
1544 | 1523 | | |
| |||
1655 | 1634 | | |
1656 | 1635 | | |
1657 | 1636 | | |
1658 | | - | |
1659 | | - | |
1660 | | - | |
1661 | 1637 | | |
1662 | 1638 | | |
1663 | 1639 | | |
| |||
1683 | 1659 | | |
1684 | 1660 | | |
1685 | 1661 | | |
1686 | | - | |
1687 | | - | |
1688 | | - | |
| 1662 | + | |
1689 | 1663 | | |
1690 | 1664 | | |
1691 | 1665 | | |
| |||
0 commit comments