MartinVolerich

MartinVolerich

Having received great answers to my questions about json_api endpoints, I have just come across a situation where I am requesting “included” resources using the Swagger / OpenAPI UI, which will respond with the public attributes or any “default_fields” set in the related resource’s json_api block.

But… when I also add a “default_fields” line to the parent resource (in the domain’s json_api base_routes, I find that I’m correctly receiving those default_fields, but… it has the unexpected/unwanted side effect of no longer returning the public fields (or default_fields) of the related included resource.

Is this a bug? Or is there something else I can or should configure to get this to work?

Thanks
Martin

Showing Posts 1 to 6

zachdaniel

zachdaniel

Creator of Ash

This sounds like a bug. Please work up a reproduction or failing test and submit an issue :slight_smile:

MartinVolerich

MartinVolerich OP

Will do. What are the guidelines and best practices for doing that on this project (and I assume that this is scoped to AshJsonApi)?

Also I was stepping through the code yesterday (using pry) to see what is taking place and am happy to continue digging into this further to get a sense of what needs to be changed. Would that be helpful?

Martin

zachdaniel

zachdaniel

Creator of Ash

Ideally a test in a relevant test file, there is a test somewhere for inclusion of related resources IIRC. Barring that, a separate repo with a minimal reproduction.

As for this issue, it sounds like there is some kind of mishandling of fields somewhere, like we’re passing it down when rendering related resources, or we’re using the source resource when determining default fields instead of the destination resource. My instinct is a problem somewhere in the serializer.

MartinVolerich

MartinVolerich OP

I found where the problem lies (for this case).

The situation is that I am including a related resource (in this case “Product”) in the GET and relying on the public of default_fields set for that resource.

If I set the default_fields for the main “base” resource (in my case “Survey”) in the base_route, then the included resource isn’t queried for its fields.

However if I set the default_fields for the main “base” resource in the resource module itself (inside the json_api block), then the other related resources is asked for it’s fields.

The problem is due to the logic at line 675 of the AshJsonApi.Serializer where fields is determined to be one of three lists.

fields =
Map.get(request.fields, resource) || Map.get(request.route, :default_fields) ||
default_attributes(resource)

If I manually swap the second and third parts then the default_fields on the route itself isn’t used when there are default fields set for the resource. And of course default_attributes has logic that will look to the explicit default_fields or the public attributes.

So a workaround is to just set the fields to be used as defaults in the resource and not in the route (in this case), but I’d be interested to hear what approach makes most sense.

FWIW, I’ll also post this in the issues list for the project.

Thanks
Martin

zachdaniel

zachdaniel

Creator of Ash

Ah, right you are. This is a simple fix, which is that we should only be using the default fields from the route for the top level resource, not for included resources.

zachdaniel

zachdaniel

Creator of Ash

great sleuthing! Thank you for finding this :bowing_man:

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews