Skip to content

Commit

Permalink
Merge pull request #189 from kjeldahl/main
Browse files Browse the repository at this point in the history
Changes dynamic and lexical schemas to be stored internally by the URI as a string
  • Loading branch information
davishmcclurg authored Aug 24, 2024
2 parents 1878fe8 + 2ca36fd commit 2ecf188
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 deletions.
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
end

def lexical!(namespace_uri)
@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 => {} }
@resources ||= Resources.new
end

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

0 comments on commit 2ecf188

Please sign in to comment.