Rohde-Schwarz / sparrow

Define your request and response JSON format with a Rack middleware!
MIT License
13 stars 6 forks source link

Sparrow breaks static file serving from Rails #17

Closed vassilevsky closed 3 years ago

vassilevsky commented 8 years ago

Hello 👻

First of all, thank you for this useful gem. It brought piece between front-end and back-end developers in our team 🙏

We use Apipie to document our API. We mount it at /apipie in all environments except production. It loads a bunch of static files to style API docs nicely. Examples:

/apipie/stylesheets/bundled/bootstrap.min.css
/apipie/javascripts/bundled/jquery-1.7.2.js
/apipie/javascripts/apipie.js

Naturally, we've enabled serving them from Rails because those are the only assets our app has — it's just an API server. Enabling the Asset Pipeline for them seems like an overkill.

Sparrow breaks serving these assets.

When Sparrow is enabled, this is what happens when you request a static file:

$ curl -i http://CENSORED/apipie/stylesheets/bundled/bootstrap.min.css
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 17 Nov 2015 13:14:19 GMT
Content-Type: text/css
Content-Length: 81150
Connection: keep-alive
Vary: Accept-Encoding
Status: 200 OK
Last-Modified: Fri, 23 Oct 2015 07:25:14 GMT
Cache-Control: no-cache
X-Request-Id: wcID2XAz
X-Runtime: 0.002703

curl: (18) transfer closed with 81120 bytes remaining to read
#<Rack::File:0x007f774eb73be0>%

In other words, it dumps a Ruby representation of the response object rather than its actual contents.

We've solved this problem for us by excluding /apipie in the configuration:

--- a/config/initializers/sparrow.rb
+++ b/config/initializers/sparrow.rb
@@ -1,4 +1,5 @@
 Sparrow.configure do |config|
+  config.excluded_routes = ["apipie"]
   config.allowed_content_types = ["application/json"]
   config.default_json_request_key_transformation_strategy  = "underscore"
   config.default_json_response_key_transformation_strategy = "camelize"

I hope this information helps you fix response handling 🖖

anmuel commented 8 years ago

Hey vassilevsky, Thank you for your contribution :) Your described use case is exactly why we started the project :)

As you described you already have a workaround for the problem (in fact that's originally why we put that option in :D)

The response handling is like top priority in refactoring order of the code, so I will dig into this within the next days 🖖