Skip to content

Commit 62013f0

Browse files
committed
Use JSON ingestion as schema definition extension
1 parent d8c3c70 commit 62013f0

48 files changed

Lines changed: 157 additions & 931 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

config/site/support/doctest_helper.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
require "elastic_graph/apollo/schema_definition/api_extension"
1010
require "elastic_graph/schema_artifacts/runtime_metadata/schema_element_names"
1111
require "elastic_graph/schema_definition/api"
12+
require "elastic_graph/schema_definition/extension_module_support"
1213
require "elastic_graph/schema_definition/schema_artifact_manager"
1314
require "elastic_graph/warehouse/schema_definition/api_extension"
1415
require "rspec/mocks"
@@ -60,7 +61,7 @@ module ElasticGraph
6061
@api = SchemaDefinition::API.new(
6162
SchemaArtifacts::RuntimeMetadata::SchemaElementNames.new(form: :camelCase, overrides: {}),
6263
true,
63-
extension_modules: extension_modules
64+
extension_modules: SchemaDefinition::ExtensionModuleSupport.default_extension_modules + extension_modules
6465
)
6566

6667
# This is required in all schemas, but we don't want to have to put in all our examples,
@@ -96,6 +97,7 @@ module ElasticGraph
9697
# `schema.json_schema_version` raises an error when the version is set more than once.
9798
# By default we set it above. Here we clear it to allow our example to set it.
9899
schema.state.json_schema_version = nil
100+
schema.state.json_schema_version_setter_location = nil
99101
end
100102
end
101103

elasticgraph-apollo/spec/unit/elastic_graph/apollo/apollo_directives_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ def self.with_both_casing_forms(&block)
552552
end
553553

554554
def define_schema(&block)
555-
extension_modules = [SchemaDefinition::APIExtension]
555+
extension_modules = ::ElasticGraph::SchemaDefinition::ExtensionModuleSupport.default_extension_modules + [SchemaDefinition::APIExtension]
556556
super(schema_element_name_form: schema_element_name_form, extension_modules: extension_modules, &block)
557557
end
558558
end

elasticgraph-apollo/spec/unit/elastic_graph/apollo/schema_definition_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1431,7 +1431,9 @@ def expect_identifiable_type_tagging_of_token(&type_def_for)
14311431
end
14321432

14331433
def define_schema(with_apollo: true, &block)
1434-
extension_modules = with_apollo ? [SchemaDefinition::APIExtension] : []
1434+
# Always include the JSON ingestion default so `Results#json_schemas_for` is available in both modes.
1435+
extension_modules = ::ElasticGraph::SchemaDefinition::ExtensionModuleSupport.default_extension_modules
1436+
extension_modules += [SchemaDefinition::APIExtension] if with_apollo
14351437
super(schema_element_name_form: schema_element_name_form, extension_modules: extension_modules, &block)
14361438
end
14371439
end

elasticgraph-json_ingestion/lib/elastic_graph/json_ingestion/schema_definition/results_extension.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def json_schema_indexing_field_types_by_name
121121
end
122122
.sort_by(&:name)
123123
.to_h do |type|
124-
# @type var indexing_field_type: ::ElasticGraph::SchemaDefinition::Indexing::_FieldType
124+
# @type var indexing_field_type: Indexing::_JSONFieldType
125125
indexing_field_type = _ = type.to_indexing_field_type
126126
[type.name, indexing_field_type]
127127
end

elasticgraph-json_ingestion/sig/elastic_graph/json_ingestion.rbs

Lines changed: 0 additions & 4 deletions
This file was deleted.

elasticgraph-json_ingestion/sig/elastic_graph/json_ingestion/schema_definition/indexing/json_schema_with_metadata.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module ElasticGraph::JSONIngestion
3939

4040
attr_reader unused_deprecated_elements: ::Set[::ElasticGraph::SchemaDefinition::SchemaElements::DeprecatedElement]
4141

