diff --git a/lib/swagger_yard/openapi.rb b/lib/swagger_yard/openapi.rb index 923330c..0872893 100644 --- a/lib/swagger_yard/openapi.rb +++ b/lib/swagger_yard/openapi.rb @@ -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| diff --git a/spec/lib/swagger_yard/openapi_spec.rb b/spec/lib/swagger_yard/openapi_spec.rb index 6bd572b..5a3b79f 100644 --- a/spec/lib/swagger_yard/openapi_spec.rb +++ b/spec/lib/swagger_yard/openapi_spec.rb @@ -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' }