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

Support the `accept` option for actions in the json schema #35

Closed zachdaniel closed 3 years ago

zachdaniel commented 4 years ago

We should look at the new accept option for create/update actions, and only expose those fields in the json schema for that route.

transhapHigsn commented 3 years ago

@zachdaniel I'm thinking of picking this up? can you tell me from where to start?

zachdaniel commented 3 years ago

Sure! A recent addition to ash was the ability to say what attributes and relationships will be accepted by an action. https://hexdocs.pm/ash/Ash.Resource.Dsl.Create.html?#create/2

For example, if you had this in a resource:

actions do
  create :hire do
    accept [:first_name, :last_name]
  end
end

You're saying that the only attributes that can be changed are first_name and last_name. This also applies to relationship changes.

To support this, you'll want to update the json_schema, which is used for api validation.

Here https://github.com/ash-project/ash_json_api/blob/master/lib/ash_json_api/json_schema/json_schema.ex#L554 you can find where we say what attributes are allowed for a given action. We want to make sure to only include attributes in that list (as well as the required list) if they are in the accept list on the action (which is the first argument here: https://github.com/ash-project/ash_json_api/blob/master/lib/ash_json_api/json_schema/json_schema.ex#L537. If the accept list is nil, then it should keep its current behavior.

We need to make the same change for relationships, here: https://github.com/ash-project/ash_json_api/blob/master/lib/ash_json_api/json_schema/json_schema.ex#L559

zachdaniel commented 3 years ago

This same thing needs to be done on the patch function below the one I just linked you to.

zachdaniel commented 3 years ago

Also, you'll want to find a good place to test this. The tests aren't in great shape at the moment, so let me know if you can't find a good place to add tests and I'll help you out.

transhapHigsn commented 3 years ago

@zachdaniel Sure, let me go through this once then.

transhapHigsn commented 3 years ago

@zachdaniel Need some help with test cases. Is there some place where I can reach out to you (gitter/slack anything)?

zachdaniel commented 3 years ago

Sure! The discord is the best place, but slack works too: https://www.ash-elixir.org/community

zachdaniel commented 3 years ago

Closed by #40