1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
require "set"
module JsonSchema
class ReferenceExpander
attr_accessor :errors
attr_accessor :store
def expand(schema, options = {})
@errors = []
@local_store = DocumentStore.new
@schema = schema
@schema_paths = {}
@store = options[:store] || DocumentStore.new
# If the given JSON schema is _just_ a JSON reference and nothing else,
# short circuit the whole expansion process and return the result.
if schema.reference && !schema.expanded?
return dereference(schema, [])
end
@uri = URI.parse(schema.uri)
@store.each do |uri, store_schema|
build_schema_paths(uri, store_schema)
end
# we run #to_s on lookup for URIs; the #to_s of nil is ""
build_schema_paths("", schema)
traverse_schema(schema)
refs = unresolved_refs(schema).sort
if refs.count > 0
message = %{Couldn't resolve references: #{refs.to_a.join(", ")}.}
@errors << SchemaError.new(schema, message, :unresolved_references)
end
@errors.count == 0
end
def expand!(schema, options = {})
if !expand(schema, options)
raise AggregateError.new(@errors)
end
true
end
private
def add_reference(schema)
uri = URI.parse(schema.uri)
# In case we've already added a schema for the same reference, don't
# re-add it unless the new schema's pointer path is shorter than the one
# we've already stored.
stored_schema = lookup_reference(uri)
if stored_schema && stored_schema.pointer.length < schema.pointer.length
return
end
if uri.absolute?
@store.add_schema(schema)
else
@local_store.add_schema(schema)
end
end
def build_schema_paths(uri, schema)
return if schema.reference
paths = @schema_paths[uri] ||= {}
paths[schema.pointer] = schema
schema_children(schema) do |subschema|
build_schema_paths(uri, subschema)
end
# Also insert alternate tree for schema's custom URI. O(crazy).
if schema.uri != uri
fragment, parent = schema.fragment, schema.parent
schema.fragment, schema.parent = "#", nil
build_schema_paths(schema.uri, schema)
schema.fragment, schema.parent = fragment, parent
end
end
def dereference(ref_schema, ref_stack, parent_ref: nil)
ref = ref_schema.reference
# Some schemas don't have a reference, but do
# have children. If that's the case, we need to
# dereference the subschemas.
if !ref
schema_children(ref_schema) do |subschema|
next unless subschema.reference
next if ref_schema.uri == parent_ref.uri.to_s
if !subschema.reference.uri && parent_ref
subschema.reference = JsonReference::Reference.new("#{parent_ref.uri}#{subschema.reference.pointer}")
end
dereference(subschema, ref_stack)
end
return true
end
# detects a reference cycle
if ref_stack.include?(ref)
message = %{Reference loop detected: #{ref_stack.sort.join(", ")}.}
@errors << SchemaError.new(ref_schema, message, :loop_detected)
return false
end
new_schema = resolve_reference(ref_schema)
return false unless new_schema
# if the reference resolved to a new reference we need to continue
# dereferencing until we either hit a non-reference schema, or a
# reference which is already resolved
if new_schema.reference && !new_schema.expanded?
success = dereference(new_schema, ref_stack + [ref])
return false unless success
end
# If the reference schema is a global reference
# then we'll need to manually expand any nested
# references.
if ref.uri
schema_children(new_schema) do |subschema|
# Don't bother if the subschema points to the same
# schema as the reference schema.
next if ref_schema == subschema
if subschema.reference
# If the subschema has a reference, then
# we don't need to recurse if the schema is
# already expanded.
next if subschema.expanded?
if !subschema.reference.uri
# the subschema's ref is local to the file that the
# subschema is in; however since there's no URI
# the 'resolve_pointer' method would try to look it up
# within @schema. So: manually reconstruct the reference to
# use the URI of the parent ref.
subschema.reference = JsonReference::Reference.new("#{ref.uri}#{subschema.reference.pointer}")
end
end
if subschema.items && subschema.items.reference
next if subschema.expanded?
if !subschema.items.reference.uri
# The subschema's ref is local to the file that the
# subschema is in. Manually reconstruct the reference
# so we can resolve it.
subschema.items.reference = JsonReference::Reference.new("#{ref.uri}#{subschema.items.reference.pointer}")
end
end
# If we're recursing into a schema via a global reference, then if
# the current subschema doesn't have a reference, we have no way of
# figuring out what schema we're in. The resolve_pointer method will
# default to looking it up in the initial schema. Instead, we're
# passing the parent ref here, so we can grab the URI
# later if needed.
dereference(subschema, ref_stack, parent_ref: ref)
end
end
# copy new schema into existing one while preserving parent, fragment,
# and reference
parent = ref_schema.parent
ref_schema.copy_from(new_schema)
ref_schema.parent = parent
# correct all parent references to point back to ref_schema instead of
# new_schema
if ref_schema.original?
schema_children(ref_schema) do |schema|
schema.parent = ref_schema
end
end
true
end
def lookup_pointer(uri, pointer)
paths = @schema_paths[uri.to_s] ||= {}
paths[pointer]
end
def lookup_reference(uri)
if uri.absolute?
@store.lookup_schema(uri.to_s)
else
@local_store.lookup_schema(uri.to_s)
end
end
def resolve_pointer(ref_schema, resolved_schema)
ref = ref_schema.reference
if !(new_schema = lookup_pointer(ref.uri, ref.pointer))
new_schema = JsonPointer.evaluate(resolved_schema, ref.pointer)
# couldn't resolve pointer within known schema; that's an error
if new_schema.nil?
message = %{Couldn't resolve pointer "#{ref.pointer}".}
@errors << SchemaError.new(resolved_schema, message, :unresolved_pointer)
return
end
# Try to aggressively detect a circular dependency in case of another
# reference. See:
#
# https://github.com/brandur/json_schema/issues/50
#
if new_schema.reference &&
new_new_schema = lookup_pointer(ref.uri, new_schema.reference.pointer)
new_new_schema.clones << ref_schema
else
# Parse a new schema and use the same parent node. Basically this is
# exclusively for the case of a reference that needs to be
# de-referenced again to be resolved.
build_schema_paths(ref.uri, resolved_schema)
end
else
# insert a clone record so that the expander knows to expand it when
# the schema traversal is finished
new_schema.clones << ref_schema
end
new_schema
end
def resolve_reference(ref_schema)
ref = ref_schema.reference
uri = ref.uri
if uri && uri.host
scheme = uri.scheme || "http"
# allow resolution if something we've already parsed has claimed the
# full URL
if @store.lookup_schema(uri.to_s)
resolve_uri(ref_schema, uri)
else
message =
%{Reference resolution over #{scheme} is not currently supported (URI: #{uri}).}
@errors << SchemaError.new(ref_schema, message, :scheme_not_supported)
nil
end
# absolute
elsif uri && uri.path[0] == "/"
resolve_uri(ref_schema, uri)
# relative
elsif uri
# Build an absolute path using the URI of the current schema.
#
# Note that this code path will never currently be hit because the
# incoming reference schema will never have a URI.
if ref_schema.uri
schema_uri = ref_schema.uri.chomp("/")
resolve_uri(ref_schema, URI.parse(schema_uri + "/" + uri.path))
else
nil
end
# just a JSON Pointer -- resolve against schema root
else
resolve_pointer(ref_schema, @schema)
end
end
def resolve_uri(ref_schema, uri)
if schema = lookup_reference(uri)
resolve_pointer(ref_schema, schema)
else
message = %{Couldn't resolve URI: #{uri.to_s}.}
@errors << SchemaError.new(ref_schema, message, :unresolved_pointer)
nil
end
end
def schema_children(schema)
schema.all_of.each { |s| yield s }
schema.any_of.each { |s| yield s }
schema.one_of.each { |s| yield s }
schema.definitions.each { |_, s| yield s }
schema.pattern_properties.each { |_, s| yield s }
schema.properties.each { |_, s| yield s }
if additional = schema.additional_properties
if additional.is_a?(Schema)
yield additional
end
end
if schema.not
yield schema.not
end
# can either be a single schema (list validation) or multiple (tuple
# validation)
if items = schema.items
if items.is_a?(Array)
items.each { |s| yield s }
else
yield items
end
end
# dependencies can either be simple or "schema"; only replace the
# latter
schema.dependencies.
each_value { |s| yield s if s.is_a?(Schema) }
# schemas contained inside hyper-schema links objects
if schema.links
schema.links.each { |l|
yield l.schema if l.schema
yield l.target_schema if l.target_schema
}
end
end
def unresolved_refs(schema)
# prevent endless recursion
return [] unless schema.original?
arr = []
schema_children(schema) do |subschema|
if !subschema.expanded?
arr += [subschema.reference]
else
arr += unresolved_refs(subschema)
end
end
arr
end
def traverse_schema(schema)
add_reference(schema)
schema_children(schema) do |subschema|
if subschema.reference && !subschema.expanded?
dereference(subschema, [])
end
if !subschema.reference
traverse_schema(subschema)
end
end
# after finishing a schema traversal, find all clones and re-hydrate them
if schema.original?
schema.clones.each do |clone_schema|
parent = clone_schema.parent
clone_schema.copy_from(schema)
clone_schema.parent = parent
end
end
end
end
end
|