Skip to content

Commit 4d1e860

Browse files
fix(gate): struct typecheck codegen (#983)
<!-- 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 --> - Fixup for #980 <!-- 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 - **New Features** - Enhanced object validation now accepts additional properties when allowed, offering more flexible data processing. - Introduced a new REST endpoint that returns structured, stringified data based on provided input. - **Tests** - Added a test case to verify the functionality of the new REST endpoint and ensure robust input validation. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent d8fe98d commit 4d1e860

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

src/typegate/src/engine/typecheck/code_generator.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,12 @@ export class CodeGenerator {
229229
);
230230
}
231231

232-
this.validation(
233-
"keys.size > 0",
234-
`\`unexpected fields: \${[...keys].join(', ')}\``,
235-
);
232+
if (!typeNode.additionalProps) {
233+
this.validation(
234+
"keys.size > 0",
235+
`\`unexpected fields: \${[...keys].join(', ')}\``,
236+
);
237+
}
236238
this.line("}");
237239
return Object.values(typeNode.properties);
238240
}

tests/typecheck/input_validator_test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0.
22
// SPDX-License-Identifier: MPL-2.0
33

4-
import { gql, Meta } from "../utils/mod.ts";
4+
import { gql, Meta, rest } from "../utils/mod.ts";
55

66
import { Type } from "@metatype/typegate/typegraph/type_node.ts";
77
import { InputValidationCompiler } from "@metatype/typegate/engine/typecheck/input.ts";
@@ -252,4 +252,13 @@ Meta.test("input validator compiler", async (t) => {
252252
})
253253
.on(e);
254254
});
255+
256+
await t.should("pass for any struct (rest)", async () => {
257+
await rest
258+
.get('stringifyStruct?params={"key":"value"}')
259+
.expectBody((body) => {
260+
assert(typeof body.stringifyStruct === "string");
261+
})
262+
.on(e);
263+
});
255264
});

tests/typecheck/typecheck.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,22 @@ def typecheck(g: Graph):
107107
empty = t.struct({}).rename("WillNotHaveAnyEffectLikeOtherScalars")
108108

109109
stringify_struct = deno.func(
110-
t.struct({"params": t.struct({}, additional_props=True, name="params")}),
110+
t.struct(
111+
{"params": t.struct({}, additional_props=True, name="params")},
112+
name="struct_params",
113+
),
111114
t.string(),
112115
code="({params}) => JSON.stringify(params)",
113116
)
114117

118+
g.rest(
119+
"""
120+
query stringifyStruct($params: struct_params!) {
121+
stringifyStruct(params: $params)
122+
}
123+
"""
124+
)
125+
115126
g.expose(
116127
my_policy,
117128
createUser=create_user,

0 commit comments

Comments
 (0)