mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-22 22:06:43 +00:00
Adds anyOf support to requestBody
This commit is contained in:
parent
eb4e6045c5
commit
cd348b53f8
@ -8,6 +8,18 @@ class BlogsController < ApplicationController
|
|||||||
respond_with @blog
|
respond_with @blog
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# POST /blogs/flexible
|
||||||
|
def flexible_create
|
||||||
|
|
||||||
|
# contrived example to play around with new anyOf and oneOf
|
||||||
|
# request body definition for 3.0
|
||||||
|
blog_params = params.require(:blog).permit(:title, :content, :headline, :text)
|
||||||
|
|
||||||
|
|
||||||
|
@blog = Blog.create(blog_params)
|
||||||
|
respond_with @blog
|
||||||
|
end
|
||||||
|
|
||||||
# Put /blogs/1
|
# Put /blogs/1
|
||||||
def upload
|
def upload
|
||||||
@blog = Blog.find_by_id(params[:id])
|
@blog = Blog.find_by_id(params[:id])
|
||||||
|
|||||||
@ -3,6 +3,9 @@
|
|||||||
class Blog < ActiveRecord::Base
|
class Blog < ActiveRecord::Base
|
||||||
validates :content, presence: true
|
validates :content, presence: true
|
||||||
|
|
||||||
|
alias_attribute :headline, :title
|
||||||
|
alias_attribute :text, :content
|
||||||
|
|
||||||
def as_json(_options)
|
def as_json(_options)
|
||||||
{
|
{
|
||||||
id: id,
|
id: id,
|
||||||
|
|||||||
@ -1,4 +1,6 @@
|
|||||||
TestApp::Application.routes.draw do
|
TestApp::Application.routes.draw do
|
||||||
|
|
||||||
|
post '/blogs/flexible', to: 'blogs#flexible_create'
|
||||||
resources :blogs
|
resources :blogs
|
||||||
put '/blogs/:id/upload', to: 'blogs#upload'
|
put '/blogs/:id/upload', to: 'blogs#upload'
|
||||||
|
|
||||||
|
|||||||
@ -16,6 +16,9 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
|
|||||||
request_body_json schema: { '$ref' => '#/components/schemas/blog' },
|
request_body_json schema: { '$ref' => '#/components/schemas/blog' },
|
||||||
examples: :blog
|
examples: :blog
|
||||||
|
|
||||||
|
request_body_text_plain
|
||||||
|
request_body_xml schema: { '$ref' => '#/components/schemas/blog' }
|
||||||
|
|
||||||
let(:blog) { { blog: { title: 'foo', content: 'bar' } } }
|
let(:blog) { { blog: { title: 'foo', content: 'bar' } } }
|
||||||
|
|
||||||
response '201', 'blog created' do
|
response '201', 'blog created' do
|
||||||
@ -54,6 +57,30 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
path '/blogs/flexible' do
|
||||||
|
post 'Creates a blog flexible body' do
|
||||||
|
tags 'Blogs'
|
||||||
|
description 'Creates a flexible blog from provided data'
|
||||||
|
operationId 'createFlexibleBlog'
|
||||||
|
consumes 'application/json'
|
||||||
|
produces 'application/json'
|
||||||
|
|
||||||
|
request_body_json schema: {
|
||||||
|
:oneOf => [{'$ref' => '#/components/schemas/blog'},
|
||||||
|
{'$ref' => '#/components/schemas/flexible_blog'}]
|
||||||
|
},
|
||||||
|
examples: :flexible_blog
|
||||||
|
|
||||||
|
let(:flexible_blog) { { blog: { headline: 'my headline', text: 'my text' } } }
|
||||||
|
|
||||||
|
response '201', 'flexible blog created' do
|
||||||
|
schema '$ref' => '#/components/schemas/blog'
|
||||||
|
run_test!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
path '/blogs/{id}' do
|
path '/blogs/{id}' do
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -57,6 +57,16 @@ RSpec.configure do |config|
|
|||||||
thumbnail: { type: 'string' }
|
thumbnail: { type: 'string' }
|
||||||
},
|
},
|
||||||
required: %w[id title content thumbnail]
|
required: %w[id title content thumbnail]
|
||||||
|
},
|
||||||
|
flexible_blog: {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
id: { type: 'integer' },
|
||||||
|
headline: { type: 'string' },
|
||||||
|
text: { type: 'string', nullable: true },
|
||||||
|
thumbnail: { type: 'string' }
|
||||||
|
},
|
||||||
|
required: %w[id headline thumbnail]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
securitySchemes: {
|
securitySchemes: {
|
||||||
|
|||||||
@ -105,6 +105,16 @@
|
|||||||
"schema": {
|
"schema": {
|
||||||
"$ref": "#/components/schemas/blog"
|
"$ref": "#/components/schemas/blog"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"test/plain": {
|
||||||
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"application/xml": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/blog"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -183,6 +193,64 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"/blogs/flexible": {
|
||||||
|
"post": {
|
||||||
|
"summary": "Creates a blog flexible body",
|
||||||
|
"tags": [
|
||||||
|
"Blogs"
|
||||||
|
],
|
||||||
|
"description": "Creates a flexible blog from provided data",
|
||||||
|
"operationId": "createFlexibleBlog",
|
||||||
|
"requestBody": {
|
||||||
|
"required": true,
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"examples": {
|
||||||
|
"flexible_blog": {
|
||||||
|
"value": {
|
||||||
|
"blog": {
|
||||||
|
"headline": "my headline",
|
||||||
|
"text": "my text"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"schema": {
|
||||||
|
"oneOf": [
|
||||||
|
{
|
||||||
|
"$ref": "#/components/schemas/blog"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"$ref": "#/components/schemas/flexible_blog"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"parameters": [
|
||||||
|
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"201": {
|
||||||
|
"description": "flexible blog created",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"example": {
|
||||||
|
"id": 1,
|
||||||
|
"title": "my headline",
|
||||||
|
"content": "my text",
|
||||||
|
"thumbnail": null
|
||||||
|
},
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/blog"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"/blogs/{id}": {
|
"/blogs/{id}": {
|
||||||
"get": {
|
"get": {
|
||||||
"summary": "Retrieves a blog",
|
"summary": "Retrieves a blog",
|
||||||
@ -336,6 +404,29 @@
|
|||||||
"content",
|
"content",
|
||||||
"thumbnail"
|
"thumbnail"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
"flexible_blog": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"type": "integer"
|
||||||
|
},
|
||||||
|
"headline": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"text": {
|
||||||
|
"type": "string",
|
||||||
|
"nullable": true
|
||||||
|
},
|
||||||
|
"thumbnail": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"id",
|
||||||
|
"headline",
|
||||||
|
"thumbnail"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"securitySchemes": {
|
"securitySchemes": {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user