Skip to content

Commit 9ce0620

Browse files
committed
fix: validate attribute_input_types keys are actual public attributes
1 parent 25aaa62 commit 9ce0620

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

lib/resource/verifiers/verify_field_dependencies.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,13 @@ defmodule AshGraphql.Resource.Verifiers.VerifyFieldDependencies do
183183
input_types = AshGraphql.Resource.Info.attribute_input_types(dsl)
184184

185185
if is_list(input_types) and input_types != [] do
186+
# Only check keys that are actual public attributes. Non-attribute keys
187+
# (e.g. relationships) are caught by VerifyFieldReferences as invalid.
188+
attribute_names =
189+
dsl |> Ash.Resource.Info.public_attributes() |> MapSet.new(& &1.name)
190+
186191
Enum.reduce(input_types, warnings, fn {attr, _type}, acc ->
187-
if MapSet.member?(visible, attr) do
192+
if not MapSet.member?(attribute_names, attr) or MapSet.member?(visible, attr) do
188193
acc
189194
else
190195
[invisible_field_warning(resource, attr, "attribute_input_types") | acc]

lib/resource/verifiers/verify_field_references.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ defmodule AshGraphql.Resource.Verifiers.VerifyFieldReferences do
4545
)
4646

4747
validate_option(dsl, resource, :relationships, relationship_names, "relationship")
48+
validate_option(dsl, resource, :attribute_input_types, attribute_names, "attribute")
4849

4950
:ok
5051
end
@@ -88,6 +89,9 @@ defmodule AshGraphql.Resource.Verifiers.VerifyFieldReferences do
8889
defp get_option(dsl, :sortable_fields), do: AshGraphql.Resource.Info.sortable_fields(dsl)
8990
defp get_option(dsl, :filterable_fields), do: AshGraphql.Resource.Info.filterable_fields(dsl)
9091

92+
defp get_option(dsl, :attribute_input_types),
93+
do: AshGraphql.Resource.Info.attribute_input_types(dsl)
94+
9195
# relationships/1 in Info falls back to all public relationships when nil,
9296
# so we read the raw option to only validate when explicitly set by the user.
9397
defp get_option(dsl, :relationships) do

test/verify_field_references_test.exs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,27 @@ defmodule AshGraphql.VerifyFieldReferencesTest do
182182
assert :ok = VerifyFieldReferences.verify(dsl_state())
183183
end
184184
end
185+
186+
describe "attribute_input_types" do
187+
test "raises for unknown attribute" do
188+
dsl = set_graphql_option(dsl_state(), :attribute_input_types, nonexistent: :string)
189+
190+
assert_raise Spark.Error.DslError, ~r/`:nonexistent`.*attribute_input_types/s, fn ->
191+
VerifyFieldReferences.verify(dsl)
192+
end
193+
end
194+
195+
test "raises for relationship name (not an attribute)" do
196+
dsl = set_graphql_option(dsl_state(), :attribute_input_types, related: :string)
197+
198+
assert_raise Spark.Error.DslError, ~r/`:related`.*attribute_input_types/s, fn ->
199+
VerifyFieldReferences.verify(dsl)
200+
end
201+
end
202+
203+
test "passes for valid attribute" do
204+
dsl = set_graphql_option(dsl_state(), :attribute_input_types, name: :string)
205+
assert :ok = VerifyFieldReferences.verify(dsl)
206+
end
207+
end
185208
end

0 commit comments

Comments
 (0)