How to add additional fields to an open_api response

I’m loving Ash and AshJsonApi and have created a “read” api that allows a user to login using the password strategy (from AshAuthentication).

My question / unknown is how to take values from the resource metadata and return it as part of the OpenApi(/Swagger) response - similar to what the AshAuthentication.Plug handle_success callback is doing?

How can I craft & add additional fields to the response that is being returned?

Thanks
Martin

FWIW, I tried using a calculation to bring up the metadata.token to the main resource, which works well using the code_gen functions in the REPL, but still not changing the response in the OpenAPI / Swagger page

Have you added metadata to your read action definition?

Thanks. Yes I did add that read.metadata line.

You’ll need to configure your JSON API route to include the metadata with the response, eg.

        post :sign_in_with_password do
          route "/password/sign_in"

          metadata fn _subject, user, _request ->
            %{token: user.__metadata__.token}
          end
        end

https://hexdocs.pm/ash_json_api/dsl-ashjsonapi-domain.html#json_api-routes-base_route-index-metadata

Wonderful. That makes a ton of sense. I’d used the metadata macro on the action but not the route. Thanks a million.

(and once again your response made me realize that I need to read the documentation better - and know where to look.)

Martin

Also, when is your book going to be available? Is it the one that Zach mentioned is coming on PragProg? I keep looking there for a beta announcement and cannot wait to get a copy.

Martin

Sooooon hopefully! Yes, that’s the one - hopefully we’ll have some news about it in the next few weeks!

1 Like