From 8c673f0ccc162ed06ca8361844b6efd5d2503049 Mon Sep 17 00:00:00 2001 From: zengraf Date: Thu, 24 Oct 2024 08:48:12 +0200 Subject: [PATCH] TI-427: Add compatibility with Grape 2.2 --- lib/grape-swagger.rb | 18 ++++++++++++++++++ .../doc_methods/produces_consumes.rb | 2 +- lib/grape-swagger/endpoint.rb | 4 ++-- tastyworks-grape_swagger.gemspec | 2 +- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/grape-swagger.rb b/lib/grape-swagger.rb index 21aa13175..a0f382646 100644 --- a/lib/grape-swagger.rb +++ b/lib/grape-swagger.rb @@ -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 diff --git a/lib/grape-swagger/doc_methods/produces_consumes.rb b/lib/grape-swagger/doc_methods/produces_consumes.rb index 3359e4b6b..5fae277e2 100644 --- a/lib/grape-swagger/doc_methods/produces_consumes.rb +++ b/lib/grape-swagger/doc_methods/produces_consumes.rb @@ -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 diff --git a/lib/grape-swagger/endpoint.rb b/lib/grape-swagger/endpoint.rb index 75bdb119b..ffdf290ae 100644 --- a/lib/grape-swagger/endpoint.rb +++ b/lib/grape-swagger/endpoint.rb @@ -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 diff --git a/tastyworks-grape_swagger.gemspec b/tastyworks-grape_swagger.gemspec index 361620bc4..fd13c924f 100644 --- a/tastyworks-grape_swagger.gemspec +++ b/tastyworks-grape_swagger.gemspec @@ -18,7 +18,7 @@ Gem::Specification.new do |s| s.metadata['github_repo'] = 'ssh://github.com/tastyworks/grape-swagger' s.required_ruby_version = '>= 2.5' - s.add_runtime_dependency 'grape', '~> 1.3' + s.add_runtime_dependency 'grape', '>= 1.3', '< 3.0' s.files = `git ls-files`.split("\n") s.require_paths = ['lib']