Giter Club home page Giter Club logo

Comments (7)

jeffblake avatar jeffblake commented on August 22, 2024 1

@yosiat First of all, I've been playing with the library for a few hours and I'm really liking it so far. It takes all of the bloat and bad performance out of AMS, and keeps the good bits! In particular, I like that Panko does not hack itself into render json:.

In order for me to transition off AMS I need to be able to specify only and excludes of nested relationship attributes.

  1. I'd love to see a global config option to only serialize one level deep. This will prevent infinite loops, improve performance, and get me 90% of the way towards getting off AMS.
  2. As a workaround I've tried playing with the only and excludes options to try and mimic 1 level of serialization, but I'm getting hung up on specifying attributes and associations of nested relationships. For example, I've tried both:
    def self.filters_for(context, scope)
      {
        only: [:id, :name, { venue: [:id, :name]}]
      }
    end

and

    def self.filters_for(context, scope)
      {
        only: {
          instance: [:id, :name],
          venue: { instance: [:id, :name] }
        }
      }
    end

And a few other variations but I can never get the attributes/relationships of venue to filter.

I'll always get back:

        {
            "id": 80714,
            "name": "Stockholm 03.05.20",

        },
        {
            "id": 80955,
            "name": "StriveX"
        },

Where I want to see:

        {
            "id": 80714,
            "name": "Stockholm 03.05.20",
            "venue": {
              "id": 2,
              "name": "Theater"
            }
        }

I have to do this because venue has other associations (like address) that I don't want to render unless it's explicitly requested (goes back to the nested rendering issue).

The way AMS works now is I can pass in a string such as

venue,registration_types.prices and get back the venue association, plus a nested association of registration_types --> prices
All other associations are omitted because the user explicitly requested certain ones.

This is how AMS parses the string: https://github.com/jsonapi-rb/jsonapi-renderer/blob/master/lib/jsonapi/include_directive.rb

And details on how it works: https://github.com/rails-api/active_model_serializers/blob/v0.10.6/docs/general/adapters.md#include-option

AMS splits the attributes and relationships into two separate options: fields and include. I like your DSL of combining it into one option, I just need to write a converter.

Thanks!

from panko_serializer.

jeffblake avatar jeffblake commented on August 22, 2024 1

I've also tried passing it into the ArraySerializer as such:

fields = { instance: [:id, :name, :start, :end],  registration_types: { instance: [:id, :name], prices: { instance: [:currency, :amount] }  }

Panko::ArraySerializer.new(collection, each_serializer: serializer, only: fields)

The subject in question is an Event (date, time, venue, etc)

I would expect this to only include the registration_types association, with the prices nested association, however I will also get back all other associations of the root object like venue, etc.

In other words, if I pass in a only option, I would expect that each attribute and association has to be whitelisted / present in that hash, in order to be rendered.

And if the only and exclude option is not present, it would default to rendering all of the attributes of the root object, and all of the attributes of the first level of associations.

Thoughts?

from panko_serializer.

yosiat avatar yosiat commented on August 22, 2024

@railsfactory-selva do you have any example why you would like to set filtering globally for all serializers? and can you point me to AMS example for setting it globally?

from panko_serializer.

railsfactory-selva avatar railsfactory-selva commented on August 22, 2024

@yosiat We would like to have one-level serializer by default, and include nesting just whenever required as like AMS (or) there should be option to change it to 'one level' globally which would be helpful for many people.

In AMS, by default it's one-level and we can change it to deep nesting globally, by adding below code at initializers
ActiveModelSerializers.config.default_includes = '**'

Ref: https://stackoverflow.com/questions/32079897/serializing-deeply-nested-associations-with-active-model-serializers

from panko_serializer.

jeffblake avatar jeffblake commented on August 22, 2024

This would also help me out a lot in trying to migrate off of AMS

from panko_serializer.

esnrRahman avatar esnrRahman commented on August 22, 2024

Hello @yosiat ! Thanks so much for this gem. Definitely a better alternative to AMS and keeping it minimalistic allows developers to customize its serializer a lot.

Hello @jeffblake ! Thank you so much for posting this. I have been tearing my hair out with this issue. So definitely +1 this problem.

But the funny thing is I actually have two serializers. One works and the other doesn't. And the nature of both these serializers have been written in a similar format.

DOESNT WORK =>

Screen Shot 2020-03-08 at 10 35 46 PM

NOTE: Customer model is inherited from base User model. I am not sure whether that makes a difference.

NOTE 2: So sorry for the screenshot. I cant seem to code it out using the tildas.

The other one that works is of similar nature. Has a serializer that references another one. And in the class method, the association is filtered.

Apologies beforehand if the code snippet is not descriptive enough. Please let me know if anything is vague and will try to clarify! Thanks beforehand!

from panko_serializer.

esnrRahman avatar esnrRahman commented on August 22, 2024

Hey @jeffblake ! I was able to figure this out. This has been written somewhere (in fine print :P) in the docs. But if you try the following, it should work =>

 def self.filters_for(context, scope)
 {
      only: {
        instance: [:id, :name, :venue], # <= add venue here
        venue: { instance: [:id, :name] }
      }
}
end

As per docs written in the Associations sections -

Let's dissect only option we passed -

instance - list of attributes (and associations) we want to serializer for current instance of the serializer, in this case - PostSerializer.
author, comments - here we specify the list of attributes we want to serialize for each association.

from panko_serializer.

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.