-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
46 changed files
with
2,367 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/.bundle/ | ||
/.yardoc | ||
/_yardoc/ | ||
/coverage/ | ||
/doc/ | ||
/pkg/ | ||
/spec/reports/ | ||
/tmp/ | ||
.DS_Store | ||
|
||
# rspec failure tracking | ||
.rspec_status | ||
|
||
# rubocop cache of remote rules file | ||
.rubocop-https---dev-k-io-rubocop-* | ||
|
||
*.gem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--format documentation | ||
--color | ||
--require spec_helper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
inherit_from: | ||
- https://dev.k.io/rubocop/rubocop.yml | ||
|
||
AllCops: | ||
TargetRubyVersion: 2.7 | ||
Exclude: | ||
- Rakefile | ||
- "bin/**/*" | ||
|
||
# Always use double quotes | ||
Style/StringLiterals: | ||
EnforcedStyle: double_quotes | ||
|
||
Style/Documentation: | ||
Enabled: false | ||
|
||
Metrics/CyclomaticComplexity: | ||
Enabled: false | ||
|
||
Metrics/PerceivedComplexity: | ||
Enabled: false | ||
|
||
Metrics/AbcSize: | ||
Enabled: false | ||
|
||
Metrics/MethodLength: | ||
Max: 50 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3.2.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
source "https://rubygems.org" | ||
|
||
# Specify your gem's dependencies in apia-open_api.gemspec | ||
gemspec | ||
|
||
gem "rake", "~> 13.0" | ||
|
||
gem "rspec", "~> 3.0" | ||
|
||
gem "rubocop", "~> 1.21" | ||
|
||
gem "apia", "~> 3.5" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
apia-open_api (0.1.0) | ||
activesupport (>= 6) | ||
|
||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
activesupport (7.0.6) | ||
concurrent-ruby (~> 1.0, >= 1.0.2) | ||
i18n (>= 1.6, < 2) | ||
minitest (>= 5.1) | ||
tzinfo (~> 2.0) | ||
apia (3.5.0) | ||
json | ||
rack | ||
ast (2.4.2) | ||
concurrent-ruby (1.2.2) | ||
diff-lcs (1.5.0) | ||
i18n (1.14.1) | ||
concurrent-ruby (~> 1.0) | ||
json (2.6.3) | ||
minitest (5.20.0) | ||
parallel (1.23.0) | ||
parser (3.2.2.3) | ||
ast (~> 2.4.1) | ||
racc | ||
racc (1.7.1) | ||
rack (3.0.8) | ||
rainbow (3.1.1) | ||
rake (13.0.6) | ||
regexp_parser (2.8.1) | ||
rexml (3.2.6) | ||
rspec (3.12.0) | ||
rspec-core (~> 3.12.0) | ||
rspec-expectations (~> 3.12.0) | ||
rspec-mocks (~> 3.12.0) | ||
rspec-core (3.12.2) | ||
rspec-support (~> 3.12.0) | ||
rspec-expectations (3.12.3) | ||
diff-lcs (>= 1.2.0, < 2.0) | ||
rspec-support (~> 3.12.0) | ||
rspec-mocks (3.12.6) | ||
diff-lcs (>= 1.2.0, < 2.0) | ||
rspec-support (~> 3.12.0) | ||
rspec-support (3.12.1) | ||
rubocop (1.52.0) | ||
json (~> 2.3) | ||
parallel (~> 1.10) | ||
parser (>= 3.2.0.0) | ||
rainbow (>= 2.2.2, < 4.0) | ||
regexp_parser (>= 1.8, < 3.0) | ||
rexml (>= 3.2.5, < 4.0) | ||
rubocop-ast (>= 1.28.0, < 2.0) | ||
ruby-progressbar (~> 1.7) | ||
unicode-display_width (>= 2.4.0, < 3.0) | ||
rubocop-ast (1.29.0) | ||
parser (>= 3.2.1.0) | ||
ruby-progressbar (1.13.0) | ||
tzinfo (2.0.6) | ||
concurrent-ruby (~> 1.0) | ||
unicode-display_width (2.4.2) | ||
|
||
PLATFORMS | ||
arm64-darwin-22 | ||
|
||
DEPENDENCIES | ||
apia (~> 3.5) | ||
apia-open_api! | ||
rake (~> 13.0) | ||
rspec (~> 3.0) | ||
rubocop (~> 1.21) | ||
|
||
BUNDLED WITH | ||
2.4.19 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2023 Krystal Hosting Ltd | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,96 @@ | ||
# Apia OpenAPI Specification | ||
|
||
This gem can generate an [OpenAPI](https://www.openapis.org/) compatible schema from an API implemented using [Apia](https://github.com/krystal/apia). | ||
|
||
## Installation | ||
|
||
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. | ||
|
||
Install the gem and add to the application's Gemfile by executing: | ||
|
||
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG | ||
|
||
If bundler is not being used to manage dependencies, install the gem by executing: | ||
|
||
$ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG | ||
|
||
## Usage | ||
|
||
The schema can be mounted in much the same way as an [Apia API](https://github.com/krystal/apia) itself. | ||
|
||
For example, for a Ruby on Rails application: | ||
|
||
```ruby | ||
module MyApp | ||
class Application < Rails::Application | ||
|
||
config.middleware.use Apia::OpenApi::Rack, | ||
api_class: "CoreAPI::Base", | ||
schema_path: "/core/v1/schema/openapi.json", | ||
base_url: "http://katapult-api.localhost/core/v1" | ||
|
||
end | ||
end | ||
``` | ||
|
||
Where `CoreAPI::Base` is the name of the API class that inherits from `Apia::API`. | ||
|
||
## Generating a client library from the spec | ||
|
||
It's possible to generate a client library from the generated OpenAPI schema using [OpenAPI Generator](https://openapi-generator.tech/). | ||
|
||
For example we can generate a Ruby client with the following: | ||
|
||
```bash | ||
brew install openapi-generator | ||
openapi-generator generate -i openapi.json -g ruby -o openapi-client --additional-properties=gemName=myapp-openapi-client,moduleName=MyAppOpenAPIClient | ||
``` | ||
|
||
The generated client will be in the `openapi-client` directory and will contain a readme with instructions on how to use it. | ||
|
||
## Development | ||
|
||
After checking out the repo, run `bin/setup` to install dependencies. | ||
|
||
In `/examples` there is an example Apia API application that can be used to try out the gem. | ||
|
||
Run `rackup` from the root of `/examples` to start the [rack app](https://github.com/rack/rack) running the example API. | ||
To view the generated OpenAPI schema, visit: http://127.0.0.1:9292/core/v1/schema/openapi.json | ||
`/examples/config.ru` shows how to mount the schema endpoint. | ||
|
||
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. | ||
|
||
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. | ||
|
||
e.g with this docker-compose.yml file: | ||
|
||
```yml | ||
version: "3.3" | ||
services: | ||
swagger-editor: | ||
image: swaggerapi/swagger-editor:next-v5 | ||
container_name: "swagger-editor" | ||
ports: | ||
- "8081:80" | ||
``` | ||
Run `docker-compose up` and visit http://localhost:8081 to view the swagger editor. | ||
|
||
### Tests and Linting | ||
|
||
- `bin/rspec` | ||
- `bin/rubocop` | ||
|
||
You can also run `bin/console` for an interactive prompt that will allow you to experiment. | ||
|
||
## Releasing a new version | ||
|
||
TODO: Write instructions for releasing a new version of the gem. | ||
|
||
## Contributing | ||
|
||
Bug reports and pull requests are welcome on GitHub at https://github.com/krystal/apia-open_api. | ||
|
||
## License | ||
|
||
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bundler/gem_tasks" | ||
require "rspec/core/rake_task" | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
|
||
require "rubocop/rake_task" | ||
|
||
RuboCop::RakeTask.new | ||
|
||
task default: %i[spec rubocop] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "lib/apia/open_api/version" | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "apia-open_api" | ||
spec.version = Apia::OpenApi::VERSION | ||
spec.authors = ["Paul Sturgess"] | ||
|
||
spec.summary = "Apia OpenAPI spec generator" | ||
spec.homepage = "https://github.com/krystal/apia-openapi" | ||
spec.license = "MIT" | ||
spec.required_ruby_version = ">= 2.7.0" | ||
|
||
spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'" | ||
|
||
spec.metadata["homepage_uri"] = spec.homepage | ||
spec.metadata["source_code_uri"] = "https://github.com/krystal/apia-openapi" | ||
spec.metadata["changelog_uri"] = "https://github.com/krystal/apia-openapi/changelog.md" | ||
|
||
spec.metadata["rubygems_mfa_required"] = "true" | ||
|
||
# Specify which files should be added to the gem when it is released. | ||
# The `git ls-files -z` loads the files in the RubyGem that have been added into git. | ||
spec.files = Dir.chdir(__dir__) do | ||
`git ls-files -z`.split("\x0").reject do |f| | ||
(File.expand_path(f) == __FILE__) || | ||
f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile]) | ||
end | ||
end | ||
spec.bindir = "exe" | ||
spec.executables = spec.files.grep(/\Aexe\//) { |f| File.basename(f) } | ||
spec.require_paths = ["lib"] | ||
|
||
spec.add_dependency "activesupport", ">= 6" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
require "bundler/setup" | ||
require "apia/open_api" | ||
|
||
# You can add fixtures and/or initialization code here to make experimenting | ||
# with your gem easier. You can also use a different console, if you like. | ||
|
||
require "irb" | ||
IRB.start(__FILE__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
# | ||
# This file was generated by Bundler. | ||
# | ||
# The application 'rspec' is installed as part of a gem, and | ||
# this file is here to facilitate running it. | ||
# | ||
|
||
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) | ||
|
||
bundle_binstub = File.expand_path("bundle", __dir__) | ||
|
||
if File.file?(bundle_binstub) | ||
if File.read(bundle_binstub, 300).include?("This file was generated by Bundler") | ||
load(bundle_binstub) | ||
else | ||
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. | ||
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") | ||
end | ||
end | ||
|
||
require "rubygems" | ||
require "bundler/setup" | ||
|
||
load Gem.bin_path("rspec-core", "rspec") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
# | ||
# This file was generated by Bundler. | ||
# | ||
# The application 'rubocop' is installed as part of a gem, and | ||
# this file is here to facilitate running it. | ||
# | ||
|
||
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) | ||
|
||
bundle_binstub = File.expand_path("bundle", __dir__) | ||
|
||
if File.file?(bundle_binstub) | ||
if File.read(bundle_binstub, 300).include?("This file was generated by Bundler") | ||
load(bundle_binstub) | ||
else | ||
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. | ||
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") | ||
end | ||
end | ||
|
||
require "rubygems" | ||
require "bundler/setup" | ||
|
||
load Gem.bin_path("rubocop", "rubocop") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
IFS=$'\n\t' | ||
set -vx | ||
|
||
bundle install | ||
|
||
# Do any other automated setup that you need to do here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Apia Example | ||
|
||
This directory contains an example application which can be used to try out this gem. | ||
|
||
Refer to the main readme for more information on how to run it. |
Oops, something went wrong.