Skip to content

Commit cd388a6

Browse files
fix(functions-tools): forward needsJsonData when wrapping composite type in opt
1 parent 9c37327 commit cd388a6

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

packages/functions-tools/src/converters/zod-to-rust.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const schemaToRustType = ({
103103
const inner = schemaToRustType({schema: schema.inner, structName});
104104
const fieldType = `Option<${inner.fieldType}>`;
105105
return inner.kind === 'composite'
106-
? composite({fieldType, structs: inner.structs})
106+
? composite({fieldType, structs: inner.structs, needsJsonData: inner.needsJsonData})
107107
: primitive({fieldType});
108108
}
109109

packages/functions-tools/src/tests/converters/zod-to-rust.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ describe('object with enum field', () => {
8787
'#[derive(CandidType, Serialize, Deserialize, Clone, JsonData)]\npub struct MyFunctionArgs {\n pub status: MyFunctionArgsStatus,\n}'
8888
].join('\n\n')
8989
);
90+
91+
rust(
92+
'myFunction',
93+
z.object({role: z.enum(['admin', 'user']).optional()}),
94+
[
95+
'#[derive(CandidType, Serialize, Deserialize, Clone)]\npub enum MyFunctionArgsRole {\n #[serde(rename = "admin")]\n Admin,\n #[serde(rename = "user")]\n User,\n}',
96+
'#[derive(CandidType, Serialize, Deserialize, Clone, JsonData)]\npub struct MyFunctionArgs {\n pub role: Option<MyFunctionArgsRole>,\n}'
97+
].join('\n\n')
98+
);
9099
});
91100

92101
// ─── Arrays ───────────────────────────────────────────────────────────────────

0 commit comments

Comments
 (0)