Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

block format for JSONAPI::Deserializable::Resource #18

Open
fredmaremyob opened this issue Oct 27, 2017 · 9 comments
Open

block format for JSONAPI::Deserializable::Resource #18

fredmaremyob opened this issue Oct 27, 2017 · 9 comments

Comments

@fredmaremyob
Copy link

Hi

I am trying to use DeserializableResourceType.call() but receiving a {} return value.

I am not sure what the format for the blocks (type, attributes, relationship and id ) in the JSONAPI::Deserializable::Resource would be.

Thank you very much

@hectorsq
Copy link

Fred,

I am having the same problem and have been struggling with it for hours.

In Rails (4.2.9) the received params hash has a top level :_jsonapi key and then the expected JSON:API :data key. The :_jsonapi key appears when the request's Content Type is application/vnd.api+json.

After some debugging I found that it works if if you call the deserializer as follows

DeserializableResourceType.call(params[:_jsonapi][:data])

This is a deserializer definition which gets all the received attributes

class DeserializableObservation < JSONAPI::Deserializable::Resource
  type
  id
  attributes
end

You can whitelist the attributes too.

class DeserializableObservation < JSONAPI::Deserializable::Resource
  type
  id
  attribute :code
  attribute :name
end

You can get an idea of the DSL syntax here https://github.com/jsonapi-rb/jsonapi-deserializable/tree/master/spec/resource/DSL

I tried with no luck to use deserializable_resource :observation

Regards,
Héctor

@fredmaremyob
Copy link
Author

Thank you for the advice Héctor!

@beauby
Copy link
Member

beauby commented Nov 1, 2017

This clearly needs proper docs, I agree.

In general, the idea is that one defines a custom deserializable resource as @hectorsq explained, using the DSL.
In jsonapi-rails, there is a default deserializable resource that whitelists everything, and it is used when calling deserializable_resource :foo without further options.

@hectorsq could you expand on the following?

I tried with no luck to use deserializable_resource :observation

Also, the lib is currently tested against Rails 5 only, but I'd be happy to merge a PR to test it against more versions of rails.

@hectorsq
Copy link

hectorsq commented Nov 1, 2017

@beauby

When I add deserializable_resource :observationthe following is added to params

  "observation" => {
    "type" => nil
  }

One thing to notice is that the payload in the params hash is contained under a _jsonapi key

Here is a fragment:

{
  "_jsonapi" => {
    "data" => {
      "id" => "3B2F0580C0E84218151E8F94F2622196",
      "attributes" => {
        "code" => "apellido_paterno",
        "name" => "Apellido paterno",
        "loinc" => nil,
        "value" => "ACEVEDO"
      },
      "type" => "observations"
    }
  },
  "controller" => "medical_record/api/v1/observations",
  "action" => "update",
  "id" => "3B2F0580C0E84218151E8F94F2622196",
  "observation" => {
    "type" => nil
  }
}

@hectorsq
Copy link

hectorsq commented Nov 1, 2017

@beauby

I've done some research and noticed that the _jsonapi key is required.

I debugged my controller and the code inside the before_action block in deserializable_resource method is not executed. I will investigate if this is related to the Rails version.

@beauby
Copy link
Member

beauby commented Nov 2, 2017

@hectorsq: Thinking about your issue, it is really weird that you do get an :observation param added at all if this line is not being executed as you mentioned. 🤔

@aamir-pk
Copy link

Anyone cal tell me the best alternative to jsonapi-rails as it clearly fails when it comes to deserialization. Serialization is also not production ready, it cannot properly serialize errors (it misses source key)?

Thanks

@beauby
Copy link
Member

beauby commented Nov 29, 2017

Anyone cal tell me the best alternative to jsonapi-rails

Valid alternatives, with their own issues: jsonapi-serializers, jsonapi-resources.

as it clearly fails when it comes to deserialization.

Nothing is less clear. Would you mind elaborating?

Serialization is also not production ready, it cannot properly serialize errors (it misses source key)?

It is, it can and it doesn't.

Thanks

You're welcome.

@lemattma
Copy link

@beauby

I'm also getting type => nil from the deserializer process.

I think the reason might be that the jsonapi parser calls .with_indifferent_access, so inside deserializable_resource you can access for instance controller.params[:_jsonapi][:data], however, after calling controller.params[:_jsonapi].to_unsafe_hash, you seem to lose the ability to use symbols to access keys, so below in that method, when you do resource = klass.new(hash[:data]), hash[:data] is nil but you get the correct data if you do hash['data'].

Could you please tell me if I'm missing anything here?
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants