Skip to content

Commit a6b31de

Browse files
committed
Copy formatter and content type defaults from grape
1 parent 012afc9 commit a6b31de

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

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)