Skip to content

Commit a4190cf

Browse files
authored
Grape 2.2.0 compatibility (#940)
* Run `rubocop -a` * Update changelog * Run CI against grape 2.1.x and 2.2.x * Copy formatter and content type defaults from grape * Use matrix to reduce duplication in CI workflow
1 parent a5e2575 commit a4190cf

File tree

6 files changed

+59
-106
lines changed

6 files changed

+59
-106
lines changed

.github/workflows/ci.yml

Lines changed: 35 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -22,118 +22,52 @@ jobs:
2222
bundler-cache: true
2323
- name: Run rubocop
2424
run: bundle exec rubocop --parallel --format progress
25-
26-
grape-17:
27-
runs-on: ubuntu-latest
28-
needs: ['rubocop']
29-
env:
30-
GRAPE_VERSION: '1.7.1'
25+
test:
3126
strategy:
3227
matrix:
33-
ruby-version: ['3.0', '3.1', '3.2', '3.3', 'head']
34-
steps:
35-
- name: Check out branch
36-
uses: actions/checkout@v4
37-
- name: Set up Ruby
38-
uses: ruby/setup-ruby@v1
39-
with:
40-
ruby-version: ${{ matrix.ruby-version }}
41-
- name: Run rspec wo model parser
42-
run: |
43-
bundle update
44-
bundle exec rspec
45-
- name: Run rspec w entity parser
46-
env:
47-
MODEL_PARSER: grape-swagger-entity
48-
run: |
49-
bundle update
50-
bundle exec rspec
51-
- name: Run rspec w representable parser
52-
env:
53-
MODEL_PARSER: grape-swagger-representable
54-
run: |
55-
bundle update
56-
bundle exec rspec
57-
58-
grape-18:
28+
entry:
29+
- { ruby: '3.0', grape: '1.7.1' }
30+
- { ruby: '3.1', grape: '1.7.1' }
31+
- { ruby: '3.2', grape: '1.7.1' }
32+
- { ruby: '3.3', grape: '1.7.1' }
33+
- { ruby: 'head', grape: '1.7.1' }
34+
- { ruby: '3.0', grape: '1.8.0' }
35+
- { ruby: '3.1', grape: '1.8.0' }
36+
- { ruby: '3.2', grape: '1.8.0' }
37+
- { ruby: '3.3', grape: '1.8.0' }
38+
- { ruby: 'head', grape: '1.8.0' }
39+
- { ruby: '3.0', grape: '2.0.0' }
40+
- { ruby: '3.1', grape: '2.0.0' }
41+
- { ruby: '3.2', grape: '2.0.0' }
42+
- { ruby: '3.3', grape: '2.0.0' }
43+
- { ruby: 'head', grape: '2.0.0' }
44+
- { ruby: '3.0', grape: '2.1.3' }
45+
- { ruby: '3.1', grape: '2.1.3' }
46+
- { ruby: '3.2', grape: '2.1.3' }
47+
- { ruby: '3.3', grape: '2.1.3' }
48+
- { ruby: 'head', grape: '2.1.3' }
49+
- { ruby: '3.0', grape: '2.2.0' }
50+
- { ruby: '3.1', grape: '2.2.0' }
51+
- { ruby: '3.2', grape: '2.2.0' }
52+
- { ruby: '3.3', grape: '2.2.0' }
53+
- { ruby: 'head', grape: '2.2.0' }
54+
- { ruby: '3.0', grape: 'HEAD' }
55+
- { ruby: '3.1', grape: 'HEAD' }
56+
- { ruby: '3.2', grape: 'HEAD' }
57+
- { ruby: '3.3', grape: 'HEAD' }
58+
- { ruby: 'head', grape: 'HEAD' }
59+
name: test (ruby=${{ matrix.entry.ruby }}, grape=${{ matrix.entry.grape }})
5960
runs-on: ubuntu-latest
6061
needs: ['rubocop']
6162
env:
62-
GRAPE_VERSION: '1.8.0'
63-
strategy:
64-
matrix:
65-
ruby-version: ['3.0', '3.1', '3.2', '3.3', 'head']
66-
steps:
67-
- name: Check out branch
68-
uses: actions/checkout@v4
69-
- name: Set up Ruby
70-
uses: ruby/setup-ruby@v1
71-
with:
72-
ruby-version: ${{ matrix.ruby-version }}
73-
- name: Run rspec wo model parser
74-
run: |
75-
bundle update
76-
bundle exec rspec
77-
- name: Run rspec w entity parser
78-
env:
79-
MODEL_PARSER: grape-swagger-entity
80-
run: |
81-
bundle update
82-
bundle exec rspec
83-
- name: Run rspec w representable parser
84-
env:
85-
MODEL_PARSER: grape-swagger-representable
86-
run: |
87-
bundle update
88-
bundle exec rspec
89-
90-
grape-20:
91-
runs-on: ubuntu-latest
92-
needs: ['rubocop']
93-
env:
94-
GRAPE_VERSION: '2.0.0'
95-
strategy:
96-
matrix:
97-
ruby-version: ['3.0', '3.1', '3.2', '3.3', 'head']
98-
steps:
99-
- name: Check out branch
100-
uses: actions/checkout@v4
101-
- name: Set up Ruby
102-
uses: ruby/setup-ruby@v1
103-
with:
104-
ruby-version: ${{ matrix.ruby-version }}
105-
- name: Run rspec wo model parser
106-
run: |
107-
bundle update
108-
bundle exec rspec
109-
- name: Run rspec w entity parser
110-
env:
111-
MODEL_PARSER: grape-swagger-entity
112-
run: |
113-
bundle update
114-
bundle exec rspec
115-
- name: Run rspec w representable parser
116-
env:
117-
MODEL_PARSER: grape-swagger-representable
118-
run: |
119-
bundle update
120-
bundle exec rspec
121-
122-
grape-HEAD:
123-
runs-on: ubuntu-latest
124-
needs: ['rubocop']
125-
env:
126-
GRAPE_VERSION: 'HEAD'
127-
strategy:
128-
matrix:
129-
ruby-version: ['3.0', '3.1', '3.2', '3.3', 'head']
63+
GRAPE_VERSION: ${{ matrix.entry.grape }}
13064
steps:
13165
- name: Check out branch
13266
uses: actions/checkout@v4
13367
- name: Set up Ruby
13468
uses: ruby/setup-ruby@v1
13569
with:
136-
ruby-version: ${{ matrix.ruby-version }}
70+
ruby-version: ${{ matrix.entry.ruby }}
13771
- name: Run rspec wo model parser
13872
run: |
13973
bundle update

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#### Fixes
88

9+
* [#940](https://github.com/ruby-grape/grape-swagger/pull/940): Grape 2.2.0 compatibility - [@padde](https://github.com/padde)
910
* Your contribution here.
1011

1112

grape-swagger.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ Gem::Specification.new do |s|
1515
s.metadata['rubygems_mfa_required'] = 'true'
1616

1717
s.required_ruby_version = '>= 3.0'
18-
s.add_runtime_dependency 'grape', '>= 1.7', '< 3.0'
19-
s.add_runtime_dependency 'rack-test', '~> 2'
18+
s.add_dependency 'grape', '>= 1.7', '< 3.0'
19+
s.add_dependency 'rack-test', '~> 2'
2020

2121
s.files = Dir['lib/**/*', '*.md', 'LICENSE.txt', 'grape-swagger.gemspec']
2222
s.require_paths = ['lib']

lib/grape-swagger.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,24 @@ def model_parsers
1818
end
1919
end
2020
autoload :Rake, 'grape-swagger/rake/oapi_tasks'
21+
22+
# Copied from https://github.com/ruby-grape/grape/blob/v2.2.0/lib/grape/formatter.rb
23+
FORMATTER_DEFAULTS = {
24+
json: Grape::Formatter::Json,
25+
jsonapi: Grape::Formatter::Json,
26+
serializable_hash: Grape::Formatter::SerializableHash,
27+
txt: Grape::Formatter::Txt,
28+
xml: Grape::Formatter::Xml
29+
}.freeze
30+
31+
# Copied from https://github.com/ruby-grape/grape/blob/v2.2.0/lib/grape/content_types.rb
32+
CONTENT_TYPE_DEFAULTS = {
33+
xml: 'application/xml',
34+
serializable_hash: 'application/json',
35+
json: 'application/json',
36+
binary: 'application/octet-stream',
37+
txt: 'text/plain'
38+
}.freeze
2139
end
2240

2341
module SwaggerRouting

lib/grape-swagger/doc_methods/produces_consumes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class << self
77
def call(*args)
88
return ['application/json'] unless args.flatten.present?
99

10-
args.flatten.map { |x| Grape::ContentTypes::CONTENT_TYPES[x] || x }.uniq
10+
args.flatten.map { |x| GrapeSwagger::CONTENT_TYPE_DEFAULTS[x] || x }.uniq
1111
end
1212
end
1313
end

lib/grape-swagger/endpoint.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ def content_types_for(target_class)
1111

1212
if content_types.empty?
1313
formats = [target_class.format, target_class.default_format].compact.uniq
14-
formats = Grape::Formatter.formatters(**{}).keys if formats.empty?
15-
content_types = Grape::ContentTypes::CONTENT_TYPES.select do |content_type, _mime_type|
14+
formats = GrapeSwagger::FORMATTER_DEFAULTS.keys if formats.empty?
15+
content_types = GrapeSwagger::CONTENT_TYPE_DEFAULTS.select do |content_type, _mime_type|
1616
formats.include? content_type
1717
end.values
1818
end

0 commit comments

Comments
 (0)