lib/grape-swagger/entity/parser.rb in grape-swagger-entity-0.1.3 vs lib/grape-swagger/entity/parser.rb in grape-swagger-entity-0.1.4
- old
+ new
@@ -23,52 +23,92 @@
end
private
def parse_grape_entity_params(params)
- return if params.nil?
+ return unless params
- params.each_with_object({}) do |x, memo|
- next if x[1].fetch(:documentation, {}).fetch(:in, nil).to_s == 'header'
- x[0] = x.last[:as] if x.last[:as]
+ params.each_with_object({}) do |(entity_name, entity_options), memo|
+ next if entity_options.fetch(:documentation, {}).fetch(:in, nil).to_s == 'header'
- model = x.last[:using] if x.last[:using].present?
- model ||= x.last[:documentation][:type] if x.last[:documentation] && could_it_be_a_model?(x.last[:documentation])
+ entity_name = entity_options[:as] if entity_options[:as]
+ model = entity_options[:using] if entity_options[:using].present?
+ if entity_options[:documentation] && could_it_be_a_model?(entity_options[:documentation])
+ model ||= entity_options[:documentation][:type]
+ end
+
if model
name = endpoint.send(:expose_params_from_model, model)
- memo[x.first] = if x.last[:documentation] && x.last[:documentation][:is_array]
- { 'type' => 'array', 'items' => { '$ref' => "#/definitions/#{name}" } }
- else
- { '$ref' => "#/definitions/#{name}" }
- end
+ memo[entity_name] = entity_model_type(name, entity_options)
else
- documented_type = x.last[:type]
- documented_type ||= x.last[:documentation][:type] if x.last[:documentation]
+ documented_type = entity_options[:type]
+
+ if entity_options[:documentation]
+ documented_type ||= entity_options[:documentation][:type]
+ end
+
data_type = GrapeSwagger::DocMethods::DataType.call(documented_type)
if GrapeSwagger::DocMethods::DataType.primitive?(data_type)
data = GrapeSwagger::DocMethods::DataType.mapping(data_type)
- memo[x.first] = { type: data.first, format: data.last }
+
+ memo[entity_name] = {
+ type: data.first,
+ format: data.last
+ }
else
- memo[x.first] = { type: data_type }
+ memo[entity_name] = {
+ type: data_type
+ }
end
- memo[x.first][:enum] = x.last[:values] if x.last[:values] && x.last[:values].is_a?(Array)
+ if entity_options[:values] && entity_options[:values].is_a?(Array)
+ memo[entity_name][:enum] = entity_options[:values]
+ end
+
+ if entity_options[:documentation] && entity_options[:documentation][:is_array]
+ memo[entity_name] = {
+ type: :array,
+ items: memo.delete(entity_name)
+ }
+ end
end
- memo[x.first][:description] = x.last[:documentation][:desc] if x.last[:documentation] && x.last[:documentation][:desc]
+
+ if entity_options[:documentation] && entity_options[:documentation][:desc]
+ memo[entity_name][:description] = entity_options[:documentation][:desc]
+ end
end
end
def could_it_be_a_model?(value)
- (
- value[:type].to_s.include?('Entity') || value[:type].to_s.include?('Entities')
- ) || (
- value[:type] &&
- value[:type].is_a?(Class) &&
- !GrapeSwagger::DocMethods::DataType.primitive?(value[:type].name.downcase) &&
- !value[:type] == Array
- )
+ direct_model_type?(value[:type]) || ambiguous_model_type?(value[:type])
+ end
+
+ def direct_model_type?(type)
+ type.to_s.include?('Entity') || type.to_s.include?('Entities')
+ end
+
+ def ambiguous_model_type?(type)
+ type &&
+ type.is_a?(Class) &&
+ !GrapeSwagger::DocMethods::DataType.primitive?(type.name.downcase) &&
+ !type == Array
+ end
+
+ def entity_model_type(name, entity_options)
+ if entity_options[:documentation] && entity_options[:documentation][:is_array]
+ {
+ 'type' => 'array',
+ 'items' => {
+ '$ref' => "#/definitions/#{name}"
+ }
+ }
+ else
+ {
+ '$ref' => "#/definitions/#{name}"
+ }
+ end
end
end
end
end