From 78f39c4568d22bd02b2a2fcdd4c5fb98008d7515 Mon Sep 17 00:00:00 2001 From: Jacob Kjeldahl Date: Mon, 1 Jul 2024 09:09:53 +0200 Subject: [PATCH 1/2] Changes dynamic and lexical schemas to be stored internally by the URI as a string Fixes #188 --- lib/json_schemer.rb | 1 + lib/json_schemer/draft201909/vocab/core.rb | 6 ++-- lib/json_schemer/draft202012/vocab/core.rb | 12 ++++---- lib/json_schemer/resources.rb | 34 ++++++++++++++++++++++ lib/json_schemer/schema.rb | 9 +++--- 5 files changed, 48 insertions(+), 14 deletions(-) create mode 100644 lib/json_schemer/resources.rb diff --git a/lib/json_schemer.rb b/lib/json_schemer.rb index 56a92bae..5d8f1477 100644 --- a/lib/json_schemer.rb +++ b/lib/json_schemer.rb @@ -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 diff --git a/lib/json_schemer/draft201909/vocab/core.rb b/lib/json_schemer/draft201909/vocab/core.rb index b06a938e..3c83d3b5 100644 --- a/lib/json_schemer/draft201909/vocab/core.rb +++ b/lib/json_schemer/draft201909/vocab/core.rb @@ -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 @@ -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 diff --git a/lib/json_schemer/draft202012/vocab/core.rb b/lib/json_schemer/draft202012/vocab/core.rb index c5d1182a..fabad404 100644 --- a/lib/json_schemer/draft202012/vocab/core.rb +++ b/lib/json_schemer/draft202012/vocab/core.rb @@ -37,7 +37,7 @@ 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 @@ -45,7 +45,7 @@ def parse 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 @@ -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 @@ -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 diff --git a/lib/json_schemer/resources.rb b/lib/json_schemer/resources.rb new file mode 100644 index 00000000..d2696928 --- /dev/null +++ b/lib/json_schemer/resources.rb @@ -0,0 +1,34 @@ +# 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][namespace_uri.to_s] + end + + def dynamic?(namespace_uri) + @resources[:dynamic].key?(namespace_uri.to_s) + end + end +end diff --git a/lib/json_schemer/schema.rb b/lib/json_schemer/schema.rb index 259e2417..04ea115e 100644 --- a/lib/json_schemer/schema.rb +++ b/lib/json_schemer/schema.rb @@ -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 @@ -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| @@ -328,7 +327,7 @@ def defs_keyword end def resources - @resources ||= { :lexical => {}, :dynamic => {} } + @resources ||= Resources.new end def error(formatted_instance_location:, **options) From 2ca36fd698e4b40abc225295ec35d5efcc0ac051 Mon Sep 17 00:00:00 2001 From: Jacob Kjeldahl Date: Thu, 11 Jul 2024 21:56:07 +0200 Subject: [PATCH 2/2] Removes unused method dynamic --- lib/json_schemer/resources.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/json_schemer/resources.rb b/lib/json_schemer/resources.rb index d2696928..5f136f1c 100644 --- a/lib/json_schemer/resources.rb +++ b/lib/json_schemer/resources.rb @@ -23,10 +23,6 @@ def dynamic!(namespace_uri) @resources[:dynamic].fetch(namespace_uri.to_s) end - def dynamic(namespace_uri) - @resources[:dynamic][namespace_uri.to_s] - end - def dynamic?(namespace_uri) @resources[:dynamic].key?(namespace_uri.to_s) end