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

[WIP] Open api3 rebased #928

Draft
wants to merge 55 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
4604e1a
wip: openapi 3.0
Oct 13, 2018
f29e14c
move files to respective version folders
Oct 14, 2018
ac90d9f
begin to parse params
Oct 20, 2018
6bac652
fix failing tests
Nov 1, 2018
af9b11d
Initial support for openapi3 requestBody
Nov 10, 2018
c266c5b
OpenAPI3 parameters and request bodies are actually OpenAPI3 compliant
Nov 12, 2018
c7170da
Fix array parameter type (not custom types for now)
Nov 16, 2018
94a3fee
add tests
Nov 17, 2018
3189dfa
Initial support for openapi3 components/schemas
Nov 17, 2018
d3120f8
Fix tests
Nov 17, 2018
7f846a6
Add test
Nov 18, 2018
54de96d
Add test
Nov 25, 2018
aeb40a7
Fix edge cases with ranges, floats and strings
Nov 25, 2018
1c85b58
Add test
Nov 25, 2018
ce8c852
Add test
Nov 25, 2018
a7872ca
Add test
Nov 25, 2018
f0f2816
Add test
Nov 25, 2018
48932a5
Add test
Nov 25, 2018
ac5ad64
Better implementation of request body
Nov 30, 2018
a41d712
Fix test
Nov 30, 2018
c2e6a5a
Add test
Nov 30, 2018
47ec64e
Add test
Nov 30, 2018
5d3b522
Add test
Nov 30, 2018
d305600
fix array test
Dec 2, 2018
dcdcc38
Fix test
Dec 6, 2018
1518be3
Add test
Dec 6, 2018
8998cbf
Add test
Dec 6, 2018
e1d09c1
Add test
Dec 6, 2018
3eb21a2
Add test
Dec 6, 2018
12e7a4f
Add test
Dec 6, 2018
d65d13e
Add test
Dec 6, 2018
bbf21b9
Add test
Dec 6, 2018
5ff4d39
Add test
Dec 6, 2018
4c470fb
Add test
Dec 6, 2018
f50a24a
Fix tests
Dec 6, 2018
7a3fb37
Add test
Dec 7, 2018
a89c16f
Add test
Dec 7, 2018
c0d9ee9
Add test
Dec 7, 2018
7199caf
Add test
Dec 7, 2018
1ebeb33
Fix file response body
Dec 11, 2018
b3eaa88
Add examples spec
Dec 17, 2018
b4c95fe
Fix host test
Dec 27, 2018
230efaf
Add specs
Dec 27, 2018
5d6bb3a
Fix guarded endpoint spec, after a rebase
Dec 28, 2018
9713a2b
Fix spec
Dec 28, 2018
1f9c449
Add spec
Dec 30, 2018
9b6cb2a
Add spec
Jan 20, 2019
2cbb17b
Fix: Prevent class name collisions in specs with ApiClassDefinitionCl…
numbata Jul 13, 2024
fd14ca9
Fix ParamsParser call from OpenAPI3 module
numbata Jul 13, 2024
f55b906
Skip anonymous API class definition deletion in specs
numbata Jul 13, 2024
510457f
Cleanup a bit
numbata Jul 14, 2024
752525a
Set the right $ref object for the request body
numbata Jul 14, 2024
dcecc68
Specs fix
numbata Jul 14, 2024
00ed99e
Add schema validation against OpenAPI3 specification
numbata Jul 14, 2024
5288b69
Getting rid of the debug noise a little bit
numbata Jul 14, 2024
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
Prev Previous commit
Next Next commit
OpenAPI3 parameters and request bodies are actually OpenAPI3 compliant
blake authored and numbata committed Jul 12, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit c266c5bb8e90086acfe1c1b1a333c7fff0565eb6
7 changes: 4 additions & 3 deletions lib/grape-swagger/openapi_3/doc_methods/parse_params.rb
Original file line number Diff line number Diff line change
@@ -52,13 +52,14 @@ def document_default_value(settings)
end

