Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Innovation: Initial appmap + rswag setup #367

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ gem 'puma', '~> 6'
gem 'rack-cors'
gem 'rails', '~> 7.1'
gem 'roo'
gem 'rswag-api'
gem 'rswag-ui'
gem 'scout_apm'
gem 'sentry-rails'

group :development, :test do
gem 'appmap'
gem 'bullet'
gem 'dotenv-rails'
gem 'factory_bot_rails'
Expand All @@ -46,6 +49,7 @@ group :development, :test do
gem 'rspec'
gem 'rspec_junit_formatter'
gem 'rspec-rails'
gem 'rswag-specs'
gem 'rubocop', require: false
gem 'rubocop-graphql', require: false
gem 'rubocop-rails', require: false
Expand Down
24 changes: 24 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ GEM
administrate-field-active_storage (1.0.1)
administrate (>= 0.2.2)
rails (>= 7.0)
appmap (1.1.0)
activesupport
method_source
rack
reverse_markdown
ast (2.4.2)
aws-eventstream (1.2.0)
aws-partitions (1.718.0)
Expand Down Expand Up @@ -218,6 +223,8 @@ GEM
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (2.6.3)
json-schema (4.3.0)
addressable (>= 2.8)
jwt (2.2.3)
kaminari (1.2.2)
activesupport (>= 4.1.0)
Expand Down Expand Up @@ -358,6 +365,8 @@ GEM
regexp_parser (2.7.0)
reline (0.5.9)
io-console (~> 0.5)
reverse_markdown (2.1.1)
nokogiri
rexml (3.3.0)
strscan
roo (2.10.1)
Expand Down Expand Up @@ -386,6 +395,17 @@ GEM
rspec-support (3.12.0)
rspec_junit_formatter (0.6.0)
rspec-core (>= 2, < 4, != 2.12.0)
rswag-api (2.13.0)
activesupport (>= 3.1, < 7.2)
railties (>= 3.1, < 7.2)
rswag-specs (2.13.0)
activesupport (>= 3.1, < 7.2)
json-schema (>= 2.2, < 5.0)
railties (>= 3.1, < 7.2)
rspec-core (>= 2.14)
rswag-ui (2.13.0)
actionpack (>= 3.1, < 7.2)
railties (>= 3.1, < 7.2)
rubocop (1.47.0)
json (~> 2.3)
parallel (~> 1.10)
Expand Down Expand Up @@ -503,6 +523,7 @@ PLATFORMS
DEPENDENCIES
administrate (~> 0.20.1)
administrate-field-active_storage
appmap
aws-sdk-s3
bootsnap
bullet
Expand Down Expand Up @@ -539,6 +560,9 @@ DEPENDENCIES
rspec
rspec-rails
rspec_junit_formatter
rswag-api
rswag-specs
rswag-ui
rubocop
rubocop-graphql
rubocop-rails
Expand Down
11 changes: 11 additions & 0 deletions appmap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
name: editor-api
language: ruby
appmap_dir: tmp/appmap
packages:
- path: app
- path: lib
exclude:
- controllers/admin
- dashboard
- graphql
14 changes: 14 additions & 0 deletions config/initializers/rswag_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Rswag::Api.configure do |c|

# Specify a root folder where Swagger JSON files are located
# This is used by the Swagger middleware to serve requests for API descriptions
# NOTE: If you're using rswag-specs to generate Swagger, you'll need to ensure
# that it's configured to generate files in the same folder
c.openapi_root = Rails.root.to_s + '/swagger'

# Inject a lambda function to alter the returned Swagger prior to serialization
# The function will have access to the rack env for the current request
# For example, you could leverage this to dynamically assign the "host" property
#
#c.swagger_filter = lambda { |swagger, env| swagger['host'] = env['HTTP_HOST'] }
end
16 changes: 16 additions & 0 deletions config/initializers/rswag_ui.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Rswag::Ui.configure do |c|

# List the Swagger endpoints that you want to be documented through the
# swagger-ui. The first parameter is the path (absolute or relative to the UI
# host) to the corresponding endpoint and the second is a title that will be
# displayed in the document selector.
# NOTE: If you're using rspec-api to expose Swagger files
# (under openapi_root) as JSON or YAML endpoints, then the list below should
# correspond to the relative paths for those endpoints.

c.swagger_endpoint '/api-docs/v1/swagger.yaml', 'API V1 Docs'

# Add Basic Auth in case your API is private
# c.basic_auth_enabled = true
# c.basic_auth_credentials 'username', 'password'
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# rubocop:disable Metrics/BlockLength
Rails.application.routes.draw do
mount Rswag::Api::Engine => '/api-docs'
mount Rswag::Ui::Engine => '/api-docs'
namespace :admin do
mount GoodJob::Engine => 'good_job'
resources :components
Expand Down
100 changes: 100 additions & 0 deletions spec/requests/api/projects_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
require 'swagger_helper'

RSpec.describe 'api/projects', type: :request do

path '/api/projects' do

get('list projects') do
response(200, 'successful') do

after do |example|
example.metadata[:response][:content] = {
'application/json' => {
example: JSON.parse(response.body, symbolize_names: true)
}
}
end
run_test!
end
end

post('create project') do
response(200, 'successful') do

after do |example|
example.metadata[:response][:content] = {
'application/json' => {
example: JSON.parse(response.body, symbolize_names: true)
}
}
end
run_test!
end
end
end

path '/api/projects/{id}' do
# You'll want to customize the parameter types...
parameter name: 'id', in: :path, type: :string, description: 'id'

get('show project') do
response(200, 'successful') do
let(:id) { '123' }

after do |example|
example.metadata[:response][:content] = {
'application/json' => {
example: JSON.parse(response.body, symbolize_names: true)
}
}
end
run_test!
end
end

patch('update project') do
response(200, 'successful') do
let(:id) { '123' }

after do |example|
example.metadata[:response][:content] = {
'application/json' => {
example: JSON.parse(response.body, symbolize_names: true)
}
}
end
run_test!
end
end

put('update project') do
response(200, 'successful') do
let(:id) { '123' }

after do |example|
example.metadata[:response][:content] = {
'application/json' => {
example: JSON.parse(response.body, symbolize_names: true)
}
}
end
run_test!
end
end

delete('delete project') do
response(200, 'successful') do
let(:id) { '123' }

after do |example|
example.metadata[:response][:content] = {
'application/json' => {
example: JSON.parse(response.body, symbolize_names: true)
}
}
end
run_test!
end
end
end
end
43 changes: 43 additions & 0 deletions spec/swagger_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

require 'rails_helper'

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('swagger').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 openapi_root
# By default, the operations defined in spec files are added to the first
# document below. You can override this behavior by adding a openapi_spec tag to the
# the root example_group in your specs, e.g. describe '...', openapi_spec: 'v2/swagger.json'
config.openapi_specs = {
'v1/swagger.yaml' => {
openapi: '3.0.1',
info: {
title: 'API V1',
version: 'v1'
},
paths: {},
servers: [
{
url: 'https://{defaultHost}',
variables: {
defaultHost: {
default: 'www.example.com'
}
}
}
]
}
}

# Specify the format of the output Swagger file when running 'rswag:specs:swaggerize'.
# The openapi_specs 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
Loading