Skip to content

Commit 129a52b

Browse files
authored
Better empty optionals (#65)
* Silently ignore weird fields. As long as required things show up. * Warn about ignored fields.
1 parent 39394bf commit 129a52b

30 files changed

+201
-155
lines changed

R/as.R

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,24 @@
2424
if (rlang::is_named2(x)) {
2525
force(x_arg)
2626
x <- rlang::set_names(x, snakecase::to_snake_case)
27-
if (any(names(x) %in% valid_names)) {
28-
x <- as.list(x)[names(x) %in% valid_names]
29-
if (length(extra_names)) {
30-
to_rename <- names(x) %in% names(extra_names)
31-
names(x)[to_rename] <- extra_names[names(x)[to_rename]]
32-
}
33-
return(x)
27+
ignored_names <- names(x)[!names(x) %in% valid_names]
28+
x <- as.list(x)[names(x) %in% valid_names]
29+
if (length(extra_names)) {
30+
to_rename <- names(x) %in% names(extra_names)
31+
names(x)[to_rename] <- extra_names[names(x)[to_rename]]
3432
}
33+
x <- x %|0|% NULL
34+
if (length(ignored_names)) {
35+
cli::cli_warn(
36+
c(
37+
"{.arg {x_arg}} expects names {.or {.val {valid_names}}}.",
38+
"*" = "Ignored names: {.val {ignored_names}}."
39+
),
40+
class = "rapid_warning_extra_names"
41+
)
42+
}
43+
44+
return(x)
3545
}
3646

3747
cli::cli_abort(

R/components-security_scheme.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ S7::method(as_security_scheme, class_list) <- function(x) {
7878
if (!length(x) || !any(lengths(x))) {
7979
return(NULL)
8080
}
81-
switch(snakecase::to_snake_case(x$type),
81+
type <- snakecase::to_snake_case(x$type)
82+
x$type <- NULL
83+
switch(type,
8284
api_key = as_api_key_security_scheme(x),
8385
# http = as_http_security_scheme(x),
8486
# mutual_tls = as_mutual_tls_security_scheme(x),

R/servers.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ S7::method(as_servers, class_list) <- function(x) {
134134
)
135135
}
136136
)
137+
if (!any(lengths(x))) {
138+
x <- NULL
139+
}
137140

138141
servers(
139142
url = purrr::map_chr(x, "url"),

tests/testthat/_snaps/components-security_scheme-api_key.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
@ parameter_name: chr "parm1"
1919
@ location : chr "query"
2020

21-
# as_api_key_security_scheme() errors for un/misnamed input
21+
# as_api_key_security_scheme() errors for unnamed input
2222

2323
Code
24-
as_api_key_security_scheme(list(a = "Jon", b = "[email protected]"))
24+
as_api_key_security_scheme(list("Jon", "[email protected]"))
2525
Condition
2626
Error:
2727
! `x` must have names "parameter_name", "location", "in", or "name".

tests/testthat/_snaps/components-security_scheme-oauth2-authorization_code_flow.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
@ authorization_url: chr "https://auth.ebay.com/oauth2/authorize"
124124
@ token_url : chr "https://api.ebay.com/identity/v1/oauth2/token"
125125

126-
# as_oauth2_authorization_code_flow() errors for un/misnamed input
126+
# as_oauth2_authorization_code_flow() errors for unnamed input
127127

128128
Code
129129
as_oauth2_authorization_code_flow("a")
@@ -132,15 +132,6 @@
132132
! `x` must have names "refresh_url", "scopes", "authorization_url", or "token_url".
133133
* Any other names are ignored.
134134

135-
---
136-
137-
Code
138-
as_oauth2_authorization_code_flow(list(a = "Jon", b = "[email protected]"))
139-
Condition
140-
Error:
141-
! `x` must have names "refresh_url", "scopes", "authorization_url", or "token_url".
142-
* Any other names are ignored.
143-
144135
# as_oauth2_authorization_code_flow() errors for bad classes
145136

146137
Code

tests/testthat/_snaps/components-security_scheme-oauth2-implicit_flow.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@
9292
.. @ description: chr [1:2] "View and manage your account settings" ...
9393
@ authorization_url: chr "https://auth.ebay.com/oauth2/authorize"
9494

95-
# as_oauth2_implicit_flow() errors for unnamed or misnamed input
95+
# as_oauth2_implicit_flow() errors for unnamed input
9696

9797
Code
98-
as_oauth2_implicit_flow(list(a = "Jon", b = "[email protected]"))
98+
as_oauth2_implicit_flow(list("Jon", "[email protected]"))
9999
Condition
100100
Error:
101101
! `x` must have names "refresh_url", "scopes", or "authorization_url".

tests/testthat/_snaps/components-security_scheme-oauth2-token_flow.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@
9393
.. @ description: chr [1:2] "View and manage your account settings" ...
9494
@ token_url : chr "https://auth.ebay.com/oauth2/token"
9595

96-
# as_oauth2_token_flow() errors for unnamed or misnamed input
96+
# as_oauth2_token_flow() errors for unnamed input
9797

9898
Code
99-
as_oauth2_token_flow(list(a = "Jon", b = "[email protected]"))
99+
as_oauth2_token_flow(list("Jon", "[email protected]"))
100100
Condition
101101
Error:
102102
! `x` must have names "refresh_url", "scopes", or "token_url".

tests/testthat/_snaps/components-security_scheme-oauth2.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@
7575
.. @ authorization_url: chr(0)
7676
.. @ token_url : chr(0)
7777

78-
# as_oauth2_security_scheme() errors for unnamed or misnamed input
78+
# as_oauth2_security_scheme() errors for unnamed input
7979

8080
Code
81-
as_oauth2_security_scheme(list(a = "Jon", b = "[email protected]"))
81+
as_oauth2_security_scheme(list("Jon", "[email protected]"))
8282
Condition
8383
Error:
8484
! `x` must contain a named flows object.

tests/testthat/_snaps/components-security_scheme_collection.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
@ details : <rapid::security_scheme_details> list()
4040
@ description: chr(0)
4141

42-
# as_security_scheme_collection() errors for un/misnamed input
42+
# as_security_scheme_collection() errors for unnamed input
4343

4444
Code
4545
as_security_scheme_collection(as.list(letters))

tests/testthat/_snaps/components.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
.. @ details : <rapid::security_scheme_details> list()
1111
.. @ description: chr(0)
1212

13-
# as_component_collection() errors for unnamed or misnamed input
13+
# as_component_collection() errors for unnamed input
1414

1515
Code
1616
as_component_collection(as.list(letters))
@@ -22,7 +22,7 @@
2222
---
2323

2424
Code
25-
as_component_collection(list(a = "My Cool API"))
25+
as_component_collection(list("My Cool API"))
2626
Condition
2727
Error:
2828
! `x` must have names "security_schemes".

tests/testthat/_snaps/info-contact.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
@ email: chr(0)
6565
@ url : chr(0)
6666

67-
# as_contact() errors informatively for unnamed or misnamed input
67+
# as_contact() errors informatively for unnamed input
6868

6969
Code
7070
as_contact(letters)
@@ -76,7 +76,7 @@
7676
---
7777

7878
Code
79-
as_contact(list(a = "Jon", b = "[email protected]"))
79+
as_contact(list("Jon", "[email protected]"))
8080
Condition
8181
Error:
8282
! `x` must have names "name", "email", or "url".
@@ -85,7 +85,7 @@
8585
---
8686

8787
Code
88-
as_contact(c(a = "Jon", b = "[email protected]"))
88+
as_contact(c("Jon", "[email protected]"))
8989
Condition
9090
Error:
9191
! `x` must have names "name", "email", or "url".

tests/testthat/_snaps/info-license.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
@ identifier: chr "technically these have a fancy required format"
112112
@ url : chr(0)
113113

114-
# as_license() errors informatively for unnamed or misnamed input
114+
# as_license() errors informatively for unnamed input
115115

116116
Code
117117
as_license(letters)
@@ -120,15 +120,6 @@
120120
! `x` must have names "name", "identifier", or "url".
121121
* Any other names are ignored.
122122

123-
---
124-
125-
Code
126-
as_license(list(a = "Apache 2.0", b = "https://opensource.org/license/apache-2-0/"))
127-
Condition
128-
Error:
129-
! `x` must have names "name", "identifier", or "url".
130-
* Any other names are ignored.
131-
132123
# as_license() errors informatively for bad classes
133124

134125
Code

tests/testthat/_snaps/info.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
@ summary : chr(0)
5151
@ terms_of_service: chr(0)
5252

53-
# as_info() errors informatively for unnamed or misnamed input
53+
# as_info() errors informatively for unnamed input
5454

5555
Code
5656
as_info(letters)
@@ -62,7 +62,7 @@
6262
---
6363

6464
Code
65-
as_info(list(a = "My Cool API"))
65+
as_info(list("My Cool API"))
6666
Condition
6767
Error:
6868
! `x` must have names "title", "version", "contact", "description", "license", "summary", or "terms_of_service".

tests/testthat/_snaps/servers-string_replacements.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
.. $ : NULL
125125
@ description: chr [1:3] "The active user's folder." NA NA
126126

127-
# as_string_replacements() errors for un/misnamed input
127+
# as_string_replacements() errors for unnamed input
128128

129129
Code
130130
as_string_replacements(letters)
@@ -135,7 +135,7 @@
135135
---
136136

137137
Code
138-
as_string_replacements(list(a = "Jon", b = "[email protected]"))
138+
as_string_replacements(list("Jon", "[email protected]"))
139139
Condition
140140
Error in `purrr::map_chr()`:
141141
i In index: 1.
@@ -145,7 +145,7 @@
145145
---
146146

147147
Code
148-
as_string_replacements(c(a = "Jon", b = "[email protected]"))
148+
as_string_replacements(c("Jon", "[email protected]"))
149149
Condition
150150
Error:
151151
! Can't coerce `x` <character> to <string_replacements>.

tests/testthat/_snaps/servers.md

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
@ description: chr(0)
1010
@ variables : <rapid::server_variables> list()
1111

12-
# as_servers() errors informatively for unnamed or misnamed input
12+
# as_servers() errors informatively for unnamed input
1313

1414
Code
1515
as_servers(list(letters))
@@ -20,17 +20,6 @@
2020
! `x[[i]]` must have names "url", "description", or "variables".
2121
* Any other names are ignored.
2222

23-
---
24-
25-
Code
26-
as_servers(list(list(a = "https://example.com", b = "A cool server.")))
27-
Condition
28-
Error in `purrr::map()`:
29-
i In index: 1.
30-
Caused by error in `as_servers()`:
31-
! `x[[i]]` must have names "url", "description", or "variables".
32-
* Any other names are ignored.
33-
3423
# as_servers() errors informatively for bad classes
3524

3625
Code

tests/testthat/_snaps/zz-rapid.md

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
Error:
9292
! Can't coerce `x` <logical> to <rapid>.
9393

94-
# as_rapid() errors informatively for unnamed or misnamed input
94+
# as_rapid() errors informatively for unnamed input
9595

9696
Code
9797
as_rapid(list(letters))
@@ -105,30 +105,19 @@
105105
---
106106

107107
Code
108-
as_rapid(list(list(a = "https://example.com", b = "A cool server.")))
108+
as_rapid(list(list("https://example.com", "A cool server.")))
109109
Condition
110110
Error:
111111
! `x` must be comprised of properly formed, supported elements.
112112
Caused by error:
113113
! `x` must have names "info", "servers", "components", or "security".
114114
* Any other names are ignored.
115115

116-
# as_rapid() fails gracefully for unsupported urls
117-
118-
Code
119-
as_rapid(url("https://api.apis.guru/v2/openapi.yaml"))
120-
Condition
121-
Error:
122-
! `x` must be comprised of properly formed, supported elements.
123-
Caused by error:
124-
! `x` must have names "security_schemes".
125-
* Any other names are ignored.
126-
127116
# as_rapid() works for urls
128117

129118
Code
130-
as_rapid(url(
131-
"https://api.apis.guru/v2/specs/amazonaws.com/AWSMigrationHub/2017-05-31/openapi.yaml"))
119+
suppressWarnings(as_rapid(url(
120+
"https://api.apis.guru/v2/specs/amazonaws.com/AWSMigrationHub/2017-05-31/openapi.yaml")))
132121
Output
133122
<rapid::rapid>
134123
@ info : <rapid::info>
@@ -187,3 +176,49 @@
187176
.. .. $ : chr(0)
188177
.. @ rapid_class_requirement: chr "security_scheme"
189178

179+
# as_rapid() works for empty optional fields
180+
181+
Code
182+
suppressWarnings(as_rapid(x))
183+
Output
184+
<rapid::rapid>
185+
@ info : <rapid::info>
186+
.. @ title : chr "OpenFEC"
187+
.. @ version : chr "1.0"
188+
.. @ contact : <rapid::contact>
189+
.. .. @ name : chr(0)
190+
.. .. @ email: chr(0)
191+
.. .. @ url : chr(0)
192+
.. @ description : chr "This application programming interface (API) allows you to explore the way candidates and committees fund their"| __truncated__
193+
.. @ license : <rapid::license>
194+
.. .. @ name : chr(0)
195+
.. .. @ identifier: chr(0)
196+
.. .. @ url : chr(0)
197+
.. @ summary : chr(0)
198+
.. @ terms_of_service: chr(0)
199+
@ servers : <rapid::servers>
200+
.. @ url : chr "/v1"
201+
.. @ description: chr(0)
202+
.. @ variables : <rapid::server_variables> list()
203+
@ components: <rapid::component_collection>
204+
.. @ security_schemes: <rapid::security_scheme_collection>
205+
.. .. @ name : chr [1:3] "ApiKeyHeaderAuth" "ApiKeyQueryAuth" "apiKey"
206+
.. .. @ details : <rapid::security_scheme_details> List of 3
207+
.. .. .. $ : <rapid::api_key_security_scheme>
208+
.. .. .. ..@ parameter_name: chr "X-Api-Key"
209+
.. .. .. ..@ location : chr "header"
210+
.. .. .. $ : <rapid::api_key_security_scheme>
211+
.. .. .. ..@ parameter_name: chr "api_key"
212+
.. .. .. ..@ location : chr "query"
213+
.. .. .. $ : <rapid::api_key_security_scheme>
214+
.. .. .. ..@ parameter_name: chr "api_key"
215+
.. .. .. ..@ location : chr "query"
216+
.. .. @ description: chr(0)
217+
@ security : <rapid::security_requirements>
218+
.. @ name : chr [1:3] "ApiKeyHeaderAuth" "ApiKeyQueryAuth" "apiKey"
219+
.. @ required_scopes :List of 3
220+
.. .. $ : chr(0)
221+
.. .. $ : chr(0)
222+
.. .. $ : chr(0)
223+
.. @ rapid_class_requirement: chr "security_scheme"
224+

tests/testthat/fixtures/fec.rds

94 KB
Binary file not shown.

0 commit comments

Comments
 (0)