Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): Optional JSON params where undefined is not valid #134

Merged
merged 4 commits into from
Aug 31, 2023

Conversation

legobeat
Copy link
Contributor

@legobeat legobeat commented Aug 29, 2023

Follow-up to #130 (#129).

This should now:

  • Not specify undefined as valid JSON value in types
  • Keep params field optional in types
  • Preserve backwards-compatability

The hardcoded types were extracted from the resulting json.d.ts after a build of v8.0.0 (current main), with the only change in that params: was changed into params?:. This seems to be necessary due to the way superstruct omit and optional work.

@legobeat legobeat added the help wanted Extra attention is needed label Aug 29, 2023
@legobeat legobeat requested a review from Gudahtt August 29, 2023 01:39
@legobeat legobeat marked this pull request as ready for review August 29, 2023 01:39
@legobeat legobeat requested a review from a team as a code owner August 29, 2023 01:39
@legobeat
Copy link
Contributor Author

legobeat commented Aug 29, 2023

Full diff of resulting built package and typings compared to v8.0.0:

CJS

diff -x '*.map' -wr -U1 dist-v8.0.0/cjs/json.js dist-728519a3/cjs/json.js
--- dist-v8.0.0/cjs/json.js
+++ dist-728519a3/cjs/json.js
@@ -161,6 +161,6 @@
 });
-const JsonRpcParamsStruct = (0, _superstruct.optional)((0, _superstruct.union)([
+const JsonRpcParamsStruct = (0, _superstruct.union)([
     (0, _superstruct.record)((0, _superstruct.string)(), JsonStruct),
     (0, _superstruct.array)(JsonStruct)
-]));
+]);
 const JsonRpcRequestStruct = (0, _superstruct.object)({
@@ -169,7 +169,9 @@
     method: (0, _superstruct.string)(),
-    params: JsonRpcParamsStruct
+    params: (0, _superstruct.optional)(JsonRpcParamsStruct)
+});
+const JsonRpcNotificationStruct = (0, _superstruct.object)({
+    jsonrpc: JsonRpcVersionStruct,
+    method: (0, _superstruct.string)(),
+    params: (0, _superstruct.optional)(JsonRpcParamsStruct)
 });
-const JsonRpcNotificationStruct = (0, _superstruct.omit)(JsonRpcRequestStruct, [
-    'id'
-]);
 function isJsonRpcNotification(value) {

ESM

diff -x '*.map' -wr -U1 dist-v8.0.0/esm/json.js dist-728519a3/esm/json.js
--- dist-v8.0.0/esm/json.js
+++ dist-728519a3/esm/json.js
@@ -1,2 +1,2 @@
-import { any, array, boolean, coerce, create, define, integer, is, lazy, literal, nullable, number, object, omit, optional, record, string, union, unknown } from 'superstruct';
+import { any, array, boolean, coerce, create, define, integer, is, lazy, literal, nullable, number, object, optional, record, string, union, unknown } from 'superstruct';
 import { assertStruct } from './assert';
@@ -90,6 +90,6 @@
 });
-export const JsonRpcParamsStruct = optional(union([
+export const JsonRpcParamsStruct = union([
     record(string(), JsonStruct),
     array(JsonStruct)
-]));
+]);
 export const JsonRpcRequestStruct = object({
@@ -98,7 +98,9 @@
     method: string(),
-    params: JsonRpcParamsStruct
+    params: optional(JsonRpcParamsStruct)
+});
+export const JsonRpcNotificationStruct = object({
+    jsonrpc: JsonRpcVersionStruct,
+    method: string(),
+    params: optional(JsonRpcParamsStruct)
 });
-export const JsonRpcNotificationStruct = omit(JsonRpcRequestStruct, [
-    'id'
-]);
 /**

Typings

diff -x '*.map' -wr -U1 dist-v8.0.0/types/json.d.ts dist-728519a3/types/json.d.ts
--- dist-v8.0.0/types/json.d.ts
+++ dist-728519a3/types/json.d.ts
@@ -94,10 +94,10 @@
     id: string | number | null;
+    jsonrpc: '2.0';
     method: string;
-    jsonrpc: "2.0";
-    params: Json[] | Record<string, Json>;
+    params?: Json[] | Record<string, Json>;
 }, {
     id: Struct<string | number | null, null>;
-    jsonrpc: Struct<"2.0", "2.0">;
+    jsonrpc: Struct<'2.0', '2.0'>;
     method: Struct<string, null>;
-    params: Struct<Json[] | Record<string, Json>, null>;
+    params?: Struct<Json[] | Record<string, Json>, null>;
 }>;
@@ -113,11 +113,10 @@
 export declare const JsonRpcNotificationStruct: Struct<{
+    jsonrpc: '2.0';
     method: string;
-    jsonrpc: "2.0";
-    params: Json[] | Record<string, Json>;
-}, Omit<{
-    id: Struct<string | number | null, null>;
-    jsonrpc: Struct<"2.0", "2.0">;
+    params?: Json[] | Record<string, Json>;
+}, {
+    jsonrpc: Struct<'2.0', '2.0'>;
     method: Struct<string, null>;
-    params: Struct<Json[] | Record<string, Json>, null>;
-}, "id">>;
+    params?: Struct<Json[] | Record<string, Json>, null>;
+}>;
 /**

@legobeat legobeat removed the help wanted Extra attention is needed label Aug 29, 2023
@legobeat legobeat requested review from a team August 29, 2023 01:56
@legobeat legobeat requested a review from kumavis August 29, 2023 02:28
legobeat added a commit to legobeat/rpc-errors that referenced this pull request Aug 29, 2023
@legobeat legobeat requested review from Gudahtt and a team and removed request for Gudahtt August 29, 2023 18:27
Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit worried that this will cause repercussions that we might not have accounted for yet. We should see if we can test these changes in core, Snaps, and perhaps even other libraries like json-rpc-engine or eth-json-rpc-middleware first. I don't know if I will have time to look into this right now, just want to advise that we might want to tread carefully here.

@legobeat
Copy link
Contributor Author

legobeat commented Aug 29, 2023

@mcmire
Copy link
Contributor

mcmire commented Aug 29, 2023

Ah nice, thank you. I see CI passes in each case (i.e. no type errors). That's a good sign at least!

@legobeat
Copy link
Contributor Author

@mcmire Updated above comment with eth-json-rpc-middleware and snaps.

@legobeat

This comment was marked as resolved.

@legobeat

This comment was marked as outdated.

@legobeat
Copy link
Contributor Author

legobeat commented Aug 30, 2023

See #135 - a less hacky alternative. Merging it into this one.

Thank you @Mrtenz for making the final (:crossed_fingers:) implementation :1st_place_medal:

@legobeat legobeat marked this pull request as draft August 30, 2023 10:13
@legobeat legobeat marked this pull request as ready for review August 30, 2023 10:20
@legobeat legobeat merged commit e8cad2f into MetaMask:main Aug 31, 2023
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants