Skip to content

Commit 94d4607

Browse files
fix(serializer): attributes with hashes as values ending up without camelCase keys in Rails
In Rails, doing ``` WebAuthn::Credential.options_for_create(user: { name: 'john', id: WebAuthn.generate_user_id }, authenticator_selection: { user_verification: "required" }).as_json ``` results in: ``` {:challenge=>"QVFtcdn_znYUWzAzmBSGpOPWweTs1tBmqPQENeGjUvA", :timeout=>120000, :extensions=>{}, :rp=>{:name=>"WebAuthn Rails Demo App"}, :user=>{:name=>"john", :id=>"8kzxeLCJ13lE5g7T7pE0TxCRxy_4Mcpp1lWS6geXJAiz7rPXHOWSJy_Ou33GP7lCvn_u5uIhDZzOez_cWoMGcA", :displayName=>"john"}, :pubKeyCredParams=>[{"type"=>"public-key", "alg"=>-7}, {"type"=>"public-key", "alg"=>-37}, {"type"=>"public-key", "alg"=>-257}], :authenticatorSelection=>{"user_verification"=>"required"}} ``` (Notice the `user_verification` key under `authenticatorSelection` without being camelCased) This causes errors with some authenticators – particularly with 1Password. The issue is that for the attributes whose value is a hash – as in Rails, hash objects respond to `as_json` – are not going through the part of the condition that calls camelCases its keys. To fix this, this commit attempts to go back at the initial approach - before 0aab124 – that first converts the object into a hash and then camel cases its keys, even though more iterations are performed on the object.
1 parent 00b30c5 commit 94d4607

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

lib/webauthn/json_serializer.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@ module WebAuthn
44
module JSONSerializer
55
# Argument wildcard for Ruby on Rails controller automatic object JSON serialization
66
def as_json(*)
7-
to_hash_with_camelized_keys
7+
deep_camelize_keys(to_hash)
88
end
99

1010
private
1111

12-
def to_hash_with_camelized_keys
12+
def to_hash
1313
attributes.each_with_object({}) do |attribute_name, hash|
1414
value = send(attribute_name)
1515

16-
if value && value.respond_to?(:as_json)
17-
hash[camelize(attribute_name)] = value.as_json
18-
elsif value
19-
hash[camelize(attribute_name)] = deep_camelize_keys(value)
16+
if value.respond_to?(:as_json)
17+
value = value.as_json
18+
end
19+
20+
if value
21+
hash[attribute_name] = value
2022
end
2123
end
2224
end

0 commit comments

Comments
 (0)