ash-project / ash_json_api

The JSON:API extension for the Ash Framework
https://hexdocs.pm/ash_json_api
MIT License
56 stars 40 forks source link

Add test to show post requests don't return calculations #96

Closed Rodeoclash closed 1 year ago

Rodeoclash commented 1 year ago

This adds a test that now fails due to the lack of calculations being returned after a post/create request.

I've included default_fields in an attempt to get this to return the field, in my main branch this is returning #Ash.NotLoaded<:calculation> which means that the field is being selected for serialisation, however, in this repo I can't seem to trigger the same behaviour. Instead, I'm getting no field back at all. I've tried upgrading the version of the underlying Ash framework to match my code but it doesn't seem to make a difference.

Either way, I'd distil this down to the following: Calculated fields are not returned when post requests are completed

This calculation works fine for a read requests by using the preparations macro or by using a prepare under the actions as expected.

Ideally, I don't want the consumer of my API to have to make a POST to create the record then do a read to get the calculations on it but I can live with that if it's a won't/can't fix.

The response coming back after trying to add the calculated field:

  resp_body: %{
    "data" => %{
      "attributes" => %{
        "email" => nil,
        "hidden" => "hidden",
        "name" => "Post 1"
      },
      "id" => "13930440-a1aa-4699-bfb0-7673322dd2ef",
      "links" => %{},
      "meta" => %{},
      "relationships" => %{"author" => %{"links" => %{}, "meta" => %{}}},
      "type" => "post"
    },
    "jsonapi" => %{"version" => "1.0"},
    "links" => %{"self" => "http://www.example.com/posts"}
  },
zachdaniel commented 1 year ago

🚀 Thank you for your contribution! 🚀

Rodeoclash commented 1 year ago

Ahh, this didn't fix it, I just added a test case which fails (and would be fixed calculations being run after a create action).

I wasn't sure how to fix, I just wanted to show the behaviour. Merging this will have broken main

zachdaniel commented 1 year ago

Yep! I merged this and then fixed main.