Giter VIP home page Giter VIP logo

Comments (3)

lucas-nelson avatar lucas-nelson commented on June 30, 2024 1

I'm running into a semi-related problem. The JaSerializer.Builder.ResourceIdentifier.build function is causing the data key on a %JaSerializer.Builder.Relationship{} to be set to :empty_relationship.

The JaSerializer.Builder.Relationship.build function has an empty? function that operates on that struct and seems to be missing handling for this. If I add: defp empty?(%__MODULE__{data: :empty_relationship, links: nil, meta: nil}), do: true to JaSerializer.Builder.Relationship, the relationship (correctly IMO) does not appear in the output.

Perhaps the same change would fix this issue?

from ja_serializer.

beerlington avatar beerlington commented on June 30, 2024

Hey sorry for the delay on this. Yes you are right, this does seem broken. Looking at the spec, it seems like the correct response would be %{"data" => nil }. I'm surprised I haven't run into this before so I'll see if I can reproduce it. Stay tuned.

from ja_serializer.

beerlington avatar beerlington commented on June 30, 2024

@lucas-nelson Looking into how we handle empty relationships elsewhere, I don't believe stripping out the relationship is always the right approach. In the case of a link where the parent ID is used, I believe we still want to return the links because they are valid.

For example, if we had this:

  defmodule ArticleSerializer do
    use JaSerializer

    has_one(
      :author,
      link: "/articles/:id/author",
      serializer: PersonSerializer,
    )
  end

We would still want it serialized since the link could technically still be valid:

    "relationships": {
      "author": {
        "links": {
          "related": "/articles/1/author"
        },
        "data": null
      },

Instead what I'm going to do is remove any broken links from the links array when it's serialized. This seems to satisfy the JSON API spec where it says:

If present, a related resource link MUST reference a valid URL, even if the relationship isn’t currently associated with any target resources. Additionally, a related resource link MUST NOT change because its relationship’s content changes.

So then in @mhanberg 's original example, the response would just be %{"data" => nil}

from ja_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.