Skip to content

Commit e0e51a2

Browse files
committed
Cleanup a little
1 parent 2f97a24 commit e0e51a2

File tree

13 files changed

+23
-141
lines changed

13 files changed

+23
-141
lines changed

Gemfile

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ gem ENV.fetch('MODEL_PARSER', nil) if ENV.key?('MODEL_PARSER')
1616

1717
group :development, :test do
1818
gem 'bundler'
19-
gem 'dry-schema'
2019
gem 'grape-entity'
2120
gem 'pry', platforms: [:mri]
2221
gem 'pry-byebug', platforms: [:mri]

lib/grape-swagger.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def model_parsers
2525
serializable_hash: Grape::Formatter::SerializableHash,
2626
json: Grape::Formatter::Json,
2727
jsonapi: Grape::Formatter::Json,
28-
txt: Grape::Formatter::Txt,
28+
txt: Grape::Formatter::Txt
2929
}.freeze
3030

3131
# Copied from https://github.com/ruby-grape/grape/blob/v2.2.0/lib/grape/content_types.rb

lib/grape-swagger/doc_methods/build_model_definition.rb

+1-21
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ module GrapeSwagger
44
module DocMethods
55
class BuildModelDefinition
66
class << self
7-
OBJECT_ATTRIBUTE_KEYS = %i[
8-
$ref type
9-
].freeze
10-
117
def build(_model, properties, required, other_def_properties = {})
128
definition = { type: 'object', properties: properties }.merge(other_def_properties)
139

@@ -17,10 +13,6 @@ def build(_model, properties, required, other_def_properties = {})
1713
end
1814

1915
def parse_params_from_model(parsed_response, model, model_name)
20-
# If the parsed response looks like a complete object (e.g., containing `$ref` or `type`),
21-
# it uses the provided response as-is.
22-
return parsed_response if complete_object?(parsed_response)
23-
2416
if parsed_response.is_a?(Hash) && parsed_response.keys.first == :allOf
2517
refs_or_models = parsed_response[:allOf]
2618
parsed = parse_refs_and_models(refs_or_models, model)
@@ -62,26 +54,14 @@ def parse_properties(properties)
6254

6355
def parse_refs_and_models(refs_or_models, model)
6456
refs_or_models.map do |ref_or_models|
65-
if complete_object?(ref_or_models)
57+
if ref_or_models.is_a?(Hash) && ref_or_models.keys.first == '$ref'
6658
ref_or_models
6759
else
6860
properties, required = ref_or_models
6961
GrapeSwagger::DocMethods::BuildModelDefinition.build(model, properties, required)
7062
end
7163
end
7264
end
73-
74-
private
75-
76-
# Checks if the parsed response is already a complete object.
77-
#
78-
# @param parsed_response [Hash] The parsed response to check.
79-
# @return [Boolean] True if the response has object attributes.
80-
def complete_object?(parsed_response)
81-
return false unless parsed_response.is_a?(Hash)
82-
83-
parsed_response.keys.intersect?(OBJECT_ATTRIBUTE_KEYS)
84-
end
8565
end
8666
end
8767
end

lib/grape-swagger/endpoint.rb

+6-8
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22

33
require 'active_support'
44
require 'active_support/core_ext/string/inflections'
5-
require 'grape-swagger/endpoint/params_parser'
6-
require 'grape-swagger/endpoint/path_params_parser'
7-
require 'grape-swagger/endpoint/contract_parser'
8-
require 'grape-swagger/endpoint/header_params_parser'
5+
require_relative 'request_param_parsers/headers'
6+
require_relative 'request_param_parsers/route'
7+
require_relative 'request_param_parsers/body'
98

109
module Grape
1110
class Endpoint # rubocop:disable Metrics/ClassLength
1211
REQUEST_PARAM_PARSERS = [
13-
GrapeSwagger::Endpoint::HeaderParamsParser,
14-
GrapeSwagger::Endpoint::PathParamsParser,
15-
GrapeSwagger::Endpoint::ContractParser,
16-
GrapeSwagger::Endpoint::ParamsParser
12+
GrapeSwagger::RequestParamParsers::Headers,
13+
GrapeSwagger::RequestParamParsers::Route,
14+
GrapeSwagger::RequestParamParsers::Body
1715
].freeze
1816

1917
def content_types_for(target_class)

lib/grape-swagger/endpoint/contract_parser.rb

-38
This file was deleted.

lib/grape-swagger/endpoint/params_parser.rb renamed to lib/grape-swagger/request_param_parsers/body.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# frozen_string_literal: true
22

33
module GrapeSwagger
4-
module Endpoint
5-
class ParamsParser
4+
module RequestParamParsers
5+
class Body
66
attr_reader :route, :params, :settings, :endpoint
77

88
def self.parse(route, params, settings, endpoint)
@@ -29,7 +29,6 @@ def parse
2929
memo[name] = options
3030
end
3131
end
32-
alias parse_request_params parse
3332

3433
private
3534

lib/grape-swagger/endpoint/header_params_parser.rb renamed to lib/grape-swagger/request_param_parsers/headers.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# frozen_string_literal: true
22

