Skip to content

Commit 18e69cc

Browse files
authored
Merge pull request #2762 from ruby-grape/chore/json-parse-fallback
Parse request bodies with JSON.parse in the stdlib fallback
2 parents 0f9f44f + 2aa3b3d commit 18e69cc

4 files changed

Lines changed: 6 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
* [#2752](https://github.com/ruby-grape/grape/pull/2752): Skip per-request `ActiveSupport::Notifications` payload and dispatch when no subscriber is listening, via private `instrument_<event>` guards on `Endpoint`/`Middleware::Formatter` - [@ericproulx](https://github.com/ericproulx).
6262
* [#2757](https://github.com/ruby-grape/grape/pull/2757): Build the `Grape::Cookies` jar only when a cookie is read or written (via a new `Grape::Request#cookies?` predicate gating response-cookie flushing), and drop the jar's now-redundant lazy-parse `Proc` - [@ericproulx](https://github.com/ericproulx).
6363
* [#2756](https://github.com/ruby-grape/grape/pull/2756): Tighten dependency lower bounds to their compatibility floors (`rack >= 2.2.4`, `zeitwerk >= 2.6`, `dry-configurable >= 1.0`) - [@ericproulx](https://github.com/ericproulx).
64+
* [#2762](https://github.com/ruby-grape/grape/pull/2762): Parse request bodies with `JSON.parse` in the stdlib JSON fallback, dropping the `create_additions: false` wrapper from #2759 (`JSON.parse` never honours `json_class`) - [@ericproulx](https://github.com/ericproulx).
6465
* Your contribution here.
6566

6667
#### Fixes

lib/grape/json.rb

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,7 @@ module Grape
44
if defined?(::MultiJson)
55
Json = ::MultiJson
66
else
7-
module Json
8-
ParseError = ::JSON::ParserError
9-
10-
def self.load(str)
11-
::JSON.load(str, nil, create_additions: false)
12-
end
13-
14-
def self.dump(obj, *)
15-
::JSON.dump(obj, *)
16-
end
17-
end
7+
Json = ::JSON
8+
Json::ParseError = Json::ParserError
189
end
1910
end

lib/grape/parser/json.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Grape
44
module Parser
55
class Json < Base
66
def self.call(object, _env)
7-
::Grape::Json.load(object)
7+
::Grape::Json.parse(object)
88
rescue ::Grape::Json::ParseError
99
# handle JSON parsing errors via the rescue handlers or provide error message
1010
raise Grape::Exceptions::InvalidMessageBody.new('application/json')

spec/grape/parser/json_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
end
1515

1616
# Verify that json_class payloads are treated as plain data and do not
17-
# trigger Ruby object instantiation via JSON.load's create_additions mechanism.
17+
# trigger Ruby object instantiation. JSON.parse never honours the json_class
18+
# additions mechanism (unlike JSON.load), so the named class is never built.
1819
context 'when the request body contains a json_class key' do
1920
let(:triggered) { [] }
2021

0 commit comments

Comments
 (0)