Skip to content

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

Merged
legobeat merged 4 commits into
MetaMask:mainfrom
legobeat:fix-types-undefined-optional
Aug 31, 2023
Merged

fix(types): Optional JSON params where undefined is not valid#134
legobeat merged 4 commits into
MetaMask:mainfrom
legobeat:fix-types-undefined-optional

Conversation

@legobeat

@legobeatlegobeat commented Aug 29, 2023

Copy link
Copy Markdown
Contributor

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 superstructomit and optional work.

@legobeatlegobeat added the help wanted Extra attention is needed label Aug 29, 2023
@legobeat
legobeat requested a review from GudahttAugust 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 ownerAugust 29, 2023 01:39
@legobeat

legobeat commented Aug 29, 2023

Copy link
Copy Markdown
ContributorAuthor

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>;
+}>;
/**

@legobeatlegobeat removed the help wanted Extra attention is needed label Aug 29, 2023
@legobeat
legobeat requested review from a teamAugust 29, 2023 01:56
@legobeat
legobeat requested a review from kumavisAugust 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 a team and Gudahtt and removed request for GudahttAugust 29, 2023 18:27

@mcmiremcmire left a comment

Copy link
Copy Markdown
Contributor

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

legobeat commented Aug 29, 2023

Copy link
Copy Markdown
ContributorAuthor

@mcmire

mcmire commented Aug 29, 2023

Copy link
Copy Markdown
Contributor

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
Copy Markdown
ContributorAuthor

@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

legobeat commented Aug 30, 2023

Copy link
Copy Markdown
ContributorAuthor

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
Sign up for freeto 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

@legobeat@mcmire@Mrtenz@danroc