cerebris / jsonapi-resources

A resource-focused Rails library for developing JSON:API compliant servers.
http://jsonapi-resources.com
MIT License
2.32k stars 532 forks source link

ensure_correct_media_type does not validate against an invalid Accept header #699

Closed jerelmiller closed 8 years ago

jerelmiller commented 8 years ago

I noticed that the ensure_correct_media_type before_action does not validate against an invalid Accept header. It appears to only validate against the Content-Type header.

jerelmiller commented 8 years ago

After looking into this further, it appears that the default behavior in this gem is already correct in that it should accept an Accept header that does not specify the json api media type. However, if the media type is there, it just needs to validate that it doesn't include media type parameters.

Here's a useful resource for this very topic.

https://github.com/elliotttf/jsonapi-headers/commit/db85f3215238014ff980cdad07fec7f6071042f2

peco8 commented 8 years ago

+1

peco8 commented 8 years ago

This actually validates the Accept header with 'GET' method. Not 'POST'

    it 'returns 406 with invalid Accept: header' do
      headers = {
        'ACCEPT' => 'application/vnd.api+jsondummmy'
      }
      # post '/api/v1/subscriptions', json_body.to_json ,headers # <= returns 415
      get '/api/v1/subscriptions', headers # <= returns 406
      expect(response.status).to eq 406 # Not acceptable
    end

Is this JSON-API specific?

peco8 commented 8 years ago

For now working around for this is

within controller.rb

before_action :restrict_accept_header

def restrict_accept_header      
      render json: { msg: 'Accept-Header must be application/json' }, status: 406 unless request.headers['Accept'] =~ /application\/vnd\.api\+json/ # :not_acceptable       
end

This is not JSON-API specific error message, but it works.