From 8d6d04afa27be286c62b5ec8913568f18bd3891d Mon Sep 17 00:00:00 2001 From: Peter Scholz Date: Fri, 18 Mar 2016 19:48:13 +0100 Subject: [PATCH 1/3] removes `allowMultiple`, not in spec --- lib/grape-swagger/doc_methods/parse_params.rb | 3 +-- lib/grape-swagger/endpoint.rb | 10 ------- spec/support/api_swagger_v2_result.rb | 26 +++++++++---------- .../api_swagger_v2_response_spec.rb | 4 +-- spec/swagger_v2/boolean_params_spec.rb | 2 +- spec/swagger_v2/float_api_spec.rb | 2 +- spec/swagger_v2/form_params_spec.rb | 6 ++--- spec/swagger_v2/param_type_spec.rb | 3 +-- spec/swagger_v2/param_values_spec.rb | 8 +----- spec/swagger_v2/params_array_spec.rb | 12 ++++----- spec/swagger_v2/params_hash_spec.rb | 12 ++++----- spec/swagger_v2/params_nested_spec.rb | 8 +++--- spec/swagger_v2/simple_mounted_api_spec.rb | 8 +++--- 13 files changed, 43 insertions(+), 61 deletions(-) diff --git a/lib/grape-swagger/doc_methods/parse_params.rb b/lib/grape-swagger/doc_methods/parse_params.rb index 00dfd7c4..39e1b7e3 100644 --- a/lib/grape-swagger/doc_methods/parse_params.rb +++ b/lib/grape-swagger/doc_methods/parse_params.rb @@ -30,8 +30,7 @@ def call(param, value, route) name: name, description: description, type: data_type, - required: required, - allowMultiple: is_array + required: required } if GrapeSwagger::DocMethods::DataType::PRIMITIVE_MAPPINGS.key?(data_type) diff --git a/lib/grape-swagger/endpoint.rb b/lib/grape-swagger/endpoint.rb index b573c767..7c88738f 100644 --- a/lib/grape-swagger/endpoint.rb +++ b/lib/grape-swagger/endpoint.rb @@ -3,16 +3,6 @@ module Grape class Endpoint - PRIMITIVE_MAPPINGS = { - 'integer' => %w(integer int32), - 'long' => %w(integer int64), - 'float' => %w(number float), - 'double' => %w(number double), - 'byte' => %w(string byte), - 'date' => %w(string date), - 'dateTime' => %w(string date-time) - }.freeze - def content_types_for(target_class) content_types = (target_class.content_types || {}).values diff --git a/spec/support/api_swagger_v2_result.rb b/spec/support/api_swagger_v2_result.rb index 8acedce6..6023e462 100644 --- a/spec/support/api_swagger_v2_result.rb +++ b/spec/support/api_swagger_v2_result.rb @@ -83,7 +83,7 @@ class ApiError < Grape::Entity "get"=>{ "produces"=>["application/json"], "parameters"=>[ - {"in"=>"array", "name"=>"elements", "description"=>"Set of configuration", "type"=>"string", "required"=>true, "allowMultiple"=>true, "items"=>{"type"=>"string"}}], + {"in"=>"array", "name"=>"elements", "description"=>"Set of configuration", "type"=>"string", "required"=>true, "items"=>{"type"=>"string"}}], "tags"=>["other_thing"], "operationId"=>"getV3OtherThingElements", "responses"=>{"200"=>{"description"=>"nested route inside namespace", "schema"=>{"$ref"=>"#/definitions/QueryInput"}}}, @@ -93,10 +93,10 @@ class ApiError < Grape::Entity "get"=>{ "produces"=>["application/json"], "parameters"=>[ - {"in"=>"query", "name"=>"id", "description"=>"Identity of Something", "type"=>"integer", "required"=>false, "allowMultiple"=>false, "format"=>"int32"}, - {"in"=>"query", "name"=>"text", "description"=>"Content of something.", "type"=>"string", "required"=>false, "allowMultiple"=>false}, - {"in"=>"query", "name"=>"links", "description"=>nil, "type"=>"link", "required"=>false, "allowMultiple"=>true}, - {"in"=>"query", "name"=>"others", "description"=>nil, "type"=>"text", "required"=>false, "allowMultiple"=>false}], + {"in"=>"query", "name"=>"id", "description"=>"Identity of Something", "type"=>"integer", "required"=>false, "format"=>"int32"}, + {"in"=>"query", "name"=>"text", "description"=>"Content of something.", "type"=>"string", "required"=>false}, + {"in"=>"query", "name"=>"links", "description"=>nil, "type"=>"link", "required"=>false}, + {"in"=>"query", "name"=>"others", "description"=>nil, "type"=>"text", "required"=>false}], "tags"=>["thing"], "operationId"=>"getThing", "responses"=>{ @@ -106,8 +106,8 @@ class ApiError < Grape::Entity "produces"=>["application/json"], "consumes"=>["application/json"], "parameters"=>[ - {"in"=>"formData", "name"=>"text", "description"=>"Content of something.", "type"=>"string", "required"=>true, "allowMultiple"=>false}, - {"in"=>"body", "name"=>"links", "description"=>nil, "type"=>"Array", "required"=>true, "allowMultiple"=>true}], + {"in"=>"formData", "name"=>"text", "description"=>"Content of something.", "type"=>"string", "required"=>true}, + {"in"=>"body", "name"=>"links", "description"=>nil, "type"=>"Array", "required"=>true}], "tags"=>["thing"], "operationId"=>"postThing", "responses"=>{ @@ -118,7 +118,7 @@ class ApiError < Grape::Entity "get"=>{ "produces"=>["application/json"], "parameters"=>[ - {"in"=>"path", "name"=>"id", "description"=>nil, "type"=>"integer", "required"=>true, "allowMultiple"=>false, "format"=>"int32"}], + {"in"=>"path", "name"=>"id", "description"=>nil, "type"=>"integer", "required"=>true, "format"=>"int32"}], "tags"=>["thing"], "operationId"=>"getThingId", "responses"=>{ @@ -128,15 +128,15 @@ class ApiError < Grape::Entity "produces"=>["application/json"], "consumes"=>["application/json"], "parameters"=>[ - {"in"=>"path", "name"=>"id", "description"=>nil, "type"=>"integer", "required"=>true, "allowMultiple"=>false, "format"=>"int32"}, - {"in"=>"formData", "name"=>"text", "description"=>"Content of something.", "type"=>"string", "required"=>false, "allowMultiple"=>false}, - {"in"=>"body", "name"=>"links", "description"=>nil, "type"=>"Array", "required"=>false, "allowMultiple"=>true}], + {"in"=>"path", "name"=>"id", "description"=>nil, "type"=>"integer", "required"=>true, "format"=>"int32"}, + {"in"=>"formData", "name"=>"text", "description"=>"Content of something.", "type"=>"string", "required"=>false}, + {"in"=>"body", "name"=>"links", "description"=>nil, "type"=>"Array", "required"=>false}], "tags"=>["thing"], "operationId"=>"putThingId", "responses"=>{"200"=>{"description"=>"This updates Thing.", "schema"=>{"$ref"=>"#/definitions/Something"}}}}, "delete"=>{ "produces"=>["application/json"], - "parameters"=>[{"in"=>"path", "name"=>"id", "description"=>nil, "type"=>"integer", "required"=>true, "allowMultiple"=>false, "format"=>"int32"}], + "parameters"=>[{"in"=>"path", "name"=>"id", "description"=>nil, "type"=>"integer", "required"=>true, "format"=>"int32"}], "tags"=>["thing"], "operationId"=>"deleteThingId", "responses"=>{"200"=>{"description"=>"This deletes Thing.", "schema"=>{"$ref"=>"#/definitions/Something"}}} @@ -153,7 +153,7 @@ class ApiError < Grape::Entity "/dummy/{id}"=>{ "delete"=>{ "produces"=>["application/json"], - "parameters"=>[{"in"=>"path", "name"=>"id", "description"=>nil, "type"=>"integer", "required"=>true, "allowMultiple"=>false, "format"=>"int32"}], + "parameters"=>[{"in"=>"path", "name"=>"id", "description"=>nil, "type"=>"integer", "required"=>true, "format"=>"int32"}], "tags"=>["dummy"], "operationId"=>"deleteDummyId", "responses"=>{"200"=>{"description"=>"dummy route."}, "401"=>{"description"=>"Unauthorized"}} diff --git a/spec/swagger_v2/api_swagger_v2_response_spec.rb b/spec/swagger_v2/api_swagger_v2_response_spec.rb index d765ffe6..b3596f19 100644 --- a/spec/swagger_v2/api_swagger_v2_response_spec.rb +++ b/spec/swagger_v2/api_swagger_v2_response_spec.rb @@ -140,8 +140,8 @@ def app "produces"=>["application/json"], "consumes"=>["application/json"], "parameters"=>[ - {"in"=>"formData", "name"=>"description", "description"=>nil, "type"=>"string", "required"=>false, "allowMultiple"=>false}, - {"in"=>"formData", "name"=>"$responses", "description"=>nil, "type"=>"string", "required"=>false, "allowMultiple"=>true}], + {"in"=>"formData", "name"=>"description", "description"=>nil, "type"=>"string", "required"=>false}, + {"in"=>"formData", "name"=>"$responses", "description"=>nil, "type"=>"string", "required"=>false}], "tags"=>["params_response"], "operationId"=>"postParamsResponse", "responses"=>{ diff --git a/spec/swagger_v2/boolean_params_spec.rb b/spec/swagger_v2/boolean_params_spec.rb index cf6675e1..9d1e6969 100644 --- a/spec/swagger_v2/boolean_params_spec.rb +++ b/spec/swagger_v2/boolean_params_spec.rb @@ -24,7 +24,7 @@ def app it 'converts boolean types' do expect(subject).to eq [ - {"in"=>"formData", "name"=>"a_boolean", "description"=>nil, "type"=>"boolean", "required"=>true, "allowMultiple"=>false} + {"in"=>"formData", "name"=>"a_boolean", "description"=>nil, "type"=>"boolean", "required"=>true} ] end end diff --git a/spec/swagger_v2/float_api_spec.rb b/spec/swagger_v2/float_api_spec.rb index 8bd8762e..4c1ba8ad 100644 --- a/spec/swagger_v2/float_api_spec.rb +++ b/spec/swagger_v2/float_api_spec.rb @@ -24,7 +24,7 @@ def app it 'converts float types' do expect(subject).to eq [ - {"in"=>"formData", "name"=>"a_float", "description"=>nil, "type"=>"number", "required"=>true, "allowMultiple"=>false, "format"=>"float"} + {"in"=>"formData", "name"=>"a_float", "description"=>nil, "type"=>"number", "required"=>true, "format"=>"float"} ] end end diff --git a/spec/swagger_v2/form_params_spec.rb b/spec/swagger_v2/form_params_spec.rb index ec7a93c1..063df386 100644 --- a/spec/swagger_v2/form_params_spec.rb +++ b/spec/swagger_v2/form_params_spec.rb @@ -57,9 +57,9 @@ def app it 'treats Symbol parameter as form param' do expect(subject['paths']['/items/{id}']['post']['parameters']).to eq [ - {"in"=>"path", "name"=>"id", "description"=>"id of item", "type"=>"integer", "required"=>true, "allowMultiple"=>false, "format"=>"int32"}, - {"in"=>"formData", "name"=>"name", "description"=>"name of item", "type"=>"string", "required"=>true, "allowMultiple"=>false}, - {"in"=>"formData", "name"=>"conditions", "description"=>"conditions of item", "type"=>"string", "required"=>false, "allowMultiple"=>false, "enum"=>["one", "two"]} + {"in"=>"path", "name"=>"id", "description"=>"id of item", "type"=>"integer", "required"=>true, "format"=>"int32"}, + {"in"=>"formData", "name"=>"name", "description"=>"name of item", "type"=>"string", "required"=>true}, + {"in"=>"formData", "name"=>"conditions", "description"=>"conditions of item", "type"=>"string", "required"=>false, "enum"=>["one", "two"]} ] end end diff --git a/spec/swagger_v2/param_type_spec.rb b/spec/swagger_v2/param_type_spec.rb index 4ac171c0..26f1cbba 100644 --- a/spec/swagger_v2/param_type_spec.rb +++ b/spec/swagger_v2/param_type_spec.rb @@ -28,8 +28,7 @@ def app "name"=>"input", "description"=>nil, "type"=>"string", - "required"=>true, - "allowMultiple"=>false + "required"=>true }] end diff --git a/spec/swagger_v2/param_values_spec.rb b/spec/swagger_v2/param_values_spec.rb index 7e970dc9..f94bf578 100644 --- a/spec/swagger_v2/param_values_spec.rb +++ b/spec/swagger_v2/param_values_spec.rb @@ -50,7 +50,6 @@ def first_parameter_info(request) "description"=>nil, "type"=>"string", "required"=>true, - "allowMultiple"=>false, "enum"=>["a", "b", "c"] }] end @@ -66,7 +65,6 @@ def first_parameter_info(request) "description"=>nil, "type"=>"string", "required"=>true, - "allowMultiple"=>false, "enum"=>["d", "e", "f"] }] end @@ -81,8 +79,7 @@ def first_parameter_info(request) "name"=>"letter", "description"=>nil, "type"=>"string", - "required"=>true, - "allowMultiple"=>false + "required"=>true }] end @@ -95,7 +92,6 @@ def first_parameter_info(request) "description"=>nil, "type"=>"integer", "required"=>true, - "allowMultiple"=>false, "format"=>"int32", "minimum"=>-5, "maximum"=>5 @@ -142,7 +138,6 @@ def first_parameter_info(request) "description"=>nil, "type"=>"string", "required"=>true, - "allowMultiple"=>false, "enum"=>"string" }] end @@ -158,7 +153,6 @@ def first_parameter_info(request) "description"=>nil, "type"=>"number", "required"=>true, - "allowMultiple"=>false, "format"=>"float" }] end diff --git a/spec/swagger_v2/params_array_spec.rb b/spec/swagger_v2/params_array_spec.rb index 0e20ae96..66c92640 100644 --- a/spec/swagger_v2/params_array_spec.rb +++ b/spec/swagger_v2/params_array_spec.rb @@ -39,8 +39,8 @@ def app specify do expect(subject['paths']['/groups']['post']['parameters']).to eql([ - {"in"=>"formData", "name"=>"required_group[][required_param_1]", "description"=>nil, "type"=>"string", "required"=>true, "allowMultiple"=>true}, - {"in"=>"formData", "name"=>"required_group[][required_param_2]", "description"=>nil, "type"=>"string", "required"=>true, "allowMultiple"=>true} + {"in"=>"formData", "name"=>"required_group[][required_param_1]", "description"=>nil, "type"=>"string", "required"=>true}, + {"in"=>"formData", "name"=>"required_group[][required_param_2]", "description"=>nil, "type"=>"string", "required"=>true} ]) end end @@ -53,10 +53,10 @@ def app specify do expect(subject['paths']['/type_given']['post']['parameters']).to eql([ - {"in"=>"formData", "name"=>"typed_group[][id]", "description"=>"integer given", "type"=>"integer", "required"=>true, "allowMultiple"=>true, "format"=>"int32"}, - {"in"=>"formData", "name"=>"typed_group[][name]", "description"=>"string given", "type"=>"string", "required"=>true, "allowMultiple"=>true}, - {"in"=>"formData", "name"=>"typed_group[][email]", "description"=>"email given", "type"=>"string", "required"=>false, "allowMultiple"=>true}, - {"in"=>"formData", "name"=>"typed_group[][others]", "description"=>nil, "type"=>"integer", "required"=>false, "allowMultiple"=>true, "format"=>"int32", "enum"=>[1, 2, 3]} + {"in"=>"formData", "name"=>"typed_group[][id]", "description"=>"integer given", "type"=>"integer", "required"=>true, "format"=>"int32"}, + {"in"=>"formData", "name"=>"typed_group[][name]", "description"=>"string given", "type"=>"string", "required"=>true}, + {"in"=>"formData", "name"=>"typed_group[][email]", "description"=>"email given", "type"=>"string", "required"=>false}, + {"in"=>"formData", "name"=>"typed_group[][others]", "description"=>nil, "type"=>"integer", "required"=>false, "format"=>"int32", "enum"=>[1, 2, 3]} ]) end end diff --git a/spec/swagger_v2/params_hash_spec.rb b/spec/swagger_v2/params_hash_spec.rb index e8a6fdce..f83f6b54 100644 --- a/spec/swagger_v2/params_hash_spec.rb +++ b/spec/swagger_v2/params_hash_spec.rb @@ -40,8 +40,8 @@ def app specify do expect(subject['paths']['/use_groups']['post']).to include('parameters') expect(subject['paths']['/use_groups']['post']['parameters']).to eql([ - {"in"=>"formData", "name"=>"required_group[required_param_1]", "description"=>nil, "type"=>"string", "required"=>true, "allowMultiple"=>false}, - {"in"=>"formData", "name"=>"required_group[required_param_2]", "description"=>nil, "type"=>"string", "required"=>true, "allowMultiple"=>false} + {"in"=>"formData", "name"=>"required_group[required_param_1]", "description"=>nil, "type"=>"string", "required"=>true}, + {"in"=>"formData", "name"=>"required_group[required_param_2]", "description"=>nil, "type"=>"string", "required"=>true} ]) end end @@ -55,10 +55,10 @@ def app specify do expect(subject['paths']['/use_given_type']['post']).to include('parameters') expect(subject['paths']['/use_given_type']['post']['parameters']).to eql([ - {"in"=>"formData", "name"=>"typed_group[id]", "description"=>"integer given", "type"=>"integer", "required"=>true, "allowMultiple"=>false, "format"=>"int32"}, - {"in"=>"formData", "name"=>"typed_group[name]", "description"=>"string given", "type"=>"string", "required"=>true, "allowMultiple"=>false}, - {"in"=>"formData", "name"=>"typed_group[email]", "description"=>"email given", "type"=>"string", "required"=>false, "allowMultiple"=>false}, - {"in"=>"formData", "name"=>"typed_group[others]", "description"=>nil, "type"=>"integer", "required"=>false, "allowMultiple"=>false, "format"=>"int32", "enum"=>[1, 2, 3]} + {"in"=>"formData", "name"=>"typed_group[id]", "description"=>"integer given", "type"=>"integer", "required"=>true, "format"=>"int32"}, + {"in"=>"formData", "name"=>"typed_group[name]", "description"=>"string given", "type"=>"string", "required"=>true}, + {"in"=>"formData", "name"=>"typed_group[email]", "description"=>"email given", "type"=>"string", "required"=>false}, + {"in"=>"formData", "name"=>"typed_group[others]", "description"=>nil, "type"=>"integer", "required"=>false, "format"=>"int32", "enum"=>[1, 2, 3]} ]) end end diff --git a/spec/swagger_v2/params_nested_spec.rb b/spec/swagger_v2/params_nested_spec.rb index c2587c61..d967ff7c 100644 --- a/spec/swagger_v2/params_nested_spec.rb +++ b/spec/swagger_v2/params_nested_spec.rb @@ -41,8 +41,8 @@ def app specify do expect(subject['paths']['/nested_array']['post']['parameters']).to eql([ - {"in"=>"formData", "name"=>"a_array[][param_1]", "description"=>nil, "type"=>"integer", "required"=>true, "allowMultiple"=>true, "format"=>"int32"}, - {"in"=>"formData", "name"=>"a_array[][b_array][][param_2]", "description"=>nil, "type"=>"string", "required"=>true, "allowMultiple"=>true} + {"in"=>"formData", "name"=>"a_array[][param_1]", "description"=>nil, "type"=>"integer", "required"=>true, "format"=>"int32"}, + {"in"=>"formData", "name"=>"a_array[][b_array][][param_2]", "description"=>nil, "type"=>"string", "required"=>true} ]) end end @@ -55,8 +55,8 @@ def app specify do expect(subject['paths']['/nested_hash']['post']['parameters']).to eql([ - {"in"=>"formData", "name"=>"a_hash[param_1]", "description"=>nil, "type"=>"integer", "required"=>true, "allowMultiple"=>false, "format"=>"int32"}, - {"in"=>"formData", "name"=>"a_hash[b_hash][param_2]", "description"=>nil, "type"=>"string", "required"=>true, "allowMultiple"=>false} + {"in"=>"formData", "name"=>"a_hash[param_1]", "description"=>nil, "type"=>"integer", "required"=>true, "format"=>"int32"}, + {"in"=>"formData", "name"=>"a_hash[b_hash][param_2]", "description"=>nil, "type"=>"string", "required"=>true} ]) end end diff --git a/spec/swagger_v2/simple_mounted_api_spec.rb b/spec/swagger_v2/simple_mounted_api_spec.rb index a21ac0d7..07c0445e 100644 --- a/spec/swagger_v2/simple_mounted_api_spec.rb +++ b/spec/swagger_v2/simple_mounted_api_spec.rb @@ -110,7 +110,7 @@ def app "post"=>{ "produces"=>["application/json"], "consumes"=>["application/json"], - "parameters"=>[{"in"=>"formData", "name"=>"items[]", "description"=>"array of items", "type"=>"string", "required"=>false, "allowMultiple"=>true}], + "parameters"=>[{"in"=>"formData", "name"=>"items[]", "description"=>"array of items", "type"=>"string", "required"=>false}], "tags"=>["items"], "operationId"=>"postItems", "responses"=>{"201"=>{"description"=>"this takes an array of parameters"}} @@ -118,7 +118,7 @@ def app "/custom"=>{ "get"=>{ "produces"=>["application/json"], - "parameters"=>[{"in"=>"query", "name"=>"custom", "description"=>"array of items", "type"=>"CustomType", "required"=>false, "allowMultiple"=>true}], + "parameters"=>[{"in"=>"query", "name"=>"custom", "description"=>"array of items", "type"=>"CustomType", "required"=>false}], "tags"=>["custom"], "operationId"=>"getCustom", "responses"=>{"200"=>{"description"=>"this uses a custom parameter"}}} @@ -213,7 +213,7 @@ def app "post"=>{ "produces"=>["application/json"], "consumes"=>["application/json"], - "parameters"=>[{"in"=>"formData", "name"=>"items[]", "description"=>"array of items", "type"=>"string", "required"=>false, "allowMultiple"=>true}], + "parameters"=>[{"in"=>"formData", "name"=>"items[]", "description"=>"array of items", "type"=>"string", "required"=>false}], "tags"=>["items"], "operationId"=>"postItems", "responses"=>{"201"=>{"description"=>"this takes an array of parameters"}}} @@ -232,7 +232,7 @@ def app "/custom"=>{ "get"=>{ "produces"=>["application/json"], - "parameters"=>[{"in"=>"query", "name"=>"custom", "description"=>"array of items", "type"=>"CustomType", "required"=>false, "allowMultiple"=>true}], + "parameters"=>[{"in"=>"query", "name"=>"custom", "description"=>"array of items", "type"=>"CustomType", "required"=>false}], "tags"=>["custom"], "operationId"=>"getCustom", "responses"=>{"200"=>{"description"=>"this uses a custom parameter"}}} From 168146d452bae4c5ac37e5f95e5b3d7975694b03 Mon Sep 17 00:00:00 2001 From: Peter Scholz Date: Thu, 24 Mar 2016 00:30:38 +0100 Subject: [PATCH 2/3] adds format to definition property type --- .travis.yml | 2 +- lib/grape-swagger/doc_methods/data_type.rb | 35 +++- lib/grape-swagger/doc_methods/parse_params.rb | 39 ++-- lib/grape-swagger/endpoint.rb | 13 +- spec/lib/data_type_spec.rb | 6 +- spec/support/api_swagger_v2_result.rb | 188 +++++++++--------- .../api_swagger_v2_extensions_spec.rb | 2 +- .../api_swagger_v2_response_spec.rb | 12 +- .../api_swagger_v2_type-format_spec.rb | 121 +++++++++++ spec/swagger_v2/response_model_spec.rb | 7 +- 10 files changed, 284 insertions(+), 141 deletions(-) create mode 100644 spec/swagger_v2/api_swagger_v2_type-format_spec.rb diff --git a/.travis.yml b/.travis.yml index a1781a2d..2a43db97 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,4 +16,4 @@ env: - GRAPE_VERSION=0.12.0 - GRAPE_VERSION=0.13.0 - GRAPE_VERSION=0.14.0 - - GRAPE_VERSION=HEAD + # - GRAPE_VERSION=HEAD diff --git a/lib/grape-swagger/doc_methods/data_type.rb b/lib/grape-swagger/doc_methods/data_type.rb index cd319490..259784ba 100644 --- a/lib/grape-swagger/doc_methods/data_type.rb +++ b/lib/grape-swagger/doc_methods/data_type.rb @@ -6,20 +6,20 @@ def call(value) raw_data_type = value[:type] if value.is_a?(Hash) raw_data_type ||= 'string' case raw_data_type.to_s + when 'Boolean', 'Date', 'Integer', 'String', 'Float', 'JSON', 'Array' + raw_data_type.to_s.downcase when 'Hash' 'object' - when 'Rack::Multipart::UploadedFile' - 'File' + when 'Rack::Multipart::UploadedFile', 'File' + 'file' when 'Virtus::Attribute::Boolean' 'boolean' - when 'Boolean', 'Date', 'Integer', 'String', 'Float' - raw_data_type.to_s.downcase when 'BigDecimal' - 'long' - when 'DateTime' + 'double' + when 'DateTime', 'Time' 'dateTime' when 'Numeric' - 'double' + 'long' when 'Symbol' 'string' else @@ -37,6 +37,22 @@ def parse_entity_name(model) entity_parts.join('::') end end + + def request_primitive?(type) + request_primitives.include?(type.to_s.downcase) + end + + def primitive?(type) + primitives.include?(type.to_s.downcase) + end + + def request_primitives + primitives + %w(object string boolean file json array) + end + + def primitives + PRIMITIVE_MAPPINGS.keys.map(&:downcase) + end end PRIMITIVE_MAPPINGS = { @@ -46,7 +62,10 @@ def parse_entity_name(model) 'double' => %w(number double), 'byte' => %w(string byte), 'date' => %w(string date), - 'dateTime' => %w(string date-time) + 'dateTime' => %w(string date-time), + 'binary' => %w(string binary), + 'password' => %w(string password), + 'email' => %w(string email) }.freeze end end diff --git a/lib/grape-swagger/doc_methods/parse_params.rb b/lib/grape-swagger/doc_methods/parse_params.rb index 39e1b7e3..cab8ca91 100644 --- a/lib/grape-swagger/doc_methods/parse_params.rb +++ b/lib/grape-swagger/doc_methods/parse_params.rb @@ -7,20 +7,19 @@ def call(param, value, route) path = route.route_path method = route.route_method - additional_documentation = value.is_a?(Hash) ? value[:documentation] : nil data_type = GrapeSwagger::DocMethods::DataType.call(value) - - if additional_documentation && value.is_a?(Hash) + additional_documentation = value[:documentation] + if additional_documentation value = additional_documentation.merge(value) end - description = value.is_a?(Hash) ? value[:desc] || value[:description] : nil - required = value.is_a?(Hash) ? value[:required] : false - default_value = value.is_a?(Hash) ? value[:default] : nil - example = value.is_a?(Hash) ? value[:example] : nil - is_array = value.is_a?(Hash) ? (value[:is_array] || false) : false - values = value.is_a?(Hash) ? value[:values] : nil - name = (value.is_a?(Hash) && value[:full_name]) || param + description = value[:desc] || value[:description] || nil + required = value[:required] || false + default_value = value[:default] || nil + example = value[:example] || nil + is_array = value[:is_array] || false + values = value[:values] || nil + name = value[:full_name] || param enum_or_range_values = parse_enum_or_range_values(values) value_type = { value: value, data_type: data_type, path: path } @@ -29,31 +28,29 @@ def call(param, value, route) in: param_type(value_type, param, method, is_array), name: name, description: description, - type: data_type, required: required } - if GrapeSwagger::DocMethods::DataType::PRIMITIVE_MAPPINGS.key?(data_type) - parsed_params[:type], parsed_params[:format] = GrapeSwagger::DocMethods::DataType::PRIMITIVE_MAPPINGS[data_type] + if GrapeSwagger::DocMethods::DataType.primitive?(data_type) + data = GrapeSwagger::DocMethods::DataType::PRIMITIVE_MAPPINGS[data_type] + parsed_params[:type], parsed_params[:format] = data + else + parsed_params[:type] = data_type end parsed_params[:items] = @array_items if @array_items.present? - parsed_params[:defaultValue] = example if example - parsed_params[:defaultValue] = default_value if default_value && example.blank? + parsed_params[:default] = example if example + parsed_params[:default] = default_value if default_value && example.blank? parsed_params.merge!(enum_or_range_values) if enum_or_range_values parsed_params end - def primitive?(type) - %w(object integer long float double string byte boolean date datetime).include? type.to_s.downcase - end - private def param_type(value_type, param, method, is_array) - # TODO: use `value_type.dig():value, :documentation, :param_type)` instead req ruby2.3 + # TODO: use `value_type.dig():value, :documentation, :param_type)` instead, req ruby2.3 # if value_type[:value].is_a?(Hash) && value_type[:value].key?(:documentation) && @@ -69,7 +66,7 @@ def param_type(value_type, param, method, is_array) when value_type[:path].include?("{#{param}}") 'path' when %w(POST PUT PATCH).include?(method) - primitive?(value_type[:data_type]) ? 'formData' : 'body' + GrapeSwagger::DocMethods::DataType.request_primitive?(value_type[:data_type]) ? 'formData' : 'body' else 'query' end diff --git a/lib/grape-swagger/endpoint.rb b/lib/grape-swagger/endpoint.rb index 7c88738f..87561e04 100644 --- a/lib/grape-swagger/endpoint.rb +++ b/lib/grape-swagger/endpoint.rb @@ -238,7 +238,16 @@ def parse_response_params(params) { '$ref' => "#/definitions/#{name}" } end else - memo[x.first] = { type: GrapeSwagger::DocMethods::DataType.call(x.last[:documentation] || x.last) } + + data_type = GrapeSwagger::DocMethods::DataType.call(x.last[:documentation] || x.last) + + if GrapeSwagger::DocMethods::DataType.primitive?(data_type) + data = GrapeSwagger::DocMethods::DataType::PRIMITIVE_MAPPINGS[data_type] + memo[x.first] = { type: data.first, format: data.last } + else + memo[x.first] = { type: data_type } + end + memo[x.first][:enum] = x.last[:values] if x.last[:values] && x.last[:values].is_a?(Array) end end @@ -269,7 +278,7 @@ def could_it_be_a_model?(value) ) || ( value[:type] && value[:type].is_a?(Class) && - !GrapeSwagger::DocMethods::ParseParams.primitive?(value[:type].name.downcase) && + !GrapeSwagger::DocMethods::DataType.primitive?(value[:type].name.downcase) && !value[:type] == Array ) end diff --git a/spec/lib/data_type_spec.rb b/spec/lib/data_type_spec.rb index c9a00ae4..6b255a16 100644 --- a/spec/lib/data_type_spec.rb +++ b/spec/lib/data_type_spec.rb @@ -22,7 +22,7 @@ describe "Rack::Multipart::UploadedFile" do let(:value) { { type: Rack::Multipart::UploadedFile } } - it { expect(subject).to eql 'File' } + it { expect(subject).to eql 'file' } end describe "Virtus::Attribute::Boolean" do @@ -34,7 +34,7 @@ describe "BigDecimal" do let(:value) { { type: BigDecimal } } - it { expect(subject).to eql 'long' } + it { expect(subject).to eql 'double' } end describe "DateTime" do @@ -46,7 +46,7 @@ describe "Numeric" do let(:value) { { type: Numeric } } - it { expect(subject).to eql 'double' } + it { expect(subject).to eql 'long' } end describe "Symbol" do diff --git a/spec/support/api_swagger_v2_result.rb b/spec/support/api_swagger_v2_result.rb index 6023e462..a3e9c784 100644 --- a/spec/support/api_swagger_v2_result.rb +++ b/spec/support/api_swagger_v2_result.rb @@ -67,104 +67,102 @@ class ApiError < Grape::Entity "termsOfServiceUrl"=>"www.The-URL-of-the-terms-and-service.com", "contact"=>{"name"=>"Contact name", "email"=>"Contact@email.com", "url"=>"Contact URL"}, "license"=>{"name"=>"The name of the license.", "url"=>"www.The-URL-of-the-license.org"}, - "version"=>"v1" - }, - "swagger"=>"2.0", - "produces"=>["application/json"], - "host"=>"example.org", - "basePath"=>"/api", - "tags"=>[{"name"=>"other_thing", "description"=>"Operations about other_things"}, - {"name"=>"thing", "description"=>"Operations about things"}, - {"name"=>"thing2", "description"=>"Operations about thing2s"}, - {"name"=>"dummy", "description"=>"Operations about dummies"}], - "schemes"=>["https", "http"], - "paths"=>{ - "/v3/other_thing/{elements}"=>{ - "get"=>{ - "produces"=>["application/json"], - "parameters"=>[ - {"in"=>"array", "name"=>"elements", "description"=>"Set of configuration", "type"=>"string", "required"=>true, "items"=>{"type"=>"string"}}], + "version"=>"v1"}, + "swagger"=>"2.0", + "produces"=>["application/json"], + "host"=>"example.org", + "basePath"=>"/api", + "tags"=>[ + {"name"=>"other_thing", "description"=>"Operations about other_things"}, + {"name"=>"thing", "description"=>"Operations about things"}, + {"name"=>"thing2", "description"=>"Operations about thing2s"}, + {"name"=>"dummy", "description"=>"Operations about dummies"} + ], + "schemes"=>["https", "http"], + "paths"=>{ + "/v3/other_thing/{elements}"=>{ + "get"=>{ + "produces"=>["application/json"], + "parameters"=>[{"in"=>"array", "name"=>"elements", "description"=>"Set of configuration", "required"=>true, "type"=>"string", "items"=>{"type"=>"string"}}], + "responses"=>{"200"=>{"description"=>"nested route inside namespace", "schema"=>{"$ref"=>"#/definitions/QueryInput"}}}, "tags"=>["other_thing"], "operationId"=>"getV3OtherThingElements", - "responses"=>{"200"=>{"description"=>"nested route inside namespace", "schema"=>{"$ref"=>"#/definitions/QueryInput"}}}, "x-amazon-apigateway-auth"=>{"type"=>"none"}, - "x-amazon-apigateway-integration"=>{"type"=>"aws", "uri"=>"foo_bar_uri", "httpMethod"=>"get"}}}, - "/thing"=>{ - "get"=>{ - "produces"=>["application/json"], - "parameters"=>[ - {"in"=>"query", "name"=>"id", "description"=>"Identity of Something", "type"=>"integer", "required"=>false, "format"=>"int32"}, - {"in"=>"query", "name"=>"text", "description"=>"Content of something.", "type"=>"string", "required"=>false}, - {"in"=>"query", "name"=>"links", "description"=>nil, "type"=>"link", "required"=>false}, - {"in"=>"query", "name"=>"others", "description"=>nil, "type"=>"text", "required"=>false}], - "tags"=>["thing"], - "operationId"=>"getThing", - "responses"=>{ - "200"=>{"description"=>"This gets Things.", "schema"=>{"$ref"=>"#/definitions/Thing"}}, - "401"=>{"description"=>"Unauthorized", "schema"=>{"$ref"=>"#/definitions/ApiError"}}}}, - "post"=>{ - "produces"=>["application/json"], - "consumes"=>["application/json"], - "parameters"=>[ - {"in"=>"formData", "name"=>"text", "description"=>"Content of something.", "type"=>"string", "required"=>true}, - {"in"=>"body", "name"=>"links", "description"=>nil, "type"=>"Array", "required"=>true}], - "tags"=>["thing"], - "operationId"=>"postThing", - "responses"=>{ - "201"=>{"description"=>"This creates Thing.", "schema"=>{"$ref"=>"#/definitions/Something"}}, - "422"=>{"description"=>"Unprocessible Entity"}} - }}, - "/thing/{id}"=>{ - "get"=>{ - "produces"=>["application/json"], - "parameters"=>[ - {"in"=>"path", "name"=>"id", "description"=>nil, "type"=>"integer", "required"=>true, "format"=>"int32"}], - "tags"=>["thing"], - "operationId"=>"getThingId", - "responses"=>{ - "200"=>{"description"=>"getting a single thing", "schema"=>{"$ref"=>"#/definitions/Thing"}}, - "401"=>{"description"=>"Unauthorized"}}}, - "put"=>{ - "produces"=>["application/json"], - "consumes"=>["application/json"], - "parameters"=>[ - {"in"=>"path", "name"=>"id", "description"=>nil, "type"=>"integer", "required"=>true, "format"=>"int32"}, - {"in"=>"formData", "name"=>"text", "description"=>"Content of something.", "type"=>"string", "required"=>false}, - {"in"=>"body", "name"=>"links", "description"=>nil, "type"=>"Array", "required"=>false}], - "tags"=>["thing"], - "operationId"=>"putThingId", - "responses"=>{"200"=>{"description"=>"This updates Thing.", "schema"=>{"$ref"=>"#/definitions/Something"}}}}, - "delete"=>{ - "produces"=>["application/json"], - "parameters"=>[{"in"=>"path", "name"=>"id", "description"=>nil, "type"=>"integer", "required"=>true, "format"=>"int32"}], - "tags"=>["thing"], - "operationId"=>"deleteThingId", - "responses"=>{"200"=>{"description"=>"This deletes Thing.", "schema"=>{"$ref"=>"#/definitions/Something"}}} - }}, - "/thing2"=>{ - "get"=>{ - "produces"=>["application/json"], - "tags"=>["thing2"], - "operationId"=>"getThing2", - "responses"=>{ - "200"=>{"description"=>"get Horses", "schema"=>{"$ref"=>"#/definitions/Something"}}, - "401"=>{"description"=>"HorsesOutError", "schema"=>{"$ref"=>"#/definitions/ApiError"}}} - }}, - "/dummy/{id}"=>{ - "delete"=>{ - "produces"=>["application/json"], - "parameters"=>[{"in"=>"path", "name"=>"id", "description"=>nil, "type"=>"integer", "required"=>true, "format"=>"int32"}], - "tags"=>["dummy"], - "operationId"=>"deleteDummyId", - "responses"=>{"200"=>{"description"=>"dummy route."}, "401"=>{"description"=>"Unauthorized"}} - }}}, - "definitions"=>{ - "QueryInputElement"=>{"type"=>"object", "properties"=>{"key"=>{"type"=>"string"}, "value"=>{"type"=>"string"}}}, - "QueryInput"=>{"type"=>"object", "properties"=>{"elements"=>{"type"=>"array", "items"=>{"$ref"=>"#/definitions/QueryInputElement"}}}}, - "Thing"=>{"properties"=>{"id"=>{"type"=>"integer"}, "text"=>{"type"=>"string"}, "links"=>{"type"=>"link"}, "others"=>{"type"=>"text"}}}, - "ApiError"=>{"type"=>"object", "properties"=>{"code"=>{"type"=>"integer"}, "message"=>{"type"=>"string"}}}, - "Something"=>{"type"=>"object", "properties"=>{"id"=>{"type"=>"integer"}, "text"=>{"type"=>"string"}, "links"=>{"type"=>"link"}, "others"=>{"type"=>"text"}}} - }} + "x-amazon-apigateway-integration"=>{"type"=>"aws", "uri"=>"foo_bar_uri", "httpMethod"=>"get"} + }}, + "/thing"=>{ + "get"=>{ + "produces"=>["application/json"], + "parameters"=>[ + {"in"=>"query", "name"=>"id", "description"=>"Identity of Something", "required"=>false, "type"=>"integer", "format"=>"int32"}, + {"in"=>"query", "name"=>"text", "description"=>"Content of something.", "required"=>false, "type"=>"string"}, + {"in"=>"query", "name"=>"links", "description"=>nil, "required"=>false, "type"=>"link"}, + {"in"=>"query", "name"=>"others", "description"=>nil, "required"=>false, "type"=>"text"} + ], + "responses"=>{"200"=>{"description"=>"This gets Things.", "schema"=>{"$ref"=>"#/definitions/Thing"}}, "401"=>{"description"=>"Unauthorized", "schema"=>{"$ref"=>"#/definitions/ApiError"}}}, + "tags"=>["thing"], + "operationId"=>"getThing" + }, + "post"=>{ + "produces"=>["application/json"], + "consumes"=>["application/json"], + "parameters"=>[ + {"in"=>"formData", "name"=>"text", "description"=>"Content of something.", "required"=>true, "type"=>"string"}, + {"in"=>"formData", "name"=>"links", "description"=>nil, "required"=>true, "type"=>"array"} + ], + "responses"=>{"201"=>{"description"=>"This creates Thing.", "schema"=>{"$ref"=>"#/definitions/Something"}}, "422"=>{"description"=>"Unprocessible Entity"}}, + "tags"=>["thing"], + "operationId"=>"postThing" + }}, + "/thing/{id}"=>{ + "get"=>{ + "produces"=>["application/json"], + "parameters"=>[{"in"=>"path", "name"=>"id", "description"=>nil, "required"=>true, "type"=>"integer", "format"=>"int32"}], + "responses"=>{"200"=>{"description"=>"getting a single thing", "schema"=>{"$ref"=>"#/definitions/Thing"}}, "401"=>{"description"=>"Unauthorized"}}, + "tags"=>["thing"], + "operationId"=>"getThingId" + }, + "put"=>{ + "produces"=>["application/json"], + "consumes"=>["application/json"], + "parameters"=>[ + {"in"=>"path", "name"=>"id", "description"=>nil, "required"=>true, "type"=>"integer", "format"=>"int32"}, + {"in"=>"formData", "name"=>"text", "description"=>"Content of something.", "required"=>false, "type"=>"string"}, + {"in"=>"formData", "name"=>"links", "description"=>nil, "required"=>false, "type"=>"array"} + ], + "responses"=>{"200"=>{"description"=>"This updates Thing.", "schema"=>{"$ref"=>"#/definitions/Something"}}}, + "tags"=>["thing"], + "operationId"=>"putThingId" + }, + "delete"=>{ + "produces"=>["application/json"], + "parameters"=>[{"in"=>"path", "name"=>"id", "description"=>nil, "required"=>true, "type"=>"integer", "format"=>"int32"}], + "responses"=>{"200"=>{"description"=>"This deletes Thing.", "schema"=>{"$ref"=>"#/definitions/Something"}}}, + "tags"=>["thing"], + "operationId"=>"deleteThingId" + }}, + "/thing2"=>{ + "get"=>{ + "produces"=>["application/json"], + "responses"=>{"200"=>{"description"=>"get Horses", "schema"=>{"$ref"=>"#/definitions/Something"}}, "401"=>{"description"=>"HorsesOutError", "schema"=>{"$ref"=>"#/definitions/ApiError"}}}, + "tags"=>["thing2"], + "operationId"=>"getThing2" + }}, + "/dummy/{id}"=>{ + "delete"=>{ + "produces"=>["application/json"], + "parameters"=>[{"in"=>"path", "name"=>"id", "description"=>nil, "required"=>true, "type"=>"integer", "format"=>"int32"}], + "responses"=>{"200"=>{"description"=>"dummy route."}, "401"=>{"description"=>"Unauthorized"}}, + "tags"=>["dummy"], + "operationId"=>"deleteDummyId" + }}}, + "definitions"=>{ + "QueryInputElement"=>{"type"=>"object", "properties"=>{"key"=>{"type"=>"string"}, "value"=>{"type"=>"string"}}}, + "QueryInput"=>{"type"=>"object", "properties"=>{"elements"=>{"type"=>"array", "items"=>{"$ref"=>"#/definitions/QueryInputElement"}}}}, + "Thing"=>{"properties"=>{"id"=>{"type"=>"integer", "format"=>"int32"}, "text"=>{"type"=>"string"}, "links"=>{"type"=>"link"}, "others"=>{"type"=>"text"}}}, + "ApiError"=>{"type"=>"object", "properties"=>{"code"=>{"type"=>"integer", "format"=>"int32"}, "message"=>{"type"=>"string"}}}, + "Something"=>{"type"=>"object", "properties"=>{"id"=>{"type"=>"integer", "format"=>"int32"}, "text"=>{"type"=>"string"}, "links"=>{"type"=>"link"}, "others"=>{"type"=>"text"}}} + }} end let(:http_verbs) { %w[get post put delete]} diff --git a/spec/swagger_v2/api_swagger_v2_extensions_spec.rb b/spec/swagger_v2/api_swagger_v2_extensions_spec.rb index b8b32e53..b8d162bf 100644 --- a/spec/swagger_v2/api_swagger_v2_extensions_spec.rb +++ b/spec/swagger_v2/api_swagger_v2_extensions_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe 'exposing' do +describe 'extension' do include_context "the api entities" before :all do diff --git a/spec/swagger_v2/api_swagger_v2_response_spec.rb b/spec/swagger_v2/api_swagger_v2_response_spec.rb index b3596f19..69518d9e 100644 --- a/spec/swagger_v2/api_swagger_v2_response_spec.rb +++ b/spec/swagger_v2/api_swagger_v2_response_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe 'exposing' do +describe 'response' do include_context "the api entities" before :all do @@ -68,9 +68,9 @@ def app "operationId"=>"getNestedType" }}}, "definitions"=>{ - "ResponseItem"=>{"type"=>"object", "properties"=>{"id"=>{"type"=>"integer"}, "name"=>{"type"=>"string"}}}, + "ResponseItem"=>{"type"=>"object", "properties"=>{"id"=>{"type"=>"integer", "format"=>"int32"}, "name"=>{"type"=>"string"}}}, "UseTemResponseAsType"=>{"type"=>"object", "properties"=>{"description"=>{"type"=>"string"}, "responses"=>{"$ref"=>"#/definitions/ResponseItem"}}}, - "ApiError"=>{"type"=>"object", "properties"=>{"code"=>{"type"=>"integer"}, "message"=>{"type"=>"string"}}} + "ApiError"=>{"type"=>"object", "properties"=>{"code"=>{"type"=>"integer", "format"=>"int32"}, "message"=>{"type"=>"string"}}} }}) end end @@ -105,13 +105,13 @@ def app "definitions"=>{ "ResponseItem"=>{ "type"=>"object", - "properties"=>{"id"=>{"type"=>"integer"}, "name"=>{"type"=>"string"}}}, + "properties"=>{"id"=>{"type"=>"integer", "format"=>"int32"}, "name"=>{"type"=>"string"}}}, "UseResponse"=>{ "type"=>"object", "properties"=>{"description"=>{"type"=>"string"}, "$responses"=>{"type"=>"array", "items"=>{"$ref"=>"#/definitions/ResponseItem"}}}}, "ApiError"=>{ "type"=>"object", - "properties"=>{"code"=>{"type"=>"integer"}, "message"=>{"type"=>"string"}}} + "properties"=>{"code"=>{"type"=>"integer", "format"=>"int32"}, "message"=>{"type"=>"string"}}} }}) end end @@ -150,7 +150,7 @@ def app }}}, "definitions"=>{ "ParamsResponse"=>{"properties"=>{"description"=>{"type"=>"string"}, "$responses"=>{"type"=>"string"}}}, - "ApiError"=>{"type"=>"object", "properties"=>{"code"=>{"type"=>"integer"}, "message"=>{"type"=>"string"}}} + "ApiError"=>{"type"=>"object", "properties"=>{"code"=>{"type"=>"integer", "format"=>"int32"}, "message"=>{"type"=>"string"}}} }}) end end diff --git a/spec/swagger_v2/api_swagger_v2_type-format_spec.rb b/spec/swagger_v2/api_swagger_v2_type-format_spec.rb new file mode 100644 index 00000000..76f05291 --- /dev/null +++ b/spec/swagger_v2/api_swagger_v2_type-format_spec.rb @@ -0,0 +1,121 @@ +require 'spec_helper' + +# mapping of parameter types +# Grape -> Swagger (OpenApi) +# (type format) +# --------------------------------------------------- +# Integer -> integer int32 +# Numeric -> integer int64 +# Float -> number float +# BigDecimal -> number double +# String -> string +# Symbol -> string +# Date -> string date +# DateTime -> string date-time +# Time -> string date-time +# 'password' -> string password +# 'email' -> string email +# Boolean -> boolean +# JSON -> json +# Rack::Multipart::UploadedFile -> file + +describe 'type format settings' do + before :all do + module TheApi + module Entities + class TypedDefinition < Grape::Entity + expose :prop_integer, documentation: { type: Integer, desc: 'prop_integer' } + expose :prop_long, documentation: { type: Numeric, desc: 'prop_long' } + expose :prop_float, documentation: { type: Float, desc: 'prop_float' } + expose :prop_double, documentation: { type: BigDecimal, desc: 'prop_double' } + expose :prop_string, documentation: { type: String, desc: 'prop_string' } + expose :prop_symbol, documentation: { type: Symbol, desc: 'prop_symbol' } + expose :prop_date, documentation: { type: Date, desc: 'prop_date' } + expose :prop_date_time, documentation: { type: DateTime, desc: 'prop_date_time' } + expose :prop_time, documentation: { type: Time, desc: 'prop_time' } + expose :prop_password, documentation: { type: 'password', desc: 'prop_password' } + expose :prop_email, documentation: { type: 'email', desc: 'prop_email' } + expose :prop_boolean, documentation: { type: Virtus::Attribute::Boolean, desc: 'prop_boolean' } + expose :prop_file, documentation: { type: File, desc: 'prop_file' } + expose :prop_json, documentation: { type: JSON, desc: 'prop_json' } + end + end + + class TypeFormatApi < Grape::API + desc 'full set of request data types', + success: TheApi::Entities::TypedDefinition + + params do + # grape supported data types + requires :param_integer, type: Integer + requires :param_long, type: Numeric + requires :param_float, type: Float + requires :param_double, type: BigDecimal + optional :param_string, type: String + optional :param_symbol, type: Symbol + requires :param_date, type: Date + requires :param_date_time, type: DateTime + requires :param_time, type: Time + requires :param_password, type: 'password' + requires :param_email, type: 'email' + optional :param_boolean, type: Boolean + optional :param_file, type: File + optional :param_json, type: JSON + end + + post '/request_types' do + { "declared_params" => declared(params) } + end + + add_swagger_documentation + end + end + end + + def app + TheApi::TypeFormatApi + end + + subject do + get '/swagger_doc/request_types' + JSON.parse(last_response.body) + end + + specify do + expect(subject['paths']['/request_types']['post']['parameters']).to eql([ + {"in"=>"formData", "name"=>"param_integer", "description"=>nil, "required"=>true, "type"=>"integer", "format"=>"int32"}, + {"in"=>"formData", "name"=>"param_long", "description"=>nil, "required"=>true, "type"=>"integer", "format"=>"int64"}, + {"in"=>"formData", "name"=>"param_float", "description"=>nil, "required"=>true, "type"=>"number", "format"=>"float"}, + {"in"=>"formData", "name"=>"param_double", "description"=>nil, "required"=>true, "type"=>"number", "format"=>"double"}, + {"in"=>"formData", "name"=>"param_string", "description"=>nil, "required"=>false, "type"=>"string"}, + {"in"=>"formData", "name"=>"param_symbol", "description"=>nil, "required"=>false, "type"=>"string"}, + {"in"=>"formData", "name"=>"param_date", "description"=>nil, "required"=>true, "type"=>"string", "format"=>"date"}, + {"in"=>"formData", "name"=>"param_date_time", "description"=>nil, "required"=>true, "type"=>"string", "format"=>"date-time"}, + {"in"=>"formData", "name"=>"param_time", "description"=>nil, "required"=>true, "type"=>"string", "format"=>"date-time"}, + {"in"=>"formData", "name"=>"param_password", "description"=>nil, "required"=>true, "type"=>"string", "format"=>"password"}, + {"in"=>"formData", "name"=>"param_email", "description"=>nil, "required"=>true, "type"=>"string", "format"=>"email"}, + {"in"=>"formData", "name"=>"param_boolean", "description"=>nil, "required"=>false, "type"=>"boolean"}, + {"in"=>"formData", "name"=>"param_file", "description"=>nil, "required"=>false, "type"=>"file"}, + {"in"=>"formData", "name"=>"param_json", "description"=>nil, "required"=>false, "type"=>"json"} + ]) + end + + specify do + expect(subject['definitions']['TypedDefinition']['properties']).to eql({ + "prop_integer"=>{"type"=>"integer", "format"=>"int32"}, + "prop_long"=>{"type"=>"integer", "format"=>"int64"}, + "prop_float"=>{"type"=>"number", "format"=>"float"}, + "prop_double"=>{"type"=>"number", "format"=>"double"}, + "prop_string"=>{"type"=>"string"}, + "prop_symbol"=>{"type"=>"string"}, + "prop_date"=>{"type"=>"string", "format"=>"date"}, + "prop_date_time"=>{"type"=>"string", "format"=>"date-time"}, + "prop_time"=>{"type"=>"string", "format"=>"date-time"}, + "prop_password"=>{"type"=>"string", "format"=>"password"}, + "prop_email"=>{"type"=>"string", "format"=>"email"}, + "prop_boolean"=>{"type"=>"boolean"}, + "prop_file"=>{"type"=>"file"}, + "prop_json"=>{"type"=>"json"} + }) + end +end diff --git a/spec/swagger_v2/response_model_spec.rb b/spec/swagger_v2/response_model_spec.rb index 816b7c08..858db5a1 100644 --- a/spec/swagger_v2/response_model_spec.rb +++ b/spec/swagger_v2/response_model_spec.rb @@ -191,7 +191,7 @@ def app it "it prefer entity over others" do expect(subject['definitions']).to eql({ - "Kind"=>{"type"=>"object", "properties"=>{"id"=>{"type"=>"integer"}}}, + "Kind"=>{"type"=>"object", "properties"=>{"id"=>{"type"=>"integer", "format"=>"int32"}}}, "Tag"=>{"type"=>"object", "properties"=>{"name"=>{"type"=>"string"}}}, "Relation"=>{"type"=>"object", "properties"=>{"name"=>{"type"=>"string"}}}, "SomeEntity"=>{ @@ -202,8 +202,7 @@ def app "kind2"=>{"$ref"=>"#/definitions/Kind"}, "kind3"=>{"$ref"=>"#/definitions/Kind"}, "tags"=>{"type"=>"array", "items"=>{"$ref"=>"#/definitions/Tag"}}, - "relation"=>{"$ref"=>"#/definitions/Relation"}}} - }) - + "relation"=>{"$ref"=>"#/definitions/Relation"} + }}}) end end From 7ed82414ed974349df9f2bbb4e7a6302c2d6f5c1 Mon Sep 17 00:00:00 2001 From: Peter Scholz Date: Sat, 26 Mar 2016 02:33:46 +0100 Subject: [PATCH 3/3] updates CHANGELOG Update CHANGELOG.md --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9db049b1..bf3aa16b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ n.n.n / 2016-03-16 ================== +[#358](https://github.com/ruby-grape/grape-swagger/pull/358) + +- removes `allowMultiple` property from params +- adds `format` to definition property +- renames `defaultValue` to `default` + + [#356](https://github.com/ruby-grape/grape-swagger/pull/356) - adds `consumes` setting