Skip to content

Commit

Permalink
Merge fad0b83 into 0baa9fe
Browse files Browse the repository at this point in the history
  • Loading branch information
notEthan committed Oct 16, 2021
2 parents 0baa9fe + fad0b83 commit 02c3392
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 73 deletions.
14 changes: 7 additions & 7 deletions lib/jsi/base.rb
Expand Up @@ -46,12 +46,12 @@ def inspect
else
schema_names = jsi_class_schemas.map do |schema|
mod = schema.jsi_schema_module
if mod.name && schema.schema_id
"#{mod.name} (#{schema.schema_id})"
if mod.name && schema.schema_uri
"#{mod.name} (#{schema.schema_uri})"
elsif mod.name
mod.name
elsif schema.schema_id
schema.schema_id
elsif schema.schema_uri
schema.schema_uri.to_s
else
schema.jsi_ptr.uri
end
Expand Down Expand Up @@ -83,8 +83,8 @@ def schema_classes_const_name
schema_names = jsi_class_schemas.map do |schema|
if schema.jsi_schema_module.name
schema.jsi_schema_module.name
elsif schema.schema_id
schema.schema_id
elsif schema.schema_uri
schema.schema_uri.to_s
else
nil
end
Expand Down Expand Up @@ -429,7 +429,7 @@ def jsi_object_group_text
"#{class_name} (#{schema_module_names.join(', ')})"
end
else
schema_names = jsi_schemas.map { |schema| schema.jsi_schema_module.name || schema.schema_id }.compact
schema_names = jsi_schemas.map { |schema| schema.jsi_schema_module.name || schema.schema_uri }.compact
if schema_names.empty?
"JSI"
else
Expand Down
65 changes: 26 additions & 39 deletions lib/jsi/schema.rb
Expand Up @@ -266,49 +266,36 @@ def schema_absolute_uri
end
end

# @return [String, nil] an absolute id for the schema, with a json pointer fragment. nil if
# no parent of this schema defines an id.
def schema_id
return @schema_id if instance_variable_defined?(:@schema_id)
@schema_id = begin
# start from self and ascend parents looking for an 'id' property.
# append a fragment to that id (appending to an existing fragment if there
# is one) consisting of the path from that parent to our schema_node.
node_for_id = self
path_from_id_node = []
done = false

while !done
content_for_id = node_for_id.jsi_node_content
if node_for_id.is_a?(JSI::Schema) && content_for_id.respond_to?(:to_hash)
parent_id = content_for_id.key?('$id') && content_for_id['$id'].respond_to?(:to_str) ? content_for_id['$id'].to_str :
content_for_id.key?('id') && content_for_id['id'].respond_to?(:to_str) ? content_for_id['id'].to_str : nil
end
# @return [Addressable::URI, nil] a URI which refers to this schema.
# nil if no parent of this schema defines an id.
# see {#schema_uris} for all URIs known to refer to this schema.
def schema_uri
schema_uris.first
end

if parent_id || node_for_id.jsi_ptr.root?
done = true
else
path_from_id_node.unshift(node_for_id.jsi_ptr.reference_tokens.last)
node_for_id = node_for_id.jsi_parent_node
end
end
if parent_id
parent_auri = Addressable::URI.parse(parent_id)
if parent_auri.fragment
# add onto the fragment
parent_id_path = Ptr.from_fragment(parent_auri.fragment).reference_tokens
path_from_id_node = parent_id_path + path_from_id_node
parent_auri.fragment = nil
#else: no fragment so parent_id good as is
end
# @return [Array<Addressable::URI>] URIs which refer to this schema
def schema_uris
jsi_memoize(:schema_uris) do
each_schema_uri.to_a
end
end

schema_id = parent_auri.merge(fragment: Ptr.new(path_from_id_node).fragment).to_s
# @yield [Addressable::URI] each URI which refers to this schema
def each_schema_uri
return to_enum(__method__) unless block_given?

schema_id
else
nil
end
yield schema_absolute_uri if schema_absolute_uri

parent_schemas = jsi_subschema_resource_ancestors.reverse_each.select do |resource|
resource.is_a?(Schema) && resource.schema_absolute_uri
end

parent_schemas.each do |parent_schema|
relative_ptr = self.jsi_ptr.ptr_relative_to(parent_schema.jsi_ptr)
yield parent_schema.schema_absolute_uri.merge(fragment: relative_ptr.fragment)
end

nil
end

