-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Raise exceptions on empty definitions
- Loading branch information
Showing
9 changed files
with
221 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class EmptyClass | ||
end | ||
|
||
module GrapeSwagger | ||
class EmptyModelParser | ||
attr_reader :model | ||
attr_reader :endpoint | ||
|
||
def initialize(model, endpoint) | ||
@model = model | ||
@endpoint = endpoint | ||
end | ||
|
||
def call | ||
{} | ||
end | ||
end | ||
end | ||
|
||
GrapeSwagger.model_parsers.register(GrapeSwagger::EmptyModelParser, EmptyClass) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module GrapeSwagger | ||
class MockParser | ||
attr_reader :model | ||
attr_reader :endpoint | ||
|
||
def initialize(model, endpoint) | ||
@model = model | ||
@endpoint = endpoint | ||
end | ||
|
||
def call | ||
{ | ||
mock_data: { | ||
type: :string, | ||
description: "it's a mock" | ||
} | ||
} | ||
end | ||
end | ||
end | ||
|
||
GrapeSwagger.model_parsers.register(GrapeSwagger::MockParser, OpenStruct) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
require 'spec_helper' | ||
|
||
describe 'Errors' do | ||
describe 'Empty model error' do | ||
let!(:app) do | ||
Class.new(Grape::API) do | ||
format :json | ||
|
||
desc 'Empty model get.' do | ||
http_codes [ | ||
{ code: 200, message: 'get Empty model', model: EmptyClass } | ||
] | ||
end | ||
get '/empty_model' do | ||
something = OpenStruct.new text: 'something' | ||
present something, with: EmptyClass | ||
end | ||
|
||
version 'v3', using: :path | ||
add_swagger_documentation api_version: 'v1', | ||
base_path: '/api', | ||
info: { | ||
title: 'The API title to be displayed on the API homepage.', | ||
description: 'A description of the API.', | ||
contact_name: 'Contact name', | ||
contact_email: 'Contact@email.com', | ||
contact_url: 'Contact URL', | ||
license: 'The name of the license.', | ||
license_url: 'www.The-URL-of-the-license.org', | ||
terms_of_service_url: 'www.The-URL-of-the-terms-and-service.com' | ||
} | ||
end | ||
end | ||
|
||
it 'should raise SwaggerSpec exception' do | ||
expect { get '/v3/swagger_doc' }.to raise_error(GrapeSwagger::Errors::SwaggerSpec, "Empty model EmptyClass, swagger 2.0 doesn't support empty definitions.") | ||
end | ||
end | ||
|
||
describe 'Parser not found error' do | ||
let!(:app) do | ||
Class.new(Grape::API) do | ||
format :json | ||
|
||
desc 'Wrong model get.' do | ||
http_codes [ | ||
{ code: 200, message: 'get Wrong model', model: Hash } | ||
] | ||
end | ||
get '/wrong_model' do | ||
something = OpenStruct.new text: 'something' | ||
present something, with: Hash | ||
end | ||
|
||
version 'v3', using: :path | ||
add_swagger_documentation api_version: 'v1', | ||
base_path: '/api', | ||
info: { | ||
title: 'The API title to be displayed on the API homepage.', | ||
description: 'A description of the API.', | ||
contact_name: 'Contact name', | ||
contact_email: 'Contact@email.com', | ||
contact_url: 'Contact URL', | ||
license: 'The name of the license.', | ||
license_url: 'www.The-URL-of-the-license.org', | ||
terms_of_service_url: 'www.The-URL-of-the-terms-and-service.com' | ||
} | ||
end | ||
end | ||
|
||
it 'should raise UnregisteredParser exception' do | ||
expect { get '/v3/swagger_doc' }.to raise_error(GrapeSwagger::Errors::UnregisteredParser, 'No parser registred for Hash.') | ||
end | ||
end | ||
end |