42-
def initialize: (::ElasticGraph::SchemaDefinition::Results) -> void
42+
def initialize: ((::ElasticGraph::SchemaDefinition::Results & ResultsExtension)) -> void
4343
def merge_metadata_into: (::Hash[::String, untyped]) -> JSONSchemaWithMetadata
4444

4545
private

elasticgraph-json_ingestion/sig/elastic_graph/json_ingestion/schema_definition/results_extension.rbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ module ElasticGraph
1818
@json_schema_field_metadata_by_type_and_field_name: ::Hash[::String, ::Hash[::String, Indexing::JSONSchemaFieldMetadata]]?
1919
@current_public_json_schema: ::Hash[::String, untyped]?
2020
@json_schema_with_metadata_merger: Indexing::JSONSchemaWithMetadata::Merger?
21-
@json_schema_indexing_field_types_by_name: ::Hash[::String, ::ElasticGraph::SchemaDefinition::Indexing::_FieldType]?
21+
@json_schema_indexing_field_types_by_name: ::Hash[::String, Indexing::_JSONFieldType]?
2222

2323
def json_ingestion_state: () -> (::ElasticGraph::SchemaDefinition::State & StateExtension)
2424
def json_schema_with_metadata_merger: () -> Indexing::JSONSchemaWithMetadata::Merger
2525
def build_public_json_schema: () -> ::Hash[::String, untyped]
26-
def json_schema_indexing_field_types_by_name: () -> ::Hash[::String, ::ElasticGraph::SchemaDefinition::Indexing::_FieldType]
26+
def json_schema_indexing_field_types_by_name: () -> ::Hash[::String, Indexing::_JSONFieldType]
2727
end
2828
end
2929
end

elasticgraph-json_ingestion/sig/elastic_graph/json_ingestion/schema_definition/schema_artifact_manager_extension.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module ElasticGraph
66

77
private
88

9-
@json_schemas_artifact: ::ElasticGraph::SchemaDefinition::SchemaArtifact[untyped]
9+
@json_schemas_artifact: ::ElasticGraph::SchemaDefinition::SchemaArtifact[untyped]?
1010

1111
def json_ingestion_schema_definition_results: () -> (::ElasticGraph::SchemaDefinition::Results & ResultsExtension)
1212
def artifacts_from_schema_def: () -> ::Array[::ElasticGraph::SchemaDefinition::SchemaArtifact[untyped]]

elasticgraph-schema_definition/elasticgraph-schema_definition.gemspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ Gem::Specification.new do |spec|
4343

4444
spec.add_dependency "elasticgraph-graphql", ElasticGraph::VERSION # needed since we validate that scalar `coerce_with` options are valid (which loads scalar coercion adapters)
4545
spec.add_dependency "elasticgraph-indexer", ElasticGraph::VERSION # needed since we validate that scalar `prepare_for_indexing_with` options are valid (which loads indexing preparer adapters)
46+
# `elasticgraph-json_ingestion` is the default ingestion serializer extension. It's a soft dep here
47+
# so `default_extension_modules` can fall back to `[]` when the gem isn't installed (e.g. for apps
48+
# that supply a different serializer via `Gemfile-custom`), but most apps want it.
4649
spec.add_dependency "elasticgraph-json_ingestion", ElasticGraph::VERSION
4750
spec.add_dependency "elasticgraph-schema_artifacts", ElasticGraph::VERSION
4851
spec.add_dependency "elasticgraph-support", ElasticGraph::VERSION

elasticgraph-schema_definition/lib/elastic_graph/schema_definition/api.rb

Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def union_type(name, &block)
295295
# ElasticGraph.define_schema do |schema|
296296
# schema.scalar_type "URL" do |t|
297297
# t.mapping type: "keyword"
298-
# t.json_schema type: "string", format: "uri"
298+
# t.json_schema type: "string"
299299
# end
300300
# end
301301
def scalar_type(name, &block)
@@ -454,67 +454,23 @@ def results
454454
@results ||= @factory.new_results
455455
end
456456