# @return [Module] a module representing this schema. see {JSI::SchemaClasses.module_for_schema}.
Expand Down
10 changes: 5 additions & 5 deletions lib/jsi/schema_classes.rb
Expand Up @@ -3,15 +3,15 @@
module JSI
# JSI Schema Modules are extended with JSI::SchemaModule
module SchemaModule
# @return [String] absolute schema_id of the schema this module represents.
# see {Schema#schema_id}.
def schema_id
schema.schema_id
# @return [String] absolute schema_uri of the schema this module represents.
# see {Schema#schema_uri}.
def schema_uri
schema.schema_uri
end

# @return [String]
def inspect
uri = schema.schema_id || schema.jsi_ptr.uri
uri = schema.schema_uri || schema.jsi_ptr.uri
if name
"#{name} (#{uri})"
else
Expand Down
8 changes: 4 additions & 4 deletions test/base_array_test.rb
Expand Up @@ -188,28 +188,28 @@
let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/base_array_test/withid', 'items' => [{}, {}, {}]} }
let(:subject) { schema.new_jsi(instance) }
it 'inspects' do
assert_equal("#[<JSI (https://schemas.jsi.unth.net/base_array_test/withid#)> \"foo\", \#{<JSI (https://schemas.jsi.unth.net/base_array_test/withid#/items/1)> \"lamp\" => [3]}, #[<JSI (https://schemas.jsi.unth.net/base_array_test/withid#/items/2)> \"q\", \"r\"], {\"four\"=>4}]", subject.inspect)
assert_equal("#[<JSI (https://schemas.jsi.unth.net/base_array_test/withid)> \"foo\", \#{<JSI (https://schemas.jsi.unth.net/base_array_test/withid#/items/1)> \"lamp\" => [3]}, #[<JSI (https://schemas.jsi.unth.net/base_array_test/withid#/items/2)> \"q\", \"r\"], {\"four\"=>4}]", subject.inspect)
end
end
describe '#pretty_print with id' do
let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/base_array_test/withid', 'items' => [{}, {}, {}]} }
let(:subject) { schema.new_jsi(instance) }
it 'pretty prints' do
assert_equal("#[<JSI (https://schemas.jsi.unth.net/base_array_test/withid#)>\n \"foo\",\n \#{<JSI (https://schemas.jsi.unth.net/base_array_test/withid#/items/1)>\n \"lamp\" => [3]\n },\n #[<JSI (https://schemas.jsi.unth.net/base_array_test/withid#/items/2)>\n \"q\",\n \"r\"\n ],\n {\"four\"=>4}\n]\n", subject.pretty_inspect)
assert_equal("#[<JSI (https://schemas.jsi.unth.net/base_array_test/withid)>\n \"foo\",\n \#{<JSI (https://schemas.jsi.unth.net/base_array_test/withid#/items/1)>\n \"lamp\" => [3]\n },\n #[<JSI (https://schemas.jsi.unth.net/base_array_test/withid#/items/2)>\n \"q\",\n \"r\"\n ],\n {\"four\"=>4}\n]\n", subject.pretty_inspect)
end
end
describe '#inspect with id SortOfArray' do
let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/base_array_test/withid', 'items' => [{}, {}, {}]} }
let(:subject) { schema.new_jsi(SortOfArray.new(instance)) }
it 'inspects' do
assert_equal("#[<JSI (https://schemas.jsi.unth.net/base_array_test/withid#) SortOfArray> \"foo\", \#{<JSI (https://schemas.jsi.unth.net/base_array_test/withid#/items/1)> \"lamp\" => [3]}, #[<JSI (https://schemas.jsi.unth.net/base_array_test/withid#/items/2)> \"q\", \"r\"], {\"four\"=>4}]", subject.inspect)
assert_equal("#[<JSI (https://schemas.jsi.unth.net/base_array_test/withid) SortOfArray> \"foo\", \#{<JSI (https://schemas.jsi.unth.net/base_array_test/withid#/items/1)> \"lamp\" => [3]}, #[<JSI (https://schemas.jsi.unth.net/base_array_test/withid#/items/2)> \"q\", \"r\"], {\"four\"=>4}]", subject.inspect)
end
end
describe '#pretty_print with id SortOfArray' do
let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/base_array_test/withid', 'items' => [{}, {}, {}]} }
let(:subject) { schema.new_jsi(SortOfArray.new(instance)) }
it 'inspects' do
assert_equal("#[<JSI (https://schemas.jsi.unth.net/base_array_test/withid#) SortOfArray>\n \"foo\",\n \#{<JSI (https://schemas.jsi.unth.net/base_array_test/withid#/items/1)>\n \"lamp\" => [3]\n },\n #[<JSI (https://schemas.jsi.unth.net/base_array_test/withid#/items/2)>\n \"q\",\n \"r\"\n ],\n {\"four\"=>4}\n]\n", subject.pretty_inspect)
assert_equal("#[<JSI (https://schemas.jsi.unth.net/base_array_test/withid) SortOfArray>\n \"foo\",\n \#{<JSI (https://schemas.jsi.unth.net/base_array_test/withid#/items/1)>\n \"lamp\" => [3]\n },\n #[<JSI (https://schemas.jsi.unth.net/base_array_test/withid#/items/2)>\n \"q\",\n \"r\"\n ],\n {\"four\"=>4}\n]\n", subject.pretty_inspect)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions test/base_hash_test.rb
Expand Up @@ -232,28 +232,28 @@
let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/base_hash_test/withid', 'properties' => {'foo' => {}, 'bar' => {}}} }
let(:subject) { schema.new_jsi(instance) }
it 'inspects' do
assert_equal("\#{<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#)> \"foo\" => \#{<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#/properties/foo)> \"x\" => \"y\"}, \"bar\" => #[<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#/properties/bar)> 9], \"baz\" => [true]}", subject.inspect)
assert_equal("\#{<JSI (https://schemas.jsi.unth.net/base_hash_test/withid)> \"foo\" => \#{<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#/properties/foo)> \"x\" => \"y\"}, \"bar\" => #[<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#/properties/bar)> 9], \"baz\" => [true]}", subject.inspect)
end
end
describe '#pretty_print with id' do
let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/base_hash_test/withid', 'properties' => {'foo' => {}, 'bar' => {}}} }
let(:subject) { schema.new_jsi(instance) }
it 'inspects' do
assert_equal("\#{<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#)>\n \"foo\" => \#{<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#/properties/foo)>\n \"x\" => \"y\"\n },\n \"bar\" => #[<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#/properties/bar)>\n 9\n ],\n \"baz\" => [true]\n}\n", subject.pretty_inspect)
assert_equal("\#{<JSI (https://schemas.jsi.unth.net/base_hash_test/withid)>\n \"foo\" => \#{<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#/properties/foo)>\n \"x\" => \"y\"\n },\n \"bar\" => #[<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#/properties/bar)>\n 9\n ],\n \"baz\" => [true]\n}\n", subject.pretty_inspect)
end
end
describe '#inspect with id SortOfHash' do
let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/base_hash_test/withid', 'properties' => {'foo' => {}, 'bar' => {}}} }
let(:subject) { schema.new_jsi(SortOfHash.new(instance)) }
it 'inspects' do
assert_equal("\#{<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#) SortOfHash> \"foo\" => \#{<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#/properties/foo)> \"x\" => \"y\"}, \"bar\" => #[<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#/properties/bar)> 9], \"baz\" => [true]}", subject.inspect)
assert_equal("\#{<JSI (https://schemas.jsi.unth.net/base_hash_test/withid) SortOfHash> \"foo\" => \#{<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#/properties/foo)> \"x\" => \"y\"}, \"bar\" => #[<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#/properties/bar)> 9], \"baz\" => [true]}", subject.inspect)
end
end
describe '#pretty_print with id SortOfHash' do
let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/base_hash_test/withid', 'properties' => {'foo' => {}, 'bar' => {}}} }
let(:subject) { schema.new_jsi(SortOfHash.new(instance)) }
it 'inspects' do
assert_equal("\#{<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#) SortOfHash>\n \"foo\" => \#{<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#/properties/foo)>\n \"x\" => \"y\"\n },\n \"bar\" => #[<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#/properties/bar)>\n 9\n ],\n \"baz\" => [true]\n}\n", subject.pretty_inspect)
assert_equal("\#{<JSI (https://schemas.jsi.unth.net/base_hash_test/withid) SortOfHash>\n \"foo\" => \#{<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#/properties/foo)>\n \"x\" => \"y\"\n },\n \"bar\" => #[<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#/properties/bar)>\n 9\n ],\n \"baz\" => [true]\n}\n", subject.pretty_inspect)
end
end
describe '#inspect jsi_object_group_text' do
Expand Down
14 changes: 7 additions & 7 deletions test/base_test.rb
Expand Up @@ -21,21 +21,21 @@
describe 'with schema id' do
let(:schema_content) { {'$id' => 'https://jsi/foo'} }
it 'is (JSI Schema Class: ...) for generated subclass with id' do
assert_equal("(JSI Schema Class: https://jsi/foo#)", subject.class.inspect)
assert_equal("(JSI Schema Class: https://jsi/foo)", subject.class.inspect)
end
end
it 'is the constant name plus id for a class assigned to a constant' do
assert_equal(%q(NamedSchemaInstance (https://schemas.jsi.unth.net/test/base/named_schema#)), NamedSchemaInstance.inspect)
assert_equal(%q(NamedSchemaInstance (https://schemas.jsi.unth.net/test/base/named_schema)), NamedSchemaInstance.inspect)
end
it 'is not the constant name when the constant name has been generated from the schema_id' do
assert_equal("JSI::SchemaClasses::Xhttps___schemas_jsi_unth_net_test_base_named_schema_two_", NamedSchemaInstanceTwo.name)
assert_equal("(JSI Schema Class: https://schemas.jsi.unth.net/test/base/named_schema_two#)", NamedSchemaInstanceTwo.inspect)
it 'is not the constant name when the constant name has been generated from the schema_uri' do
assert_equal("JSI::SchemaClasses::Xhttps___schemas_jsi_unth_net_test_base_named_schema_two", NamedSchemaInstanceTwo.name)
assert_equal("(JSI Schema Class: https://schemas.jsi.unth.net/test/base/named_schema_two)", NamedSchemaInstanceTwo.inspect)
end
end
describe 'class name' do
let(:schema_content) { {'$id' => 'https://jsi/BaseTest'} }
it 'generates a class name from schema_id' do
assert_equal('JSI::SchemaClasses::Xhttps___jsi_BaseTest_', subject.class.name)
it 'generates a class name from schema_uri' do
assert_equal('JSI::SchemaClasses::Xhttps___jsi_BaseTest', subject.class.name)
end
it 'uses an existing name' do
assert_equal('NamedSchemaInstance', NamedSchemaInstance.name)
Expand Down
35 changes: 28 additions & 7 deletions test/schema_test.rb
Expand Up @@ -24,33 +24,54 @@
assert_equal({'description' => 'items!'}, schema_items.as_json)
end
end
describe '#schema_id' do
describe '#schema_uri' do
it "hasn't got one" do
assert_nil(JSI.new_schema({}).schema_id)
assert_nil(JSI.new_schema({}).schema_uri)
end
it 'uses a given id with a fragment' do
schema = JSI.new_schema({'$id' => 'https://schemas.jsi.unth.net/test/given_id_with_fragment#'})
assert_equal('https://schemas.jsi.unth.net/test/given_id_with_fragment#', schema.schema_id)
assert_equal(Addressable::URI.parse('https://schemas.jsi.unth.net/test/given_id_with_fragment#'), schema.schema_uri)
end
it 'uses a given id (adding a fragment)' do
schema = JSI.new_schema({'$id' => 'https://schemas.jsi.unth.net/test/given_id'})
assert_equal('https://schemas.jsi.unth.net/test/given_id#', schema.schema_id)
assert_equal(Addressable::URI.parse('https://schemas.jsi.unth.net/test/given_id#'), schema.schema_uri)
end
it 'uses a pointer in the fragment' do
schema = JSI.new_schema({
'$id' => 'https://schemas.jsi.unth.net/test/uses_pointer_in_fragment#',
'properties' => {'foo' => {'type' => 'object'}},
})
subschema = schema['properties']['foo']
assert_equal('https://schemas.jsi.unth.net/test/uses_pointer_in_fragment#/properties/foo', subschema.schema_id)
assert_equal(Addressable::URI.parse('https://schemas.jsi.unth.net/test/uses_pointer_in_fragment#/properties/foo'), subschema.schema_uri)
end
it 'uses a pointer in the fragment relative to the fragment of the root' do
it 'uses a pointer in the fragment, ignoring a pointer in the fragment of the root id' do
schema = JSI.new_schema({
'$id' => 'https://schemas.jsi.unth.net/test/id_has_pointer#/notroot',
'properties' => {'foo' => {'type' => 'object'}},
})
subschema = schema['properties']['foo']
assert_equal('https://schemas.jsi.unth.net/test/id_has_pointer#/notroot/properties/foo', subschema.schema_id)
assert_equal(Addressable::URI.parse('https://schemas.jsi.unth.net/test/id_has_pointer#/properties/foo'), subschema.schema_uri)
end
end
describe '#schema_uris' do
let(:schema) { JSI.new_schema(schema_content) }
describe 'two ids' do
let(:schema_content) do
{
"$id": "https://example.com/foo",
"items": {
"$id": "https://example.com/bar",
"additionalProperties": { }
}
}
end
it 'has its absolute URI and both by pointer fragment' do
assert_equal([
Addressable::URI.parse("https://example.com/bar"),
Addressable::URI.parse("https://example.com/bar#"),
Addressable::URI.parse("https://example.com/foo#/items"),
], schema.items.schema_uris)
end
end
end
describe '#schema_absolute_uri, #anchor' do
Expand Down

0 comments on commit 02c3392

Please sign in to comment.