Skip to content

Commit b0a2d06

Browse files
Adding a structured object enum fallback
1 parent f73b1d4 commit b0a2d06

File tree

4 files changed

+257
-4
lines changed

4 files changed

+257
-4
lines changed

tests/codegen/snapshot/snapshots/test_unknown_enum/enumService/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
NeedsenumInput,
1616
NeedsenumOutput,
1717
)
18+
from .needsEnumObject import (
19+
encode_NeedsenumobjectInput,
20+
NeedsenumobjectOutput,
21+
NeedsenumobjectInput,
22+
NeedsenumobjectErrors,
23+
)
1824

1925

2026
class EnumserviceService:
@@ -39,3 +45,22 @@ async def needsEnum(
3945
),
4046
timeout,
4147
)
48+
49+
async def needsEnumObject(
50+
self,
51+
input: NeedsenumobjectInput,
52+
timeout: datetime.timedelta,
53+
) -> NeedsenumobjectOutput:
54+
return await self.client.send_rpc(
55+
"enumService",
56+
"needsEnumObject",
57+
input,
58+
encode_NeedsenumobjectInput,
59+
lambda x: TypeAdapter(NeedsenumobjectOutput).validate_python(
60+
x # type: ignore[arg-type]
61+
),
62+
lambda x: TypeAdapter(NeedsenumobjectErrors).validate_python(
63+
x # type: ignore[arg-type]
64+
),
65+
timeout,
66+
)

tests/codegen/snapshot/snapshots/test_unknown_enum/enumService/needsEnum.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Literal,
1111
Optional,
1212
Mapping,
13+
NewType,
1314
NotRequired,
1415
Union,
1516
Tuple,
@@ -24,5 +25,11 @@
2425

2526
NeedsenumInput = Literal["in_first"] | Literal["in_second"]
2627
encode_NeedsenumInput: Callable[["NeedsenumInput"], Any] = lambda x: x
27-
NeedsenumOutput = Literal["out_first"] | Literal["out_second"]
28-
NeedsenumErrors = Literal["err_first"] | Literal["err_second"]
28+
NeedsenumOutputAnyOf__Unknown = NewType("NeedsenumOutputAnyOf__Unknown", object)
29+
NeedsenumOutput = (
30+
Literal["out_first"] | Literal["out_second"] | NeedsenumOutputAnyOf__Unknown
31+
)
32+
NeedsenumErrorsAnyOf__Unknown = NewType("NeedsenumErrorsAnyOf__Unknown", object)
33+
NeedsenumErrors = (
34+
Literal["err_first"] | Literal["err_second"] | NeedsenumErrorsAnyOf__Unknown
35+
)
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# ruff: noqa
2+
# Code generated by river.codegen. DO NOT EDIT.
3+
from collections.abc import AsyncIterable, AsyncIterator
4+
import datetime
5+
from typing import (
6+
Any,
7+
Callable,
8+
Dict,
9+
List,
10+
Literal,
11+
Optional,
12+
Mapping,
13+
NewType,
14+
NotRequired,
15+
Union,
16+
Tuple,
17+
TypedDict,
18+
)
19+
20+
from pydantic import BaseModel, Field, TypeAdapter
21+
from replit_river.error_schema import RiverError
22+
23+
import replit_river as river
24+
25+
26+
encode_NeedsenumobjectInputOneOf_in_first: Callable[
27+
["NeedsenumobjectInputOneOf_in_first"], Any
28+
] = lambda x: {
29+
k: v
30+
for (k, v) in (
31+
{
32+
"$kind": x.get("kind"),
33+
"value": x.get("value"),
34+
}
35+
).items()
36+
if v is not None
37+
}
38+
39+
40+
class NeedsenumobjectInputOneOf_in_first(TypedDict):
41+
kind: Literal["in_first"]
42+
value: str
43+
44+
45+
encode_NeedsenumobjectInputOneOf_in_second: Callable[
46+
["NeedsenumobjectInputOneOf_in_second"], Any
47+
] = lambda x: {
48+
k: v
49+
for (k, v) in (
50+
{
51+
"$kind": x.get("kind"),
52+
"bleep": x.get("bleep"),
53+
}
54+
).items()
55+
if v is not None
56+
}
57+
58+
59+
class NeedsenumobjectInputOneOf_in_second(TypedDict):
60+
kind: Literal["in_second"]
61+
bleep: int
62+
63+
64+
NeedsenumobjectInput = (
65+
NeedsenumobjectInputOneOf_in_first | NeedsenumobjectInputOneOf_in_second
66+
)
67+
68+
encode_NeedsenumobjectInput: Callable[["NeedsenumobjectInput"], Any] = (
69+
lambda x: encode_NeedsenumobjectInputOneOf_in_first(x)
70+
if x["kind"] == "in_first"
71+
else encode_NeedsenumobjectInputOneOf_in_second(x)
72+
)
73+
74+
75+
class NeedsenumobjectOutputFooOneOf_out_first(BaseModel):
76+
kind: Literal["out_first"] = Field(
77+
"out_first",
78+
alias="$kind", # type: ignore
79+
)
80+
81+
foo: int
82+
83+
84+
class NeedsenumobjectOutputFooOneOf_out_second(BaseModel):
85+
kind: Literal["out_second"] = Field(
86+
"out_second",
87+
alias="$kind", # type: ignore
88+
)
89+
90+
bar: int
91+
92+
93+
NeedsenumobjectOutputFooAnyOf__Unknown = NewType(
94+
"NeedsenumobjectOutputFooAnyOf__Unknown", object
95+
)
96+
NeedsenumobjectOutputFoo = (
97+
NeedsenumobjectOutputFooOneOf_out_first
98+
| NeedsenumobjectOutputFooOneOf_out_second
99+
| NeedsenumobjectOutputFooAnyOf__Unknown
100+
)
101+
102+
103+
class NeedsenumobjectOutput(BaseModel):
104+
foo: Optional[NeedsenumobjectOutputFoo] = None
105+
106+
107+
class NeedsenumobjectErrorsFooAnyOf_0(RiverError):
108+
beep: Optional[Literal["err_first"]] = None
109+
110+
111+
class NeedsenumobjectErrorsFooAnyOf_1(RiverError):
112+
borp: Optional[Literal["err_second"]] = None
113+
114+
115+
NeedsenumobjectErrorsFooAnyOf__Unknown = NewType(
116+
"NeedsenumobjectErrorsFooAnyOf__Unknown", object
117+
)
118+
NeedsenumobjectErrorsFoo = (
119+
NeedsenumobjectErrorsFooAnyOf_0
120+
| NeedsenumobjectErrorsFooAnyOf_1
121+
| NeedsenumobjectErrorsFooAnyOf__Unknown
122+
)
123+
124+
125+
class NeedsenumobjectErrors(RiverError):
126+
foo: Optional[NeedsenumobjectErrorsFoo] = None

