Skip to content

Commit 80da3b7

Browse files
author
Scott
committed
initial appmap + rswag setup
1 parent 41667ec commit 80da3b7

File tree

9 files changed

+1465
-0
lines changed

9 files changed

+1465
-0
lines changed

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ gem 'puma', '~> 6'
3333
gem 'rack-cors'
3434
gem 'rails', '~> 7.1'
3535
gem 'roo'
36+
gem 'rswag-api'
37+
gem 'rswag-ui'
3638
gem 'scout_apm'
3739
gem 'sentry-rails'
3840

3941
group :development, :test do
42+
gem 'appmap'
4043
gem 'bullet'
4144
gem 'dotenv-rails'
4245
gem 'factory_bot_rails'
@@ -46,6 +49,7 @@ group :development, :test do
4649
gem 'rspec'
4750
gem 'rspec_junit_formatter'
4851
gem 'rspec-rails'
52+
gem 'rswag-specs'
4953
gem 'rubocop', require: false
5054
gem 'rubocop-graphql', require: false
5155
gem 'rubocop-rails', require: false

Gemfile.lock

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ GEM
9898
administrate-field-active_storage (1.0.1)
9999
administrate (>= 0.2.2)
100100
rails (>= 7.0)
101+
appmap (1.1.0)
102+
activesupport
103+
method_source
104+
rack
105+
reverse_markdown
101106
ast (2.4.2)
102107
aws-eventstream (1.2.0)
103108
aws-partitions (1.718.0)
@@ -218,6 +223,8 @@ GEM
218223
railties (>= 4.2.0)
219224
thor (>= 0.14, < 2.0)
220225
json (2.6.3)
226+
json-schema (4.3.0)
227+
addressable (>= 2.8)
221228
jwt (2.2.3)
222229
kaminari (1.2.2)
223230
activesupport (>= 4.1.0)
@@ -358,6 +365,8 @@ GEM
358365
regexp_parser (2.7.0)
359366
reline (0.5.9)
360367
io-console (~> 0.5)
368+
reverse_markdown (2.1.1)
369+
nokogiri
361370
rexml (3.3.0)
362371
strscan
363372
roo (2.10.1)
@@ -386,6 +395,17 @@ GEM
386395
rspec-support (3.12.0)
387396
rspec_junit_formatter (0.6.0)
388397
rspec-core (>= 2, < 4, != 2.12.0)
398+
rswag-api (2.13.0)
399+
activesupport (>= 3.1, < 7.2)
400+
railties (>= 3.1, < 7.2)
401+
rswag-specs (2.13.0)
402+
activesupport (>= 3.1, < 7.2)
403+
json-schema (>= 2.2, < 5.0)
404+
railties (>= 3.1, < 7.2)
405+
rspec-core (>= 2.14)
406+
rswag-ui (2.13.0)
407+
actionpack (>= 3.1, < 7.2)
408+
railties (>= 3.1, < 7.2)
389409
rubocop (1.47.0)
390410
json (~> 2.3)
391411
parallel (~> 1.10)
@@ -503,6 +523,7 @@ PLATFORMS
503523
DEPENDENCIES
504524
administrate (~> 0.20.1)
505525
administrate-field-active_storage
526+
appmap
506527
aws-sdk-s3
507528
bootsnap
508529
bullet
@@ -539,6 +560,9 @@ DEPENDENCIES
539560
rspec
540561
rspec-rails
541562
rspec_junit_formatter
563+
rswag-api
564+
rswag-specs
565+
rswag-ui
542566
rubocop
543567
rubocop-graphql
544568
rubocop-rails

appmap.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
name: editor-api
3+
language: ruby
4+
appmap_dir: tmp/appmap
5+
packages:
6+
- path: app
7+
- path: lib
8+
exclude:
9+
- controllers/admin
10+
- dashboard
11+
- graphql

config/initializers/rswag_api.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Rswag::Api.configure do |c|
2+
3+
# Specify a root folder where Swagger JSON files are located
4+
# This is used by the Swagger middleware to serve requests for API descriptions
5+
# NOTE: If you're using rswag-specs to generate Swagger, you'll need to ensure
6+
# that it's configured to generate files in the same folder
7+
c.openapi_root = Rails.root.to_s + '/swagger'
8+
9+
# Inject a lambda function to alter the returned Swagger prior to serialization
10+
# The function will have access to the rack env for the current request
11+
# For example, you could leverage this to dynamically assign the "host" property
12+
#
13+
#c.swagger_filter = lambda { |swagger, env| swagger['host'] = env['HTTP_HOST'] }
14+
end

