Skip to content

Commit

Permalink
Grape 2.2.0 compatibility (#940)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
padde authored Sep 21, 2024
1 parent a5e2575 commit a4190cf
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 106 deletions.
136 changes: 35 additions & 101 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,118 +22,52 @@ jobs:
bundler-cache: true
- name: Run rubocop
run: bundle exec rubocop --parallel --format progress

grape-17:
runs-on: ubuntu-latest
needs: ['rubocop']
env:
GRAPE_VERSION: '1.7.1'
test:
strategy:
matrix:
ruby-version: ['3.0', '3.1', '3.2', '3.3', 'head']
steps:
- name: Check out branch
uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
- name: Run rspec wo model parser
run: |
bundle update
bundle exec rspec
- name: Run rspec w entity parser
env:
MODEL_PARSER: grape-swagger-entity
run: |
bundle update
bundle exec rspec
- name: Run rspec w representable parser
env:
MODEL_PARSER: grape-swagger-representable
run: |
bundle update
bundle exec rspec
grape-18:
entry:
- { ruby: '3.0', grape: '1.7.1' }
- { ruby: '3.1', grape: '1.7.1' }
- { ruby: '3.2', grape: '1.7.1' }
- { ruby: '3.3', grape: '1.7.1' }
- { ruby: 'head', grape: '1.7.1' }
- { ruby: '3.0', grape: '1.8.0' }
- { ruby: '3.1', grape: '1.8.0' }
- { ruby: '3.2', grape: '1.8.0' }
- { ruby: '3.3', grape: '1.8.0' }
- { ruby: 'head', grape: '1.8.0' }
- { ruby: '3.0', grape: '2.0.0' }
- { ruby: '3.1', grape: '2.0.0' }
- { ruby: '3.2', grape: '2.0.0' }
- { ruby: '3.3', grape: '2.0.0' }
- { ruby: 'head', grape: '2.0.0' }
- { ruby: '3.0', grape: '2.1.3' }
- { ruby: '3.1', grape: '2.1.3' }
- { ruby: '3.2', grape: '2.1.3' }
- { ruby: '3.3', grape: '2.1.3' }
- { ruby: 'head', grape: '2.1.3' }
- { ruby: '3.0', grape: '2.2.0' }
- { ruby: '3.1', grape: '2.2.0' }
- { ruby: '3.2', grape: '2.2.0' }
- { ruby: '3.3', grape: '2.2.0' }
- { ruby: 'head', grape: '2.2.0' }
- { ruby: '3.0', grape: 'HEAD' }
- { ruby: '3.1', grape: 'HEAD' }
- { ruby: '3.2', grape: 'HEAD' }
- { ruby: '3.3', grape: 'HEAD' }
- { ruby: 'head', grape: 'HEAD' }
name: test (ruby=${{ matrix.entry.ruby }}, grape=${{ matrix.entry.grape }})
runs-on: ubuntu-latest
needs: ['rubocop']
env:
GRAPE_VERSION: '1.8.0'
strategy:
matrix:
ruby-version: ['3.0', '3.1', '3.2', '3.3', 'head']
steps:
- name: Check out branch
uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
- name: Run rspec wo model parser
run: |
bundle update
bundle exec rspec
- name: Run rspec w entity parser
env:
MODEL_PARSER: grape-swagger-entity
run: |
bundle update
bundle exec rspec
- name: Run rspec w representable parser
env:
MODEL_PARSER: grape-swagger-representable
run: |
bundle update
bundle exec rspec
grape-20:
runs-on: ubuntu-latest
needs: ['rubocop']
env:
GRAPE_VERSION: '2.0.0'
strategy:
matrix:
ruby-version: ['3.0', '3.1', '3.2', '3.3', 'head']
steps:
- name: Check out branch
uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
- name: Run rspec wo model parser
run: |
bundle update
bundle exec rspec
- name: Run rspec w entity parser
env:
MODEL_PARSER: grape-swagger-entity
run: |
bundle update
bundle exec rspec
- name: Run rspec w representable parser
env:
MODEL_PARSER: grape-swagger-representable
run: |
bundle update
bundle exec rspec
grape-HEAD:
runs-on: ubuntu-latest
needs: ['rubocop']
env:
GRAPE_VERSION: 'HEAD'
strategy:
matrix:
ruby-version: ['3.0', '3.1', '3.2', '3.3', 'head']
GRAPE_VERSION: ${{ matrix.entry.grape }}
steps:
- name: Check out branch
uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
ruby-version: ${{ matrix.entry.ruby }}
- name: Run rspec wo model parser
run: |
bundle update
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#### Fixes

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


Expand Down
4 changes: 2 additions & 2 deletions grape-swagger.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ Gem::Specification.new do |s|
s.metadata['rubygems_mfa_required'] = 'true'

s.required_ruby_version = '>= 3.0'
s.add_runtime_dependency 'grape', '>= 1.7', '< 3.0'
s.add_runtime_dependency 'rack-test', '~> 2'
s.add_dependency 'grape', '>= 1.7', '< 3.0'
s.add_dependency 'rack-test', '~> 2'

s.files = Dir['lib/**/*', '*.md', 'LICENSE.txt', 'grape-swagger.gemspec']
s.require_paths = ['lib']
Expand Down
18 changes: 18 additions & 0 deletions lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ def model_parsers
end
end
autoload :Rake, 'grape-swagger/rake/oapi_tasks'

# Copied from https://github.com/ruby-grape/grape/blob/v2.2.0/lib/grape/formatter.rb
FORMATTER_DEFAULTS = {
json: Grape::Formatter::Json,
jsonapi: Grape::Formatter::Json,
serializable_hash: Grape::Formatter::SerializableHash,
txt: Grape::Formatter::Txt,
xml: Grape::Formatter::Xml
}.freeze

# Copied from https://github.com/ruby-grape/grape/blob/v2.2.0/lib/grape/content_types.rb
CONTENT_TYPE_DEFAULTS = {
xml: 'application/xml',
serializable_hash: 'application/json',
json: 'application/json',
binary: 'application/octet-stream',
txt: 'text/plain'
}.freeze
end

module SwaggerRouting
Expand Down
2 changes: 1 addition & 1 deletion lib/grape-swagger/doc_methods/produces_consumes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class << self
def call(*args)
return ['application/json'] unless args.flatten.present?

args.flatten.map { |x| Grape::ContentTypes::CONTENT_TYPES[x] || x }.uniq
args.flatten.map { |x| GrapeSwagger::CONTENT_TYPE_DEFAULTS[x] || x }.uniq
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/grape-swagger/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def content_types_for(target_class)

if content_types.empty?
formats = [target_class.format, target_class.default_format].compact.uniq
formats = Grape::Formatter.formatters(**{}).keys if formats.empty?
content_types = Grape::ContentTypes::CONTENT_TYPES.select do |content_type, _mime_type|
formats = GrapeSwagger::FORMATTER_DEFAULTS.keys if formats.empty?
content_types = GrapeSwagger::CONTENT_TYPE_DEFAULTS.select do |content_type, _mime_type|
formats.include? content_type
end.values
end
Expand Down

0 comments on commit a4190cf

Please sign in to comment.