Skip to content

Commit f905d53

Browse files
authored
Fix/ruby27 compat and ci
2 parents e9a6d27 + 342a8b4 commit f905d53

5 files changed

Lines changed: 120 additions & 24 deletions

File tree

.github/workflows/ci.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ jobs:
1111
strategy:
1212
fail-fast: false
1313
matrix:
14-
ruby: ['2.7', '3.0', '3.1']
14+
include:
15+
- ruby: '2.7'
16+
- ruby: '3.0'
17+
- ruby: '3.1'
18+
- ruby: '3.2'
19+
- ruby: '3.3'
20+
- ruby: '3.4'
1521

1622
steps:
1723
- uses: actions/checkout@v4

lib/jsonapi/swagger/json.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,20 @@ def initialize(path = 'swagger/v1/swagger.json')
99
end
1010

1111
def parse_doc
12-
@doc ||= JSON.parse(load) rescue Hash.new{ |h, k| h[k]= {} }
12+
@doc ||= begin
13+
parsed = JSON.parse(load)
14+
15+
doc = Hash.new { |h, k| h[k] = {} }
16+
doc.merge!(parsed) if parsed.is_a?(Hash)
17+
18+
if doc.key?('paths') && !doc['paths'].is_a?(Hash)
19+
doc['paths'] = {}
20+
end
21+
22+
doc
23+
rescue StandardError
24+
Hash.new { |h, k| h[k] = {} }
25+
end
1326
end
1427

1528
def base_path
@@ -28,4 +41,4 @@ def load
2841

2942
end
3043
end
31-
end
44+
end

spec/integration/swagger_generator_spec.rb

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ class NamedBase
1313
class << self
1414
attr_accessor :_source_root, :_destination_root
1515

16-
def desc(*) = nil
16+
def desc(*)
17+
nil
18+
end
1719

1820
def source_root(path = nil)
1921
self._source_root = path if path
@@ -77,13 +79,33 @@ def template(src, dest)
7779
relation.define_singleton_method(:belongs_to?) { true }
7880

7981
resource = Class.new(JSONAPI::Resource) do
80-
def self._attributes = { id: nil, name: nil }
81-
def self._relationships = { company: OpenStruct.new(class_name: 'Company', table_name: 'companies') }
82-
def self.sortable_fields = [:name]
83-
def self.creatable_fields = [:name]
84-
def self.updatable_fields = [:name]
85-
def self.filters = { name: {} }
86-
def self.mutable? = true
82+
def self._attributes
83+
{ id: nil, name: nil }
84+
end
85+
86+
def self._relationships
87+
{ company: OpenStruct.new(class_name: 'Company', table_name: 'companies') }
88+
end
89+
90+
def self.sortable_fields
91+
[:name]
92+
end
93+
94+
def self.creatable_fields
95+
[:name]
96+
end
97+
98+
def self.updatable_fields
99+
[:name]
100+
end
101+
102+
def self.filters
103+
{ name: {} }
104+
end
105+
106+
def self.mutable?
107+
true
108+
end
87109
end
88110

89111
# Ensure relationship object has belongs_to? and table_name.

spec/jsonapi/swagger/resource_spec.rb

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,33 @@
1414

1515
it 'wraps JSONAPI::Resource subclasses' do
1616
klass = Class.new(JSONAPI::Resource) do
17-
def self._attributes = { id: {}, name: {} }
18-
def self._relationships = {}
19-
def self.sortable_fields = []
20-
def self.creatable_fields = []
21-
def self.updatable_fields = []
22-
def self.filters = {}
23-
def self.mutable? = false
17+
def self._attributes
18+
{ id: {}, name: {} }
19+
end
20+
21+
def self._relationships
22+
{}
23+
end
24+
25+
def self.sortable_fields
26+
[]
27+
end
28+
29+
def self.creatable_fields
30+
[]
31+
end
32+
33+
def self.updatable_fields
34+
[]
35+
end
36+
37+
def self.filters
38+
{}
39+
end
40+
41+
def self.mutable?
42+
false
43+
end
2444
end
2545
stub_const('UserResource', klass)
2646

@@ -31,10 +51,21 @@ def self.mutable? = false
3151

3252
it 'wraps JSONAPI::Serializable::Resource subclasses' do
3353
klass = Class.new(JSONAPI::Serializable::Resource) do
34-
def self.type_val = :users
35-
def self.attribute_blocks = { id: nil, name: nil }
36-
def self.relationship_blocks = { company: nil }
37-
def self.link_blocks = {}
54+
def self.type_val
55+
:users
56+
end
57+
58+
def self.attribute_blocks
59+
{ id: nil, name: nil }
60+
end
61+
62+
def self.relationship_blocks
63+
{ company: nil }
64+
end
65+
66+
def self.link_blocks
67+
{}
68+
end
3869
end
3970
stub_const('SerializableUser', klass)
4071

@@ -46,8 +77,13 @@ def self.link_blocks = {}
4677

4778
it 'wraps FastJsonapi::ObjectSerializer subclasses' do
4879
klass = Class.new(FastJsonapi::ObjectSerializer) do
49-
def self.attributes_to_serialize = { id: {}, name: {} }
50-
def self.relationships_to_serialize = {}
80+
def self.attributes_to_serialize
81+
{ id: {}, name: {} }
82+
end
83+
84+
def self.relationships_to_serialize
85+
{}
86+
end
5187
end
5288
stub_const('UserSerializer', klass)
5389

spec/spec_helper.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require 'json'
44

5+
require 'logger'
56
require 'active_support'
67
require 'active_support/core_ext/hash/except'
78
require 'active_support/core_ext/object/blank'
@@ -14,6 +15,24 @@
1415
require 'jsonapi/swagger'
1516

1617
RSpec.configure do |config|
18+
config.around do |example|
19+
swagger = Jsonapi::Swagger
20+
ivars = %i[
21+
@version
22+
@info
23+
@file_path
24+
@base_path
25+
@use_rswag
26+
@attribute_default
27+
]
28+
29+
saved = ivars.to_h { |ivar| [ivar, swagger.instance_variable_get(ivar)] }
30+
31+
example.run
32+
ensure
33+
saved.each { |ivar, value| swagger.instance_variable_set(ivar, value) }
34+
end
35+
1736
config.disable_monkey_patching!
1837
config.order = :random
1938
Kernel.srand config.seed

0 commit comments

Comments
 (0)