Skip to content

Commit de462ca

Browse files
committed
Remove remaining JSON schema usage from schema definition specs
As noted on #1224, mentions of JSON schema in `elasticgraph-schema_definition/spec` should approach zero now that the JSON schema logic lives in `elasticgraph-json_ingestion`: - The graphql_schema, datastore_config, and runtime_metadata spec supports now run schemas without any extension modules, and the `json_schema` calls that existed only to satisfy the extension's scalar validation are gone. - The scalar `json_schema` requirement test (duplicated by the json_ingestion suite) is deleted, and the `long`/`unsigned_long` placeholder-inference tests that depend on JSON schema bounds moved to the json_ingestion suite (along with the built-in-scalar placeholder map, which differs with the extension loaded). - The reserved-type-name test now exercises the core `reserved_type_names` mechanism directly; the `ElasticGraphEventEnvelope` reservation is already covered by the json_ingestion suite. - `rake_tasks_spec` runs its synthetic schemas without the extension and no longer asserts on JSON schema artifacts (covered by json_ingestion's integration spec). A new short-diff test keeps `truncate_diff` fully covered. The tests that evaluate the repo's own `config/schema.rb` still load the extension, since that schema is a JSON ingestion application. The remaining mentions are the `define_schema` test-support seam (which exists for optional ingestion extensions) and JSON-the-format documentation text.
1 parent df526fc commit de462ca

16 files changed

Lines changed: 209 additions & 253 deletions

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3151,6 +3151,27 @@ def link_supertype_to_subtypes(interface_type, *subtype_names)
31513151
expect(widget_schema.dig("properties", "undocumented_field")).not_to have_key("description")
31523152
end
31533153

3154+
it "does not care if the interface and object fields have different JSON schema" do
3155+
json_schema = dump_schema do |schema|
3156+
schema.object_type "Thing" do |t|
3157+
t.implements "HasID"
3158+
t.field "id", "ID!" do |f|
3159+
f.json_schema maxLength: 40
3160+
end
3161+
t.field "name", "String"
3162+
t.index "things"
3163+
end
3164+
3165+
schema.interface_type "HasID" do |t|
3166+
t.field "id", "ID!" do |f|
3167+
f.json_schema maxLength: 30
3168+
end
3169+
end
3170+
end
3171+
3172+
expect(json_schema.dig("$defs", "Thing", "properties", "id", "allOf")).to include({"maxLength" => 40})
3173+
end
3174+
31543175
def all_type_definitions_for(&schema_definition)
31553176
dump_schema(&schema_definition).fetch("$defs")
31563177
end

elasticgraph-json_ingestion/spec/unit/elastic_graph/json_ingestion/schema_definition/schema_elements/scalar_type_extension_spec.rb

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,127 @@ module SchemaElements
8383
expect(grouping_missing_value_placeholder).to eq(nil)
8484
end
8585

