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

JSON-encode GlobalIDs as strings #149

Merged
merged 1 commit into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/global_id/global_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,8 @@ def hash
def to_param
Base64.urlsafe_encode64(to_s, padding: false)
end

def as_json(*)
to_s
end
end
14 changes: 14 additions & 0 deletions test/cases/global_id_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,20 @@ class GlobalIDCreationTest < ActiveSupport::TestCase
assert_equal URI('gid://bcx/PersonModel/1'), @person_model_gid.uri
end

test 'as JSON' do
assert_equal 'gid://bcx/Person/5', @person_gid.as_json
assert_equal '"gid://bcx/Person/5"', @person_gid.to_json

assert_equal "gid://bcx/Person/#{@uuid}", @person_uuid_gid.as_json
assert_equal "\"gid://bcx/Person/#{@uuid}\"", @person_uuid_gid.to_json

assert_equal 'gid://bcx/Person::Child/4', @person_namespaced_gid.as_json
assert_equal '"gid://bcx/Person::Child/4"', @person_namespaced_gid.to_json

assert_equal 'gid://bcx/PersonModel/1', @person_model_gid.as_json
assert_equal '"gid://bcx/PersonModel/1"', @person_model_gid.to_json
end

test 'model id' do
assert_equal '5', @person_gid.model_id
assert_equal @uuid, @person_uuid_gid.model_id
Expand Down