Skip to content

Commit

Permalink
improvement: add rswag spec
Browse files Browse the repository at this point in the history
  • Loading branch information
shatalov-boris committed Apr 10, 2024
1 parent 3f6a8c0 commit 29641b5
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
7 changes: 7 additions & 0 deletions dip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ interaction:
service: ruby
command: bundle exec appraisal

swaggerize:
description: Build swagger docs
service: ruby
environment:
RAILS_ENV: test
command: bundle exec rspec --format Rswag::Specs::SwaggerFormatter --order defined spec/requests/

rspec:
description: Run Rspec commands
service: ruby
Expand Down
1 change: 1 addition & 0 deletions sbmt-strangler.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rspec"
spec.add_development_dependency "rspec-rails"
spec.add_development_dependency "rspec_junit_formatter"
spec.add_development_dependency "rswag-specs"
spec.add_development_dependency "rubocop"
spec.add_development_dependency "rubocop-rails"
spec.add_development_dependency "rubocop-rspec"
Expand Down
31 changes: 31 additions & 0 deletions spec/internal/openapi/api.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
openapi: 3.0.1
info:
title: API V1
version: v1
servers:
- url: http://example.com
paths:
"/api/stores":
get:
summary: Получение магазинов
parameters:
- name: lat
in: query
required: true
schema:
type: number
- name: lon
in: query
required: true
schema:
type: number
responses:
'200':
description: Success
content:
application/json:
schema:
type: array
items:
type: string
8 changes: 8 additions & 0 deletions spec/internal/swagger/api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
openapi: 3.0.1
info:
title: Delivery Conditions API
version: v1
paths: {}
components:
schemas: {}
36 changes: 36 additions & 0 deletions spec/requests/api/stores_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require "swagger_helper"

describe Api::StoresController, swagger_doc: "api.yaml" do
path "/api/stores" do
get "Получение магазинов" do
consumes "application/json"
produces "application/json"

parameter name: :lat,
in: :query,
type: :number,
required: true

parameter name: :lon,
in: :query,
type: :number,
required: true

let(:lon) { 5 }
let(:lat) { 5 }

context "with success stf proxy mode", vcr: "api/stores_post_success" do # rubocop:disable RSpec/EmptyExampleGroup
response "200", "Success" do
schema({
type: "array",
items: {type: "string"}
})

run_test!
end
end
end
end
end
37 changes: 37 additions & 0 deletions spec/swagger_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require "rswag/specs"

RSpec.configure do |config|
# Specify a root folder where Swagger JSON files are generated
# NOTE: If you're using the rswag-api to serve API descriptions, you'll need
# to ensure that it's configured to serve Swagger from the same folder
config.openapi_root = Rails.root.join("openapi").to_s

# Define one or more Swagger documents and provide global metadata for each one
# When you run the 'rswag:specs:swaggerize' rake task, the complete Swagger will
# be generated at the provided relative path under swagger_root
# By default, the operations defined in spec files are added to the first
# document below. You can override this behavior by adding a swagger_doc tag to the
# the root example_group in your specs, e.g. describe '...', swagger_doc: 'v2.yaml'
config.openapi_specs = {
"api.yaml" => {
openapi: "3.0.1",
info: {
title: "API V1",
version: "v1"
},
servers: [
{
url: "http://example.com"
}
]
}
}

# Specify the format of the output Swagger file when running 'rswag:specs:swaggerize'.
# The swagger_docs configuration option has the filename including format in
# the key, this may want to be changed to avoid putting yaml in json files.
# Defaults to json. Accepts ':json' and ':yaml'.
config.openapi_format = :yaml
end

0 comments on commit 29641b5

Please sign in to comment.