oas_rails 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +90 -60
- data/app/controllers/oas_rails/oas_rails_controller.rb +2 -0
- data/app/views/layouts/oas_rails/application.html.erb +0 -16
- data/app/views/oas_rails/oas_rails/index.html.erb +27 -1
- data/lib/oas_rails/engine.rb +5 -0
- data/lib/oas_rails/oas_route.rb +1 -1
- data/lib/oas_rails/route_extractor.rb +8 -0
- data/lib/oas_rails/specification.rb +12 -2
- data/lib/oas_rails/version.rb +1 -1
- data/lib/oas_rails/yard/oas_yard_factory.rb +2 -2
- data/lib/oas_rails.rb +6 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4c6823d6b64ffb6edbeabed87d2985f006433d6c8243d6e42bf21a6c96fab1e1
|
4
|
+
data.tar.gz: 85e8a04aa51388ce0ba2f0e27da7c81176ecc1e62630e301147ac0d1bd57fbac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cfd37bb135fcbe966a2ad9972f5339f9c3c196cc10234a65570e023d8ae4386f4d55bac813f51c3da6cb5f4d2f29c49c8294f29eb391d405fa437fe5c5b34375
|
7
|
+
data.tar.gz: 49e84dddf1baeb860ae753757a72c5a9989e4c1fd49dcc9b9beb79ea52323a330eb054236e3329756192e68fd43c2cc1ec9f0ea6f269082c0191d4ca184b5596
|
data/README.md
CHANGED
@@ -30,6 +30,24 @@ After experiencing the interactive documentation in Python's fast-api framework,
|
|
30
30
|
|
31
31
|
The goal is to minimize the effort required to create comprehensive documentation. By following REST principles in Rails, we believe this is achievable. You can enhance the documentation using [Yard](https://yardoc.org/) tags.
|
32
32
|
|
33
|
+
## Table of Contents
|
34
|
+
|
35
|
+
- [Installation](#installation)
|
36
|
+
- [Configuration](#configuration)
|
37
|
+
- [Basic Information about the API](#basic-information-about-the-api)
|
38
|
+
- [Servers Information](#servers-information)
|
39
|
+
- [Tag Information](#tag-information)
|
40
|
+
- [Optional Settings](#optional-settings)
|
41
|
+
- [Authentication Settings](#authentication-settings)
|
42
|
+
- [Usage](#usage)
|
43
|
+
- [Documenting Your Endpoints](#documenting-your-endpoints)
|
44
|
+
- [Example](#example-of-documented-endpoints)
|
45
|
+
- [Securing the OasRails Engine](#securing-the-oasrails-engine)
|
46
|
+
- [Customizing the View](#customizing-the-view)
|
47
|
+
- [Contributing](#contributing)
|
48
|
+
- [Roadmap and Ideas for Improvement](#roadmap-and-ideas-for-improvement)
|
49
|
+
- [License](#license)
|
50
|
+
|
33
51
|
## Installation
|
34
52
|
|
35
53
|
1. Add this line to your Rails application's Gemfile:
|
@@ -52,23 +70,15 @@ The goal is to minimize the effort required to create comprehensive documentatio
|
|
52
70
|
|
53
71
|
You'll now have **basic documentation** based on your routes and automatically gathered information at `localhost:3000/docs`. To enhance it, create an initializer file and add [Yard](https://yardoc.org/) tags to your controller methods.
|
54
72
|
|
55
|
-
##
|
56
|
-
|
57
|
-
### Initializer File
|
73
|
+
## Configuration
|
58
74
|
|
59
|
-
|
75
|
+
To configure OasRails, you MUST create an initializer file including all your settings. The first step is to create your initializer file, which you can easily do with:
|
60
76
|
|
61
77
|
```bash
|
62
78
|
rails generate oas_rails:config
|
63
79
|
```
|
64
80
|
|
65
|
-
Then
|
66
|
-
|
67
|
-
**Almost every description in a OAS file support simple markdown**
|
68
|
-
|
69
|
-
## Configuration
|
70
|
-
|
71
|
-
To configure OasRails, edit the `config/initializers/oas_rails.rb` file. Below are the available configuration options:
|
81
|
+
Then fill it with your data. Below are the available configuration options:
|
72
82
|
|
73
83
|
### Basic Information about the API
|
74
84
|
|
@@ -100,56 +110,11 @@ To configure OasRails, edit the `config/initializers/oas_rails.rb` file. Below a
|
|
100
110
|
- `config.security_schema`: The default security schema used for authentication. Choose a predefined security schema from `[:api_key_cookie, :api_key_header, :api_key_query, :basic, :bearer, :bearer_jwt, :mutual_tls]`.
|
101
111
|
- `config.security_schemas`: Custom security schemas. Follow the [OpenAPI Specification](https://spec.openapis.org/oas/latest.html#security-scheme-object) for defining these schemas.
|
102
112
|
|
103
|
-
##
|
104
|
-
|
105
|
-
To secure the OasRails engine, which exposes an endpoint for showing the OAS definition, you can configure authentication to ensure that only authorized users have access. Here are a few methods to achieve this:
|
106
|
-
|
107
|
-
### 1. Using Basic Authentication
|
108
|
-
|
109
|
-
Use basic authentication to protect the OasRails endpoint. You can set this up in an initializer:
|
110
|
-
|
111
|
-
```ruby
|
112
|
-
# config/initializers/oas_rails.rb
|
113
|
-
OasRails::Engine.middleware.use(Rack::Auth::Basic) do |username, password|
|
114
|
-
ActiveSupport::SecurityUtils.secure_compare(Rails.application.credentials.oas_rails_username, username) &
|
115
|
-
ActiveSupport::SecurityUtils.secure_compare(Rails.application.credentials.oas_rails_password, password)
|
116
|
-
end
|
117
|
-
```
|
118
|
-
|
119
|
-
### 2. Using Devise's `authenticate` Helper
|
120
|
-
|
121
|
-
You can use Devise's `authenticate` helper to restrict access to the OasRails endpoint. For example, you can allow only admin users to access the endpoint:
|
122
|
-
|
123
|
-
```ruby
|
124
|
-
# config/routes.rb
|
125
|
-
# ...
|
126
|
-
authenticate :user, ->(user) { user.admin? } do
|
127
|
-
mount OasRails::Engine, at: '/docs'
|
128
|
-
end
|
129
|
-
```
|
130
|
-
|
131
|
-
### 3. Custom Authentication
|
132
|
-
|
133
|
-
To support custom authentication, you can extend the OasRails' ApplicationController using a hook. This allows you to add custom before actions to check for specific user permissions:
|
134
|
-
|
135
|
-
```ruby
|
136
|
-
# config/initializers/oas_rails.rb
|
137
|
-
|
138
|
-
ActiveSupport.on_load(:oas_rails_application_controller) do
|
139
|
-
# context here is OasRails::ApplicationController
|
140
|
-
|
141
|
-
before_action do
|
142
|
-
raise ActionController::RoutingError.new('Not Found') unless current_user&.admin?
|
143
|
-
end
|
113
|
+
## Usage
|
144
114
|
|
145
|
-
|
146
|
-
# Load the current user
|
147
|
-
User.find(session[:user_id]) # Adjust according to your authentication logic
|
148
|
-
end
|
149
|
-
end
|
150
|
-
```
|
115
|
+
In addition to the information provided in the initializer file and the data that can be extracted from the routes and methods automatically, it is essential to document your API in the following way. The documentation is created **with the help of YARD**, so the methods are documented with **comment tags**.
|
151
116
|
|
152
|
-
|
117
|
+
### Documenting Your Endpoints
|
153
118
|
|
154
119
|
Almost every description in an OAS file supports simple markdown. The following tags are available for documenting your endpoints:
|
155
120
|
|
@@ -331,9 +296,74 @@ class UsersController < ApplicationController
|
|
331
296
|
end
|
332
297
|
```
|
333
298
|
|
299
|
+
## Securing the OasRails Engine
|
300
|
+
|
301
|
+
To secure the OasRails engine, which exposes an endpoint for showing the OAS definition, you can configure authentication to ensure that only authorized users have access. Here are a few methods to achieve this:
|
302
|
+
|
303
|
+
### 1. Using Basic Authentication
|
304
|
+
|
305
|
+
Use basic authentication to protect the OasRails endpoint. You can set this up in an initializer:
|
306
|
+
|
307
|
+
```ruby
|
308
|
+
# config/initializers/oas_rails.rb
|
309
|
+
OasRails::Engine.middleware.use(Rack::Auth::Basic) do |username, password|
|
310
|
+
ActiveSupport::SecurityUtils.secure_compare(Rails.application.credentials.oas_rails_username, username) &
|
311
|
+
ActiveSupport::SecurityUtils.secure_compare(Rails.application.credentials.oas_rails_password, password)
|
312
|
+
end
|
313
|
+
```
|
314
|
+
|
315
|
+
### 2. Using Devise's `authenticate` Helper
|
316
|
+
|
317
|
+
You can use Devise's `authenticate` helper to restrict access to the OasRails endpoint. For example, you can allow only admin users to access the endpoint:
|
318
|
+
|
319
|
+
```ruby
|
320
|
+
# config/routes.rb
|
321
|
+
# ...
|
322
|
+
authenticate :user, ->(user) { user.admin? } do
|
323
|
+
mount OasRails::Engine, at: '/docs'
|
324
|
+
end
|
325
|
+
```
|
326
|
+
|
327
|
+
### 3. Custom Authentication
|
328
|
+
|
329
|
+
To support custom authentication, you can extend the OasRails' ApplicationController using a hook. This allows you to add custom before actions to check for specific user permissions:
|
330
|
+
|
331
|
+
```ruby
|
332
|
+
# config/initializers/oas_rails.rb
|
333
|
+
|
334
|
+
ActiveSupport.on_load(:oas_rails_application_controller) do
|
335
|
+
# context here is OasRails::ApplicationController
|
336
|
+
|
337
|
+
before_action do
|
338
|
+
raise ActionController::RoutingError.new('Not Found') unless current_user&.admin?
|
339
|
+
end
|
340
|
+
|
341
|
+
def current_user
|
342
|
+
# Load the current user
|
343
|
+
User.find(session[:user_id]) # Adjust according to your authentication logic
|
344
|
+
end
|
345
|
+
end
|
346
|
+
```
|
347
|
+
|
348
|
+
## Customizing the View
|
349
|
+
|
350
|
+
The OasRails engine provides an easy way to display your OpenAPI Specification (OAS) within your Rails application. By default, it includes an `index` view in the `OasRailsController` that displays [RapiDoc](https://rapidocweb.com/) through a CDN with default configurations. You can easily override this view to replace RapiDoc entirely or configure it differently.
|
351
|
+
|
352
|
+
#### Overriding the `index` View
|
353
|
+
|
354
|
+
To override the `index` view provided by the OasRails engine, follow these steps:
|
355
|
+
|
356
|
+
1. **Create the Override View File**: In your host application, create a new file at the path `app/views/oas_rails/oas_rails/index.html.erb`. If the directories do not exist, you will need to create them.
|
357
|
+
|
358
|
+
2. **Customize the View**: Open the newly created `index.html.erb` file and add your custom HTML and ERB code to display the OAS as desired. You can refer to the source code of this project for guidance.
|
359
|
+
|
360
|
+
#### Using the Custom View
|
361
|
+
|
362
|
+
Once the custom view file is in place, Rails will automatically use it instead of the view provided by the OasRails engine. This allows you to fully customize the presentation of the OAS without modifying the engine's code.
|
363
|
+
|
334
364
|
## Contributing
|
335
365
|
|
336
|
-
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**. If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star
|
366
|
+
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**. If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star⭐! Thanks again!
|
337
367
|
|
338
368
|
1. Fork the Project
|
339
369
|
2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)
|
@@ -1,16 +0,0 @@
|
|
1
|
-
<!DOCTYPE html>
|
2
|
-
<html>
|
3
|
-
<head>
|
4
|
-
<title>Oas rails</title>
|
5
|
-
<%= csrf_meta_tags %>
|
6
|
-
<%= csp_meta_tag %>
|
7
|
-
<meta charset="utf-8">
|
8
|
-
<!-- Important: rapi-doc uses utf8 characters -->
|
9
|
-
<script type="module" src="https://unpkg.com/rapidoc/dist/rapidoc-min.js"></script>
|
10
|
-
</head>
|
11
|
-
<body>
|
12
|
-
|
13
|
-
<%= yield %>
|
14
|
-
|
15
|
-
</body>
|
16
|
-
</html>
|
@@ -1 +1,27 @@
|
|
1
|
-
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Oas rails</title>
|
5
|
+
<%= csrf_meta_tags %>
|
6
|
+
<%= csp_meta_tag %>
|
7
|
+
<meta charset="utf-8">
|
8
|
+
<!-- Important: rapi-doc uses utf8 characters -->
|
9
|
+
<script type="module" src="https://unpkg.com/rapidoc/dist/rapidoc-min.js"></script>
|
10
|
+
</head>
|
11
|
+
<body>
|
12
|
+
<rapi-doc
|
13
|
+
spec-url = "<%= main_app.oas_rails_path %>.json"
|
14
|
+
theme = "dark"
|
15
|
+
bg-color="#0F172A"
|
16
|
+
text-color= "#f7f7f7"
|
17
|
+
show-header = 'false'
|
18
|
+
primary-color = "#2de410"
|
19
|
+
font-size="largest"
|
20
|
+
show-method-in-nav-bar="as-colored-text"
|
21
|
+
nav-text-color="#f7f7f7"
|
22
|
+
nav-item-spacing="relaxed"
|
23
|
+
allow-spec-file-download="true"
|
24
|
+
>
|
25
|
+
</rapi-doc>
|
26
|
+
</body>
|
27
|
+
</html>
|
data/lib/oas_rails/engine.rb
CHANGED
data/lib/oas_rails/oas_route.rb
CHANGED
@@ -28,6 +28,14 @@ module OasRails
|
|
28
28
|
@host_routes ||= extract_host_routes
|
29
29
|
end
|
30
30
|
|
31
|
+
# Clear Class Instance Variable @host_routes
|
32
|
+
#
|
33
|
+
# This method clear the class instance variable @host_routes
|
34
|
+
# to force a extraction of the routes again.
|
35
|
+
def clear_cache
|
36
|
+
@host_routes = nil
|
37
|
+
end
|
38
|
+
|
31
39
|
def host_paths
|
32
40
|
@host_paths ||= host_routes.map(&:path).uniq.sort
|
33
41
|
end
|
@@ -2,12 +2,22 @@ require 'json'
|
|
2
2
|
|
3
3
|
module OasRails
|
4
4
|
class Specification
|
5
|
+
# Initializes a new Specification object.
|
6
|
+
# Clears the cache if running in the development environment.
|
5
7
|
def initialize
|
6
|
-
|
7
|
-
|
8
|
+
clear_cache unless Rails.env.production?
|
9
|
+
|
8
10
|
@specification = base_spec
|
9
11
|
end
|
10
12
|
|
13
|
+
# Clears the cache for MethodSource and RouteExtractor.
|
14
|
+
#
|
15
|
+
# @return [void]
|
16
|
+
def clear_cache
|
17
|
+
MethodSource.clear_cache
|
18
|
+
RouteExtractor.clear_cache
|
19
|
+
end
|
20
|
+
|
11
21
|
def to_json(*_args)
|
12
22
|
@specification.to_json
|
13
23
|
rescue StandardError => e
|
data/lib/oas_rails/version.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module OasRails
|
2
|
-
module
|
2
|
+
module YARD
|
3
3
|
class RequestBodyTag < ::YARD::Tags::Tag
|
4
4
|
attr_accessor :klass, :schema, :required
|
5
5
|
|
@@ -41,7 +41,7 @@ module OasRails
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
-
class
|
44
|
+
class OasYARDFactory < ::YARD::Tags::DefaultFactory
|
45
45
|
## parse_tag is a prefix used by YARD
|
46
46
|
|
47
47
|
def parse_tag_with_request_body(tag_name, text)
|
data/lib/oas_rails.rb
CHANGED
@@ -27,12 +27,15 @@ module OasRails
|
|
27
27
|
|
28
28
|
autoload :Utils, "oas_rails/utils"
|
29
29
|
|
30
|
-
module
|
31
|
-
autoload :
|
30
|
+
module YARD
|
31
|
+
autoload :OasYARDFactory, 'oas_rails/yard/oas_yard_factory'
|
32
32
|
end
|
33
33
|
|
34
34
|
class << self
|
35
|
+
# Configurations for make the OasRails engine Work.
|
35
36
|
def configure
|
37
|
+
OasRails.configure_esquema!
|
38
|
+
OasRails.configure_yard!
|
36
39
|
yield config
|
37
40
|
end
|
38
41
|
|
@@ -41,7 +44,7 @@ module OasRails
|
|
41
44
|
end
|
42
45
|
|
43
46
|
def configure_yard!
|
44
|
-
::YARD::Tags::Library.default_factory =
|
47
|
+
::YARD::Tags::Library.default_factory = YARD::OasYARDFactory
|
45
48
|
yard_tags = {
|
46
49
|
'Request body' => [:request_body, :with_request_body],
|
47
50
|
'Request body Example' => [:request_body_example, :with_request_body_example],
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: oas_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- a-chacon
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-08-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: esquema
|