tests/codegen/snapshot/test_enum.py

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from replit_river.codegen.client import schema_to_river_client_codegen
88

9-
test_unknown_enum_schema: str = """
9+
test_unknown_enum_schema = """
1010
{
1111
"services": {
1212
"enumService": {
@@ -49,12 +49,107 @@
4949
}
5050
]
5151
}
52+
},
53+
"needsEnumObject": {
54+
"type": "rpc",
55+
"input": {
56+
"anyOf": [
57+
{
58+
"type": "object",
59+
"properties": {
60+
"$kind": {
61+
"const": "in_first",
62+
"type": "string"
63+
},
64+
"value": {
65+
"type": "string"
66+
}
67+
},
68+
"required": ["$kind", "value"]
69+
},
70+
{
71+
"type": "object",
72+
"properties": {
73+
"$kind": {
74+
"const": "in_second",
75+
"type": "string"
76+
},
77+
"bleep": {
78+
"type": "integer"
79+
}
80+
},
81+
"required": ["$kind", "bleep"]
82+
}
83+
]
84+
},
85+
"output": {
86+
"type": "object",
87+
"properties": {
88+
"foo": {
89+
"anyOf": [
90+
{
91+
"type": "object",
92+
"properties": {
93+
"$kind": {
94+
"const": "out_first",
95+
"type": "string"
96+
},
97+
"foo": {
98+
"type": "integer"
99+
}
100+
},
101+
"required": ["$kind", "foo"]
102+
},
103+
{
104+
"type": "object",
105+
"properties": {
106+
"$kind": {
107+
"const": "out_second",
108+
"type": "string"
109+
},
110+
"bar": {
111+
"type": "integer"
112+
}
113+
},
114+
"required": ["$kind", "bar"]
115+
}
116+
]
117+
}
118+
}
119+
},
120+
"errors": {
121+
"type": "object",
122+
"properties": {
123+
"foo": {
124+
"anyOf": [
125+
{
126+
"type": "object",
127+
"properties": {
128+
"beep": {
129+
"type": "string",
130+
"const": "err_first"
131+
}
132+
}
133+
},
134+
{
135+
"type": "object",
136+
"properties": {
137+
"borp": {
138+
"type": "string",
139+
"const": "err_second"
140+
}
141+
}
142+
}
143+
]
144+
}
145+
}
146+
}
52147
}
53148
}
54149
}
55150
}
56151
}
57-
"""
152+
"""
58153

59154

60155
class UnclosableStringIO(StringIO):

0 commit comments

Comments
 (0)