Skip to content

Commit

Permalink
Use default namespace to load nested schemas from the correct namespace
Browse files Browse the repository at this point in the history
Fixes dasch#186
  • Loading branch information
piotaixr committed Oct 3, 2023
1 parent a8b0ba4 commit 10d60c3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion avro_turf.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_dependency "avro", ">= 1.8.0", "< 1.12"
spec.add_dependency "avro", ">= 1.11.3", "< 1.12"
spec.add_dependency "excon", "~> 0.104"

spec.add_development_dependency "bundler", "~> 2.0"
Expand Down
2 changes: 1 addition & 1 deletion lib/avro_turf/schema_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def load_schema!(fullname, local_schemas_cache = {})
# Try to first resolve a referenced schema from disk.
# If this is successful, the Avro gem will have mutated the
# local_schemas_cache, adding all the new schemas it found.
load_schema!(e.type_name, local_schemas_cache)
load_schema!(::Avro::Name.make_fullname(e.type_name, e.default_namespace), local_schemas_cache)

# Attempt to re-parse the original schema now that the dependency
# has been resolved and use the now-updated local_schemas_cache to
Expand Down
29 changes: 29 additions & 0 deletions spec/schema_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,35 @@
expect(schema.fullname).to eq "message"
end

it 'searches for nested types with the correct namespace' do
define_schema "foo/bar.avsc", <<-AVSC
{
"type": "record",
"namespace": "foo",
"name": "bar",
"fields": [
{
"name": "another",
"type": "another_schema"
}
]
}
AVSC

define_schema "foo/another_schema.avsc", <<-AVSC
{
"namespace": "foo",
"name": "another_schema",
"type": "record",
"fields": [ { "name": "str", "type": "string" } ]
}
AVSC

schema = store.find('foo.bar')
expect(schema.fullname).to eq "foo.bar"
expect(schema.fields.first.type.fullname).to eq "foo.another_schema"
end

it "resolves missing references when nested schema is not a named type" do
define_schema "root.avsc", <<-AVSC
{
Expand Down
5 changes: 4 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

module Helpers
def define_schema(path, content)
File.open(File.join("spec/schemas", path), "w") do |f|
file = File.join("spec/schemas", path)
dir = File.dirname(file)
FileUtils.mkdir_p(dir)
File.open(file, "w") do |f|
f.write(content)
end
end
Expand Down

0 comments on commit 10d60c3

Please sign in to comment.