startback 2.1.4 → 2.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 16aaf55cd5b96370f06ddb5d991ab102dcca2ca11af53f1eb6f1696fe9a32c43
4
- data.tar.gz: 692b9c768939c22c1e7abe9d677cd10ea7bed01d57517e0a8c55cd43884de1d2
3
+ metadata.gz: 2a5ff1395c572da25e944d258f846657e16f307dce398071fb3d947275a03409
4
+ data.tar.gz: 9bc296a6f4fe7596354be47f36d33bbcb0d5fddda492c5e75c422d5ab4a20184
5
5
  SHA512:
6
- metadata.gz: d91037cce674c4202e56d28489e38275942a4e4fe7546485b4f16cac06f35e97185741eb995896a0b02e06b2b8da7fea5259adf25af964dd01f1a9089e988d56
7
- data.tar.gz: da6b2222a76dca27280e79367260ad1512b07b78400f3cede0e97448f96aefaf139e7ce9d40d41a442c42f3e1103f52cb25df2ee55c3a17e2c4762ed17d6e07d
6
+ metadata.gz: 6a9e62814629b538578b6bd8231da04321764dd8c0611191724c2d263c9f36605a7e6cba24cb5b36f9f5052b54700a485800801ab19f7240c66712062a35b5c8
7
+ data.tar.gz: bc588f121b99a1bdbc4378ee4e144a4e13725172683d61b040e2b095ac95b94b37772b616e76dae49b2aaceb3660fefdcf5c820cad9257309cb14ca1b7c134c8
data/CHANGELOG.md CHANGED
@@ -1,3 +1,33 @@
1
+ ## 2.1.5 - 2026-11-21
2
+
3
+ A json body sent without `Content-Type: application/json` was read as empty.
4
+ **Nothing to do to get the fix**, unless your application reads the request
5
+ body by itself.
6
+
7
+ ### `json_body` reads the body from its start
8
+
9
+ Rack parses the body of a POST as form data when its content type is
10
+ `application/x-www-form-urlencoded` -- what `curl -d` sends by default -- or
11
+ missing altogether, and Sinatra asks for the parameters before any route is
12
+ reached. Rack 2 rewound the input afterwards, Rack 3 does not. Since 2.0.0,
13
+ `Web::Api#json_body` therefore found the body already consumed in those two
14
+ cases, loaded an empty string and returned `nil`.
15
+
16
+ Nothing raises. The route goes on with a `nil` input and typically answers a
17
+ 400 about it, which is how it surfaced: a service calling another one with
18
+ the http gem and `body: JSON.dump(...)`, which sets no content type, something
19
+ that had been working for years.
20
+
21
+ `json_body` now rewinds the input before reading it. `loaded_body` was not
22
+ affected: it only loads json when the content type says so, and Rack leaves
23
+ such a body alone.
24
+
25
+ An application reading `request.body` by itself is exposed in the very same
26
+ way, and has to rewind it too: see the new section 11 of
27
+ [UPGRADING.md](UPGRADING.md). An input that cannot be rewound is left as it
28
+ is: Rack 3 no longer requires one to be, though those of puma and Rack::Test
29
+ are.
30
+
1
31
  ## 2.1.4 - 2026-11-20
2
32
 
3
33
  No change to Startback itself. This release exists to rebuild the docker
data/UPGRADING.md CHANGED
@@ -419,6 +419,42 @@ them. A content-negotiating endpoint may need its schema widened accordingly
419
419
 
420
420
  ---
421
421
 
422
+ ## 11. A request body is empty when read
423
+
424
+ **You will see** a route receiving `nil` or an empty string where a client
425
+ did send a body, and usually answering a 400 about its input. Only for some
426
+ clients: those sending their body without a content type, or with
427
+ `application/x-www-form-urlencoded`, which is what `curl -d` does unless told
428
+ otherwise.
429
+
430
+ **Why.** Rack parses the body of such a POST as form data, and Sinatra asks
431
+ for the parameters before any route is reached. Rack 2 rewound the input
432
+ afterwards. Rack 3 no longer does, nor does it require an input to be
433
+ rewindable at all, so the body is simply consumed by the time your route
434
+ reads it.
435
+
436
+ Nothing raises, and well-behaved clients are fine, which is why this one
437
+ tends to be found late: typically on a service-to-service call made with
438
+ `body: JSON.dump(...)`, which sets no content type.
439
+
440
+ **Fix.** Nothing, if you go through `json_body`: it rewinds the input as of
441
+ Startback 2.1.5. If you read the body yourself, rewind it first:
442
+
443
+ ```ruby
444
+ # before
445
+ raw = request.body.read
446
+
447
+ # after
448
+ request.body.rewind
449
+ raw = request.body.read
450
+ ```
451
+
452
+ The inputs of puma and of `Rack::Test` can be rewound. Having the clients
453
+ send `Content-Type: application/json` is the other half of the fix, and the
454
+ better one where they are yours.
455
+
456
+ ---
457
+
422
458
  ## Opting out, gem by gem
