Adds anyOf support to requestBody

This commit is contained in:
Jay Danielian 2019-07-20 13:50:38 -04:00
parent eb4e6045c5
commit cd348b53f8
6 changed files with 145 additions and 0 deletions

View File

@ -8,6 +8,18 @@ class BlogsController < ApplicationController
respond_with @blog
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
def upload
@blog = Blog.find_by_id(params[:id])

View File

@ -3,6 +3,9 @@
class Blog < ActiveRecord::Base
validates :content, presence: true
alias_attribute :headline, :title
alias_attribute :text, :content
def as_json(_options)
{
id: id,

View File

@ -1,4 +1,6 @@
TestApp::Application.routes.draw do
post '/blogs/flexible', to: 'blogs#flexible_create'
resources :blogs
put '/blogs/:id/upload', to: 'blogs#upload'

View File

@ -16,6 +16,9 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
request_body_json schema: { '$ref' => '#/components/schemas/blog' },
examples: :blog
request_body_text_plain
request_body_xml schema: { '$ref' => '#/components/schemas/blog' }
let(:blog) { { blog: { title: 'foo', content: 'bar' } } }
response '201', 'blog created' do
@ -54,6 +57,30 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
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

View File

@ -57,6 +57,16 @@ RSpec.configure do |config|
thumbnail: { type: 'string' }
},
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: {

View File

@ -105,6 +105,16 @@
"schema": {
"$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}": {
"get": {
"summary": "Retrieves a blog",
@ -336,6 +404,29 @@
"content",
"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": {