33
module GrapeSwagger
4-
module Endpoint
5-
class HeaderParamsParser
4+
module RequestParamParsers
5+
class Headers
66
attr_reader :route
77

88
def self.parse(route, params, settings, endpoint)

lib/grape-swagger/endpoint/path_params_parser.rb renamed to lib/grape-swagger/request_param_parsers/route.rb

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# frozen_string_literal: true
22

33
module GrapeSwagger
4-
module Endpoint
5-
class PathParamsParser
4+
module RequestParamParsers
5+
class Route
66
DEFAULT_PARAM_TYPE = { required: true, type: 'Integer' }.freeze
77

88
attr_reader :route
@@ -29,13 +29,12 @@ def parse
2929
def build_path_params(stackable_values)
3030
params = {}
3131

32-
loop do
33-
break params unless stackable_values
34-
break params unless stackable_values.is_a? Grape::Util::StackableValues
35-
32+
while stackable_values.is_a?(Grape::Util::StackableValues)
3633
params.merge!(fetch_inherited_params(stackable_values))
3734
stackable_values = stackable_values.inherited_values
3835
end
36+
37+
params
3938
end
4039

4140
def fetch_inherited_params(stackable_values)

spec/lib/endpoint_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@
4747
end
4848

4949
describe 'parse_request_params' do
50-
let(:subject) { GrapeSwagger::Endpoint::ParamsParser }
50+
let(:subject) { GrapeSwagger::RequestParamParsers::Body }
5151
before do
52-
subject.send(:parse, nil, params, {}, nil)
52+
subject.parse(nil, params, {}, nil)
5353
end
5454

5555
context 'when params do not contain an array' do

spec/lib/endpoint/params_parser_spec.rb renamed to spec/lib/request_param_parsers/body_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
require 'spec_helper'
44

5-
describe GrapeSwagger::Endpoint::ParamsParser do
5+
describe GrapeSwagger::RequestParamParsers::Body do
66
let(:settings) { {} }
77
let(:params) { [] }
88
let(:endpoint) { nil }
99

1010
let(:parser) { described_class.new(nil, params, settings, endpoint) }
1111

1212
describe '#parse_request_params' do
13-
subject(:parse_request_params) { parser.parse_request_params }
13+
subject(:parse_request_params) { parser.parse }
1414

1515
context 'when param is of array type' do
1616
let(:params) { [['param_1', { type: 'Array[String]' }]] }
@@ -39,7 +39,7 @@
3939
let(:settings) { { array_use_braces: true } }
4040

4141
it 'does not add braces to the param key' do
42-
expect(parser.parse_request_params.keys.first).to eq 'param_1'
42+
expect(parser.parse.keys.first).to eq 'param_1'
4343
end
4444
end
4545
end

spec/spec_helper.rb

-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@
1111
Dir[File.join(Dir.getwd, 'spec/support/*.rb')].each { |f| require f }
1212
require "grape-swagger/#{MODEL_PARSER}" if MODEL_PARSER != 'mock'
1313
require File.join(Dir.getwd, "spec/support/model_parsers/#{MODEL_PARSER}_parser.rb")
14-
require_relative 'support/model_parsers/custom_parsed_type_parser'
1514

1615
require 'grape-entity'
1716
require 'grape-swagger-entity'
1817

1918
Bundler.setup :default, :test
2019

21-
require 'pry'
2220
require 'rack'
2321
require 'rack/test'
2422
require 'super_diff/rspec' if ENV.key?('SUPER_DIFF')

spec/support/model_parsers/custom_parsed_type_parser.rb

-28
This file was deleted.

spec/swagger_v2/api_swagger_v2_param_type_body_spec.rb

+1-26
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# frozen_string_literal: true
22

33
require 'spec_helper'
4-
require 'dry-schema'
54

65
describe 'setting of param type, such as `query`, `path`, `formData`, `body`, `header`' do
76
include_context "#{MODEL_PARSER} swagger example"
@@ -68,18 +67,6 @@ class BodyParamTypeApi < Grape::API
6867
end
6968
end
7069

71-
namespace :with_dry_schema_params do
72-
contract do
73-
required(:orders).array(:hash) do
74-
required(:name).filled(:string)
75-
optional(:volume).maybe(:integer, lt?: 9)
76-
end
77-
end
78-
post do
79-
{ 'declared_params' => declared(params) }
80-
end
81-
end
82-
8370
add_swagger_documentation
8471
end
8572
end
@@ -91,7 +78,7 @@ def app
9178

9279
describe 'no entity given' do
9380
subject do
94-
get '/swagger_doc/'
81+
get '/swagger_doc/wo_entities'
9582
JSON.parse(last_response.body)
9683
end
9784

@@ -183,18 +170,6 @@ def app
183170
end
184171
end
185172

186-
describe 'with exceed type parser result' do
187-
subject do
188-
get '/swagger_doc/with_dry_schema_params'
189-
JSON.parse(last_response.body)
190-
end
191-
192-
specify do
193-
puts JSON.pretty_generate(subject)
194-
expect(subject['paths']['/with_dry_schema_params']).to eq({})
195-
end
196-
end
197-
198173
describe 'complex entity given' do
199174
let(:request_parameters_definition) do
200175
[

0 commit comments

Comments
 (0)