Skip to content

Commit

Permalink
Add spec
Browse files Browse the repository at this point in the history
  • Loading branch information
blake authored and numbata committed Jul 12, 2024
1 parent 1f9c449 commit 9b6cb2a
Show file tree
Hide file tree
Showing 13 changed files with 463 additions and 155 deletions.
21 changes: 16 additions & 5 deletions lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
require 'grape-swagger/errors'
require 'grape-swagger/version'
require 'grape-swagger/model_parsers'
require 'grape-swagger/swagger_2/endpoint'
require 'grape-swagger/openapi_3/endpoint'
require 'grape-swagger/openapi_3/doc_methods'
require 'grape-swagger/swagger_2/doc_methods'

module GrapeSwagger
class << self
Expand Down Expand Up @@ -140,6 +144,10 @@ def add_swagger_documentation(options = {})
@target_class.combined_routes = combined_routes
@target_class.combined_namespaces = combined_namespaces

endpoint_type = options[:openapi_version] == '3.0' ? Grape::OpenAPI3Endpoint : Grape::Swagger2Endpoint
set_endpoint_type(@target_class, endpoint_type)
set_endpoint_type(documentation_class, endpoint_type)

documentation_class
end

Expand All @@ -149,6 +157,13 @@ def version_for(options)
options[:version] = version if version
end

def set_endpoint_type(app, klass)
app.endpoints.each do |endpoint|
endpoint.class.include(klass)
set_endpoint_type(endpoint.options[:app], klass) if endpoint.options[:app]
end
end

def combine_namespaces(app)
combined_namespaces = {}
endpoints = app.endpoints.clone
Expand All @@ -171,14 +186,10 @@ def combine_namespaces(app)
end

def create_documentation_class(openapi_version)
Class.new(Grape::API) do
Class.new(GrapeInstance) do
if openapi_version == '3.0'
require 'grape-swagger/openapi_3/endpoint'
require 'grape-swagger/openapi_3/doc_methods'
extend GrapeOpenAPI::DocMethods
else
require 'grape-swagger/swagger_2/endpoint'
require 'grape-swagger/swagger_2/doc_methods'
extend GrapeSwagger::DocMethods
end
end
Expand Down
12 changes: 10 additions & 2 deletions lib/grape-swagger/doc_methods/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,17 @@ def find_definition(status, path)
response = path[method][:responses][status]
return if response.nil?

return response[:schema]['$ref'].split('/').last if response[:schema].key?('$ref')
# Swagger 2
if response[:schema]
return response[:schema]['$ref'].split('/').last if response[:schema].key?('$ref')
return response[:schema]['items']['$ref'].split('/').last if response[:schema].key?('items')
end

response[:schema]['items']['$ref'].split('/').last if response[:schema].key?('items')
# OpenAPI 3
response[:content].each do |_,v|
return v[:schema]['$ref'].split('/').last if v[:schema].key?('$ref')
return v[:schema]['items']['$ref'].split('/').last if v[:schema].key?('items')
end
end

def add_extension_to(part, extensions)
Expand Down
8 changes: 8 additions & 0 deletions lib/grape-swagger/openapi_3/doc_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def setup(options)
target_class = options[:target_class]
guard = options[:swagger_endpoint_guard]
formatter = options[:format]
api_doc = options[:api_documentation].dup
specific_api_doc = options[:specific_api_documentation].dup

class_variables_from(options)

Expand All @@ -41,6 +43,8 @@ def setup(options)
end
end

desc api_doc.delete(:desc), api_doc

instance_eval(guard) unless guard.nil?

output_path_definitions = proc do |combi_routes, endpoint|
Expand Down Expand Up @@ -70,10 +74,14 @@ def setup(options)
output_path_definitions.call(target_class.combined_namespace_routes, self)
end

desc specific_api_doc.delete(:desc), { params:
specific_api_doc.delete(:params) || {} }.merge(specific_api_doc)

params do
requires :name, type: String, desc: 'Resource name of mounted API'
optional :locale, type: Symbol, desc: 'Locale of API documentation'
end

get "#{mount_path}/:name" do
I18n.locale = params[:locale] || I18n.default_locale

Expand Down
2 changes: 1 addition & 1 deletion lib/grape-swagger/openapi_3/doc_methods/move_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module GrapeSwagger
module DocMethods
class MoveParams
class OpenAPIMoveParams
class << self
attr_accessor :definitions

Expand Down
7 changes: 4 additions & 3 deletions lib/grape-swagger/openapi_3/doc_methods/parse_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module GrapeSwagger
module DocMethods
class ParseParams
class OpenAPIParseParams
class << self
def call(param, settings, path, route, definitions)
method = route.request_method
Expand Down Expand Up @@ -44,11 +44,11 @@ def document_required(settings)
def document_range_values(settings)
values = settings[:values] || nil
enum_or_range_values = parse_enum_or_range_values(values)
@parsed_param.merge!(enum_or_range_values) if enum_or_range_values
@parsed_param[:schema].merge!(enum_or_range_values) if enum_or_range_values
end

