|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +# mapping of parameter types |
| 6 | +# Grape -> Swagger (OpenApi) |
| 7 | +# (type format) |
| 8 | +# --------------------------------------------------- |
| 9 | +# Integer -> integer int32 |
| 10 | +# Numeric -> integer int64 |
| 11 | +# Float -> number float |
| 12 | +# BigDecimal -> number double |
| 13 | +# String -> string |
| 14 | +# Symbol -> string |
| 15 | +# Date -> string date |
| 16 | +# DateTime -> string date-time |
| 17 | +# Time -> string date-time |
| 18 | +# 'password' -> string password |
| 19 | +# 'email' -> string email |
| 20 | +# Boolean -> boolean |
| 21 | +# JSON -> json |
| 22 | +# Rack::Multipart::UploadedFile -> file |
| 23 | + |
| 24 | +describe 'type format settings' do |
| 25 | + include_context "#{MODEL_PARSER} swagger example" |
| 26 | + |
| 27 | + before :all do |
| 28 | + module TheApi |
| 29 | + class TypeFormatApi < Grape::API |
| 30 | + desc 'full set of request data types', |
| 31 | + success: Entities::TypedDefinition |
| 32 | + |
| 33 | + params do |
| 34 | + # grape supported data types |
| 35 | + requires :param_integer, type: Integer |
| 36 | + requires :param_long, type: Numeric |
| 37 | + requires :param_float, type: Float |
| 38 | + requires :param_double, type: BigDecimal |
| 39 | + optional :param_string, type: String |
| 40 | + optional :param_symbol, type: Symbol |
| 41 | + requires :param_date, type: Date |
| 42 | + requires :param_date_time, type: DateTime |
| 43 | + requires :param_time, type: Time |
| 44 | + optional :param_boolean, type: Boolean |
| 45 | + optional :param_file, type: File |
| 46 | + optional :param_json, type: JSON |
| 47 | + end |
| 48 | + |
| 49 | + post '/request_types' do |
| 50 | + { 'declared_params' => declared(params) } |
| 51 | + end |
| 52 | + |
| 53 | + add_swagger_documentation openapi_version: '3.0' |
| 54 | + end |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + def app |
| 59 | + TheApi::TypeFormatApi |
| 60 | + end |
| 61 | + |
| 62 | + subject do |
| 63 | + get '/swagger_doc/request_types' |
| 64 | + JSON.parse(last_response.body) |
| 65 | + end |
| 66 | + |
| 67 | + specify do |
| 68 | + expect(subject['paths']['/request_types']['post']['requestBody']).to eql( |
| 69 | + 'content' => { |
| 70 | + 'application/json' => { |
| 71 | + 'schema' => { |
| 72 | + 'properties' => { |
| 73 | + 'param_json' => { 'type' => 'object' } |
| 74 | + }, |
| 75 | + 'type' => 'object' |
| 76 | + } |
| 77 | + }, |
| 78 | + 'application/octet-stream' => { |
| 79 | + 'schema' => { |
| 80 | + 'properties' => { |
| 81 | + 'param_file' => { |
| 82 | + 'format' => 'binary', 'type' => 'string' |
| 83 | + } |
| 84 | + }, |
| 85 | + 'type' => 'object' |
| 86 | + } |
| 87 | + }, |
| 88 | + 'application/x-www-form-urlencoded' => { |
| 89 | + 'schema' => { |
| 90 | + 'properties' => { |
| 91 | + 'param_boolean' => { 'type' => 'boolean' }, |
| 92 | + 'param_date' => { 'format' => 'date', 'type' => 'string' }, |
| 93 | + 'param_date_time' => { 'format' => 'date-time', 'type' => 'string' }, |
| 94 | + 'param_double' => { 'format' => 'double', 'type' => 'number' }, |
| 95 | + 'param_float' => { 'format' => 'float', 'type' => 'number' }, |
| 96 | + 'param_integer' => { 'format' => 'int32', 'type' => 'integer' }, |
| 97 | + 'param_long' => { 'format' => 'int64', 'type' => 'integer' }, |
| 98 | + 'param_string' => { 'type' => 'string' }, |
| 99 | + 'param_symbol' => { 'type' => 'string' }, |
| 100 | + 'param_time' => { 'format' => 'date-time', 'type' => 'string' } |
| 101 | + }, |
| 102 | + 'required' => %w[param_integer param_long param_float param_double param_date param_date_time param_time], |
| 103 | + 'type' => 'object' |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + ) |
| 108 | + end |
| 109 | + |
| 110 | + specify do |
| 111 | + expect(subject['components']['schemas']['TypedDefinition']['properties']).to eql(swagger_typed_defintion) |
| 112 | + end |
| 113 | +end |
0 commit comments