457-
# Defines the version number of the current JSON schema. Importantly, every time a change is made that impacts the JSON schema
458-
# artifact, the version number must be incremented to ensure that each different version of the JSON schema is identified by a unique
459-
# version number. The publisher will then include this version number in published events to identify the version of the schema it
460-
# was using. This avoids the need to deploy the publisher and ElasticGraph indexer at the same time to keep them in sync.
457+
# Records the JSON schema version of this schema definition.
461458
#
462-
# @note While this is an important part of how ElasticGraph is designed to support schema evolution, it can be annoying constantly
463-
# have to increment this while rapidly changing the schema during prototyping. You can disable the requirement to increment this
464-
# on every JSON schema change by setting `enforce_json_schema_version` to `false` in your `Rakefile`.
459+
# The default implementation is a no-op so that callers (and the test helpers) can invoke this
460+
# uniformly regardless of whether an ingestion-serializer extension is loaded. When
461+
# `elasticgraph-json_ingestion` is loaded, its `APIExtension` overrides this with real behavior.
465462
#
466-
# @param version [Integer] current version number of the JSON schema artifact
463+
# @param version [Integer] the JSON schema version
467464
# @return [void]
468-
# @see Local::RakeTasks#enforce_json_schema_version
469-
#
470-
# @example Set the JSON schema version to 1
471-
# ElasticGraph.define_schema do |schema|
472-
# schema.json_schema_version 1
473-
# end
474465
def json_schema_version(version)
475-
if !version.is_a?(Integer) || version < 1
476-
raise Errors::SchemaError, "`json_schema_version` must be a positive integer. Specified version: #{version}"
477-
end
478-
479-
if @state.json_schema_version
480-
raise Errors::SchemaError, "`json_schema_version` can only be set once on a schema. Previously-set version: #{@state.json_schema_version}"
481-
end
482-
483-
@state.json_schema_version = version
484-
@state.json_schema_version_setter_location = caller_locations(1, 1).to_a.first
485-
nil
486466
end
487467

488-
# Defines strictness of the JSON schema validation. By default, the JSON schema will require all fields to be provided by the
489-
# publisher (but they can be nullable) and will ignore extra fields that are not defined in the schema. Use this method to
490-
# configure this behavior.
491-
#
492-
# @param allow_omitted_fields [bool] Whether nullable fields can be omitted from indexing events.
493-
# @param allow_extra_fields [bool] Whether extra fields (e.g. beyond fields defined in the schema) can be included in indexing events.
494-
# @return [void]
468+
# Records JSON schema strictness configuration.
495469
#
496-
# @note If you allow both omitted fields and extra fields, ElasticGraph's JSON schema validation will allow (and ignore) misspelled
497-
# field names in indexing events. For example, if the ElasticGraph schema has a nullable field named `parentId` but the publisher
498-
# accidentally provides it as `parent_id`, ElasticGraph would happily ignore the `parent_id` field entirely, because `parentId`
499-
# is allowed to be omitted and `parent_id` would be treated as an extra field. Therefore, we recommend that you only set one of
500-
# these to `true` (or none).
470+
# The default implementation is a no-op; overridden by the JSON ingestion extension.
501471
#
502-
# @example Allow omitted fields and disallow extra fields
503-
# ElasticGraph.define_schema do |schema|
504-
# schema.json_schema_strictness allow_omitted_fields: true, allow_extra_fields: false
505-
# end
472+
# @return [void]
506473
def json_schema_strictness(allow_omitted_fields: false, allow_extra_fields: true)
507-
unless [true, false].include?(allow_omitted_fields)
508-
raise Errors::SchemaError, "`allow_omitted_fields` must be true or false"
509-
end
510-
511-
unless [true, false].include?(allow_extra_fields)
512-
raise Errors::SchemaError, "`allow_extra_fields` must be true or false"
513-
end
514-
515-
@state.allow_omitted_json_schema_fields = allow_omitted_fields
516-
@state.allow_extra_json_schema_fields = allow_extra_fields
517-
nil
518474
end
519475

520476
# Registers a customization callback that will be applied to every built-in type automatically provided by ElasticGraph. Provides

0 commit comments

Comments
 (0)