Skip to content

Commit 35f5ded

Browse files
authored
Add test coverage: don't make id: ID non-nullable on non-indexed types. (#1122)
After merging #1118, I realized that there's an alternate implementation that would pass the test added in that PR but would be wrong: we could always set the `id` field of any object type to be non-nullable. We only want to do so on indexed types. This adds a test confirming that `id: ID` is left as nullable in on a non-indexed type.
1 parent bd68e85 commit 35f5ded

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

elasticgraph-schema_definition/spec/unit/elastic_graph/schema_definition/json_schema_spec.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ def have_json_schema_like(type_name, *args, **kwargs)
11401140
})
11411141
end
11421142

1143-
it "makes the `id` field non-nullable in JSON schema even when declared as a nullable `ID` in the GraphQL schema" do
1143+
it "makes an `id: ID` field of an indexed type non-nullable in JSON schema" do
11441144
json_schema = dump_schema do |s|
11451145
s.object_type "Widget" do |t|
11461146
t.field "id", "ID"
@@ -1163,6 +1163,27 @@ def have_json_schema_like(type_name, *args, **kwargs)
11631163
)
11641164
end
11651165

1166+
it "leaves an `id: ID` field on a non-indexed type as nullable in the JSON schema" do
1167+
json_schema = dump_schema do |s|
1168+
s.object_type "Widget" do |t|
1169+
t.field "id", "ID"
1170+
t.field "name", "String"
1171+
end
1172+
end
1173+
1174+
expect(json_schema).to have_json_schema_like("Widget", {
1175+
"type" => "object",
1176+
"properties" => {
1177+
"id" => json_schema_ref("ID"),
1178+
"name" => json_schema_ref("String")
1179+
},
1180+
"required" => %w[id name]
1181+
}).which_matches(
1182+
{"id" => "abc", "name" => "thing"},
1183+
{"id" => nil, "name" => "thing"}
1184+
)
1185+
end
1186+
11661187
context "on an indexed type with a rollover index" do
11671188
it "makes the JSON schema for a rollover index timestamp field on the indexed type non-nullable since a target index cannot be chosen without it" do
11681189
json_schema = dump_schema do |s|

0 commit comments

Comments
 (0)