def document_type_and_format(settings, data_type)
@parsed_param[:schema] = {}
if DataType.primitive?(data_type)
data = DataType.mapping(data_type)
@parsed_param[:type], @parsed_param[:format] = data
@parsed_param[:schema][:type], @parsed_param[:schema][:format] = data
else
@parsed_param[:type] = data_type
@parsed_param[:schema][:type] = data_type
end
@parsed_param[:format] = settings[:format] if settings[:format].present?
@parsed_param[:schema][:format] = settings[:format] if settings[:format].present?
end

def document_array_param(value_type, definitions)
25 changes: 8 additions & 17 deletions lib/grape-swagger/openapi_3/endpoint.rb
Original file line number Diff line number Diff line change
@@ -117,13 +117,15 @@ def method_object(route, options, path)
method = {}
method[:summary] = summary_object(route)
method[:description] = description_object(route)
method[:parameters] = params_object(route, options, path)

parameters = params_object(route, options, path).partition { |p| p[:in] == 'body' || p[:in] == 'formData' }

method[:parameters] = parameters.last
method[:security] = security_object(route)
if %w[POST PUT PATCH].include?(route.request_method)
method[:requestBody] = response_body_object(route, options, path)
method[:requestBody] = response_body_object(route, path, parameters.first)
end

# method[:consumes] = consumes_object(route, options[:format])
produces = produces_object(route, options[:produces] || options[:format])

method[:responses] = response_object(route, produces)
@@ -200,28 +202,17 @@ def params_object(route, options, path)
parameters
end

def response_body_object(route, options, path)
parameters = partition_params(route, options).map do |param, value|
value = { required: false }.merge(value) if value.is_a?(Hash)
_, value = default_type([[param, value]]).first if value == ''
if value[:type]
expose_params(value[:type])
elsif value[:documentation]
expose_params(value[:documentation][:type])
end

GrapeSwagger::DocMethods::ParseRequestBody.call(param, value, path, route, @definitions)
end.flatten

def response_body_object(_, _, parameters)
parameters = {
'content' => parameters.group_by { |p| p[:in] }.map do |_k, v|
properties = v.map { |value| [value[:name], value.except(:name, :in, :required, :schema).merge(value[:schema])] }.to_h
required_values = v.select { |param| param[:required] }
[
'application/x-www-form-urlencoded',
{ 'schema' => {
'type' => 'object',
'required' => required_values.map { |required| required[:name] },
'properties' => v.map { |value| [value[:name], value.except(:name, :in, :required)] }.to_h
'properties' => properties
} }
]
end.to_h
81 changes: 81 additions & 0 deletions spec/openapi_3/param_multi_type_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'Params Multi Types' do
def app
Class.new(Grape::API) do
format :json

params do
if Grape::VERSION < '0.14'
requires :input, type: [String, Integer]
else
requires :input, types: [String, Integer]
end
requires :another_input, type: [String, Integer]
end
post :action do
end

add_swagger_documentation openapi_version: '3.0'
end
end

subject do
get '/swagger_doc/action'
expect(last_response.status).to eq 200
body = JSON.parse last_response.body
body['paths']['/action']['post']
end

it 'reads request body type correctly' do
expect(subject['requestBody']['content']).to eq('application/x-www-form-urlencoded' => {
'schema' => {
'properties' => { 'another_input' => { 'type' => 'string' }, 'input' => { 'type' => 'string' } },
'required' => %w[input another_input],
'type' => 'object'
}
})
end

describe 'header params' do
def app
Class.new(Grape::API) do
format :json

desc 'Some API', headers: { 'My-Header' => { required: true, description: 'Set this!' } }
params do
if Grape::VERSION < '0.14'
requires :input, type: [String, Integer]
else
requires :input, types: [String, Integer]
end
requires :another_input, type: [String, Integer]
end
post :action do
end

add_swagger_documentation openapi_version: '3.0'
end
end

it 'reads parameter type correctly' do
expect(subject['parameters']).to eq([{
'description' => 'Set this!',
'in' => 'header',
'name' => 'My-Header',
'required' => true,
'schema' => { 'type' => 'string' }
}])
end

it 'has consistent types' do
request_body_types = subject['requestBody']['content']['application/x-www-form-urlencoded']['schema']['properties'].values.map { |param| param['type'] }
expect(request_body_types).to eq(%w[string string])

request_body_types = subject['parameters'].map { |param| param['schema']['type'] }
expect(request_body_types).to eq(%w[string])
end
end
end