423
459
 
424
460
  Startback deliberately declares **wide ranges** for the dependencies it does
@@ -456,5 +492,6 @@ These cannot be opted out of, because Startback's own code depends on them:
456
492
  - [ ] No `JSON.fast_generate` left
457
493
  - [ ] `.fio` schemas grepped for `Fixnum`, `Bignum`, `FalseClass`
458
494
  - [ ] webspicy on 1.x
495
+ - [ ] Routes reading `request.body` by themselves rewind it first
459
496
  - [ ] jwt / bunny reviewed, or pinned
460
497
  - [ ] Bunny event bus exercised somewhere real, since CI does not cover it
@@ -2,7 +2,7 @@ module Startback
2
2
  module Version
3
3
  MAJOR = 2
4
4
  MINOR = 1
5
- TINY = 4
5
+ TINY = 5
6
6
  end
7
7
  VERSION = "#{Version::MAJOR}.#{Version::MINOR}.#{Version::TINY}"
8
8
  end
@@ -54,10 +54,25 @@ module Startback
54
54
  end
55
55
  end
56
56
 
57
- def json_body(body = request.body.read)
57
+ # Loads `body` as json, the body of the request by default.
58
+ #
59
+ # The latter is read from its very start. Rack parses the body of a
60
+ # POST as form data when its content type is urlencoded, or missing,
61
+ # which Sinatra triggers before any route is reached. Rack 2 used to
62
+ # rewind the body afterwards, Rack 3 does not: without rewinding it
63
+ # here, a json body sent without `Content-Type: application/json` would
64
+ # be seen as empty.
65
+ def json_body(body = rewound_body.read)
58
66
  JSON.load(body)
59
67
  end
60
68
 
69
+ # Returns the body of the request, rewound when it can be. Rack 3 no
70
+ # longer requires an input to be rewindable; those of puma and
71
+ # Rack::Test are.
72
+ def rewound_body
73
+ request.body.tap{|io| io.rewind if io.respond_to?(:rewind) }
74
+ end
75
+
61
76
  def file_body(file, ctype)
62
77
  raise UnsupportedMediaTypeError, "Unable to use `#{ctype}` as input data"
63
78
  end
@@ -47,6 +47,11 @@ module Startback
47
47
  get '/dto-path' do
48
48
  serve('DTO', DtoAble.new(PathDto.new))
49
49
  end
50
+
51
+ post '/json-body' do
52
+ content_type :json
53
+ json_body.to_json
54
+ end
50
55
  end
51
56
 
52
57
  let(:app) {
@@ -77,6 +82,45 @@ module Startback
77
82
  get '/dto-path'
78
83
  expect(last_response.body).to eql(Path.file.read)
79
84
  end
85
+
86
+ describe 'json_body' do
87
+ let(:json) {
88
+ %Q{{"todos":[{"id":1}],"done":true}}
89
+ }
90
+
91
+ it 'reads a json body' do
92
+ post '/json-body', json, { 'CONTENT_TYPE' => 'application/json' }
93
+ expect(last_response.body).to eql(json)
94
+ end
95
+
96
+ it 'reads a json body sent as text' do
97
+ post '/json-body', json, { 'CONTENT_TYPE' => 'text/plain' }
98
+ expect(last_response.body).to eql(json)
99
+ end
100
+
101
+ # Rack parses the body as form data in the next two cases, and Rack 3
102
+ # no longer rewinds it afterwards.
103
+
104
+ it 'reads a json body sent as a form' do
105
+ post '/json-body', json, { 'CONTENT_TYPE' => 'application/x-www-form-urlencoded' }
106
+ expect(last_response.body).to eql(json)
107
+ end
108
+
109
+ it 'reads a json body sent without any content type' do
110
+ # Rack::Test would default to urlencoded
111
+ env = Rack::MockRequest.env_for('/json-body', method: 'POST', input: json)
112
+ expect(env['CONTENT_TYPE']).to be_nil
113
+
114
+ status, _, body = app.call(env)
115
+ expect(status).to eql(200)
116
+ expect(body.each.to_a.join).to eql(json)
117
+ end
118
+
119
+ it 'still loads what it is given' do
120
+ api = TestedApi.new!
121
+ expect(api.send(:json_body, %Q{{"foo":"bar"}})).to eql("foo" => "bar")
122
+ end
123
+ end
80
124
  end
81
125
  end # module Web
82
126
  end # module Startback
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: startback
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.4
4
+ version: 2.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bernard Lambeau
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2026-11-20 00:00:00.000000000 Z
10
+ date: 2026-11-21 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rspec