JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Alternate solution for raising `JSONAPI::Exceptions::InvalidFieldValue` in `replace_fields`?

Open
#82 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Type: Maintenance
Dominant language
Ruby
Stars
2.3k
Forks
546
PR merge metrics
No merged PRs in 30d

Description

I'm currently using a callback before save to handle authorization.

module V1
  class BaseResource < JSONAPI::Resource
    # ...
    set_callback :save, :before, :authorize
    # ...
  end
end

I verify that authorization has been performed on each action in the ApplicationController:

class ApplicationController < JSONAPI::ResourceController
  # ...
  after_action :verify_policy_authorized, except: [:index]
  def verify_policy_authorized
    raise PolicyAuthorizationNotPerformedError unless policy_authorized?
  end
  # ...
end

Unfortunately, if an invalid value is submitted for an enum attribute the before save callback never happens because a JSONAPI::Exceptions::InvalidFieldValue is raised in _replace_fields. This causes verify_policy_authorized to not be called which raises the PolicyAuthorizationNotPerformedError.

I could obviously hack in a fix, but I wondered if this was a sign that a more general approach would be better. For example, should we provide a hook into exceptions raised and rescued which would allow the user of jsonapi-resources to handle things like bypassing policy authorization?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the enum validation path through _replace_fields and JSONAPI::Exceptions::InvalidFieldValue, then review the save callback in V1::BaseResource and verify_policy_authorized in ApplicationController. The issue proposes a general exception hook, but does not define the hook's behavior or a concrete completion test.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
api, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.