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

[Question/Support] Association metadata #497

Open
1 task done
alexcastrodev opened this issue Jan 10, 2025 · 2 comments
Open
1 task done

[Question/Support] Association metadata #497

alexcastrodev opened this issue Jan 10, 2025 · 2 comments
Assignees
Labels
enhancement New feature or request question Further information is requested

Comments

@alexcastrodev
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe your question or ask for support

Hi,

I am trying to include both data and meta objects for an association, similar to how it's done for the main serializer output using .render.

For example, I can use the following code to render categories with a meta object:

ItemCategorySerializer.render(categories, root: :data, meta: { count: categories.size })

This works perfectly. However, I need to achieve a similar structure for associations within the serializer. Specifically, I want the association to include both a data and meta object. Here’s what I’ve tried so far:

Attempt 1

I attempted to include meta directly in the association:

association :descendants, blueprint: ItemCategorySerializer, name: :subcategories do |item_category|
  ItemCategorySerializer.render(item_category.descendants, meta: { counter: item_category.descendants.count })
end

Attempt 2

I tried to manually structure data and meta:

association :descendants, blueprint: ItemCategorySerializer, name: :subcategories do |item_category|
  { data: item_category.descendants, meta: { counter: item_category.descendants.count } }
end

Attempt 3

I considered using a field for meta but found it awkward and inconsistent with the expected output structure.

None of these approaches work as intended. The meta field doesn't appear in the rendered JSON for the association, and it doesn't behave like the example in the JSON:API Serializer documentation.

Expected Behavior

For an association, I would like the output to include both data and meta fields, structured as follows:

{
  "subcategories": {
    "data": [ /* array of serialized descendants */ ],
    "meta": { "counter": 5 }
  }
}

Actual Behavior

The meta object is not included in the rendered association output. Instead, only the data field is rendered.

Questions

Is this feature not supported for associations?

Is there a workaround to achieve this structure for associations?

Thank you in advance

@alexcastrodev alexcastrodev added the question Further information is requested label Jan 10, 2025
@lessthanjacob
Copy link
Contributor

Hey @alexcastrodev! Thanks for reaching out!

At the moment, meta data is only available at the root-level of the payload, and any workaround would likely be too cumbersome to reasonably work with.

We'll take a look into this and see if it's something that'd be reasonably straightforward to implement, though.

@lessthanjacob lessthanjacob added the enhancement New feature or request label Jan 21, 2025
@lessthanjacob lessthanjacob self-assigned this Jan 21, 2025
@alexcastrodev
Copy link
Author

Hey @alexcastrodev! Thanks for reaching out!

At the moment, meta data is only available at the root-level of the payload, and any workaround would likely be too cumbersome to reasonably work with.

We'll take a look into this and see if it's something that'd be reasonably straightforward to implement, though.

Thank you for the response. I’m working on some proof of concepts to achieve a flatter response that doesn’t require the JSON:API specification.

One of the points I was questioning was about the data and meta in associations, but most models I’ve seen don’t follow this approach.

Anyway, I’ll keep testing the gem. Thanks again, and if you’d like, you can close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants