Skip to content

Commit 5521870

Browse files
authored
improve variable serialization for arrays and hashes (#40)
- Add proper serialization support for Array and Hash variable types - Fix serialization for integers and booleans - Maintain existing string serialization behavior This change ensures that arrays, hashes, integers, and booleans are properly serialized when making queries with variables to the query api.
1 parent 12a2a13 commit 5521870

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

lib/sanity/http/where.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def query_and_variables
4141
else
4242
{}.tap do |hash|
4343
variables.each do |key, value|
44-
hash["$#{key}"] = "\"#{value}\""
44+
hash["$#{key}"] = serialize_variable_value(value)
4545
end
4646
end
4747
end.merge(query: groq_query)
@@ -56,6 +56,17 @@ def request_body
5656

5757
query_and_variables.to_json
5858
end
59+
60+
def serialize_variable_value(value)
61+
case value
62+
when String
63+
"\"#{value}\""
64+
when Array, Hash
65+
value.to_json
66+
else
67+
value.to_s
68+
end
69+
end
5970
end
6071
end
6172
end

0 commit comments

Comments
 (0)