Skip to content

Commit

Permalink
fix arg encoding for component functions #30 (#30)
Browse files Browse the repository at this point in the history
need to call jsonToConvex in the runUdf syscall, otherwise values like Number.POSITIVE_INFINITY don't get passed through correctly.

see regression test.
  • Loading branch information
ldanilek authored Nov 10, 2024
1 parent 35e09c2 commit 2f4e925
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
6 changes: 6 additions & 0 deletions convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ export declare const components: {
{},
any
>;
mutationWithNumberArg: FunctionReference<
"mutation",
"internal",
{ a: number },
any
>;
schedule: FunctionReference<
"mutation",
"internal",
Expand Down
30 changes: 30 additions & 0 deletions convex/argumentsValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { expect, test } from "vitest";
import { convexTest } from "../index";
import { api } from "./_generated/api";
import schema from "./schema";
import counterSchema from "../counter/component/schema";

const counterModules = import.meta.glob("../counter/component/**/*.ts");


test("query arguments validation", async () => {
const t = convexTest(schema);
Expand Down Expand Up @@ -43,3 +47,29 @@ test("optional fields", async () => {
);
expect(result).toEqual("ok");
});

function testWithCounter() {
const t = convexTest(schema);
t.registerComponent(
"counter",
counterSchema,
counterModules
);
return t;
}

test("component mutation arguments validation", async () => {
const t = testWithCounter();
expect(
await t.mutation(api.argumentsValidation.componentMutationWithNumberArg, { a: 42 })
).toEqual(42);
await expect(
t.mutation(api.argumentsValidation.componentMutationWithNumberArg, {
a: "bad" as any,
}),
).rejects.toThrowError(/Validator error/);
expect(await t.mutation(api.argumentsValidation.componentMutationWithNumberArg, {
a: Number.POSITIVE_INFINITY,
}),
).toEqual(Number.POSITIVE_INFINITY);
});
11 changes: 11 additions & 0 deletions convex/argumentsValidation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { v } from "convex/values";
import { action, mutation, query } from "./_generated/server";
import { components } from "./_generated/api";

export const queryWithArgs = query({
args: {
Expand Down Expand Up @@ -42,3 +43,13 @@ export const queryWithOptionalArgs = query({
return "ok";
},
});

export const componentMutationWithNumberArg = mutation({
args: { a: v.any() },
handler: (ctx, args) => {
const result = ctx.runMutation(components.counter.public.mutationWithNumberArg, {
a: args.a,
});
return result;
},
});
6 changes: 6 additions & 0 deletions counter/component/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ export type Mounts = {
>;
count: FunctionReference<"query", "public", { name: string }, number>;
mutationWithNestedQuery: FunctionReference<"mutation", "public", {}, any>;
mutationWithNumberArg: FunctionReference<
"mutation",
"public",
{ a: number },
any
>;
schedule: FunctionReference<"mutation", "public", { name: string }, any>;
};
};
Expand Down
7 changes: 7 additions & 0 deletions counter/component/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,10 @@ export const mutationWithNestedQuery = mutation({
return doc!.value;
},
});

export const mutationWithNumberArg = mutation({
args: { a: v.number() },
handler: async (_ctx, args) => {
return args.a;
},
});
3 changes: 2 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1093,8 +1093,9 @@ function asyncSyscallImpl() {
name,
reference,
functionHandle,
args: udfArgs,
args: udfArgsJson,
} = args;
const udfArgs = jsonToConvex(udfArgsJson);
const functionPath = await getFunctionPathFromAddress({
name,
reference,
Expand Down

0 comments on commit 2f4e925

Please sign in to comment.