|
1 | 1 | import importlib |
2 | 2 | from io import StringIO |
| 3 | +import json |
3 | 4 |
|
4 | 5 | from pytest_snapshot.plugin import Snapshot |
5 | 6 |
|
@@ -203,3 +204,46 @@ def test_unknown_enum(snapshot: Snapshot) -> None: |
203 | 204 | x = NeedsenumErrorsTypeAdapter.validate_python(error) |
204 | 205 | assert x.code == error["code"] |
205 | 206 | assert x.message == error["message"] |
| 207 | + |
| 208 | + |
| 209 | +def test_unknown_enum_field_aliases(snapshot: Snapshot) -> None: |
| 210 | + validate_codegen( |
| 211 | + snapshot=snapshot, |
| 212 | + read_schema=lambda: StringIO(test_unknown_enum_schema), |
| 213 | + target_path="test_unknown_enum", |
| 214 | + client_name="foo", |
| 215 | + ) |
| 216 | + |
| 217 | + import tests.codegen.snapshot.snapshots.test_unknown_enum |
| 218 | + |
| 219 | + importlib.reload(tests.codegen.snapshot.snapshots.test_unknown_enum) |
| 220 | + from tests.codegen.snapshot.snapshots.test_unknown_enum.enumService.needsEnumObject import ( # noqa |
| 221 | + NeedsenumobjectOutputTypeAdapter, |
| 222 | + NeedsenumobjectOutput, |
| 223 | + NeedsenumobjectOutputFooOneOf_out_first, |
| 224 | + ) |
| 225 | + |
| 226 | + initial = NeedsenumobjectOutput(foo=NeedsenumobjectOutputFooOneOf_out_first(foo=5)) |
| 227 | + result = NeedsenumobjectOutputTypeAdapter.dump_json( |
| 228 | + initial, |
| 229 | + by_alias=True, |
| 230 | + ) |
| 231 | + |
| 232 | + obj = json.loads(result) |
| 233 | + |
| 234 | + # Make sure we are testing what we think we are testing |
| 235 | + assert "foo" in obj |
| 236 | + |
| 237 | + # We must not include the un-aliased field name |
| 238 | + assert "kind" not in obj["foo"] |
| 239 | + |
| 240 | + # We must include the aliased field name |
| 241 | + assert "$kind" in obj["foo"] |
| 242 | + |
| 243 | + # ... and finally that the values are what we think they should be |
| 244 | + assert obj["foo"]["$kind"] == "out_first" |
| 245 | + assert obj["foo"]["foo"] == 5 |
| 246 | + |
| 247 | + # And one more sanity check for the decoder |
| 248 | + decoded = NeedsenumobjectOutputTypeAdapter.validate_json(result) |
| 249 | + assert decoded == initial |
0 commit comments