Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OpenAPI nullable properties #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/swagger_yard/openapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ def response(resp, op)
end
end

def property(prop)
prop.type.schema_with(model_path: model_path).tap do |h|
unless h['$ref']
h["description"] = prop.description if prop.description && !prop.description.strip.empty?
h["nullable"] = true if prop.nullable
h["example"] = prop.example if prop.example
end
end
end

def security_defs(security_objects)
defs = super
Hash[defs.map do |name, d|
Expand Down
25 changes: 25 additions & 0 deletions spec/lib/swagger_yard/openapi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,31 @@

end

context "models" do
let(:model) { SwaggerYard::Model.from_yard_object(yard_class('MyModel', content)) }
let(:spec) { stub(path_objects: SwaggerYard::Paths.new([]), tag_objects: [],
security_objects: [], model_objects: { model.id => model }) }

subject { described_class.new(spec).to_h["components"]["schemas"] }

context 'nullables' do
subject { super()['MyModel']['properties'] }

context "with a nullable flag" do
let(:content) { ['@model MyModel', '@property name(nullable) [string] Name'] }

its(['name', 'type']) { is_expected.to eq('string') }
its(['name', 'nullable']) { is_expected.to eq(true) }
end

context "with a nullable model" do
let(:content) { ['@model MyModel', '@property name(nullable) [Name] Name'] }

its(['name']) { is_expected.to eq('$ref' => '#/components/schemas/Name') }
end
end
end

context 'with config.openapi_version set and Swagger.new' do
subject { SwaggerYard::Swagger.new.to_h }
before { SwaggerYard.config.openapi_version = '3.0.0' }
Expand Down