explicit 0.2.8 → 0.2.9
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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dd88a349ccfac424582d31d9d4f325f5ff03ede07eee06204938643b836310a5
|
4
|
+
data.tar.gz: 31c41a8b16b54c1d3e337cc7fb1458af43d24a97123e5f9d68470daf7b90c6dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c58b39e3709dbc6edba831aa1c746eb919c0c059ca2c386ac9840ff7ed101b9b0b7342e1e6885b368c3456e23b17e31fc54c8608b2d33efeacdfc3d41895ef55
|
7
|
+
data.tar.gz: 8e6b2055a6c55db8192a9ff5fd60773bfad06d1acc136b5056eb037ae3b76593f1d6b594c44c3c7ac63c13bb7f1a2d38a37c09cd670b40c0dd50c775595e0015
|
@@ -2,6 +2,7 @@
|
|
2
2
|
<html>
|
3
3
|
<head>
|
4
4
|
<title><%= page_title || "API Documentation" %></title>
|
5
|
+
<meta charset="UTF-8">
|
5
6
|
|
6
7
|
<% if favicon_url %>
|
7
8
|
<link rel="icon" href="<%= favicon_url %>" />
|
@@ -50,7 +51,7 @@
|
|
50
51
|
Version <%= version %>
|
51
52
|
</div>
|
52
53
|
<div class="p-1 w-1/2">
|
53
|
-
<%= link_to url_helpers.
|
54
|
+
<%= link_to "https://petstore.swagger.io/?url=#{url_helpers.explicit_documentation_swagger_url(host: request.host)}", target: "_blank", class: "flex items-center justify-center gap-1 text-neutral-900" do %>
|
54
55
|
<span>Swagger</span>
|
55
56
|
|
56
57
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="currentColor" class="size-4">
|
@@ -12,6 +12,20 @@ module Explicit::Documentation::Output
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def swagger_document
|
15
|
+
paths = build_paths_from_requests
|
16
|
+
|
17
|
+
securitySchemes = {}.tap do |hash|
|
18
|
+
requests = paths.flat_map { |path, methods| methods.values }
|
19
|
+
|
20
|
+
if requests.filter { _1.dig(:security, 0, :basicAuth) }.any?
|
21
|
+
hash[:basicAuth] = { type: "http", scheme: "basic" }
|
22
|
+
end
|
23
|
+
|
24
|
+
if requests.filter { _1.dig(:security, 0, :bearerAuth) }.any?
|
25
|
+
hash[:bearerAuth] = { type: "http", scheme: "bearer" }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
15
29
|
{
|
16
30
|
openapi: "3.0.1",
|
17
31
|
info: {
|
@@ -24,7 +38,8 @@ module Explicit::Documentation::Output
|
|
24
38
|
}
|
25
39
|
],
|
26
40
|
tags: build_tags_from_sections,
|
27
|
-
paths: build_paths_from_requests
|
41
|
+
paths: build_paths_from_requests,
|
42
|
+
components: { securitySchemes: }
|
28
43
|
}
|
29
44
|
end
|
30
45
|
|
@@ -86,13 +101,23 @@ module Explicit::Documentation::Output
|
|
86
101
|
request = page.request
|
87
102
|
route = request.routes.first
|
88
103
|
|
104
|
+
security =
|
105
|
+
if request.requires_basic_authorization?
|
106
|
+
[ { basicAuth: [] } ]
|
107
|
+
elsif request.requires_bearer_authorization?
|
108
|
+
[ { bearerAuth: [] } ]
|
109
|
+
else
|
110
|
+
nil
|
111
|
+
end
|
112
|
+
|
89
113
|
paths[route.path_with_curly_syntax][route.method.to_s] = {
|
90
114
|
tags: [ section.name ],
|
91
115
|
summary: request.get_title,
|
92
116
|
description: request.get_description,
|
93
117
|
parameters: build_parameters(request),
|
94
118
|
requestBody: build_request_body(request),
|
95
|
-
responses: build_responses(request)
|
119
|
+
responses: build_responses(request),
|
120
|
+
security:
|
96
121
|
}.compact_blank
|
97
122
|
end
|
98
123
|
end
|
@@ -102,7 +127,11 @@ module Explicit::Documentation::Output
|
|
102
127
|
|
103
128
|
def build_parameters(request)
|
104
129
|
headers =
|
105
|
-
request.headers_type.attributes.
|
130
|
+
request.headers_type.attributes.filter_map do |name, type|
|
131
|
+
# NOTE: skip Authorization header because swagger prefers the `security` directive for basic and bearer
|
132
|
+
# authorization schemas. If not basic or bearer, then we add the Authorization header.
|
133
|
+
next if name == "Authorization" && !request.custom_authorization_format?
|
134
|
+
|
106
135
|
{
|
107
136
|
name: name.to_s,
|
108
137
|
in: "header",
|
data/lib/explicit/request.rb
CHANGED
@@ -12,13 +12,15 @@ class Explicit::Request
|
|
12
12
|
|
13
13
|
instance_eval(&block)
|
14
14
|
|
15
|
+
define_missing_path_params!
|
16
|
+
|
15
17
|
if Explicit.configuration.rescue_from_invalid_params? && @params.any?
|
16
18
|
@responses[422] << {
|
17
19
|
error: "invalid_params",
|
18
20
|
params: [
|
19
21
|
:description,
|
20
22
|
"An object containing error messages for all invalid params",
|
21
|
-
[:hash, :string, :string]
|
23
|
+
[ :hash, :string, :string ]
|
22
24
|
]
|
23
25
|
}
|
24
26
|
end
|
@@ -76,19 +78,19 @@ class Explicit::Request
|
|
76
78
|
raise ArgumentError("duplicated param #{name}") if @params.key?(name)
|
77
79
|
|
78
80
|
if options[:optional]
|
79
|
-
type = [:nilable, type]
|
81
|
+
type = [ :nilable, type ]
|
80
82
|
end
|
81
83
|
|
82
84
|
if (defaultval = options[:default])
|
83
|
-
type = [:default, defaultval, type]
|
85
|
+
type = [ :default, defaultval, type ]
|
84
86
|
end
|
85
87
|
|
86
88
|
if (description = options[:description])
|
87
|
-
type = [:description, description, type]
|
89
|
+
type = [ :description, description, type ]
|
88
90
|
end
|
89
91
|
|
90
92
|
if @routes.first&.params&.include?(name)
|
91
|
-
type = [:_param_location, :path, type]
|
93
|
+
type = [ :_param_location, :path, type ]
|
92
94
|
end
|
93
95
|
|
94
96
|
@params[name] = type
|
@@ -140,6 +142,31 @@ class Explicit::Request
|
|
140
142
|
end
|
141
143
|
|
142
144
|
def responses_type(status:)
|
143
|
-
Explicit::Type.build([:one_of, *@responses[status]])
|
145
|
+
Explicit::Type.build([ :one_of, *@responses[status] ])
|
146
|
+
end
|
147
|
+
|
148
|
+
def custom_authorization_format?
|
149
|
+
@headers.key?("Authorization") && !requires_basic_authorization? && !requires_bearer_authorization?
|
150
|
+
end
|
151
|
+
|
152
|
+
def requires_basic_authorization?
|
153
|
+
authorization = headers_type.attributes["Authorization"]
|
154
|
+
|
155
|
+
authorization&.format&.to_s&.include?("Basic")
|
144
156
|
end
|
157
|
+
|
158
|
+
def requires_bearer_authorization?
|
159
|
+
authorization = headers_type.attributes["Authorization"]
|
160
|
+
|
161
|
+
authorization&.format&.to_s&.include?("Bearer")
|
162
|
+
end
|
163
|
+
|
164
|
+
private
|
165
|
+
def define_missing_path_params!
|
166
|
+
@routes.first&.params&.each do |path_param_name|
|
167
|
+
if @params[path_param_name.to_sym].blank?
|
168
|
+
param(path_param_name.to_sym, :string)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
145
172
|
end
|
data/lib/explicit/version.rb
CHANGED