swagger_docs_rails 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/MIT-LICENSE +1 -1
- data/README.md +27 -0
- data/app/controllers/swagger_docs_rails/swagger_controller.rb +1 -6
- data/lib/generators/swagger_docs_rails/install/templates/swagger_docs_rails.rb +24 -0
- data/lib/swagger_docs_rails/configuration.rb +35 -1
- data/lib/swagger_docs_rails/openapi_builder.rb +26 -7
- data/lib/swagger_docs_rails/security_scheme_builder.rb +62 -0
- data/lib/swagger_docs_rails/version.rb +1 -1
- data/lib/swagger_docs_rails.rb +1 -0
- data/lib/tasks/swagger_docs_rails.rake +2 -2
- metadata +4 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ed2391636fb27b506bd380cfa91da5914c2ec8080316824ac8c7bf8f90c49df1
|
|
4
|
+
data.tar.gz: 25acbaf01c6a26adb15329144491a3deae5c4d8ff867ef62176ccdc18168db5a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a01043004ed6d611756e6e28d3f68dc33a23d67bba4287aa7fb5156ebb162ad460f1bd426d962c4e1cb66f7fb87ad4a3ada7e2e9451b6dbcaf2f03164628141e
|
|
7
|
+
data.tar.gz: 473408951994dc9cf74ae75345eed034c6f85de8978084066ef699ebf3e2d8a76d1f5ff67a9fb5be7508a59554e24488f08a931b688a235f5537d0bd59db99e0
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
- Esquemas de segurança configuráveis (`security_schemes` e `global_security`).
|
|
6
|
+
- Suporte a autenticação Bearer JWT e API Key (header ou query) no OpenAPI e Swagger UI.
|
|
7
|
+
- Template de install e README atualizados com exemplos de `Authorize`.
|
|
8
|
+
|
|
9
|
+
## 0.1.2
|
|
10
|
+
|
|
11
|
+
- Atualiza os metadados de autoria da gem para Ordex Sistemas.
|
|
12
|
+
|
|
3
13
|
## 0.1.1
|
|
4
14
|
|
|
5
15
|
- Atualização de metadados para nova publicação da gem.
|
data/MIT-LICENSE
CHANGED
data/README.md
CHANGED
|
@@ -47,9 +47,36 @@ SwaggerDocsRails.configure do |config|
|
|
|
47
47
|
config.password = ENV.fetch("SWAGGER_PASSWORD", "")
|
|
48
48
|
config.additional_controller_paths = []
|
|
49
49
|
config.output_path = Rails.root.join("public", "swagger", "openapi.json")
|
|
50
|
+
|
|
51
|
+
# Autenticação no Swagger UI (botão Authorize)
|
|
52
|
+
config.security_schemes = [
|
|
53
|
+
{ name: "BearerAuth", type: :http, scheme: "bearer", bearer_format: "JWT",
|
|
54
|
+
description: "Token JWT" },
|
|
55
|
+
{ name: "ApiKeyAuth", type: :api_key, in: "header", param_name: "X-Api-Key",
|
|
56
|
+
description: "Chave interna da API" }
|
|
57
|
+
]
|
|
58
|
+
config.global_security = [{ BearerAuth: [] }, { ApiKeyAuth: [] }]
|
|
50
59
|
end
|
|
51
60
|
```
|
|
52
61
|
|
|
62
|
+
### Autenticação (Bearer, API Key)
|
|
63
|
+
|
|
64
|
+
A gem gera `components.securitySchemes` e `security` global a partir da configuração.
|
|
65
|
+
No Swagger UI, use **Authorize** para informar JWT ou API Key; `persistAuthorization` mantém os valores na sessão.
|
|
66
|
+
|
|
67
|
+
Tipos suportados em `security_schemes`:
|
|
68
|
+
|
|
69
|
+
| `type` | Campos principais |
|
|
70
|
+
|-----------|---------------------------------------------------------|
|
|
71
|
+
| `:http` | `scheme`, `bearer_format`, `description` |
|
|
72
|
+
| `:api_key`| `param_name` (ou `header_name`), `in` (`header`/`query`) |
|
|
73
|
+
|
|
74
|
+
`global_security` define quais esquemas exigir. Vários hashes em sequência = **OR** (Bearer **ou** ApiKey):
|
|
75
|
+
|
|
76
|
+
```ruby
|
|
77
|
+
config.global_security = [{ BearerAuth: [] }, { ApiKeyAuth: [] }]
|
|
78
|
+
```
|
|
79
|
+
|
|
53
80
|
## Uso
|
|
54
81
|
|
|
55
82
|
Gere o arquivo OpenAPI:
|
|
@@ -62,12 +62,7 @@ module SwaggerDocsRails
|
|
|
62
62
|
|
|
63
63
|
def build_doc(server_url)
|
|
64
64
|
if configuracao.live_reload || self.class.cached_doc.nil?
|
|
65
|
-
config =
|
|
66
|
-
title: configuracao.title,
|
|
67
|
-
version: configuracao.version,
|
|
68
|
-
description: configuracao.description,
|
|
69
|
-
server_url: server_url
|
|
70
|
-
}
|
|
65
|
+
config = configuracao.to_generator_config.merge(server_url: server_url)
|
|
71
66
|
|
|
72
67
|
document = SwaggerDocsRails::Generator.new(config: config).generate
|
|
73
68
|
self.class.cached_doc = JSON.generate(document)
|
|
@@ -11,4 +11,28 @@ SwaggerDocsRails.configure do |config|
|
|
|
11
11
|
config.password = ENV.fetch("SWAGGER_PASSWORD", "")
|
|
12
12
|
config.additional_controller_paths = []
|
|
13
13
|
config.output_path = Rails.root.join("public", "swagger", "openapi.json")
|
|
14
|
+
|
|
15
|
+
# Autenticação documentada no Swagger UI (botão "Authorize")
|
|
16
|
+
config.security_schemes = [
|
|
17
|
+
{
|
|
18
|
+
name: "BearerAuth",
|
|
19
|
+
type: :http,
|
|
20
|
+
scheme: "bearer",
|
|
21
|
+
bearer_format: "JWT",
|
|
22
|
+
description: "Token JWT (Authorization: Bearer <token>)"
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
name: "ApiKeyAuth",
|
|
26
|
+
type: :api_key,
|
|
27
|
+
in: "header",
|
|
28
|
+
param_name: "X-Api-Key",
|
|
29
|
+
description: "Chave interna da API"
|
|
30
|
+
}
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
# OR: qualquer um dos esquemas acima satisfaz a segurança global
|
|
34
|
+
config.global_security = [
|
|
35
|
+
{ BearerAuth: [] },
|
|
36
|
+
{ ApiKeyAuth: [] }
|
|
37
|
+
]
|
|
14
38
|
end
|
|
@@ -4,7 +4,8 @@ module SwaggerDocsRails
|
|
|
4
4
|
class Configuration
|
|
5
5
|
attr_accessor :enabled, :title, :version, :description,
|
|
6
6
|
:server_url, :live_reload, :username, :password,
|
|
7
|
-
:additional_controller_paths, :output_path
|
|
7
|
+
:additional_controller_paths, :output_path,
|
|
8
|
+
:security_schemes, :global_security
|
|
8
9
|
|
|
9
10
|
def initialize
|
|
10
11
|
@enabled = !Rails.env.production?
|
|
@@ -17,6 +18,39 @@ module SwaggerDocsRails
|
|
|
17
18
|
@password = ENV.fetch("SWAGGER_PASSWORD", "")
|
|
18
19
|
@additional_controller_paths = []
|
|
19
20
|
@output_path = Rails.root.join("public", "swagger", "openapi.json")
|
|
21
|
+
@security_schemes = esquemas_seguranca_padrao
|
|
22
|
+
@global_security = [{ BearerAuth: [] }]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def esquemas_seguranca_padrao
|
|
26
|
+
[
|
|
27
|
+
{
|
|
28
|
+
name: "BearerAuth",
|
|
29
|
+
type: :http,
|
|
30
|
+
scheme: "bearer",
|
|
31
|
+
bearer_format: "JWT",
|
|
32
|
+
description: "Token JWT (header Authorization: Bearer <token>)"
|
|
33
|
+
}
|
|
34
|
+
]
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def openapi_security_schemes
|
|
38
|
+
SecuritySchemeBuilder.build_all(security_schemes)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def openapi_global_security
|
|
42
|
+
SecuritySchemeBuilder.build_global_security(global_security)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def to_generator_config
|
|
46
|
+
{
|
|
47
|
+
title: title,
|
|
48
|
+
version: version,
|
|
49
|
+
description: description,
|
|
50
|
+
server_url: server_url,
|
|
51
|
+
security_schemes: openapi_security_schemes,
|
|
52
|
+
global_security: openapi_global_security
|
|
53
|
+
}
|
|
20
54
|
end
|
|
21
55
|
end
|
|
22
56
|
end
|
|
@@ -74,7 +74,7 @@ module SwaggerDocsRails
|
|
|
74
74
|
tags: build_tags(versions),
|
|
75
75
|
paths: paths,
|
|
76
76
|
components: components,
|
|
77
|
-
security:
|
|
77
|
+
security: build_global_security
|
|
78
78
|
}
|
|
79
79
|
end
|
|
80
80
|
|
|
@@ -599,14 +599,21 @@ module SwaggerDocsRails
|
|
|
599
599
|
}
|
|
600
600
|
end
|
|
601
601
|
|
|
602
|
+
def build_global_security
|
|
603
|
+
requisitos = @config[:global_security]
|
|
604
|
+
return requisitos if requisitos.present?
|
|
605
|
+
|
|
606
|
+
esquemas = @config[:security_schemes]
|
|
607
|
+
if esquemas.is_a?(Hash) && esquemas.any?
|
|
608
|
+
esquemas.keys.map { |nome| { nome.to_s => [] } }
|
|
609
|
+
else
|
|
610
|
+
[{ "BearerAuth" => [] }]
|
|
611
|
+
end
|
|
612
|
+
end
|
|
613
|
+
|
|
602
614
|
def build_components
|
|
603
615
|
{
|
|
604
|
-
securitySchemes:
|
|
605
|
-
BearerAuth: {
|
|
606
|
-
type: "http", scheme: "bearer", bearerFormat: "JWT",
|
|
607
|
-
description: "Token JWT obtido em /api/v1/auth/login"
|
|
608
|
-
}
|
|
609
|
-
},
|
|
616
|
+
securitySchemes: build_security_schemes,
|
|
610
617
|
schemas: @schema_parser.to_openapi_schemas
|
|
611
618
|
.merge(@dynamic_schemas)
|
|
612
619
|
.merge(shared_schemas),
|
|
@@ -624,6 +631,18 @@ module SwaggerDocsRails
|
|
|
624
631
|
}
|
|
625
632
|
end
|
|
626
633
|
|
|
634
|
+
def build_security_schemes
|
|
635
|
+
esquemas = @config[:security_schemes]
|
|
636
|
+
return esquemas if esquemas.is_a?(Hash) && esquemas.any?
|
|
637
|
+
|
|
638
|
+
{
|
|
639
|
+
"BearerAuth" => {
|
|
640
|
+
type: "http", scheme: "bearer", bearerFormat: "JWT",
|
|
641
|
+
description: "Token JWT (header Authorization: Bearer <token>)"
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
end
|
|
645
|
+
|
|
627
646
|
def schema_exists?(schema_name)
|
|
628
647
|
return true if %w[Error ValidationError GenericResponse GenericInput].include?(schema_name)
|
|
629
648
|
return true if @dynamic_schemas.key?(schema_name)
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SwaggerDocsRails
|
|
4
|
+
module SecuritySchemeBuilder
|
|
5
|
+
module_function
|
|
6
|
+
|
|
7
|
+
def to_openapi(scheme)
|
|
8
|
+
tipo = scheme[:type].to_s.downcase.tr("-", "_").to_sym
|
|
9
|
+
|
|
10
|
+
case tipo
|
|
11
|
+
when :http, :bearer
|
|
12
|
+
http_scheme(scheme)
|
|
13
|
+
when :api_key, :apikey
|
|
14
|
+
api_key_scheme(scheme)
|
|
15
|
+
else
|
|
16
|
+
raise ArgumentError, "Tipo de segurança não suportado: #{scheme[:type]} (use :http ou :api_key)"
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def build_all(schemes)
|
|
21
|
+
Array(schemes).each_with_object({}) do |scheme, resultado|
|
|
22
|
+
nome = scheme[:name] || scheme["name"]
|
|
23
|
+
raise ArgumentError, "security_schemes: :name é obrigatório" if nome.nil? || nome.to_s.strip.empty?
|
|
24
|
+
|
|
25
|
+
resultado[nome.to_s] = to_openapi(scheme)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def build_global_security(requirements)
|
|
30
|
+
Array(requirements).map do |requisito|
|
|
31
|
+
requisito.each_with_object({}) do |(nome, escopos), hash|
|
|
32
|
+
hash[nome.to_s] = Array(escopos)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def http_scheme(scheme)
|
|
38
|
+
{
|
|
39
|
+
type: "http",
|
|
40
|
+
scheme: scheme[:scheme].presence || "bearer",
|
|
41
|
+
bearerFormat: scheme[:bearer_format].presence || scheme[:bearerFormat].presence || "JWT",
|
|
42
|
+
description: scheme[:description].presence || "Token Bearer"
|
|
43
|
+
}.compact
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def api_key_scheme(scheme)
|
|
47
|
+
local = scheme[:in].presence || scheme[:location].presence || "header"
|
|
48
|
+
nome_param = scheme[:param_name].presence || scheme[:header_name].presence ||
|
|
49
|
+
scheme[:name_param].presence
|
|
50
|
+
if nome_param.nil? || nome_param.to_s.strip.empty?
|
|
51
|
+
raise ArgumentError, "security_schemes api_key: informe :param_name ou :header_name"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
{
|
|
55
|
+
type: "apiKey",
|
|
56
|
+
in: local.to_s,
|
|
57
|
+
name: nome_param,
|
|
58
|
+
description: scheme[:description].presence || "Chave de API"
|
|
59
|
+
}.compact
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
data/lib/swagger_docs_rails.rb
CHANGED
|
@@ -4,6 +4,7 @@ require "json"
|
|
|
4
4
|
require "fileutils"
|
|
5
5
|
require "swagger_docs_rails/version"
|
|
6
6
|
require "swagger_docs_rails/configuration"
|
|
7
|
+
require "swagger_docs_rails/security_scheme_builder"
|
|
7
8
|
require "swagger_docs_rails/schema_parser"
|
|
8
9
|
require "swagger_docs_rails/controller_parser"
|
|
9
10
|
require "swagger_docs_rails/openapi_builder"
|
|
@@ -15,12 +15,12 @@ namespace :swagger do
|
|
|
15
15
|
"API"
|
|
16
16
|
end
|
|
17
17
|
|
|
18
|
-
dados_configuracao =
|
|
18
|
+
dados_configuracao = config.to_generator_config.merge(
|
|
19
19
|
title: ENV.fetch("SWAGGER_TITLE", config.title || app_name),
|
|
20
20
|
version: ENV.fetch("SWAGGER_VERSION", config.version || "1.0.0"),
|
|
21
21
|
description: ENV.fetch("SWAGGER_DESC", config.description || "Documentação OpenAPI gerada automaticamente"),
|
|
22
22
|
server_url: ENV.fetch("SWAGGER_SERVER", config.server_url || "/")
|
|
23
|
-
|
|
23
|
+
)
|
|
24
24
|
|
|
25
25
|
puts "→ Parsing db/schema.rb..."
|
|
26
26
|
schema_parser = SwaggerDocsRails::SchemaParser.new
|
metadata
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: swagger_docs_rails
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
-
|
|
7
|
+
- ordex sistemas
|
|
8
8
|
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
@@ -32,7 +32,7 @@ dependencies:
|
|
|
32
32
|
description: Rails Engine para gerar documentação OpenAPI 3.0 a partir do schema.rb
|
|
33
33
|
e controllers Rails.
|
|
34
34
|
email:
|
|
35
|
-
-
|
|
35
|
+
- ordexsistemas@gmail.com
|
|
36
36
|
executables: []
|
|
37
37
|
extensions: []
|
|
38
38
|
extra_rdoc_files: []
|
|
@@ -51,6 +51,7 @@ files:
|
|
|
51
51
|
- lib/swagger_docs_rails/generator.rb
|
|
52
52
|
- lib/swagger_docs_rails/openapi_builder.rb
|
|
53
53
|
- lib/swagger_docs_rails/schema_parser.rb
|
|
54
|
+
- lib/swagger_docs_rails/security_scheme_builder.rb
|
|
54
55
|
- lib/swagger_docs_rails/version.rb
|
|
55
56
|
- lib/tasks/swagger_docs_rails.rake
|
|
56
57
|
homepage: https://rubygems.org/gems/swagger_docs_rails
|