Skip to content

Commit 85c939d

Browse files
committed
Correctly check content type for custom renderers
this fixes a bug where content type is overwritten to `text/plain` if a custom renderer sets the (default) application-json content type using implicit render
1 parent 884248d commit 85c939d

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

lib/rage/controller/api.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ def prepare_action_params(action_name = nil, **opts, &block)
425425
end
426426
end # class << self
427427
428-
DEFAULT_CONTENT_TYPE = "application/json; charset=utf-8"
428+
DEFAULT_CONTENT_TYPE = "application/json; charset=utf-8".dup
429429
private_constant :DEFAULT_CONTENT_TYPE
430430
431431
# @private
@@ -492,7 +492,7 @@ def render(json: nil, plain: nil, sse: nil, status: nil)
492492
json.is_a?(String) ? json : json.to_json
493493
else
494494
ct = @__headers["content-type"]
495-
@__headers["content-type"] = "text/plain; charset=utf-8" if ct.nil? || ct == DEFAULT_CONTENT_TYPE
495+
@__headers["content-type"] = "text/plain; charset=utf-8" if ct.nil? || ct.equal?(DEFAULT_CONTENT_TYPE)
496496
plain.to_s
497497
end
498498

spec/configuration/custom_renderer_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,5 +184,24 @@ def build_controller(&block)
184184
[202, { "content-type" => "application/json; charset=utf-8" }, ["{\"message\":\"test\"}"]]
185185
)
186186
end
187+
188+
it "doesn't overwrite default content type for renderers with implicit render" do
189+
config.renderer(:jsonld) do |data|
190+
headers["content-type"] = "application/json; charset=utf-8"
191+
data.merge(:@context => "https://schema.org").to_json
192+
end
193+
194+
config.__finalize
195+
196+
controller = build_controller do
197+
define_method(:index) do
198+
render jsonld: { name: "Alice" }
199+
end
200+
end
201+
202+
expect(run_action(controller, :index)).to match(
203+
[200, { "content-type" => "application/json; charset=utf-8" }, [include("Alice")]]
204+
)
205+
end
187206
end
188207
end

0 commit comments

Comments
 (0)