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

Removed uuidtools #344

Open
wants to merge 1 commit into
base: master
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
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,3 @@ matrix:
gemfile: "gemfiles/Gemfile.multi_json.x"
- rvm: "2.3.1"
gemfile: "gemfiles/Gemfile.yajl-ruby.x"
- rvm: "2.3.1"
gemfile: "gemfiles/Gemfile.uuidtools.x"
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Changed
- Replaced UUIDs with SHA1 hashes as the cache key for parsed schemas

## [2.7.0] - 2016-09-29

### Fixed
Expand Down
5 changes: 0 additions & 5 deletions gemfiles/Gemfile.uuidtools.x

This file was deleted.

6 changes: 6 additions & 0 deletions lib/json-schema/util/uri.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'addressable/uri'
require 'digest/sha1'

module JSON
module Util
Expand Down Expand Up @@ -54,6 +55,11 @@ def self.file_uri(uri)
Addressable::URI.convert_path(parsed_uri.path)
end

def self.fake_uri(string)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably better to name this hash_uri

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that it should rather have hash in the name than fake. But actually I would not call it hash_uri because the method itself does not hash URIs but is used to hash a schema. So maybe hash_schema would be a better name. But now it actually does not care about whether the input is a schema or not, it takes a string and returns a URI created from the hash of that string. So maybe uri_from_sha1 oder uri_from_sha1_hash or something like that would be more precise. But also more verbose. I'm personally getting more and more a fan of being as precise as possible with naming methods as it eases reasoning about code (we likely have some worse candidates here). What do you think?

digest = Digest::SHA1.hexdigest(string)
parse(digest)
end

def self.unescape_uri(uri)
Addressable::URI.unescape(uri)
end
Expand Down
285 changes: 0 additions & 285 deletions lib/json-schema/util/uuid.rb

This file was deleted.

18 changes: 3 additions & 15 deletions lib/json-schema/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -507,14 +507,6 @@ def validators_for_names(names)

private

if Gem::Specification::find_all_by_name('uuidtools').any?
require 'uuidtools'
@@fake_uuid_generator = lambda{|s| UUIDTools::UUID.sha1_create(UUIDTools::UUID_URL_NAMESPACE, s).to_s }
else
require 'json-schema/util/uuid'
@@fake_uuid_generator = lambda{|s| JSON::Util::UUID.create_v5(s,JSON::Util::UUID::Nil).to_s }
end

def serialize schema
if defined?(MultiJson)
MultiJson.respond_to?(:dump) ? MultiJson.dump(schema) : MultiJson.encode(schema)
Expand All @@ -523,15 +515,11 @@ def serialize schema
end
end

def fake_uuid schema
@@fake_uuid_generator.call(schema)
end

def initialize_schema(schema)
if schema.is_a?(String)
begin
# Build a fake URI for this
schema_uri = JSON::Util::URI.parse(fake_uuid(schema))
schema_uri = JSON::Util::URI.fake_uri(schema)
schema = JSON::Schema.new(JSON::Validator.parse(schema), schema_uri, @options[:version])
if @options[:list] && @options[:fragment].nil?
schema = schema.to_array_schema
Expand All @@ -553,14 +541,14 @@ def initialize_schema(schema)
schema = self.class.schema_for_uri(schema_uri)
if @options[:list] && @options[:fragment].nil?
schema = schema.to_array_schema
schema.uri = JSON::Util::URI.parse(fake_uuid(serialize(schema.schema)))
schema.uri = JSON::Util::URI.fake_uri(serialize(schema.schema))
Validator.add_schema(schema)
end
schema
end
end
elsif schema.is_a?(Hash)
schema_uri = JSON::Util::URI.parse(fake_uuid(serialize(schema)))
schema_uri = JSON::Util::URI.fake_uri(serialize(schema))
schema = JSON::Schema.stringify(schema)
schema = JSON::Schema.new(schema, schema_uri, @options[:version])
if @options[:list] && @options[:fragment].nil?
Expand Down
11 changes: 11 additions & 0 deletions test/uri_util_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require File.expand_path('../support/test_helper', __FILE__)

class UriUtilTest < Minitest::Test
def test_fake_uri_is_unique
refute_equal JSON::Util::URI.fake_uri('foo'), JSON::Util::URI.fake_uri('bar')
end

def test_fake_uri_is_reproducible
assert_equal JSON::Util::URI.fake_uri('foo'), JSON::Util::URI.fake_uri('foo')
end
end