History

fix(google-discovery): JSON-safe Option encoding for persisted bindings (#706)

* fix(google-discovery): JSON-safe Option encoding for persisted bindings Schema.Option in Effect v4 is a self-declaration (Type == Encoded == Option), so encoded values still hold real Option instances. When the binding column (JSON-mode text) gets JSON.stringify'd, Option.toJSON emits {_id, _tag, value} -- which decode then rejects on read with "Expected Option, got {_id:Option,_tag:Some,value:...}", failing tools.list with a 500. Replace Schema.Option(...) with Schema.OptionFromOptional(...) -- the v4 equivalent of v3's optionalWith({ as: "Option" }) -- so encoded form is a JSON-safe absent/present field while the runtime type stays Option<...>. Matches the pattern already used by openapi. Existing rows persisted with the broken shape need to be cleared (delete + re-add the source, or DELETE FROM google_discovery_binding WHERE source_id = '...') so bindings re-derive with the corrected encoding. * test(google-discovery): add Effect-native repro for Option JSON encoding bug Reproduces the broken vs fixed encoding through Effect's own JSON I/O pipeline (Schema.encodeEffect + Schema.UnknownFromJsonString + platform-node FileSystem) so the bug class is captured without any JSON.parse/stringify on our side. * test(google-discovery): use Exit.isFailure instead of inspecting _tag * chore: oxfmt graphql plugin test * fmt ---------

Saatvik Arya committed 6 months ago

Commit 745a2ba1 parent fb2057b5 files · +134 −21Browse files

Unified · Split

5 files
  1. bun.lock +1 −0
  2. packages/core/sdk/src/executor.test.ts +16 −14
  3. packages/plugins/google-discovery/package.json +1 −0
  4. packages/plugins/google-discovery/src/sdk/option-json-repro.test.ts +109 −0
  5. packages/plugins/google-discovery/src/sdk/types.ts +7 −7
bun.lock+1−0
@@ -585,6 +585,7 @@ · 584 unmodified lines
585585 },
586586 "devDependencies": {
587587 "@effect/atom-react": "catalog:",
588+ "@effect/platform-node": "catalog:",
588589 "@effect/vitest": "catalog:",
589590 "@executor-js/react": "workspace:*",
590591 "@types/node": "catalog:",
packages/core/sdk/src/executor.test.ts+16−14
@@ -1427,22 +1427,24 @@ describe("tenant isolation (SDK)", () => { · 1426 unmodified lines
14271427 }),
14281428 );
14291429
1430− it.effect("secrets.list surfaces provider-enumerated entries; status still gates on routed rows", () =>
1431− Effect.gen(function* () {
1432− const executor = yield* createExecutor(
1433− makeTestConfig({ plugins: [providerOnlySecretPlugin()] as const }),
1434− );
1430+ it.effect(
1431+ "secrets.list surfaces provider-enumerated entries; status still gates on routed rows",
1432+ () =>
1433+ Effect.gen(function* () {
1434+ const executor = yield* createExecutor(
1435+ makeTestConfig({ plugins: [providerOnlySecretPlugin()] as const }),
1436+ );
14351437
1436− const refs = yield* executor.secrets.list();
1437− const status = yield* executor.secrets.status("provider-token");
1438− const value = yield* executor.secrets.get("provider-token");
1438+ const refs = yield* executor.secrets.list();
1439+ const status = yield* executor.secrets.status("provider-token");
1440+ const value = yield* executor.secrets.get("provider-token");
14391441
1440− const entry = refs.find((ref) => ref.id === "provider-token");
1441− expect(entry?.provider).toBe("provider-only");
1442− expect(entry?.name).toBe("Provider token");
1443− expect(status).toBe("missing");
1444− expect(value).toBe("provider-value");
1445− }),
1442+ const entry = refs.find((ref) => ref.id === "provider-token");
1443+ expect(entry?.provider).toBe("provider-only");
1444+ expect(entry?.name).toBe("Provider token");
1445+ expect(status).toBe("missing");
1446+ expect(value).toBe("provider-value");
1447+ }),
14461448 );
14471449
14481450 it.effect("secrets.get short-circuits provider fallback in registration order", () =>
packages/plugins/google-discovery/package.json+1−0
@@ -60,6 +60,7 @@ · 59 unmodified lines
6060 },
6161 "devDependencies": {
6262 "@effect/atom-react": "catalog:",
63+ "@effect/platform-node": "catalog:",
6364 "@effect/vitest": "catalog:",
6465 "@executor-js/react": "workspace:*",
6566 "@types/node": "catalog:",
packages/plugins/google-discovery/src/sdk/option-json-repro.test.ts+109−0
@@ -0,0 +1,109 @@
1+// Reproduces the PR 706 bug class using Effect-native primitives only —
2+// no JSON.parse, no JSON.stringify, no node:fs on our side. We split the
3+// JSON boundary into two Effect schema steps:
4+//
5+// 1. Schema.encodeEffect(Inner)(value) → encoded JS shape
6+// 2. Schema.encodeEffect(UnknownFromJsonString) → JSON string
7+// 3. fs.writeFileString → fs.readFileString
8+// 4. Schema.decodeUnknownEffect(UnknownFromJsonString) → unknown
9+// 5. Schema.decodeUnknownEffect(Inner) → final value
10+//
11+// Even though every step runs through Effect, Schema.Option(X) still
12+// breaks because its Encoded type IS Option<X> — not a JSON value. So
13+// step 2's JSON-stringify (driven by Effect, not us) flattens the Option
14+// to {_id,_tag,value}, and step 5 rejects the shape.
15+//
16+// Run: vitest run packages/plugins/google-discovery/src/sdk/option-json-repro.test.ts
17+
18+import { describe, expect, it } from "@effect/vitest";
19+import * as NodeFileSystem from "@effect/platform-node/NodeFileSystem";
20+import { Effect, Exit, FileSystem, Option, Schema } from "effect";
21+
22+const Broken = Schema.Struct({ description: Schema.Option(Schema.String) });
23+const Fixed = Schema.Struct({ description: Schema.OptionFromOptional(Schema.String) });
24+
25+const broken = { description: Option.some("hello") };
26+const fixed = { description: Option.some("hello") };
27+
28+const withTmpFile = <A, E>(fn: (path: string) => Effect.Effect<A, E, FileSystem.FileSystem>) =>
29+ Effect.gen(function* () {
30+ const fs = yield* FileSystem.FileSystem;
31+ const dir = yield* fs.makeTempDirectoryScoped({ prefix: "option-repro-" });
32+ return yield* fn(`${dir}/binding.json`);
33+ }).pipe(Effect.scoped, Effect.provide(NodeFileSystem.layer));
34+
35+describe("Schema.Option round-trips through Effect-native JSON I/O", () => {
36+ it.effect("BREAKS: every step driven by Effect, still loses the Option shape", () =>
37+ withTmpFile((path) =>
38+ Effect.gen(function* () {
39+ const fs = yield* FileSystem.FileSystem;
40+
41+ // Step 1: schema encode → encoded JS shape. For Schema.Option,
42+ // the encoded `description` is still an Option instance.
43+ const encodedShape = yield* Schema.encodeEffect(Broken)(broken);
44+ expect(Option.isOption(encodedShape.description)).toBe(true);
45+
46+ // Step 2: turn the encoded shape into a JSON string via Effect.
47+ const jsonString = yield* Schema.encodeEffect(Schema.UnknownFromJsonString)(encodedShape);
48+ // This is what Effect produced — Option's toJSON shape:
49+ expect(jsonString).toContain('"_id":"Option"');
50+ expect(jsonString).toContain('"_tag":"Some"');
51+
52+ // Step 3 + 4: round-trip via the platform FileSystem.
53+ yield* fs.writeFileString(path, jsonString);
54+ const onDisk = yield* fs.readFileString(path);
55+
56+ // Step 5: parse string → unknown via Effect.
57+ const parsed = yield* Schema.decodeUnknownEffect(Schema.UnknownFromJsonString)(onDisk);
58+
59+ // Step 6: decode the unknown back through the schema → fails,
60+ // because the wire shape isn't an Option instance.
61+ const result = yield* Effect.exit(Schema.decodeUnknownEffect(Broken)(parsed));
62+ expect(Exit.isFailure(result)).toBe(true);
63+ }),
64+ ),
65+ );
66+
67+ it.effect("WORKS: same pipeline with Schema.OptionFromOptional", () =>
68+ withTmpFile((path) =>
69+ Effect.gen(function* () {
70+ const fs = yield* FileSystem.FileSystem;
71+
72+ const encodedShape = yield* Schema.encodeEffect(Fixed)(fixed);
73+ // Encoded form is JSON-safe: { description: "hello" }
74+ expect(encodedShape.description).toBe("hello");
75+
76+ const jsonString = yield* Schema.encodeEffect(Schema.UnknownFromJsonString)(encodedShape);
77+ expect(jsonString).toBe('{"description":"hello"}');
78+
79+ yield* fs.writeFileString(path, jsonString);
80+ const onDisk = yield* fs.readFileString(path);
81+
82+ const parsed = yield* Schema.decodeUnknownEffect(Schema.UnknownFromJsonString)(onDisk);
83+ const decoded = yield* Schema.decodeUnknownEffect(Fixed)(parsed);
84+
85+ expect(Option.getOrNull(decoded.description)).toBe("hello");
86+ }),
87+ ),
88+ );
89+
90+ it.effect("WORKS: None round-trips as a missing key with OptionFromOptional", () =>
91+ withTmpFile((path) =>
92+ Effect.gen(function* () {
93+ const fs = yield* FileSystem.FileSystem;
94+ const noneVal = { description: Option.none<string>() };
95+
96+ const encodedShape = yield* Schema.encodeEffect(Fixed)(noneVal);
97+ const jsonString = yield* Schema.encodeEffect(Schema.UnknownFromJsonString)(encodedShape);
98+ expect(jsonString).toBe("{}");
99+
100+ yield* fs.writeFileString(path, jsonString);
101+ const onDisk = yield* fs.readFileString(path);
102+ const parsed = yield* Schema.decodeUnknownEffect(Schema.UnknownFromJsonString)(onDisk);
103+ const decoded = yield* Schema.decodeUnknownEffect(Fixed)(parsed);
104+
105+ expect(Option.isNone(decoded.description)).toBe(true);
106+ }),
107+ ),
108+ );
109+});
packages/plugins/google-discovery/src/sdk/types.ts+7−7
@@ -22,8 +22,8 @@ export class GoogleDiscoveryParameter extends Schema.Class<GoogleDiscoveryParame · 21 unmodified lines
2222 location: GoogleDiscoveryParameterLocation,
2323 required: Schema.Boolean,
2424 repeated: Schema.Boolean,
25− description: Schema.Option(Schema.String),
26− schema: Schema.Option(Schema.Unknown),
25+ description: Schema.OptionFromOptional(Schema.String),
26+ schema: Schema.OptionFromOptional(Schema.Unknown),
2727 }) {}
2828
2929 export class GoogleDiscoveryMethodBinding extends Schema.Class<GoogleDiscoveryMethodBinding>(
@@ -39,22 +39,22 @@ export class GoogleDiscoveryManifestMethod extends Schema.Class<GoogleDiscoveryM · 9 unmodified lines
3939 "GoogleDiscoveryManifestMethod",
4040 )({
4141 toolPath: Schema.String,
42− description: Schema.Option(Schema.String),
42+ description: Schema.OptionFromOptional(Schema.String),
4343 binding: GoogleDiscoveryMethodBinding,
44− inputSchema: Schema.Option(Schema.Unknown),
45− outputSchema: Schema.Option(Schema.Unknown),
44+ inputSchema: Schema.OptionFromOptional(Schema.Unknown),
45+ outputSchema: Schema.OptionFromOptional(Schema.Unknown),
4646 scopes: Schema.Array(Schema.String),
4747 }) {}
4848
4949 export class GoogleDiscoveryManifest extends Schema.Class<GoogleDiscoveryManifest>(
5050 "GoogleDiscoveryManifest",
5151 )({
52− title: Schema.Option(Schema.String),
52+ title: Schema.OptionFromOptional(Schema.String),
5353 service: Schema.String,
5454 version: Schema.String,
5555 rootUrl: Schema.String,
5656 servicePath: Schema.String,
57− oauthScopes: Schema.Option(Schema.Record(Schema.String, Schema.String)),
57+ oauthScopes: Schema.OptionFromOptional(Schema.Record(Schema.String, Schema.String)),
5858 schemaDefinitions: Schema.Record(Schema.String, Schema.Unknown),
5959 methods: Schema.Array(GoogleDiscoveryManifestMethod),
6060 }) {}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t