Skip to content

ParserError#json_path raises NoMethodError when parsing with symbolize_names: true #1117

Closed

Description

@auraseb

What happens

Since 3.1.0, calling json_path on a JSON::ParserError raised while parsing with symbolize_names: true raises NoMethodError as soon as the path contains an object key:

require "json"

begin
  JSON.parse('{"a": [1, {"b": xyz}]}', symbolize_names: true)
rescue JSON::ParserError => error
  error.json_path
end
# => undefined method 'valid_encoding?' for an instance of Symbol (NoMethodError)

Duplicate key errors in nested objects are affected the same way:

JSON.parse('{"x": {"a": 1, "a": 2}}', symbolize_names: true)
# rescue => error; error.json_path => NoMethodError

What should happen

error.json_path should return "$.a[1].b" (and "$.x.a" for the second example), as it does without symbolize_names.

Root cause

With symbolize_names: true the keys collected for the path are Symbols. ParserError#build_json_path accepts String and Symbol segments, but the check added in "Handle invalid encoding in JSON path keys" (#1113) calls segment.valid_encoding?, which only exists on String. Converting the segment with to_s before the checks fixes it.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions