Skip to content

Commit 9155d1a

Browse files
authored
Merge pull request #1188 from maartenvanvliet/default-enum-value-present
Fix default enum value present for SDL schemas
2 parents 03be990 + 4ec815a commit 9155d1a

File tree

3 files changed

+88
-7
lines changed

3 files changed

+88
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Bug Fix: [Add type system directives to introspection results](https://github.com/absinthe-graphql/absinthe/pull/1189)
1010
- Bug Fix: [Add `__private__` field to EnumValueDefinition](https://github.com/absinthe-graphql/absinthe/pull/1148)
1111
- Bug Fix: [Fix bug in Schema.**absinthe_types**(:all) for Persistent Term](https://github.com/absinthe-graphql/absinthe/pull/1161)
12+
- Bug Fix: [Fix default enum value check for SDL schema's](https://github.com/absinthe-graphql/absinthe/pull/1188)
1213
- Feature: [Add `import_directives` macro](https://github.com/absinthe-graphql/absinthe/pull/1158)
1314
- Feature: [Support type extensions on schema declarations](https://github.com/absinthe-graphql/absinthe/pull/1176)
1415
- Bug Fix: [Root objects are marked as referenced correctly](https://github.com/absinthe-graphql/absinthe/pull/1186)

lib/absinthe/phase/schema/validation/default_enum_value_present.ex

+7-6
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,21 @@ defmodule Absinthe.Phase.Schema.Validation.DefaultEnumValuePresent do
1515
|> Enum.filter(&match?(%Schema.EnumTypeDefinition{}, &1))
1616
|> Map.new(&{&1.identifier, &1})
1717

18-
schema = Blueprint.prewalk(schema, &validate_defaults(&1, enums))
18+
schema = Blueprint.prewalk(schema, &validate_defaults(&1, enums, schema))
1919
{:halt, schema}
2020
end
2121

2222
def validate_schema(node), do: node
2323

24-
def validate_defaults(%{default_value: nil} = node, _) do
24+
def validate_defaults(%{default_value: nil} = node, _, _) do
2525
node
2626
end
2727

28-
def validate_defaults(%{default_value: default_value, type: type} = node, enums) do
29-
type = Blueprint.TypeReference.unwrap(type)
28+
def validate_defaults(%{default_value: default_value, type: type} = node, enums, schema) do
29+
type_identifier =
30+
Blueprint.TypeReference.unwrap(type) |> Blueprint.TypeReference.to_type(schema)
3031

31-
case Map.fetch(enums, type) do
32+
case Map.fetch(enums, type_identifier) do
3233
{:ok, enum} ->
3334
values = Enum.map(enum.values, & &1.value)
3435
value_list = Enum.map(values, &"\n * #{inspect(&1)}")
@@ -52,7 +53,7 @@ defmodule Absinthe.Phase.Schema.Validation.DefaultEnumValuePresent do
5253
end
5354
end
5455

55-
def validate_defaults(node, _) do
56+
def validate_defaults(node, _, _) do
5657
node
5758
end
5859

test/absinthe/schema/rule/default_enum_value_present_test.exs

+80-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,86 @@
11
defmodule Absinthe.Schema.Rule.DefaultEnumValuePresentTest do
22
use Absinthe.Case, async: true
33

4-
describe "rule" do
4+
describe "SDL schema" do
5+
test "is enforced when the default_value is not in the enum" do
6+
schema = """
7+
defmodule BadColorSchemaSdl do
8+
use Absinthe.Schema
9+
10+
import_sdl "
11+
enum Channel {
12+
RED
13+
GREEN
14+
}
15+
16+
type Query {
17+
info(channel: Channel! = OTHER): Channel
18+
}
19+
"
20+
end
21+
"""
22+
23+
error = ~r/The default_value for an enum must be present in the enum values/
24+
25+
assert_raise(Absinthe.Schema.Error, error, fn ->
26+
Code.eval_string(schema)
27+
end)
28+
end
29+
30+
test "is enforced when the default_value is a list of enums and some items are not in the enum" do
31+
schema = """
32+
defmodule MovieSchemaSdl do
33+
use Absinthe.Schema
34+
35+
36+
import_sdl "
37+
enum MovieGenre {
38+
ACTION
39+
COMEDY
40+
SF
41+
}
42+
43+
type Query {
44+
movies(genres: [MovieGenre!]! = [ACTION, OTHER]): [MovieGenre!]!
45+
}
46+
"
47+
end
48+
"""
49+
50+
error = ~r/The default_value for an enum must be present in the enum values/
51+
52+
assert_raise(Absinthe.Schema.Error, error, fn ->
53+
Code.eval_string(schema)
54+
end)
55+
end
56+
57+
test "passes when the default_value is a list and that list is a valid enum value" do
58+
schema = """
59+
defmodule CorrectCatSchemSdl do
60+
use Absinthe.Schema
61+
62+
import_sdl "
63+
enum CatOrderBy {
64+
NAME_ASC
65+
NAME_DESC_INSERTED_AT_ASC
66+
}
67+
68+
type Query {
69+
cat(orderBy: [CatOrderBy!] = [NAME_ASC]): [Cat!]!
70+
}
71+
72+
type Cat {
73+
name: String
74+
}
75+
"
76+
end
77+
"""
78+
79+
assert Code.eval_string(schema)
80+
end
81+
end
82+
83+
describe "macro schema" do
584
test "is enforced when the default_value is not in the enum" do
685
schema = """
786
defmodule BadColorSchema do

0 commit comments

Comments
 (0)