Skip to content

Commit 4815a20

Browse files
authored
Merge pull request #1 from krystal/init-gem
This gem allows us to generate an [OpenAPI schema](https://www.openapis.org/) of an [Apia API](https://github.com/krystal/apia). ## Why are we using v3.0.0 when the latest is v.3.1.0 ? The [OpenAPI generator](https://openapi-generator.tech) does not support 3.1.0 (at least for Ruby yet). So the specification is for version 3.0.0. Annoyingly in v3.0.0, having a request body against a DELETE is deemed to be an error. And this shows up in [swagger-editor](https://editor.swagger.io/). However, after [community pressure](OAI/OpenAPI-Specification#1801), this decision was reversed and in [version 3.1.0 DELETE requests are now allowed to have a request body](OAI/OpenAPI-Specification#1937). I have successfully used the Ruby client library to use a DELETE request with a v3.0.0 schema, so I don't think it's a big deal. We can bump to 3.1.0 when the tooling is ready. ## What is implemented? - All endpoints are described by the spec. - ArgumentSet lookups with multiple methods of supplying params are handled - All the various "non-standard" Apia data types are mapped to OpenAPI ones (e.g. decimal, unix) - If `include` is declared on a field for partial object properties, then the endpoint response will accurately reflect that - Array params for get requests work in the "rails way". e.g. `user_ids[]=1,user_ids[]=2` - [swagger-editor](https://editor.swagger.io/) works, so we can use the "try it out" feature (including bearer auth) - Routes that exclude themselves from the Apia schema are excluded from the OpenAPI output - Endpoints are converted into "nice" names so that the generated client code is more pleasant to use - Apia types (enums, objects, argument sets, polymorphs) are implemented as re-usable component schemas - The spec is good enough to generate [client libraries in various programming languages](https://github.com/krystal/katapult-openapi-clients) ## What isn't implemented? - Only the "happy path" response is included in the spec, we need to add error responses - There are places in the spec where we can explicitly mark things as "required" and this has not been implemented everywhere. - Perhaps we can improve how ArgumentSet lookups are declared – currently [swagger-editor](https://editor.swagger.io/) allows both params (e.g. id and permalink) to be sent in the request which triggers an error. - We can improve the accuracy of the [data types](https://swagger.io/docs/specification/data-models/data-types/#numbers) by declaring the `format`. This is not implemented. - There's one specification test that simply asserts against a static json file generated from the example app. Perhaps we could try actually validating it with something like https://github.com/kevindew/openapi3_parser - Might be nice to dynamically determine the API version - The example app needs expanding to ensure all code-paths are triggered in the generation of the schema ## Any other known issues? - We can't have deeply nested objects in GET request query params. This just isn't defined by the OpenAPI spec. [There's GitHub issue about it](OAI/OpenAPI-Specification#1706). I don't believe we can do much here and probably we don't need to. - File uploads are not implemented, but I don't think we have a need for that. - We do not try to be too 'clever' when the endpoint field uses include to customize the properties returned. e.g. `include: '*,target[id,name]'` in this case we could actually use a `$ref` for the object referenced by the `*`. But instead, if a field uses `include` we just declare an inline schema for that field for that endpoint. - The example API has been copied and expanded from the apia repo. Some of the additional arguments and ways the objects have been expanded is nonsense, but they're there just to ensure we execute all the code paths in the schema generation. Maybe we could come up with a better example API or perhaps we just don't worry about it.
2 parents bc73dc5 + 6c7fc33 commit 4815a20

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2367
-0
lines changed

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/.bundle/
2+
/.yardoc
3+
/_yardoc/
4+
/coverage/
5+
/doc/
6+
/pkg/
7+
/spec/reports/
8+
/tmp/
9+
.DS_Store
10+
11+
# rspec failure tracking
12+
.rspec_status
13+
14+
# rubocop cache of remote rules file
15+
.rubocop-https---dev-k-io-rubocop-*
16+
17+
*.gem

.rspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--format documentation
2+
--color
3+
--require spec_helper

.rubocop.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
inherit_from:
2+
- https://dev.k.io/rubocop/rubocop.yml
3+
4+
AllCops:
5+
TargetRubyVersion: 2.7
6+
Exclude:
7+
- Rakefile
8+
- "bin/**/*"
9+
10+
# Always use double quotes
11+
Style/StringLiterals:
12+
EnforcedStyle: double_quotes
13+
14+
Style/Documentation:
15+
Enabled: false
16+
17+
Metrics/CyclomaticComplexity:
18+
Enabled: false
19+
20+
Metrics/PerceivedComplexity:
21+
Enabled: false
22+
23+
Metrics/AbcSize:
24+
Enabled: false
25+
26+
Metrics/MethodLength:
27+
Max: 50

.ruby-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.2.2

Gemfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
source "https://rubygems.org"
4+
5+
# Specify your gem's dependencies in apia-open_api.gemspec
6+
gemspec
7+
8+
gem "rake", "~> 13.0"
9+
10+
gem "rspec", "~> 3.0"
11+
12+
gem "rubocop", "~> 1.21"
13+
14+
gem "apia", "~> 3.5"

Gemfile.lock

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
PATH
2+
remote: .
3+
specs:
4+
apia-open_api (0.1.0)
5+
activesupport (>= 6)
6+
7+
GEM
8+
remote: https://rubygems.org/
9+
specs:
10+
activesupport (7.0.6)
11+
concurrent-ruby (~> 1.0, >= 1.0.2)
12+
i18n (>= 1.6, < 2)
13+
minitest (>= 5.1)
14+
tzinfo (~> 2.0)
15+
apia (3.5.0)
16+
json
17+
rack
18+
ast (2.4.2)
19+
concurrent-ruby (1.2.2)
20+
diff-lcs (1.5.0)
21+
i18n (1.14.1)
22+
concurrent-ruby (~> 1.0)
23+
json (2.6.3)
24+
minitest (5.20.0)
25+
parallel (1.23.0)
26+
parser (3.2.2.3)
27+
ast (~> 2.4.1)
28+
racc
29+
racc (1.7.1)
30+
rack (3.0.8)
31+
rainbow (3.1.1)
32+
rake (13.0.6)
33+
regexp_parser (2.8.1)
34+
rexml (3.2.6)
35+
rspec (3.12.0)
36+
rspec-core (~> 3.12.0)
37+
rspec-expectations (~> 3.12.0)
38+
rspec-mocks (~> 3.12.0)
39+
rspec-core (3.12.2)
40+
rspec-support (~> 3.12.0)
41+
rspec-expectations (3.12.3)
42+
diff-lcs (>= 1.2.0, < 2.0)
43+
rspec-support (~> 3.12.0)
44+
rspec-mocks (3.12.6)
45+
diff-lcs (>= 1.2.0, < 2.0)
46+
rspec-support (~> 3.12.0)
47+
rspec-support (3.12.1)
48+
rubocop (1.52.0)
49+
json (~> 2.3)
50+
parallel (~> 1.10)
51+
parser (>= 3.2.0.0)
52+
rainbow (>= 2.2.2, < 4.0)
53+
regexp_parser (>= 1.8, < 3.0)
54+
rexml (>= 3.2.5, < 4.0)
55+
rubocop-ast (>= 1.28.0, < 2.0)
56+
ruby-progressbar (~> 1.7)
57+
unicode-display_width (>= 2.4.0, < 3.0)
58+
rubocop-ast (1.29.0)
59+
parser (>= 3.2.1.0)
60+
ruby-progressbar (1.13.0)
61+
tzinfo (2.0.6)
62+
concurrent-ruby (~> 1.0)
63+
unicode-display_width (2.4.2)
64+
65+
PLATFORMS
66+
arm64-darwin-22
67+
68+
DEPENDENCIES
69+
apia (~> 3.5)
70+
apia-open_api!
71+
rake (~> 13.0)
72+
rspec (~> 3.0)
73+
rubocop (~> 1.21)
74+
75+
BUNDLED WITH
76+
2.4.19

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2023 Krystal Hosting Ltd
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,96 @@
11
# Apia OpenAPI Specification
22

33
This gem can generate an [OpenAPI](https://www.openapis.org/) compatible schema from an API implemented using [Apia](https://github.com/krystal/apia).
4+
5+
## Installation
6+
7+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
8+
9+
Install the gem and add to the application's Gemfile by executing:
10+
11+
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
12+
13+
If bundler is not being used to manage dependencies, install the gem by executing:
14+
15+
$ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
16+
17+
## Usage
18+
19+
The schema can be mounted in much the same way as an [Apia API](https://github.com/krystal/apia) itself.
20+
21+
For example, for a Ruby on Rails application:
22+
23+
```ruby
24+
module MyApp
25+
class Application < Rails::Application
26+
27+
config.middleware.use Apia::OpenApi::Rack,
28+
api_class: "CoreAPI::Base",
29+
schema_path: "/core/v1/schema/openapi.json",
30+
base_url: "http://katapult-api.localhost/core/v1"
31+
32+
end
33+
end
34+
```
35+
36+
Where `CoreAPI::Base` is the name of the API class that inherits from `Apia::API`.
37+
38+
## Generating a client library from the spec
39+
40+
It's possible to generate a client library from the generated OpenAPI schema using [OpenAPI Generator](https://openapi-generator.tech/).
41+
42+
For example we can generate a Ruby client with the following:
43+
44+
```bash
45+
brew install openapi-generator
46+
openapi-generator generate -i openapi.json -g ruby -o openapi-client --additional-properties=gemName=myapp-openapi-client,moduleName=MyAppOpenAPIClient
47+
```
48+
49+
The generated client will be in the `openapi-client` directory and will contain a readme with instructions on how to use it.
50+
51+
## Development
52+
53+
After checking out the repo, run `bin/setup` to install dependencies.
54+
55+
In `/examples` there is an example Apia API application that can be used to try out the gem.
56+
57+
Run `rackup` from the root of `/examples` to start the [rack app](https://github.com/rack/rack) running the example API.
58+
To view the generated OpenAPI schema, visit: http://127.0.0.1:9292/core/v1/schema/openapi.json
59+
`/examples/config.ru` shows how to mount the schema endpoint.
60+
61+
The generated schema can be viewed, validated and tried out using the online [Swagger Editor](https://editor.swagger.io/). You'll need to add the bearer token to the swagger editor to authenticate the requests. After that, they should work as expected. The bearer token is defined in main_authenticator.rb.
62+
63+
Currently the online swagger-editor only allows the OpenAPI schema v3.0.0. But it's also possible to run the swagger-editor locally, which allows us to check against v3.1.0.
64+
65+
e.g with this docker-compose.yml file:
66+
67+
```yml
68+
version: "3.3"
69+
services:
70+
swagger-editor:
71+
image: swaggerapi/swagger-editor:next-v5
72+
container_name: "swagger-editor"
73+
ports:
74+
- "8081:80"
75+
```
76+
77+
Run `docker-compose up` and visit http://localhost:8081 to view the swagger editor.
78+
79+
### Tests and Linting
80+
81+
- `bin/rspec`
82+
- `bin/rubocop`
83+
84+
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
85+
86+
## Releasing a new version
87+
88+
TODO: Write instructions for releasing a new version of the gem.
89+
90+
## Contributing
91+
92+
Bug reports and pull requests are welcome on GitHub at https://github.com/krystal/apia-open_api.
93+
94+
## License
95+
96+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

Rakefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require "bundler/gem_tasks"
4+
require "rspec/core/rake_task"
5+
6+
RSpec::Core::RakeTask.new(:spec)
7+
8+
require "rubocop/rake_task"
9+
10+
RuboCop::RakeTask.new
11+
12+
task default: %i[spec rubocop]

apia-open_api.gemspec

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "lib/apia/open_api/version"
4+
5+
Gem::Specification.new do |spec|
6+
spec.name = "apia-open_api"
7+
spec.version = Apia::OpenApi::VERSION
8+
spec.authors = ["Paul Sturgess"]
9+
10+
spec.summary = "Apia OpenAPI spec generator"
11+
spec.homepage = "https://github.com/krystal/apia-openapi"
12+
spec.license = "MIT"
13+
spec.required_ruby_version = ">= 2.7.0"
14+
15+
spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
16+
17+
spec.metadata["homepage_uri"] = spec.homepage
18+
spec.metadata["source_code_uri"] = "https://github.com/krystal/apia-openapi"
19+
spec.metadata["changelog_uri"] = "https://github.com/krystal/apia-openapi/changelog.md"
20+
21+
spec.metadata["rubygems_mfa_required"] = "true"
22+
23+
# Specify which files should be added to the gem when it is released.
24+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25+
spec.files = Dir.chdir(__dir__) do
26+
`git ls-files -z`.split("\x0").reject do |f|
27+
(File.expand_path(f) == __FILE__) ||
28+
f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
29+
end
30+
end
31+
spec.bindir = "exe"
32+
spec.executables = spec.files.grep(/\Aexe\//) { |f| File.basename(f) }
33+
spec.require_paths = ["lib"]
34+
35+
spec.add_dependency "activesupport", ">= 6"
36+
end

bin/console

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require "bundler/setup"
5+
require "apia/open_api"
6+
7+
# You can add fixtures and/or initialization code here to make experimenting
8+
# with your gem easier. You can also use a different console, if you like.
9+
10+
require "irb"
11+
IRB.start(__FILE__)

bin/rspec

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
#
5+
# This file was generated by Bundler.
6+
#
7+
# The application 'rspec' is installed as part of a gem, and
8+
# this file is here to facilitate running it.
9+
#
10+
11+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
12+
13+
bundle_binstub = File.expand_path("bundle", __dir__)
14+
15+
if File.file?(bundle_binstub)
16+
if File.read(bundle_binstub, 300).include?("This file was generated by Bundler")
17+
load(bundle_binstub)
18+
else
19+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
20+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
21+
end
22+
end
23+
24+
require "rubygems"
25+
require "bundler/setup"
26+
27+
load Gem.bin_path("rspec-core", "rspec")

bin/rubocop

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
#
5+
# This file was generated by Bundler.
6+
#
7+
# The application 'rubocop' is installed as part of a gem, and
8+
# this file is here to facilitate running it.
9+
#
10+
11+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
12+
13+
bundle_binstub = File.expand_path("bundle", __dir__)
14+
15+
if File.file?(bundle_binstub)
16+
if File.read(bundle_binstub, 300).include?("This file was generated by Bundler")
17+
load(bundle_binstub)
18+
else
19+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
20+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
21+
end
22+
end
23+
24+
require "rubygems"
25+
require "bundler/setup"
26+
27+
load Gem.bin_path("rubocop", "rubocop")

bin/setup

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
IFS=$'\n\t'
4+
set -vx
5+
6+
bundle install
7+
8+
# Do any other automated setup that you need to do here

examples/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Apia Example
2+
3+
This directory contains an example application which can be used to try out this gem.
4+
5+
Refer to the main readme for more information on how to run it.

0 commit comments

Comments
 (0)