Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Kevin Coleman
    @KevinColemanInc
    premptive thanks! to anyone who responds :)
    Edwin Liang
    @EdwinLiang_twitter
    @KevinColemanInc Are you trying to return the same record back after it was created?
    2 replies
    BilalAddam
    @BilalMAddam
    Hello, I'm having some difficulties performing multiple POST requests. When the database is empty, the first post request works just fine.However, When attempting to run many (database is not empty), I get <Too many primary objects for show excluded from capture: DSN not set Internal Server Error: Too many primary objects for show>. This exception is raised at line 62 in jsonapi/resource_serializer.rb. In fact, I tried to debug the code, one attempt was to comment the fail exception (awful I know :p). It seems to work (of course, the returned value is the first entry in the database which is not in sync with the one being created by the POST request). I would really like to know if I'm missing something or doing anything wrong. Thx in advance
    Larry Gebhardt
    @lgebhardt
    @BilalMAddam are you doing anything "special" in the controller code for these post requests?
    BilalAddam
    @BilalMAddam
    nothing special
    @lgebhardt the thing is, resource_set (argument for the function that raises this exception at line 62) contains all the table's rows (all entries). I've verified it by inspecting it. That's why, the function throws systematically this error if the len(entries) > =1.
    Larry Gebhardt
    @lgebhardt
    After a create call it should just be returning the recently created resource. Which version are you using?
    BilalAddam
    @BilalMAddam
    @lgebhardt <gem 'jsonapi-resources', '=0.10.2'>
    Larry Gebhardt
    @lgebhardt
    @BilalMAddam Somehow you're getting multiple models in the resource_set. After a create there's a find_resource_set call that uses the new id as the value for a filter on the primary key. Is it possible you added some custom logic to the id filter?
    BilalAddam
    @BilalMAddam
    @lgebhardt Indeed, that was the bug, I've overriden the apply_filter function and added some custom logic to it. Thank you very much for your support, you saved me a lot of time debugging it!
    Eric Gross
    @ericgross
    I am trying to upgrade from Rails 5 to Rails 6 and it is going well but it seems that there are no routes anymore from jsonapi-resources - is that a known issue?
    Larry Gebhardt
    @lgebhardt
    @ericgross the routes should still be working. Can you provide a bit more information? Which JR version? Anything special about your routes or setup?
    Eric Gross
    @ericgross

    @ericgross the routes should still be working. Can you provide a bit more information? Which JR version? Anything special about your routes or setup?

    @lgebhardt I am running JR 0.9.11 with Rails 6.0.3.2. I'm not doing anything special - I've got an api namespace and a v1 namespace inside that with a jsonapi_resource and some nested jsonapi_resources. The only route that doesn't seem to be working is jsonapi_resources :events, only: %i[create]and bundle exec rake routes | grep api_v1_events is empty

    Eric Gross
    @ericgross
    @lgebhardt do you have any more ideas? Not sure what to try next.
    Eric Gross
    @ericgross
    In general, how can i dig in to find out why a specified resources route isn't being added to the routing table?
    Eric Gross
    @ericgross
    The problem was that the resource was immutable!
    ahmadabdelhalim
    @ahmadabdelhalim
    Hey, guys!
    I'm trying to use json schema to validate the response. The problem I'm facing is that jsonapi-resource returns the following
    "id": "3", "type": "drivers", "links": { "self": "http://localhost:3000/api/admin/v1/drivers/3" },
    and links here is an object, so when I add that to json schema, I get the following error:
    JsonSchema::AggregateError:
       #: "object" is not a valid "links", must be a array.
    Arthur Alkmim
    @rockertux
    Good morning, everyone! Is it possible to strip the relationships and links fields from the serialized response in 0.9 or 0.10?
    I see there's a PR for that from 2016 which is not merged and from what I've read in the docs I can't see if it is possible or not. I couldn't find anything useful by skimming through the source code but I might be missing something
    Julius-Rapp
    @Julius-Rapp
    @rockertux do you know exclude_links from https://jsonapi-resources.com/v0.10/guide/resources.html#Options ?
    @rockertux relationships should be missing by default if the resource does not have any relationships defined. Alternatively, the client can use sparse fieldsets. Does that help?
    Arthur Alkmim
    @rockertux
    Hm, we do have some relationships defined but we're using them to access child fields
    I think I missed this exclude_links, thanks for pointing that!
    Arthur Alkmim
    @rockertux
    @Julius-Rapp thanks, I think I got riiight on track with that
    ManhND
    @manhnd.hebestorm_gitlab
    Hi everyone, today I added paranoia to soft-deleted, but when I tried to get index with scope with_deleted, every fields were blank
    John Malone
    @Pro777
    Has anyone figured out how to fix N+1 queries that the bullet gem recognizes in jsonapi-resource 0.10?
    John Malone
    @Pro777
    Looks like the last comment on this still works: cerebris/jsonapi-resources#634
    prasannakumar1989
    @prasannakumar1989
    Thanks for this wonderful framework. I m trying to use relationship serializer serialize_to_relationship_hash(source, requested_relationships), Can someone say what should be passed as a value requested_relationships ?
    Tobias Grasse
    @tobias_grasse_twitter
    @prasannakumar1989 Are you using 0.10.x? Looking at the code (https://github.com/cerebris/jsonapi-resources/blob/master/lib/jsonapi/resource_serializer.rb#L96), I'd say an initialized JSONAPI::Relationship class, either ToOne or ToMany.
    How you instantiate them is a different thing 😊 As I understand it, this method only serializes the given relationship, however
    prasannakumar1989
    @prasannakumar1989

    Thank you @tobias_grasse_twitter . I am evaluating 0.10 , but using 0.9

    Another question. I have a situation where the attribute name is conflicting with has_many relationship say

    attribute :tags
    
    has_many :tags

    the PATCH request seems to be in this format

    {"data":
        {    "type":"tasks",
            "id":"6",
    
            "attributes":{
                "title": "Title",
                "tags": ["Home"]
            }
        }
    }

    and expected output is in this

    {
       "data":{
          "id":"2",
          "type":"tasks",
          "attributes":{
             "title":"Updated Task Title"
          },
          "relationships":{
             "tags":{
                "data":[
                   {
                      "id":"2",
                      "type":"tags"
                   },
                   {
                      "id":"3",
                      "type":"tags"
                   }
                ]
             }
          }
       }
    }
    "included" :{ ..stuff ...}
    Thanks again for the help.
    Tobias Grasse
    @tobias_grasse_twitter
    @prasannakumar1989 Could you please share the actual output? Error or sth? I guess it's a general Rails problem because associations and attributes are both accessed as Model.methodname.
    prasannakumar1989
    @prasannakumar1989

    @tobias_grasse_twitter , I got this error

    ArgumentError: wrong number of arguments (given 1, expected 0)
    
      0) Api::V1::TaskResource 
         Failure/Error:
           it { puts JSONAPI::ResourceSerializer
                         .new(described_class)
                         .serialize_to_hash(described_class.new(sample, nil))
           }
    
         ArgumentError:
           wrong number of arguments (given 1, expected 0)
         # ./spec/resources/api/v1/task_resource_spec.rb:15:in `block (2 levels) in <top (required)>'

    My resource class definition is

    class Api::V1::TaskResource < JSONAPI::Resource
      attributes :title, :tags
    
      has_many :tags, always_include_linkage_data: true
    
    
      def tags=(tag_titles)
        @model.replace_tags!(tag_titles)
      end
    
      def self.creatable_fields(context = nil)
        super - [:tags]
      end
    
    end
    Scott González
    @scottgonzalez
    @prasannakumar1989 That’s a violation of JSON API. “...a resource can not have an attribute and relationship with the same name…” https://jsonapi.org/format/#document-resource-object-fields
    Tobias Grasse
    @tobias_grasse_twitter
    @prasannakumar1989 Ok, I meant the error message you get for your second question re: attribute and relationship with the same name. Thanks to @scottgonzalez for pointing out that in addition to probably cause conflicts with Rails' conventions, it also violates the JSON:API spec 😄 The error you shared just states that you need to pass a JSONAPI::Relationship::{ToOne,ToMany} instance to serialize_to_hash, see :point_up: 17. September 2020 15:43
    @prasannakumar1989 To be clear, the second parameter requested_relationship is only present in jsonapi-resources 0.10.x . The 0.9.x signature only requires a single TaskResource like your failing RSpec does. Maybe you mixed up the versions 😊
    I'd suggest you check out the method signature in both branches.
    John Malone
    @Pro777
    So I am working on a legacy app. We are using this gem to stand up a new public API and it is working wonderfully. I am curious if there is a way for us to leverage the jsonapi-resource classes in a new rails+erb controller.
    Tobias Grasse
    @tobias_grasse_twitter
    @Pro777 Looking at JSONAPI::Resource.initialize, I'd guess you can pass the model and an optional context (https://jsonapi-resources.com/v0.10/guide/resources.html#Context). Question is why you want to use Resource classes in such an controller?
    John Malone
    @Pro777
    @tobias_grasse_twitter The legacy app utilizes a lot of kruft that our new public API does not. By using the jsonapi-resource resource objects in the new vanilla rails frontend, we can just use the new policy objects we have created for our jsonapi.
    Dmitriy "Dima" Likhten
    @dlikhten

    Hi! I'm still on 0.9 and I was wondering if anyone can help out with this:

    I need to filter a relationship in my resource based on the context. For example:

    a person has many friends
    if I want to include person.friends.friends as an example I want to see the friends a person has and the friends they have that are mutual. Because this doesn't make sense in activerecord (this is request-specifc, not a special relationship) I would really need to make the scope records.friends.friends.where(is_friend: context[:person_id) (ignore how I build the actual where clause).

    Is this possible in 0.9?

    An alternative way to look at this:

    my PersonResource is scoped to only allow returning people who are self, or friends of whoever is in context[:person_id]. If I do include=friends.friends which is a PersonResource, the response is an error because one of the friends is not a mutual friend and thus the id is not found in the scope (and I don't want that record returned either).

    Dmitriy "Dima" Likhten
    @dlikhten

    To add a bit of context:

    Crash is in preload_included_fragments

    I assumed def records_for(association_name) would be where I can inject myself and make the necessary scoping... however that method is never invoked by the time the crash happens.

    The crash being Internal Server Error: key not found: 5735 (jsonapi/resource.rb:1354) because that key is out of scope for my resource.

    Dmitriy "Dima" Likhten
    @dlikhten
    Wondering if 0.10 solved this specific problem (I know there was a major re-do of the way relationships are queried)
    Josh Kramer
    @surjay

    Is there any way to customize relationship links? I know about the "custom_links" method/override for top-level resource links but I'm talking about the links within a relationship object. Specifically around turning a link into a JSON:API "link object". I'd like to be able to return a meta object along with the link href for things like counts. Something like this:

    "relationships": {
      "questions": {
        "links": {
          "related": {
            "href": "http://whatever.com/questions",
            "meta": {
              "count": 10
            }
          }
        },
        "data": []
      }
    }
    That or a better way to indicate counts for relationships as I've yet to find a good concensus on an approach.
    Tommy Russoniello
    @tommy-russoniello
    Josh Kramer
    @surjay

    Thanks, that might work. On a similar note, the JSON:API spec also suggests that the "relationship object" can also contain a meta object alongside "links" and "data". So something like this:

    "relationships": {
      "questions": {
        "links": {
          "related": "http://whatever.com/questions"
        },
        "data": [...],
        "meta": {
          "count": 10
        }
      }
    }

    Looking through the code and I don't see any easy way to populate/return meta at that level?

    Dmitriy "Dima" Likhten
    @dlikhten
    Idk if anyone experienced what I wrote above. Any ideas would be super helpful. Especially if someone already knows if 0.10 is the solution.