Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing Field Name in Error Response When Setting Up AshJsonApi with Generic Actions #191

Open
diogomrts opened this issue Jul 2, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@diogomrts
Copy link
Contributor

Describe the bug
When I try to setup AshJsonApi with a generic action, if there are required arguments it returns an error but without the field name:

image

To Reproduce
With a simple Ash.Resource and a route like this:

defmodule App.Core.Test do
  use Ash.Resource,
    domain: App.Core,
    extensions: [
      AshJsonApi.Resource,
    ]

  json_api do
    type "test"

    routes do
      base("/test")

      route(:get, "/:id/data", :read_data)
    end
  end

  resource do
    require_primary_key? false
  end

  attributes do
    attribute :name, :string, public?: true
  end

  actions do
    action :read_data, :struct do
      argument :id, :uuid, allow_nil?: false
      argument :name, :string, allow_nil?: false

      run fn input, context ->
        input.arguments.name
      end
    end
  end
end

Expected behavior
The field seems to be missing because the error structure is like this:

[(ash_json_api 1.3.1) lib/ash_json_api/error/error.ex:279: AshJsonApi.ToJsonApiError.Ash.Error.Changes.Required.to_json_api_error/1]
error #=> %Ash.Error.Changes.Required{
  field: :campaign_id,
  type: :argument,
  resource: Calleebree.Core.Widget,
  splode: Ash.Error,
  bread_crumbs: [],
  vars: [],
  path: [],
  stacktrace: #Splode.Stacktrace<>,
  class: :invalid
}

but the implementation for the error is looking for error.vars and not error.field

image

@diogomrts diogomrts added bug Something isn't working needs review labels Jul 2, 2024
@zachdaniel zachdaniel moved this to Up Next in Ash Framework Roadmap Jul 2, 2024
@zachdaniel zachdaniel moved this from Up Next to In Progress in Ash Framework Roadmap Jul 2, 2024
@zachdaniel zachdaniel moved this from In Progress to Up Next in Ash Framework Roadmap Jul 2, 2024
@diogomrts
Copy link
Contributor Author

diogomrts commented Nov 22, 2024

I just encountered an issue that I'm not sure is related to this when adding a new POST route pointing to a generic ation.
In this generic action with a required string argument:

action :submit do
  argument :contact_name, :string, allow_nil?: false

  run fn %Ash.ActionInput{} = input, context ->
    dbg("action")
  end
end

If we pass an empty string in the body of the POST request it will throw this error:

16:13:26.240 [error] ** (Ash.Error.Invalid)
Invalid Error

* argument contact_name is required
  (ash 3.4.41) lib/ash/error/changes/required.ex:4: Ash.Error.Changes.Required.exception/1
  (ash 3.4.41) lib/ash/action_input.ex:158: anonymous fn/2 in Ash.ActionInput.require_arguments/1
  (elixir 1.17.3) lib/enum.ex:2531: Enum."-reduce/3-lists^foldl/2-0-"/3

Which results in this same error as described above:

{
  "errors": [
    {
      "code": "required",
      "id": "4ec324c9-ab23-464d-aa02-018396a7c301",
      "meta": {},
      "status": "400",
      "title": "Required",
      "detail": "is required"
    }
  ],
  "jsonapi": {
    "version": "1.0"
  }
}

Setting the request field to null in the json body results in a proper error message:

image

@zachdaniel
Copy link
Contributor

Really need to get around to this one. Sorry its been so long :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Up Next
Development

No branches or pull requests

2 participants