-
Notifications
You must be signed in to change notification settings - Fork 463
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Swagger type decision logic to dedicated classes.
`Apipie::Generator::Swagger::TypeExtractor` is responsible to identify the Swagger type from a given validator or default to `string` if no validator is given.
- Loading branch information
1 parent
407f9de
commit 70fbc23
Showing
7 changed files
with
163 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module Apipie::Generator | ||
end |
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,2 @@ | ||
module Apipie::Generator::Swagger | ||
end |
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,16 @@ | ||
class Apipie::Generator::Swagger::Type | ||
attr_reader :str_format | ||
|
||
def initialize(type, str_format = nil) | ||
@type = type | ||
@str_format = str_format | ||
end | ||
|
||
def to_s | ||
@type | ||
end | ||
|
||
def ==(other) | ||
other.to_s == self.to_s | ||
end | ||
end |
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,69 @@ | ||
class Apipie::Generator::Swagger::TypeExtractor | ||
TYPES = { | ||
numeric: 'number', | ||
hash: 'object', | ||
array: 'array', | ||
enum: 'enum', | ||
|
||
# see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#data-types | ||
integer: Apipie::Generator::Swagger::Type.new('integer', 'int32'), | ||
long: Apipie::Generator::Swagger::Type.new('integer', 'int64'), | ||
number: Apipie::Generator::Swagger::Type.new('number'), | ||
float: Apipie::Generator::Swagger::Type.new('number', 'float'), | ||
double: Apipie::Generator::Swagger::Type.new('number', 'double'), | ||
string: Apipie::Generator::Swagger::Type.new('string'), | ||
byte: Apipie::Generator::Swagger::Type.new('string', 'byte'), | ||
binary: Apipie::Generator::Swagger::Type.new('string', 'binary'), | ||
boolean: Apipie::Generator::Swagger::Type.new('boolean'), | ||
date: Apipie::Generator::Swagger::Type.new('string', 'date'), | ||
dateTime: Apipie::Generator::Swagger::Type.new('string', 'date-time'), | ||
password: Apipie::Generator::Swagger::Type.new('string', 'password') | ||
} | ||
|
||
# @param [<Apipie::Validator::BaseValidator>] validator | ||
def initialize(validator) | ||
@validator = validator | ||
end | ||
|
||
# @param [Hash<Symbol, Apipie::Generator::Swagger::Warning>] warnings | ||
def extract_with_warnings(warnings = {}) | ||
if boolean? && warnings[:boolean].present? | ||
Apipie::Generator::Swagger::WarningWriter.instance.warn(warnings[:boolean]) | ||
end | ||
|
||
extract | ||
end | ||
|
||
private | ||
|
||
def extract | ||
expected_type = if string? | ||
:string | ||
elsif boolean? | ||
:boolean | ||
elsif enum? | ||
:enum | ||
else | ||
@validator.expected_type.to_sym | ||
end | ||
|
||
TYPES[expected_type] || @validator.expected_type | ||
end | ||
|
||
def string? | ||
@validator.blank? | ||
end | ||
|
||
def enum? | ||
@validator.is_a?(Apipie::Validator::EnumValidator) || | ||
(@validator.respond_to?(:is_enum) && @validator.is_enum) | ||
end | ||
|
||
def boolean? | ||
@_boolean ||= enum? && boolean_values? | ||
end | ||
|
||
def boolean_values? | ||
@validator.values.to_set == Set.new([true, false]) | ||
end | ||
end |
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,61 @@ | ||
require 'spec_helper' | ||
|
||
describe Apipie::Generator::Swagger::TypeExtractor do | ||
let(:validator) {} | ||
let(:extractor) { described_class.new(validator) } | ||
|
||
describe '#extarct_with_warnings' do | ||
let(:warnings) { {} } | ||
|
||
before { Apipie.configuration.swagger_suppress_warnings = false } | ||
|
||
subject { extractor.extract_with_warnings(warnings) } | ||
|
||
it { is_expected.to eq(Apipie::Generator::Swagger::TypeExtractor::TYPES[:string]) } | ||
|
||
context "when enum validator is used" do | ||
let(:enum_values) { ["Name"] } | ||
|
||
context "of type Apipie::Validator::EnumValidator" do | ||
let(:validator) { Apipie::Validator::EnumValidator.new(nil, enum_values) } | ||
|
||
it { is_expected.to eq("enum") } | ||
end | ||
|
||
context "that responds to is_enum?" do | ||
let(:validator) do | ||
Apipie::ResponseDescriptionAdapter::PropDesc::Validator.new('some-type', enum_values) | ||
end | ||
|
||
it 'returns an enum type' do | ||
expect(subject).to eq(Apipie::Generator::Swagger::TypeExtractor::TYPES[:enum]) | ||
end | ||
|
||
context 'and has `true`, `false` as values' do | ||
let(:param_description_name) { :visible } | ||
let(:enum_values) { [true, false] } | ||
|
||
it 'returns a boolean type' do | ||
expect(subject).to eq(Apipie::Generator::Swagger::TypeExtractor::TYPES[:boolean]) | ||
end | ||
|
||
context 'and a boolean warning is passed' do | ||
let(:boolean_warning) do | ||
Apipie::Generator::Swagger::Warning.for_code( | ||
Apipie::Generator::Swagger::Warning::INFERRING_BOOLEAN_CODE, | ||
'SampleController#action', | ||
{ parameter: 'some-param' } | ||
) | ||
end | ||
|
||
let(:warnings) { { boolean: boolean_warning } } | ||
|
||
it 'outputs the warning' do | ||
expect { subject }.to output(boolean_warning.warning_message).to_stderr | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |