Skip to content

Commit 4115b43

Browse files
fix: bad typenames and internal policy (#979)
<!-- Pull requests are squashed and merged using: - their title as the commit message - their description as the commit body Having a good title and description is important for the users to get readable changelog. --> <!-- 1. Explain WHAT the change is about --> - Fix bad duplicated typenames for duplicate materializers and predefined internal policy function. <!-- 3. Explain HOW users should update their code --> #### Migration notes --- - [x] The change comes with new or modified tests - [ ] Hard-to-understand functions have explanatory comments - [ ] End-user documentation is updated to reflect the change <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Summary by CodeRabbit - **New Features** - Enhanced access control with stricter authorization checks and refined data exposure across different contexts. - Introduced new type aliases for improved clarity in method return types. - Added a new internal policy for access control. - **Refactor** - Streamlined naming for inputs and outputs to ensure consistent labeling. - Adjusted evaluation logic to provide clearer security outcomes. - **Tests** - Expanded automated test scenarios to verify deduplication and authorization behavior. - Added new test cases to validate internal policy access control. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 55cece9 commit 4115b43

File tree

20 files changed

+230
-156
lines changed

20 files changed

+230
-156
lines changed

src/typegate/src/runtimes/deno/deno.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,8 @@ const predefinedFuncs: Record<
4545
pass: () => () => "PASS" as PolicyResolverOutput,
4646
internal_policy:
4747
() =>
48-
({ _: { context } }) =>
49-
context.provider === "internal"
50-
? "ALLOW"
51-
: ("PASS" as PolicyResolverOutput),
48+
({ _: { context } }): PolicyResolverOutput =>
49+
context.provider === "internal" ? "PASS" : "DENY",
5250
context_check: ({ key, value }) => {
5351
let check: (value: any) => boolean;
5452
switch (value.type) {

src/typegraph/core/src/utils/postprocess/naming.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,8 @@ fn gen_name(cx: &VisitContext, acc: &mut VisitCollector, id: u32, ty_name: &str)
351351
// we don't include optional and list nodes in
352352
// generated names (useless but also, they might be placeholders)
353353
Edge::OptionalItem | Edge::ArrayItem => continue,
354-
Edge::FunctionInput => join_if_ok!(last_name.to_string(), "input"),
355-
Edge::FunctionOutput => join_if_ok!(last_name.to_string(), "output"),
354+
Edge::FunctionInput => format!("{}_input", last_name),
355+
Edge::FunctionOutput => format!("{}_output", last_name),
356356
Edge::ObjectProp(key) => {
357357
join_if_ok!(format!("{last_name}_{key}"), ty_name)
358358
}

tests/dedup/dedup_test.ts

+50-17
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,64 @@
44
import { Meta } from "test-utils/mod.ts";
55
import { Typegraph } from "@metatype/typegate/typegraph/types.ts";
66
import { assert, assertEquals } from "@std/assert";
7+
import { FunctionNode } from "@metatype/typegate/typegraph/type_node.ts";
78

8-
Meta.test({
9-
name: "dedup test",
10-
}, async (_t) => {
9+
Meta.test("dedup test", async ({ t }) => {
1110
const { stdout } = await Meta.cli(
1211
"serialize",
1312
"--pretty",
1413
"-f",
1514
"dedup/tg.ts",
1615
);
1716
const tg: Typegraph[] = JSON.parse(stdout);
17+
1818
console.log(tg);
19-
const ints = [];
20-
const strs = [];
21-
for (const ty of tg[0].types) {
22-
if (ty.type == "integer") {
23-
ints.push(ty);
24-
}
25-
if (ty.type == "string") {
26-
strs.push(ty);
19+
20+
const [objTg, matTg] = tg;
21+
22+
await t.step("object dedup", () => {
23+
const ints = [];
24+
const strs = [];
25+
for (const ty of objTg.types) {
26+
if (ty.type == "integer") {
27+
ints.push(ty);
28+
}
29+
if (ty.type == "string") {
30+
strs.push(ty);
31+
}
2732
}
28-
}
29-
assertEquals(ints.length, 2);
30-
assert(ints.some((ty) => ty.title == "namedInt"));
31-
assertEquals(strs.length, 3);
32-
assert(strs.some((ty) => ty.format == "uuid"));
33-
assertEquals(strs.filter((ty) => ty.format == "date-time").length, 1);
33+
assertEquals(ints.length, 2);
34+
assert(ints.some((ty) => ty.title == "namedInt"));
35+
assertEquals(strs.length, 3);
36+
assert(strs.some((ty) => ty.format == "uuid"));
37+
assertEquals(strs.filter((ty) => ty.format == "date-time").length, 1);
38+
});
39+
40+
await t.step("materializer dedup", () => {
41+
const fnInput = matTg.types.findIndex(
42+
({ title }) => title === "root_f1_fn_input",
43+
);
44+
const fnOutput = matTg.types.findIndex(
45+
({ title }) => title === "root_f1_fn_output",
46+
);
47+
48+
const f1 = matTg.types.find(
49+
({ title }) => title === "root_f1_fn",
50+
) as FunctionNode;
51+
const f2 = matTg.types.find(
52+
({ title }) => title === "root_f2_fn",
53+
) as FunctionNode;
54+
55+
assertEquals(f1.input, fnInput);
56+
assertEquals(f2.input, fnInput);
57+
assertEquals(f1.output, fnOutput);
58+
assertEquals(f2.output, fnOutput);
59+
});
60+
61+
await t.step("unique type names", () => {
62+
const typeNames = objTg.types.concat(matTg.types).map(({ title }) => title);
63+
const nameSet = new Set(typeNames);
64+
65+
assertEquals(typeNames.length, nameSet.size);
66+
});
3467
});

tests/dedup/tg.ts

+25-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
import { Policy, t, typegraph } from "@typegraph/sdk";
55
import { RandomRuntime } from "@typegraph/sdk/runtimes/random";
6+
import { DenoRuntime } from "@typegraph/sdk/runtimes/deno";
67

7-
export const tg = await typegraph("dedup", (g: any) => {
8+
await typegraph("object-dedup", (g: any) => {
89
const rand = new RandomRuntime({});
910

1011
const obj1 = t.struct({
@@ -26,8 +27,28 @@ export const tg = await typegraph("dedup", (g: any) => {
2627
dateInj2: t.datetime().inject("now"),
2728
});
2829

30+
g.expose(
31+
{
32+
test: rand.gen(obj1),
33+
test2: rand.gen(obj2),
34+
},
35+
Policy.public(),
36+
);
37+
});
38+
39+
await typegraph("materializer-dedup", (g: any) => {
40+
const deno = new DenoRuntime();
41+
42+
const f = deno.func(
43+
t.struct({ i: t.integer() }),
44+
t.struct({ o: t.string() }),
45+
{
46+
code: ({ i }) => ({ o: i.toString() }),
47+
},
48+
);
49+
2950
g.expose({
30-
test: rand.gen(obj1),
31-
test2: rand.gen(obj2),
32-
}, Policy.public());
51+
f1: f.withPolicy(Policy.public()),
52+
f2: f.withPolicy(Policy.internal()),
53+
});
3354
});

tests/e2e/typegraph/__snapshots__/typegraph_test.ts.snap

+10-10
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ snapshot[`typegraphs creation 2`] = `
302302
},
303303
{
304304
"type": "object",
305-
"title": "struct_aada4",
305+
"title": "root_add_fn_input",
306306
"properties": {
307307
"first": 3,
308308
"second": 3
@@ -316,7 +316,7 @@ snapshot[`typegraphs creation 2`] = `
316316
},
317317
{
318318
"type": "float",
319-
"title": "float_9d392"
319+
"title": "float_41db1"
320320
},
321321
{
322322
"type": "function",
@@ -454,7 +454,7 @@ snapshot[`typegraphs creation 3`] = `
454454
},
455455
{
456456
"type": "object",
457-
"title": "struct_62ec9",
457+
"title": "root_one_fn_input",
458458
"properties": {
459459
"a": 3,
460460
"b": 4
@@ -468,11 +468,11 @@ snapshot[`typegraphs creation 3`] = `
468468
},
469469
{
470470
"type": "integer",
471-
"title": "struct_62ec9_a_integer"
471+
"title": "root_one_fn_input_a_integer"
472472
},
473473
{
474474
"type": "integer",
475-
"title": "struct_62ec9_b_integer",
475+
"title": "root_one_fn_input_b_integer",
476476
"minimum": 12
477477
},
478478
{
@@ -971,7 +971,7 @@ snapshot[`typegraphs creation 5`] = `
971971
},
972972
{
973973
"type": "object",
974-
"title": "struct_aada4",
974+
"title": "root_add_fn_input",
975975
"properties": {
976976
"first": 3,
977977
"second": 3
@@ -985,7 +985,7 @@ snapshot[`typegraphs creation 5`] = `
985985
},
986986
{
987987
"type": "float",
988-
"title": "float_9d392"
988+
"title": "float_41db1"
989989
},
990990
{
991991
"type": "function",
@@ -1123,7 +1123,7 @@ snapshot[`typegraphs creation 6`] = `
11231123
},
11241124
{
11251125
"type": "object",
1126-
"title": "struct_62ec9",
1126+
"title": "root_one_fn_input",
11271127
"properties": {
11281128
"a": 3,
11291129
"b": 4
@@ -1137,11 +1137,11 @@ snapshot[`typegraphs creation 6`] = `
11371137
},
11381138
{
11391139
"type": "integer",
1140-
"title": "struct_62ec9_a_integer"
1140+
"title": "root_one_fn_input_a_integer"
11411141
},
11421142
{
11431143
"type": "integer",
1144-
"title": "struct_62ec9_b_integer",
1144+
"title": "root_one_fn_input_b_integer",
11451145
"minimum": 12
11461146
},
11471147
{

tests/internal/py/logic_types.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ def new(dt_class: Any, val: Any):
8787

8888

8989
@dataclass
90-
class StructC3ed7(Struct):
90+
class RootSumFnInput(Struct):
9191
first: float
9292
second: float
9393

9494

95-
FORWARD_REFS["StructC3ed7"] = StructC3ed7
95+
FORWARD_REFS["RootSumFnInput"] = RootSumFnInput
9696

9797

98-
TypeFloatB5e57 = float
98+
TypeFloatD0c49 = float
9999

100100

101101
def __repr(value: Any):
@@ -104,10 +104,10 @@ def __repr(value: Any):
104104
return value
105105

106106

107-
def typed_remote_sum(user_fn: Callable[[StructC3ed7, Ctx], TypeFloatB5e57]):
107+
def typed_remote_sum(user_fn: Callable[[RootSumFnInput, Ctx], TypeFloatD0c49]):
108108
def exported_wrapper(raw_inp, ctx):
109-
inp: StructC3ed7 = Struct.new(StructC3ed7, raw_inp)
110-
out: TypeFloatB5e57 = user_fn(inp, ctx)
109+
inp: RootSumFnInput = Struct.new(RootSumFnInput, raw_inp)
110+
out: TypeFloatD0c49 = user_fn(inp, ctx)
111111
if isinstance(out, list):
112112
return [__repr(v) for v in out]
113113
return __repr(out)

0 commit comments

Comments
 (0)