Comments (8)
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:
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.
It should be working fine for those versions you're using. I will try to take a deeper look later.
from jsonapi-utils.
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.
Tip:
if @result.errors.blank?
jsonapi_render json: @result, status: :created
else
jsonapi_render_errors json: @result, status: :unprocessable_entity
end
:-)
from jsonapi-utils.
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.
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.
I'm also seeing this and I'm using jsonapi-utils 0.5.1 with jsonapi-resources 0.8.3
from jsonapi-utils.
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.
I'll close this issue! yay!
from jsonapi-utils.
Related Issues (20)
- Trying to override pagination...
- Decimals formatted as strings in json response
- Update jsonapi-resources 0.9.12 HOT 10
- Bump the rails version to 6.1 for tagged version 7.13.0 HOT 1
- Release a new version? HOT 1
- Support for operation processors? HOT 3
- Rendering scopes with `includes` is slow for no reason HOT 2
- Passing resource relationship in POST request HOT 1
- resource_params => NoMethodError: undefined method `keys' for nil:NilClass HOT 2
- Render multiple invalid AR objects
- Bring support to fast_jsonapi HOT 9
- Use the cache with jsonapi_render. HOT 1
- Getting Invalid resource for custom resource response on patch request
- Non-REST actions?
- JSONAPI::Utils::Exceptions::BadRequest undefined
- Relax jsonapi-resources dependency HOT 3
- Support for sorting and pagination from JR
- jsonapi/utils.rb:9:in `include': wrong number of arguments (given 0, expected 1+) (ArgumentError)
- Formatting of error's "detail" attribute
- Accept and Content-Type headers are not verified
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonapi-utils.