Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes dynamic and lexical schemas to be stored internally by the URI as a string #189

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/json_schemer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
require 'json_schemer/openapi30/vocab'
require 'json_schemer/openapi'
require 'json_schemer/configuration'
require 'json_schemer/resources'
require 'json_schemer/schema'

module JSONSchemer
Expand Down
6 changes: 3 additions & 3 deletions lib/json_schemer/draft201909/vocab/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Vocab
module Core
class RecursiveAnchor < Keyword
def parse
root.resources[:dynamic][schema.base_uri] = schema if value == true
root.resources.register(:dynamic, schema.base_uri, schema) if value == true
value
end
end
Expand All @@ -29,8 +29,8 @@ def validate(instance, instance_location, keyword_location, context)

if recursive_anchor
context.dynamic_scope.each do |ancestor|
if ancestor.root.resources.fetch(:dynamic).key?(ancestor.base_uri)
schema = ancestor.root.resources.fetch(:dynamic).fetch(ancestor.base_uri)
if ancestor.root.resources.dynamic?(ancestor.base_uri)
schema = ancestor.root.resources.dynamic!(ancestor.base_uri)
break
end
end
Expand Down
12 changes: 6 additions & 6 deletions lib/json_schemer/draft202012/vocab/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ class Id < Keyword
def parse
URI.join(schema.base_uri, value).tap do |uri|
schema.base_uri = uri
root.resources[:lexical][uri] = schema
root.resources.register(:lexical, uri, schema)
end
end
end

class Anchor < Keyword
def parse
URI.join(schema.base_uri, "##{value}").tap do |uri|
root.resources[:lexical][uri] = schema
root.resources.register(:lexical, uri, schema)
end
end
end
Expand All @@ -71,8 +71,8 @@ def validate(instance, instance_location, keyword_location, context)
class DynamicAnchor < Keyword
def parse
URI.join(schema.base_uri, "##{value}").tap do |uri|
root.resources[:lexical][uri] = schema
root.resources[:dynamic][uri] = schema
root.resources.register(:lexical, uri, schema)
root.resources.register(:dynamic, uri, schema)
end
end
end
Expand All @@ -98,8 +98,8 @@ def validate(instance, instance_location, keyword_location, context)
if dynamic_anchor
context.dynamic_scope.each do |ancestor|
dynamic_uri = URI.join(ancestor.base_uri, "##{dynamic_anchor}")
if ancestor.root.resources.fetch(:dynamic).key?(dynamic_uri)
schema = ancestor.root.resources.fetch(:dynamic).fetch(dynamic_uri)
if ancestor.root.resources.dynamic?(dynamic_uri)
schema = ancestor.root.resources.dynamic!(dynamic_uri)
break
end
end
Expand Down
30 changes: 30 additions & 0 deletions lib/json_schemer/resources.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module JSONSchemer
class Resources

def initialize
@resources ||= { :lexical => {}, :dynamic => {} }
end

def register(type, namespace_uri, schema)
@resources[type][namespace_uri.to_s] = schema
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@resources[type][namespace_uri.to_s] = schema
fail "Schema already registered as #{type} under #{namespace_uri}" if @resources[type][namespace_uri.to_s]
@resources[type][namespace_uri.to_s] = schema

Check whether the URI is already used for possibly another schema. It makes a couple of tests fail though.

end

def lexical!(namespace_uri)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just call these uri—I don't know if "namespace" is the right terminology here.

@resources[:lexical].fetch(namespace_uri.to_s)
end

def lexical(namespace_uri)
@resources[:lexical][namespace_uri.to_s]
end

def dynamic!(namespace_uri)
@resources[:dynamic].fetch(namespace_uri.to_s)
end

def dynamic?(namespace_uri)
@resources[:dynamic].key?(namespace_uri.to_s)
end
end
end
9 changes: 4 additions & 5 deletions lib/json_schemer/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,12 @@ def resolve_ref(uri)
uri.fragment = nil
end

lexical_resources = resources.fetch(:lexical)
schema = lexical_resources[uri]
schema = resources.lexical(uri)

if !schema && uri.fragment.nil?
empty_fragment_uri = uri.dup
empty_fragment_uri.fragment = ''
schema = lexical_resources[empty_fragment_uri]
schema = resources.lexical(empty_fragment_uri)
end

unless schema
Expand All @@ -196,7 +195,7 @@ def resolve_ref(uri)
)
remote_uri = remote_schema.base_uri.dup
remote_uri.fragment = location_independent_identifier if location_independent_identifier
schema = remote_schema.resources.fetch(:lexical).fetch(remote_uri)
schema = remote_schema.resources.lexical!(remote_uri)
end

schema = Hana::Pointer.parse(pointer).reduce(schema) do |obj, token|
Expand Down Expand Up @@ -328,7 +327,7 @@ def defs_keyword
end

def resources
@resources ||= { :lexical => {}, :dynamic => {} }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think instead of wrapping both of these in a single object, we should pull them into separate objects. Something like:

def resources
  @resources ||= { :lexical => Resources.new, :dynamic => Resources.new }
end

Or even separate lexical_resources and dynamic_resources methods.

And then on Resources you just have to define the Hash methods that we're using (#[], #[]=, #fetch, and #key?).

@resources ||= Resources.new
end

def error(formatted_instance_location:, **options)
Expand Down