def document_default_value(settings)
@parsed_param[:default] = settings[:default] if settings[:default].present?
@parsed_param[:schema][:default] = settings[:default] if settings[:default].present?
end

def document_type_and_format(settings, data_type)
Expand Down Expand Up @@ -76,6 +76,7 @@ def document_array_param(value_type, definitions)
if definitions[value_type[:data_type]]
array_items['$ref'] = "#/components/schemas/#{@parsed_param[:schema][:type]}"
else
puts value_type.inspect
array_items[:type] = type || @parsed_param[:schema][:type] == 'array' ? 'string' : @parsed_param[:schema][:type]
end
array_items[:format] = @parsed_param.delete(:format) if @parsed_param[:format]
Expand Down
122 changes: 0 additions & 122 deletions lib/grape-swagger/openapi_3/doc_methods/parse_request_body.rb

This file was deleted.

30 changes: 15 additions & 15 deletions lib/grape-swagger/openapi_3/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require 'grape-swagger/endpoint/params_parser'

module Grape
class Endpoint
module OpenAPI3Endpoint
def content_types_for(target_class)
content_types = (target_class.content_types || {}).values

Expand All @@ -30,10 +30,10 @@ def swagger_object(_target_class, request, options)
servers = servers.is_a?(Hash) ? [servers] : servers

object = {
info: info_object(options[:info].merge(version: options[:doc_version])),
openapi: '3.0.0',
security: options[:security],
authorizations: options[:authorizations],
info: info_object(options[:info].merge(version: options[:doc_version])),
openapi: '3.0.0',
security: options[:security],
authorizations: options[:authorizations],
servers: servers
}

Expand All @@ -50,12 +50,12 @@ def swagger_object(_target_class, request, options)
# building info object
def info_object(infos)
result = {
title: infos[:title] || 'API title',
description: infos[:description],
termsOfService: infos[:terms_of_service_url],
contact: contact_object(infos),
license: license_object(infos),
version: infos[:version]
title: infos[:title] || 'API title',
description: infos[:description],
termsOfService: infos[:terms_of_service_url],
contact: contact_object(infos),
license: license_object(infos),
version: infos[:version]
}

GrapeSwagger::DocMethods::Extensions.add_extensions_to_info(infos, result)
Expand All @@ -68,7 +68,7 @@ def info_object(infos)
def license_object(infos)
{
name: infos.delete(:license),
url: infos.delete(:license_url)
url: infos.delete(:license_url)
}.delete_if { |_, value| value.blank? }
end

Expand Down Expand Up @@ -202,11 +202,11 @@ def params_object(route, options, path)
elsif value[:documentation]
expose_params(value[:documentation][:type])
end
GrapeSwagger::DocMethods::ParseParams.call(param, value, path, route, @definitions)
GrapeSwagger::DocMethods::OpenAPIParseParams.call(param, value, path, route, @definitions)
end

if GrapeSwagger::DocMethods::MoveParams.can_be_moved?(parameters, route.request_method)
parameters = GrapeSwagger::DocMethods::MoveParams.to_definition(path, parameters, route, @definitions)
if GrapeSwagger::DocMethods::OpenAPIMoveParams.can_be_moved?(parameters, route.request_method)
parameters = GrapeSwagger::DocMethods::OpenAPIMoveParams.to_definition(path, parameters, route, @definitions)
end

parameters
Expand Down
2 changes: 0 additions & 2 deletions lib/grape-swagger/swagger_2/doc_methods.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

# require 'grape-swagger/endpoint'

require 'grape-swagger/doc_methods/status_codes'
require 'grape-swagger/doc_methods/produces_consumes'
require 'grape-swagger/doc_methods/data_type'
Expand Down
2 changes: 1 addition & 1 deletion lib/grape-swagger/swagger_2/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require 'grape-swagger/endpoint/params_parser'

module Grape
class Endpoint # rubocop:disable Metrics/ClassLength
module Swagger2Endpoint # rubocop:disable Metrics/ClassLength
def content_types_for(target_class)
content_types = (target_class.content_types || {}).values

Expand Down
2 changes: 1 addition & 1 deletion spec/openapi_3/guarded_endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def sample_auth(*scopes)
description[:auth] = { scopes: scopes }
end

Grape::API.extend self
GrapeInstance.extend self
end

describe 'a guarded api endpoint' do
Expand Down
Loading

0 comments on commit 9b6cb2a

Please sign in to comment.