mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-25 23:32:58 +00:00
First iteration of rspec driven swagger
This commit is contained in:
@@ -5,6 +5,7 @@ class Blog < ActiveRecord::Base
|
||||
|
||||
def as_json(options)
|
||||
{
|
||||
id: id,
|
||||
title: title,
|
||||
content: content
|
||||
}
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
SwaggerRails.configure do |c|
|
||||
|
||||
# List the names and paths (relative to config/swagger) of Swagger
|
||||
# documents you'd like to expose in your swagger-ui
|
||||
c.swagger_docs = {
|
||||
'API V1' => 'v1/swagger.json'
|
||||
}
|
||||
# Define the swagger documents you'd like to expose and provide global metadata
|
||||
c.swagger_doc 'v1/swagger.json' do
|
||||
{
|
||||
info: {
|
||||
title: 'API V1',
|
||||
version: 'v1'
|
||||
}
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,122 +1,79 @@
|
||||
{
|
||||
"swagger": "2.0",
|
||||
"info": {
|
||||
"version": "0.0.0",
|
||||
"title": "Dummy app for testing swagger_rails"
|
||||
},
|
||||
"paths": {
|
||||
"/blogs": {
|
||||
"post": {
|
||||
"description": "Creates a new Blog",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "X-Forwarded-For",
|
||||
"in": "header",
|
||||
"type": "string",
|
||||
"default": "client1"
|
||||
},
|
||||
{
|
||||
"name": "blog",
|
||||
"in": "body",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/Blog"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"201": {
|
||||
"description": "Created",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/Blog"
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "Invalid Request",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/RequestError"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"get": {
|
||||
"description": "Searches Bloggs",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "published",
|
||||
"in": "query",
|
||||
"type": "boolean",
|
||||
"default": "true"
|
||||
},
|
||||
{
|
||||
"name": "keywords",
|
||||
"in": "query",
|
||||
"type": "string",
|
||||
"default": "Ruby on Rails"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Ok",
|
||||
"schema": {
|
||||
"type": "array",
|
||||
"item": {
|
||||
"$ref": "#/definitions/Blog"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
"/blogs/{id}": {
|
||||
"get": {
|
||||
"description": "Retrieves a specific Blog by unique ID",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "id",
|
||||
"in": "path",
|
||||
"type": "string",
|
||||
"default": "123"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Ok",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/Blog"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"definitions": {
|
||||
"Blog": {
|
||||
"properties": {
|
||||
"swagger": "2.0",
|
||||
"info": {
|
||||
"title": "API V1",
|
||||
"version": "v1"
|
||||
},
|
||||
"paths": {
|
||||
"/blogs": {
|
||||
"post": {
|
||||
"summary": "creates a new blog",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
],
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"parameters": [
|
||||
{
|
||||
"name": "blog",
|
||||
"in": "body",
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"title": {
|
||||
"type": "string"
|
||||
"type": "string"
|
||||
},
|
||||
"content": {
|
||||
"type": "string",
|
||||
"format": "date-time"
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"example": {
|
||||
"title": "Test Blog",
|
||||
"content": "Hello World"
|
||||
}
|
||||
},
|
||||
"RequestError": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"type": "array",
|
||||
"item": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"example": {
|
||||
"title": [ "is required" ]
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"201": {
|
||||
"description": "valid request"
|
||||
},
|
||||
"422": {
|
||||
"description": "invalid request"
|
||||
}
|
||||
}
|
||||
},
|
||||
"get": {
|
||||
"summary": "searches existing blogs",
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"parameters": [
|
||||
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "valid request"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/blogs/{id}": {
|
||||
"get": {
|
||||
"summary": "retreives a specific blog",
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"parameters": [
|
||||
{
|
||||
"name": "id",
|
||||
"in": "path",
|
||||
"type": "string"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "blog found"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,19 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe 'Blogs API', doc: 'blogs/v1' do
|
||||
describe 'Blogs API', docs_path: 'blogs/v1/swagger.json' do
|
||||
|
||||
path '/blogs' do
|
||||
|
||||
operation 'post', 'creates a new blog' do
|
||||
post 'creates a new blog' do
|
||||
consumes 'application/json'
|
||||
produces 'application/json'
|
||||
body :blog
|
||||
parameter :blog, :in => :body, schema: {
|
||||
:type => :object,
|
||||
:properties => {
|
||||
title: { type: 'string' },
|
||||
content: { type: 'string' }
|
||||
}
|
||||
}
|
||||
|
||||
let(:blog) { { title: 'foo', content: 'bar' } }
|
||||
|
||||
@@ -21,7 +27,7 @@ describe 'Blogs API', doc: 'blogs/v1' do
|
||||
end
|
||||
end
|
||||
|
||||
operation 'get', 'searches existing blogs' do
|
||||
get 'searches existing blogs' do
|
||||
produces 'application/json'
|
||||
|
||||
response '200', 'valid request' do
|
||||
@@ -31,9 +37,9 @@ describe 'Blogs API', doc: 'blogs/v1' do
|
||||
end
|
||||
|
||||
path '/blogs/{id}' do
|
||||
operation 'get', 'retreives a specific blog' do
|
||||
get 'retreives a specific blog' do
|
||||
produces 'application/json'
|
||||
parameter :id, 'path'
|
||||
parameter :id, :in => :path, :type => :string
|
||||
|
||||
response '200', 'blog found' do
|
||||
let(:blog) { Blog.create(title: 'foo', content: 'bar') }
|
||||
|
||||
@@ -50,4 +50,7 @@ RSpec.configure do |config|
|
||||
config.filter_rails_from_backtrace!
|
||||
# arbitrary gems may also be filtered via:
|
||||
# config.filter_gems_from_backtrace("gem name")
|
||||
|
||||
require 'swagger_rails/rspec/adapter'
|
||||
config.extend SwaggerRails::RSpec::Adapter
|
||||
end
|
||||
|
||||
@@ -89,7 +89,4 @@ RSpec.configure do |config|
|
||||
# as the one that triggered the failure.
|
||||
Kernel.srand config.seed
|
||||
=end
|
||||
|
||||
require 'swagger_rails/rspec/adapter'
|
||||
config.extend SwaggerRails::RSpec::Adapter
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user