AE
Ash Elixirโ€ข2y ago
skander

Calculations in AshJsonApi?

Just want to make sure I'm not missing anything obvious - is it possible to include a calculation in a JsonApi request? Including one in the includes list seems to imply it's only looking for resources:
No description
10 Replies
skander
skanderOPโ€ข2y ago
Oh! Looks like it works when adding it to default_fields. Though I also need to somehow include a calculation from a relationship. Please let me know if anyone has any insights there. Ok, seems to just work if the default read on the relationship also loads the calculation in a preparation.
ZachDaniel
ZachDanielโ€ข2y ago
Hmm... That is definitely a bug can you open an issue on ash_json_api oh, you don't use includes for that you use fields we should be validating that though v https://jsonapi.org/format/#fetching-sparse-fieldsets you'd have to include all the fields you want in addition to the calculation
skander
skanderOPโ€ข2y ago
Sadly getting an error with the fields= param
curl -s -H "Content-Type: application/vnd.api+json" -H "Accept: application/vnd.api+json" 'http://localhost:4000/api/json/locations?term=bar&fields=name,address_1,grouped_weekly_schedule'
#=> {"errors":[{"code":"InvalidField","detail":"Invalid field for type locations:","id":"31401fd8-ff94-4067-9aa4-4ef7a6c697f7","source":{"parameter":"fields"},"status":"400","title":"Invalid Field"}],"jsonapi":{"version":"1.0"}}
curl -s -H "Content-Type: application/vnd.api+json" -H "Accept: application/vnd.api+json" 'http://localhost:4000/api/json/locations?term=bar&fields=name,address_1,grouped_weekly_schedule'
#=> {"errors":[{"code":"InvalidField","detail":"Invalid field for type locations:","id":"31401fd8-ff94-4067-9aa4-4ef7a6c697f7","source":{"parameter":"fields"},"status":"400","title":"Invalid Field"}],"jsonapi":{"version":"1.0"}}
I can make an issue after finishing up some things I need to get out
ZachDaniel
ZachDanielโ€ข2y ago
fields[type]=... You have to specify which type of thing you're setting the fields for i.e /foos?fields[foo]=foo,bar,baz
skander
skanderOPโ€ข2y ago
Ah got it. Still coming up with an InvalidField code with field[type]=attr1,calculated format. Even tried with a simple concat-based calculation with the same result. I can try reproing in a simplified test later.
ZachDaniel
ZachDanielโ€ข2y ago
๐Ÿค” that would be great ๐Ÿ˜„
skander
skanderOPโ€ข2y ago
1) test attributes calculated fields are rendered properly (Test.Acceptance.GetTest)
test/acceptance/get_test.exs:152
match (=) failed
code: assert %{"data" => %{"attributes" => %{^attribute => ^expected_value}}} = conn.resp_body
left: %{"data" => %{"attributes" => %{^attribute => ^expected_value}}}
right: %{
"errors" => [%{"code" => "InvalidField", "detail" => "Invalid field for type post:", "id" => "9ad15d14-d5bf-434b-ae0b-16b0a1038785", "source" => %{"parameter" => "fields[post]"}, "status" => "400", "title" => "Invalid Field"}],
"jsonapi" => %{"version" => "1.0"}
}
stacktrace:
test/acceptance/get_test.exs:155: (test)
1) test attributes calculated fields are rendered properly (Test.Acceptance.GetTest)
test/acceptance/get_test.exs:152
match (=) failed
code: assert %{"data" => %{"attributes" => %{^attribute => ^expected_value}}} = conn.resp_body
left: %{"data" => %{"attributes" => %{^attribute => ^expected_value}}}
right: %{
"errors" => [%{"code" => "InvalidField", "detail" => "Invalid field for type post:", "id" => "9ad15d14-d5bf-434b-ae0b-16b0a1038785", "source" => %{"parameter" => "fields[post]"}, "status" => "400", "title" => "Invalid Field"}],
"jsonapi" => %{"version" => "1.0"}
}
stacktrace:
test/acceptance/get_test.exs:155: (test)
ZachDaniel
ZachDanielโ€ข2y ago
๐Ÿ™‡โ€โ™‚๏ธ thanks! Hey, could you open this as a PR?
skander
skanderOPโ€ข2y ago
Sure, done! No rush of course. I might try fixing it myself in the same PR. I need to also show public calculated fields in the OpenAPI schema too

Did you find this page helpful?