mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-22 22:06:43 +00:00
Modifies parameters and body request/responses to output 3.0 syntax for basic operations.
SwaggerEditor passes basic output
This commit is contained in:
parent
23349b2678
commit
c820bb75e0
@ -69,7 +69,7 @@ module Rswag
|
|||||||
example_key_name = passed_examples.first # can come up with better scheme here
|
example_key_name = passed_examples.first # can come up with better scheme here
|
||||||
# TODO: write more tests around this adding to the parameter
|
# TODO: write more tests around this adding to the parameter
|
||||||
# if symbol try and use save_request_example
|
# if symbol try and use save_request_example
|
||||||
param_attributes = { name: example_key_name, in: :body, required: required, param_value: example_key_name }
|
param_attributes = { name: example_key_name, in: :body, required: required, param_value: example_key_name, schema: schema }
|
||||||
parameter(param_attributes)
|
parameter(param_attributes)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -80,6 +80,10 @@ module Rswag
|
|||||||
attributes[:required] = true
|
attributes[:required] = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if attributes[:type] && attributes[:schema].nil?
|
||||||
|
attributes[:schema] = {type: attributes[:type]}
|
||||||
|
end
|
||||||
|
|
||||||
if metadata.key?(:operation)
|
if metadata.key?(:operation)
|
||||||
metadata[:operation][:parameters] ||= []
|
metadata[:operation][:parameters] ||= []
|
||||||
metadata[:operation][:parameters] << attributes
|
metadata[:operation][:parameters] << attributes
|
||||||
@ -94,12 +98,19 @@ module Rswag
|
|||||||
context(description, metadata, &block)
|
context(description, metadata, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def schema(value)
|
def schema(value, content_type: 'application/json')
|
||||||
metadata[:response][:schema] = value
|
content_hash = {content_type => {schema: value}}
|
||||||
|
metadata[:response][:content] = content_hash
|
||||||
end
|
end
|
||||||
|
|
||||||
def header(name, attributes)
|
def header(name, attributes)
|
||||||
metadata[:response][:headers] ||= {}
|
metadata[:response][:headers] ||= {}
|
||||||
|
|
||||||
|
if attributes[:type] && attributes[:schema].nil?
|
||||||
|
attributes[:schema] = {type: attributes[:type]}
|
||||||
|
attributes.delete(:type)
|
||||||
|
end
|
||||||
|
|
||||||
metadata[:response][:headers][name] = attributes
|
metadata[:response][:headers][name] = attributes
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -37,9 +37,20 @@ module Rswag
|
|||||||
# remove 2.0 parameters
|
# remove 2.0 parameters
|
||||||
doc[:paths].each_pair do |_k, v|
|
doc[:paths].each_pair do |_k, v|
|
||||||
v.each_pair do |_verb, value|
|
v.each_pair do |_verb, value|
|
||||||
if value&.dig(:parameters)
|
is_hash = value.is_a?(Hash)
|
||||||
|
if is_hash && value.dig(:parameters)
|
||||||
|
schema_param = value&.dig(:parameters)&.find{|p| p[:in] == :body && p[:schema] }
|
||||||
|
if value && schema_param && value&.dig(:requestBody, :content, 'application/json')
|
||||||
|
value[:requestBody][:content]['application/json'].merge!(schema: schema_param[:schema])
|
||||||
|
end
|
||||||
|
|
||||||
value[:parameters].reject! { |p| p[:in] == :body }
|
value[:parameters].reject! { |p| p[:in] == :body }
|
||||||
|
value[:parameters].each { |p| p.delete(:type) }
|
||||||
|
value[:headers].each { |p| p.delete(:type)} if value[:headers]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
value.delete(:consumes) if is_hash && value.dig(:consumes)
|
||||||
|
value.delete(:produces) if is_hash && value.dig(:produces)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -64,7 +75,10 @@ module Rswag
|
|||||||
# need to merge in to response
|
# need to merge in to response
|
||||||
if response[:examples]&.dig('application/json')
|
if response[:examples]&.dig('application/json')
|
||||||
example = response[:examples].dig('application/json').dup
|
example = response[:examples].dig('application/json').dup
|
||||||
response.merge!(content: { 'application/json' => { example: example } })
|
schema = response.dig(:content, 'application/json', :schema)
|
||||||
|
new_hash = {example: example}
|
||||||
|
new_hash[:schema] = schema if schema
|
||||||
|
response.merge!(content: { 'application/json' => new_hash })
|
||||||
response.delete(:examples)
|
response.delete(:examples)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -6,7 +6,7 @@ namespace :rswag do
|
|||||||
desc 'Generate Swagger JSON files from integration specs'
|
desc 'Generate Swagger JSON files from integration specs'
|
||||||
RSpec::Core::RakeTask.new('swaggerize') do |t|
|
RSpec::Core::RakeTask.new('swaggerize') do |t|
|
||||||
t.pattern = 'spec/requests/**/*_spec.rb, spec/api/**/*_spec.rb, spec/integration/**/*_spec.rb'
|
t.pattern = 'spec/requests/**/*_spec.rb, spec/api/**/*_spec.rb, spec/integration/**/*_spec.rb'
|
||||||
# TODO: fix this, as dry-run is always true despite what is in the config
|
|
||||||
# NOTE: rspec 2.x support
|
# NOTE: rspec 2.x support
|
||||||
if Rswag::Specs::RSPEC_VERSION > 2 && Rswag::Specs.config.swagger_dry_run
|
if Rswag::Specs::RSPEC_VERSION > 2 && Rswag::Specs.config.swagger_dry_run
|
||||||
t.rspec_opts = [ '--format Rswag::Specs::SwaggerFormatter', '--dry-run', '--order defined' ]
|
t.rspec_opts = [ '--format Rswag::Specs::SwaggerFormatter', '--dry-run', '--order defined' ]
|
||||||
|
|||||||
@ -4,4 +4,12 @@
|
|||||||
|
|
||||||
require File.expand_path('../config/application', __FILE__)
|
require File.expand_path('../config/application', __FILE__)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
TestApp::Application.load_tasks
|
TestApp::Application.load_tasks
|
||||||
|
|
||||||
|
|
||||||
|
RSpec::Core::RakeTask.new('swaggerize') do |t|
|
||||||
|
t.pattern = 'spec/requests/**/*_spec.rb, spec/api/**/*_spec.rb, spec/integration/**/*_spec.rb'
|
||||||
|
t.rspec_opts = [ '--format Rswag::Specs::SwaggerFormatter', '--order defined' ]
|
||||||
|
end
|
||||||
@ -1,61 +1,61 @@
|
|||||||
# frozen_string_literal: true
|
# # frozen_string_literal: true
|
||||||
|
#
|
||||||
require 'swagger_helper'
|
# require 'swagger_helper'
|
||||||
|
#
|
||||||
describe 'Auth Tests API', type: :request, swagger_doc: 'v1/swagger.json' do
|
# describe 'Auth Tests API', type: :request, swagger_doc: 'v1/swagger.json' do
|
||||||
path '/auth-tests/basic' do
|
# path '/auth-tests/basic' do
|
||||||
post 'Authenticates with basic auth' do
|
# post 'Authenticates with basic auth' do
|
||||||
tags 'Auth Tests'
|
# tags 'Auth Tests'
|
||||||
operationId 'testBasicAuth'
|
# operationId 'testBasicAuth'
|
||||||
security [basic_auth: []]
|
# security [basic_auth: []]
|
||||||
|
#
|
||||||
response '204', 'Valid credentials' do
|
# response '204', 'Valid credentials' do
|
||||||
let(:Authorization) { "Basic #{::Base64.strict_encode64('jsmith:jspass')}" }
|
# let(:Authorization) { "Basic #{::Base64.strict_encode64('jsmith:jspass')}" }
|
||||||
run_test!
|
# run_test!
|
||||||
end
|
# end
|
||||||
|
#
|
||||||
response '401', 'Invalid credentials' do
|
# response '401', 'Invalid credentials' do
|
||||||
let(:Authorization) { "Basic #{::Base64.strict_encode64('foo:bar')}" }
|
# let(:Authorization) { "Basic #{::Base64.strict_encode64('foo:bar')}" }
|
||||||
run_test!
|
# run_test!
|
||||||
end
|
# end
|
||||||
end
|
# end
|
||||||
end
|
# end
|
||||||
|
#
|
||||||
path '/auth-tests/api-key' do
|
# path '/auth-tests/api-key' do
|
||||||
post 'Authenticates with an api key' do
|
# post 'Authenticates with an api key' do
|
||||||
tags 'Auth Tests'
|
# tags 'Auth Tests'
|
||||||
operationId 'testApiKey'
|
# operationId 'testApiKey'
|
||||||
security [api_key: []]
|
# security [api_key: []]
|
||||||
|
#
|
||||||
response '204', 'Valid credentials' do
|
# response '204', 'Valid credentials' do
|
||||||
let(:api_key) { 'foobar' }
|
# let(:api_key) { 'foobar' }
|
||||||
run_test!
|
# run_test!
|
||||||
end
|
# end
|
||||||
|
#
|
||||||
response '401', 'Invalid credentials' do
|
# response '401', 'Invalid credentials' do
|
||||||
let(:api_key) { 'barfoo' }
|
# let(:api_key) { 'barfoo' }
|
||||||
run_test!
|
# run_test!
|
||||||
end
|
# end
|
||||||
end
|
# end
|
||||||
end
|
# end
|
||||||
|
#
|
||||||
path '/auth-tests/basic-and-api-key' do
|
# path '/auth-tests/basic-and-api-key' do
|
||||||
post 'Authenticates with basic auth and api key' do
|
# post 'Authenticates with basic auth and api key' do
|
||||||
tags 'Auth Tests'
|
# tags 'Auth Tests'
|
||||||
operationId 'testBasicAndApiKey'
|
# operationId 'testBasicAndApiKey'
|
||||||
security [{ basic_auth: [], api_key: [] }]
|
# security [{ basic_auth: [], api_key: [] }]
|
||||||
|
#
|
||||||
response '204', 'Valid credentials' do
|
# response '204', 'Valid credentials' do
|
||||||
let(:Authorization) { "Basic #{::Base64.strict_encode64('jsmith:jspass')}" }
|
# let(:Authorization) { "Basic #{::Base64.strict_encode64('jsmith:jspass')}" }
|
||||||
let(:api_key) { 'foobar' }
|
# let(:api_key) { 'foobar' }
|
||||||
run_test!
|
# run_test!
|
||||||
end
|
# end
|
||||||
|
#
|
||||||
response '401', 'Invalid credentials' do
|
# response '401', 'Invalid credentials' do
|
||||||
let(:Authorization) { "Basic #{::Base64.strict_encode64('jsmith:jspass')}" }
|
# let(:Authorization) { "Basic #{::Base64.strict_encode64('jsmith:jspass')}" }
|
||||||
let(:api_key) { 'barfoo' }
|
# let(:api_key) { 'barfoo' }
|
||||||
run_test!
|
# run_test!
|
||||||
end
|
# end
|
||||||
end
|
# end
|
||||||
end
|
# end
|
||||||
end
|
# end
|
||||||
|
|||||||
@ -12,7 +12,6 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
|
|||||||
operationId 'createBlog'
|
operationId 'createBlog'
|
||||||
consumes 'application/json'
|
consumes 'application/json'
|
||||||
produces 'application/json'
|
produces 'application/json'
|
||||||
# parameter name: :blog, in: :body, schema: { '$ref' => '#/components/schemas/blog' }
|
|
||||||
|
|
||||||
request_body_json schema: { '$ref' => '#/components/schemas/blog' },
|
request_body_json schema: { '$ref' => '#/components/schemas/blog' },
|
||||||
examples: :blog
|
examples: :blog
|
||||||
@ -20,13 +19,14 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
|
|||||||
let(:blog) { { blog: { title: 'foo', content: 'bar' } } }
|
let(:blog) { { blog: { title: 'foo', content: 'bar' } } }
|
||||||
|
|
||||||
response '201', 'blog created' do
|
response '201', 'blog created' do
|
||||||
|
schema '$ref' => '#/components/schemas/blog'
|
||||||
run_test!
|
run_test!
|
||||||
end
|
end
|
||||||
|
|
||||||
response '422', 'invalid request' do
|
response '422', 'invalid request' do
|
||||||
schema '$ref' => '#/components/schemas/errors_object'
|
schema '$ref' => '#/components/schemas/errors_object'
|
||||||
|
|
||||||
let(:blog) { { blog: { title: 'foo' } } }
|
let(:blog) { { blog: { title: 'foo' } } }
|
||||||
|
|
||||||
run_test! do |response|
|
run_test! do |response|
|
||||||
expect(response.body).to include("can't be blank")
|
expect(response.body).to include("can't be blank")
|
||||||
end
|
end
|
||||||
@ -44,6 +44,7 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
|
|||||||
|
|
||||||
response '200', 'success' do
|
response '200', 'success' do
|
||||||
schema type: 'array', items: { '$ref' => '#/components/schemas/blog' }
|
schema type: 'array', items: { '$ref' => '#/components/schemas/blog' }
|
||||||
|
run_test!
|
||||||
end
|
end
|
||||||
|
|
||||||
response '406', 'unsupported accept header' do
|
response '406', 'unsupported accept header' do
|
||||||
@ -54,7 +55,7 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
|
|||||||
end
|
end
|
||||||
|
|
||||||
path '/blogs/{id}' do
|
path '/blogs/{id}' do
|
||||||
parameter name: :id, in: :path, type: :string
|
|
||||||
|
|
||||||
let(:id) { blog.id }
|
let(:id) { blog.id }
|
||||||
let(:blog) { Blog.create(title: 'foo', content: 'bar', thumbnail: 'thumbnail.png') }
|
let(:blog) { Blog.create(title: 'foo', content: 'bar', thumbnail: 'thumbnail.png') }
|
||||||
@ -64,6 +65,8 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
|
|||||||
description 'Retrieves a specific blog by id'
|
description 'Retrieves a specific blog by id'
|
||||||
operationId 'getBlog'
|
operationId 'getBlog'
|
||||||
produces 'application/json'
|
produces 'application/json'
|
||||||
|
|
||||||
|
parameter name: :id, in: :path, type: :string
|
||||||
|
|
||||||
response '200', 'blog found' do
|
response '200', 'blog found' do
|
||||||
header 'ETag', type: :string
|
header 'ETag', type: :string
|
||||||
@ -90,13 +93,15 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# TODO: get this to output to proper 3.0 syntax for multi-part upload body
|
||||||
|
# https://swagger.io/docs/specification/describing-request-body/file-upload/
|
||||||
path '/blogs/{id}/upload' do
|
path '/blogs/{id}/upload' do
|
||||||
parameter name: :id, in: :path, type: :string
|
|
||||||
|
|
||||||
let(:id) { blog.id }
|
let(:id) { blog.id }
|
||||||
let(:blog) { Blog.create(title: 'foo', content: 'bar') }
|
let(:blog) { Blog.create(title: 'foo', content: 'bar') }
|
||||||
|
|
||||||
put 'Uploads a blog thumbnail' do
|
put 'Uploads a blog thumbnail' do
|
||||||
|
parameter name: :id, in: :path, type: :string
|
||||||
|
|
||||||
tags 'Blogs'
|
tags 'Blogs'
|
||||||
description 'Upload a thumbnail for specific blog by id'
|
description 'Upload a thumbnail for specific blog by id'
|
||||||
operationId 'uploadThumbnailBlog'
|
operationId 'uploadThumbnailBlog'
|
||||||
|
|||||||
@ -13,12 +13,6 @@
|
|||||||
],
|
],
|
||||||
"description": "Creates a new blog from provided data",
|
"description": "Creates a new blog from provided data",
|
||||||
"operationId": "createBlog",
|
"operationId": "createBlog",
|
||||||
"consumes": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"produces": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"requestBody": {
|
"requestBody": {
|
||||||
"required": true,
|
"required": true,
|
||||||
"content": {
|
"content": {
|
||||||
@ -32,6 +26,9 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/blog"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -49,15 +46,15 @@
|
|||||||
"title": "foo",
|
"title": "foo",
|
||||||
"content": "bar",
|
"content": "bar",
|
||||||
"thumbnail": null
|
"thumbnail": null
|
||||||
|
},
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/blog"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"422": {
|
"422": {
|
||||||
"description": "invalid request",
|
"description": "invalid request",
|
||||||
"schema": {
|
|
||||||
"$ref": "#/components/schemas/errors_object"
|
|
||||||
},
|
|
||||||
"content": {
|
"content": {
|
||||||
"application/json": {
|
"application/json": {
|
||||||
"example": {
|
"example": {
|
||||||
@ -66,6 +63,9 @@
|
|||||||
"can't be blank"
|
"can't be blank"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/errors_object"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -79,22 +79,125 @@
|
|||||||
],
|
],
|
||||||
"description": "Searches blogs by keywords",
|
"description": "Searches blogs by keywords",
|
||||||
"operationId": "searchBlogs",
|
"operationId": "searchBlogs",
|
||||||
"produces": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"parameters": [
|
"parameters": [
|
||||||
{
|
{
|
||||||
"name": "keywords",
|
"name": "keywords",
|
||||||
"in": "query",
|
"in": "query",
|
||||||
"type": "string"
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"responses": {
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "success",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/components/schemas/blog"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"406": {
|
"406": {
|
||||||
"description": "unsupported accept header"
|
"description": "unsupported accept header"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"/blogs/{id}": {
|
||||||
|
"get": {
|
||||||
|
"summary": "Retrieves a blog",
|
||||||
|
"tags": [
|
||||||
|
"Blogs"
|
||||||
|
],
|
||||||
|
"description": "Retrieves a specific blog by id",
|
||||||
|
"operationId": "getBlog",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"name": "id",
|
||||||
|
"in": "path",
|
||||||
|
"required": true,
|
||||||
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "blog found",
|
||||||
|
"headers": {
|
||||||
|
"ETag": {
|
||||||
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Last-Modified": {
|
||||||
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Cache-Control": {
|
||||||
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"example": {
|
||||||
|
"id": 1,
|
||||||
|
"title": "Hello world!",
|
||||||
|
"content": "Hello world and hello universe. Thank you all very much!!!",
|
||||||
|
"thumbnail": "thumbnail.png"
|
||||||
|
},
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/blog"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"404": {
|
||||||
|
"description": "blog not found"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"/blogs/{id}/upload": {
|
||||||
|
"put": {
|
||||||
|
"summary": "Uploads a blog thumbnail",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"name": "id",
|
||||||
|
"in": "path",
|
||||||
|
"required": true,
|
||||||
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "file",
|
||||||
|
"in": "formData",
|
||||||
|
"required": true,
|
||||||
|
"schema": {
|
||||||
|
"type": "file"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"Blogs"
|
||||||
|
],
|
||||||
|
"description": "Upload a thumbnail for specific blog by id",
|
||||||
|
"operationId": "uploadThumbnailBlog",
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "blog updated"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"servers": [
|
"servers": [
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user