Skip to content

Commit 7925810

Browse files
committed
Lower minimum Ruby version to 2.5 and make JSON duplicate key detection a runtime feature check instead of a hard dependency on json >= 2.13.0
1 parent 11450ac commit 7925810

4 files changed

Lines changed: 20 additions & 4 deletions

File tree

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ jobs:
3333
os:
3434
- ubuntu-latest
3535
ruby:
36+
- "2.5"
37+
- "2.6"
3638
- "2.7"
3739
- "3.0"
3840
- "3.1"

.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
AllCops:
2-
TargetRubyVersion: 2.7
2+
TargetRubyVersion: 2.5
33
NewCops: enable
44
SuggestExtensions: false
55
Exclude:

lib/jwt/json.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,26 @@ def generate(data)
2828
# JWT::JSON.parse('{"a":1,"a":2}', allow_duplicate_keys: false)
2929
# # => raises JWT::DuplicateKeyError
3030
def parse(data, allow_duplicate_keys: true)
31-
::JSON.parse(data, allow_duplicate_key: allow_duplicate_keys)
31+
return ::JSON.parse(data) if allow_duplicate_keys
32+
33+
if supports_duplicate_key_detection?
34+
::JSON.parse(data, allow_duplicate_key: false)
35+
else
36+
::JSON.parse(data)
37+
end
3238
rescue ::JSON::ParserError => e
3339
raise JWT::DuplicateKeyError, e.message if e.message.include?('duplicate key')
3440

3541
raise
3642
end
43+
44+
private
45+
46+
def supports_duplicate_key_detection?
47+
return @supports_duplicate_key_detection if defined?(@supports_duplicate_key_detection)
48+
49+
@supports_duplicate_key_detection = Gem::Version.new(::JSON::VERSION) >= Gem::Version.new('2.13.0')
50+
end
3751
end
3852
end
3953
end

ruby-jwt.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
1515
spec.description = 'A pure ruby implementation of the RFC 7519 OAuth JSON Web Token (JWT) standard.'
1616
spec.homepage = 'https://github.com/jwt/ruby-jwt'
1717
spec.license = 'MIT'
18-
spec.required_ruby_version = '>= 2.7'
18+
spec.required_ruby_version = '>= 2.5'
1919
spec.metadata = {
2020
'bug_tracker_uri' => 'https://github.com/jwt/ruby-jwt/issues',
2121
'changelog_uri' => "https://github.com/jwt/ruby-jwt/blob/v#{JWT.gem_version}/CHANGELOG.md",
@@ -32,7 +32,7 @@ Gem::Specification.new do |spec|
3232
spec.require_paths = %w[lib]
3333

3434
spec.add_dependency 'base64'
35-
spec.add_dependency 'json', '>= 2.13.0'
35+
spec.add_dependency 'json'
3636

3737
spec.add_development_dependency 'appraisal'
3838
spec.add_development_dependency 'bundler'

0 commit comments

Comments
 (0)