86+
it "does not infer a placeholder for JSON-safe unsigned_long scalars with the default coercion adapter (which would not coerce floats back to integers)" do
87+
grouping_missing_value_placeholder = grouping_missing_value_placeholder_for(
88+
"unsigned_long",
89+
type: "integer",
90+
maximum: JSON_SAFE_LONG_MAX
91+
)
92+
93+
expect(grouping_missing_value_placeholder).to eq(nil)
94+
end
95+
96+
it "does not infer a placeholder for unsigned_long scalars when no maximum is specified" do
97+
grouping_missing_value_placeholder = grouping_missing_value_placeholder_for("unsigned_long", type: "integer") do |type|
98+
type.coerce_with "ExampleScalarCoercionAdapter", defined_at: scalar_coercion_adapter_path
99+
end
100+
101+
expect(grouping_missing_value_placeholder).to eq(nil)
102+
end
103+
104+
it "infers a numeric missing-value placeholder for long scalars exactly at the JSON-safe boundaries with custom coercion" do
105+
grouping_missing_value_placeholder = grouping_missing_value_placeholder_for(
106+
"long",
107+
type: "integer",
108+
minimum: JSON_SAFE_LONG_MIN,
109+
maximum: JSON_SAFE_LONG_MAX
110+
) do |type|
111+
type.coerce_with "ExampleScalarCoercionAdapter", defined_at: scalar_coercion_adapter_path
112+
end
113+
114+
expect(grouping_missing_value_placeholder).to eq(MISSING_NUMERIC_PLACEHOLDER)
115+
end
116+
117+
it "does not infer a placeholder for JSON-safe long scalars with the default coercion adapter" do
118+
grouping_missing_value_placeholder = grouping_missing_value_placeholder_for(
119+
"long",
120+
type: "integer",
121+
minimum: JSON_SAFE_LONG_MIN,
122+
maximum: JSON_SAFE_LONG_MAX
123+
)
124+
125+
expect(grouping_missing_value_placeholder).to eq(nil)
126+
end
127+
128+
it "does not infer a placeholder for long scalars when the minimum is one below the JSON-safe range" do
129+
grouping_missing_value_placeholder = grouping_missing_value_placeholder_for(
130+
"long",
131+
type: "integer",
132+
minimum: JSON_SAFE_LONG_MIN - 1,
133+
maximum: JSON_SAFE_LONG_MAX
134+
) do |type|
135+
type.coerce_with "ExampleScalarCoercionAdapter", defined_at: scalar_coercion_adapter_path
136+
end
137+
138+
expect(grouping_missing_value_placeholder).to eq(nil)
139+
end
140+
141+
it "does not infer a placeholder for long scalars when the maximum is one above the JSON-safe range" do
142+
grouping_missing_value_placeholder = grouping_missing_value_placeholder_for(
143+
"long",
144+
type: "integer",
145+
minimum: JSON_SAFE_LONG_MIN,
146+
maximum: JSON_SAFE_LONG_MAX + 1
147+
) do |type|
148+
type.coerce_with "ExampleScalarCoercionAdapter", defined_at: scalar_coercion_adapter_path
149+
end
150+
151+
expect(grouping_missing_value_placeholder).to eq(nil)
152+
end
153+
154+
it "does not infer a placeholder for long scalars when only one bound is specified (the other defaults to the LongString range)" do
155+
only_min = grouping_missing_value_placeholder_for("long", type: "integer", minimum: 0) do |type|
156+
type.coerce_with "ExampleScalarCoercionAdapter", defined_at: scalar_coercion_adapter_path
157+
end
158+
159+
only_max = grouping_missing_value_placeholder_for("long", type: "integer", maximum: 1000) do |type|
160+
type.coerce_with "ExampleScalarCoercionAdapter", defined_at: scalar_coercion_adapter_path
161+
end
162+
163+
expect(only_min).to eq(nil)
164+
expect(only_max).to eq(nil)
165+
end
166+
167+
it "does not infer a placeholder for long scalars when no bounds are specified" do
168+
grouping_missing_value_placeholder = grouping_missing_value_placeholder_for("long", type: "integer") do |type|
169+
type.coerce_with "ExampleScalarCoercionAdapter", defined_at: scalar_coercion_adapter_path
170+
end
171+
172+
expect(grouping_missing_value_placeholder).to eq(nil)
173+
end
174+
175+
it "has the expected placeholder for each built-in scalar type, including the JSON-safe-range-aware `JsonSafeLong` inference" do
176+
results = define_schema(schema_element_name_form: "snake_case") { |schema| }
177+
built_in_scalars = results.state.scalar_types_by_name.keys
178+
scalar_types_by_name = results.runtime_metadata.scalar_types_by_name
179+
180+
placeholders_by_scalar_type = built_in_scalars.to_h do |scalar_type|
181+
[scalar_type, scalar_types_by_name.fetch(scalar_type).grouping_missing_value_placeholder]
182+
end
183+
184+
expect(placeholders_by_scalar_type).to eq({
185+
"Boolean" => nil,
186+
"Cursor" => MISSING_STRING_PLACEHOLDER,
187+
"Date" => nil,
188+
"DateTime" => nil,
189+
"Float" => MISSING_NUMERIC_PLACEHOLDER,
190+
"ID" => MISSING_STRING_PLACEHOLDER,
191+
"Int" => MISSING_NUMERIC_PLACEHOLDER, # GraphQL automatically coerces Int values
192+
"JsonSafeLong" => MISSING_NUMERIC_PLACEHOLDER, # custom coercion adapter coerces floats back to integers
193+
"LocalTime" => nil,
194+
"LongString" => nil, # outside of the JSON safe range.
195+
"String" => MISSING_STRING_PLACEHOLDER,
196+
"TimeZone" => MISSING_STRING_PLACEHOLDER,
197+
"Untyped" => MISSING_STRING_PLACEHOLDER
198+
})
199+
end
200+
86201
def grouping_missing_value_placeholder_for(mapping_type, **json_schema_options)
87202
define_schema(schema_element_name_form: "snake_case") do |schema|
88203
schema.scalar_type "CustomScalar" do |type|
89204
type.mapping type: mapping_type
90205
type.json_schema(**json_schema_options)
91-
yield type
206+
yield type if block_given?
92207
end
93208
end.runtime_metadata.scalar_types_by_name.fetch("CustomScalar").grouping_missing_value_placeholder
94209
end

0 commit comments

Comments
 (0)