Skip to content

Commit 8d93eef

Browse files
committed
Support nil parameter serialization behavior in Rails 8.1
- Introduced `DEPRECATED_NIL_QUERY_PARAMETER_BEHAVIOR` to handle nil query parameters based on Rails version. - Updated `default_serializer` method in `routes.js` and `routes.ts` to accommodate the new behavior. - Ensured backward compatibility with Rails versions below 8.1.0.
1 parent a750b90 commit 8d93eef

File tree

5 files changed

+24
-30
lines changed

5 files changed

+24
-30
lines changed

lib/js_routes/instance.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def js_variables
9494
{
9595
'ROUTES_OBJECT' => routes_object,
9696
'DEPRECATED_FALSE_PARAMETER_BEHAVIOR' => Rails.version < '7.0.0',
97+
'DEPRECATED_NIL_QUERY_PARAMETER_BEHAVIOR' => Rails.version < '8.1.0',
9798
'DEFAULT_URL_OPTIONS' => json(@configuration.default_url_options),
9899
'PREFIX' => json(prefix),
99100
'SPECIAL_OPTIONS_KEY' => json(@configuration.special_options_key),

lib/routes.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ declare type ModuleType = "CJS" | "AMD" | "UMD" | "ESM" | "DTS" | "NIL";
6262
declare const RubyVariables: {
6363
PREFIX: string;
6464
DEPRECATED_FALSE_PARAMETER_BEHAVIOR: boolean;
65+
DEPRECATED_NIL_QUERY_PARAMETER_BEHAVIOR: boolean;
6566
SPECIAL_OPTIONS_KEY: string;
6667
DEFAULT_URL_OPTIONS: RouteParameters;
6768
SERIALIZER: Serializer;

lib/routes.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,6 @@ RubyVariables.WRAPPER(
122122
};
123123
}
124124
default_serializer(value, prefix) {
125-
if (this.is_nullable(value)) {
126-
return "";
127-
}
128125
if (!prefix && !this.is_object(value)) {
129126
throw new Error("Url parameters should be a javascript hash");
130127
}
@@ -140,21 +137,19 @@ RubyVariables.WRAPPER(
140137
if (!hasProp(value, key))
141138
continue;
142139
let prop = value[key];
143-
if (this.is_nullable(prop) && prefix) {
144-
prop = "";
145-
}
146-
if (this.is_not_nullable(prop)) {
147-
if (prefix) {
148-
key = prefix + "[" + key + "]";
149-
}
150-
result.push(this.default_serializer(prop, key));
140+
if (prefix) {
141+
key = prefix + "[" + key + "]";
151142
}
143+
result.push(this.default_serializer(prop, key));
152144
}
153145
}
154146
else {
155-
if (this.is_not_nullable(value)) {
156-
result.push(encodeURIComponent(prefix) + "=" + encodeURIComponent("" + value));
157-
}
147+
result.push(this.is_not_nullable(value) ||
148+
RubyVariables.DEPRECATED_NIL_QUERY_PARAMETER_BEHAVIOR
149+
? encodeURIComponent(prefix) +
150+
"=" +
151+
encodeURIComponent("" + (value !== null && value !== void 0 ? value : ""))
152+
: encodeURIComponent(prefix));
158153
}
159154
return result.join("&");
160155
}

lib/routes.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ type ModuleType = "CJS" | "AMD" | "UMD" | "ESM" | "DTS" | "NIL";
7979
declare const RubyVariables: {
8080
PREFIX: string;
8181
DEPRECATED_FALSE_PARAMETER_BEHAVIOR: boolean;
82+
DEPRECATED_NIL_QUERY_PARAMETER_BEHAVIOR: boolean;
8283
SPECIAL_OPTIONS_KEY: string;
8384
DEFAULT_URL_OPTIONS: RouteParameters;
8485
SERIALIZER: Serializer;
@@ -246,9 +247,6 @@ RubyVariables.WRAPPER(
246247
};
247248

248249
default_serializer(value: unknown, prefix?: string | null): string {
249-
if (this.is_nullable(value)) {
250-
return "";
251-
}
252250
if (!prefix && !this.is_object(value)) {
253251
throw new Error("Url parameters should be a javascript hash");
254252
}
@@ -262,22 +260,20 @@ RubyVariables.WRAPPER(
262260
for (let key in value) {
263261
if (!hasProp(value, key)) continue;
264262
let prop = value[key];
265-
if (this.is_nullable(prop) && prefix) {
266-
prop = "";
267-
}
268-
if (this.is_not_nullable(prop)) {
269-
if (prefix) {
270-
key = prefix + "[" + key + "]";
271-
}
272-
result.push(this.default_serializer(prop, key));
263+
if (prefix) {
264+
key = prefix + "[" + key + "]";
273265
}
266+
result.push(this.default_serializer(prop, key));
274267
}
275268
} else {
276-
if (this.is_not_nullable(value)) {
277-
result.push(
278-
encodeURIComponent(prefix) + "=" + encodeURIComponent("" + value)
279-
);
280-
}
269+
result.push(
270+
this.is_not_nullable(value) ||
271+
RubyVariables.DEPRECATED_NIL_QUERY_PARAMETER_BEHAVIOR
272+
? encodeURIComponent(prefix) +
273+
"=" +
274+
encodeURIComponent("" + (value ?? ""))
275+
: encodeURIComponent(prefix)
276+
);
281277
}
282278
return result.join("&");
283279
}

spec/js_routes/module_types/dts/routes.spec.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ declare type ModuleType = "CJS" | "AMD" | "UMD" | "ESM" | "DTS" | "NIL";
6262
declare const RubyVariables: {
6363
PREFIX: string;
6464
DEPRECATED_FALSE_PARAMETER_BEHAVIOR: boolean;
65+
DEPRECATED_NIL_QUERY_PARAMETER_BEHAVIOR: boolean;
6566
SPECIAL_OPTIONS_KEY: string;
6667
DEFAULT_URL_OPTIONS: RouteParameters;
6768
SERIALIZER: Serializer;

0 commit comments

Comments
 (0)