config/initializers/rswag_ui.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Rswag::Ui.configure do |c|
2+
3+
# List the Swagger endpoints that you want to be documented through the
4+
# swagger-ui. The first parameter is the path (absolute or relative to the UI
5+
# host) to the corresponding endpoint and the second is a title that will be
6+
# displayed in the document selector.
7+
# NOTE: If you're using rspec-api to expose Swagger files
8+
# (under openapi_root) as JSON or YAML endpoints, then the list below should
9+
# correspond to the relative paths for those endpoints.
10+
11+
c.swagger_endpoint '/api-docs/v1/swagger.yaml', 'API V1 Docs'
12+
13+
# Add Basic Auth in case your API is private
14+
# c.basic_auth_enabled = true
15+
# c.basic_auth_credentials 'username', 'password'
16+
end

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
# rubocop:disable Metrics/BlockLength
44
Rails.application.routes.draw do
5+
mount Rswag::Api::Engine => '/api-docs'
6+
mount Rswag::Ui::Engine => '/api-docs'
57
namespace :admin do
68
mount GoodJob::Engine => 'good_job'
79
resources :components

spec/requests/api/projects_spec.rb

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
require 'swagger_helper'
2+
3+
RSpec.describe 'api/projects', type: :request do
4+
5+
path '/api/projects' do
6+
7+
get('list projects') do
8+
response(200, 'successful') do
9+
10+
after do |example|
11+
example.metadata[:response][:content] = {
12+
'application/json' => {
13+
example: JSON.parse(response.body, symbolize_names: true)
14+
}
15+
}
16+
end
17+
run_test!
18+
end
19+
end
20+
21+
post('create project') do
22+
response(200, 'successful') do
23+
24+
after do |example|
25+
example.metadata[:response][:content] = {
26+
'application/json' => {
27+
example: JSON.parse(response.body, symbolize_names: true)
28+
}
29+
}
30+
end
31+
run_test!
32+
end
33+
end
34+
end
35+
36+
path '/api/projects/{id}' do
37+
# You'll want to customize the parameter types...
38+
parameter name: 'id', in: :path, type: :string, description: 'id'
39+
40+
get('show project') do
41+
response(200, 'successful') do
42+
let(:id) { '123' }
43+
44+
after do |example|
45+
example.metadata[:response][:content] = {
46+
'application/json' => {
47+
example: JSON.parse(response.body, symbolize_names: true)
48+
}
49+
}
50+
end
51+
run_test!
52+
end
53+
end
54+
55+
patch('update project') do
56+
response(200, 'successful') do
57+
let(:id) { '123' }
58+
59+
after do |example|
60+
example.metadata[:response][:content] = {
61+
'application/json' => {
62+
example: JSON.parse(response.body, symbolize_names: true)
63+
}
64+
}
65+
end
66+
run_test!
67+
end
68+
end
69+
70+
put('update project') do
71+
response(200, 'successful') do
72+
let(:id) { '123' }
73+
74+
after do |example|
75+
example.metadata[:response][:content] = {
76+
'application/json' => {
77+
example: JSON.parse(response.body, symbolize_names: true)
78+
}
79+
}
80+
end
81+
run_test!
82+
end
83+
end
84+
85+
delete('delete project') do
86+
response(200, 'successful') do
87+
let(:id) { '123' }
88+
89+
after do |example|
90+
example.metadata[:response][:content] = {
91+
'application/json' => {
92+
example: JSON.parse(response.body, symbolize_names: true)
93+
}
94+
}
95+
end
96+
run_test!
97+
end
98+
end
99+
end
100+
end

spec/swagger_helper.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.configure do |config|
6+
# Specify a root folder where Swagger JSON files are generated
7+
# NOTE: If you're using the rswag-api to serve API descriptions, you'll need
8+
# to ensure that it's configured to serve Swagger from the same folder
9+
config.openapi_root = Rails.root.join('swagger').to_s
10+
11+
# Define one or more Swagger documents and provide global metadata for each one
12+
# When you run the 'rswag:specs:swaggerize' rake task, the complete Swagger will
13+
# be generated at the provided relative path under openapi_root
14+
# By default, the operations defined in spec files are added to the first
15+
# document below. You can override this behavior by adding a openapi_spec tag to the
16+
# the root example_group in your specs, e.g. describe '...', openapi_spec: 'v2/swagger.json'
17+
config.openapi_specs = {
18+
'v1/swagger.yaml' => {
19+
openapi: '3.0.1',
20+
info: {
21+
title: 'API V1',
22+
version: 'v1'
23+
},
24+
paths: {},
25+
servers: [
26+
{
27+
url: 'https://{defaultHost}',
28+
variables: {
29+
defaultHost: {
30+
default: 'www.example.com'
31+
}
32+
}
33+
}
34+
]
35+
}
36+
}
37+
38+
# Specify the format of the output Swagger file when running 'rswag:specs:swaggerize'.
39+
# The openapi_specs configuration option has the filename including format in
40+
# the key, this may want to be changed to avoid putting yaml in json files.
41+
# Defaults to json. Accepts ':json' and ':yaml'.
42+
config.openapi_format = :yaml
43+
end

0 commit comments

Comments
 (0)