GithubHelp home page GithubHelp logo

Param not allowed about jsonapi-utils HOT 8 CLOSED

tiagopog avatar tiagopog commented on May 19, 2024
Param not allowed

from jsonapi-utils.

Comments (8)

tiagopog avatar tiagopog commented on May 19, 2024 11

@peco8, @TrevorHinesley:

I decided to investigate this case a little bit further and I figured out that the error is occurring due to the configuration of the key formatter that's being used.

Say we have this PostResource with the user_id attribute:

class PostResource < JSONAPI::Resource
  attributes :title, :body, :user_id
  has_one :author
end

When the request is parsed – using the JR's JSONAPI::Request or JSONAPI::RequestParser – it formats the allowed keys (i.e. creatable_fields), according to the configured key formatter, just before checking whether the params' keys are present among those allowed keys.

Long story short, it's checking this:

%i(user-id).include?(:user_id) #=> false

See what happens when JR parses the request:

screen shot 2017-01-28 at 3 04 16 am

To confirm the hypothesis then I looked for the key formatter:

[8] pry> JSONAPI.configuration.json_key_format
#=> :dasherized_key

So hopefully in order to fix this issue you just need to set the proper key formatter:

# config/initializers/jsonapi-resouces.rb
JSONAPI.configure do |config|
  # ...
  config.json_key_format = :underscored_key
end

from jsonapi-utils.

tiagopog avatar tiagopog commented on May 19, 2024 1

It should be working fine for those versions you're using. I will try to take a deeper look later.

from jsonapi-utils.

tiagopog avatar tiagopog commented on May 19, 2024

Hey there!

Well, I see that theApi::V1::PlanResource is declared as immutable, it will be treated then as a read-only resource. I suppose it may be the source of the error, since it may affect the resource's creatable fields list (not sure about that, JR's stuff) which, in turn, will be used to validate the params allowed for such request before the action actually gets executed.

The weird thing is that your said it wasn't happening with JR 🤔

Anyway, can you check the creatable fields of your resource, and give me feedback:

Api::V1::PlanResource.creatable_fields({}) #=> ?

from jsonapi-utils.

tiagopog avatar tiagopog commented on May 19, 2024

Tip:

if @result.errors.blank?
  jsonapi_render json: @result, status: :created
else
  jsonapi_render_errors json: @result, status: :unprocessable_entity
end

:-)

from jsonapi-utils.

peco8 avatar peco8 commented on May 19, 2024

I tested out

[6] pry(main)> Api::V1::PlanResource.creatable_fields({})
=> [:subscription, :id, :uuid, :stripe_plan_id, :name, :description, :amount, :currency, :interval, :is_active, :activated_at, :disabled_at, :created_at, :updated_at]

stripe_plan_id is creatable field.. 😭

yea, it wasn't happening in JR. ummmm

from jsonapi-utils.

peco8 avatar peco8 commented on May 19, 2024

Maybe I found out.
I used to use jsonapi-resources (0.8.0), but this library brought me jsonapi-resources (0.7.0).
Hope this helps!

gem 'jsonapi-utils', '~> 0.4.8'

You have requested to uninstall the gem:
        jsonapi-resources-0.7.0

jsonapi-utils-0.4.8 depends on jsonapi-resources (~> 0.7.0)
If you remove this gem, these dependencies will not be met.
Continue with Uninstall? [yN]  y

from jsonapi-utils.

TrevorHinesley avatar TrevorHinesley commented on May 19, 2024

I'm also seeing this and I'm using jsonapi-utils 0.5.1 with jsonapi-resources 0.8.3

from jsonapi-utils.

peco8 avatar peco8 commented on May 19, 2024

@tiagopog

Hey, sorry for the late reply.
That works! Thank you so much!! 🎉

Actually, I found that I set up the config.json_key_format = :underscored_key when I used jsonapi-resources. (I forgot doing this because I set up long time ago..)

I think this is worth to mention in README.md for those who want to move from jsonapi-resources. :bowtie:

I'll close this issue! yay!

from jsonapi-utils.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.