Skip to content

Commit

Permalink
Better empty optionals (#65)
Browse files Browse the repository at this point in the history
* Silently ignore weird fields.

As long as required things show up.

* Warn about ignored fields.
  • Loading branch information
jonthegeek authored Oct 5, 2023
1 parent 39394bf commit 129a52b
Show file tree
Hide file tree
Showing 30 changed files with 201 additions and 155 deletions.
24 changes: 17 additions & 7 deletions R/as.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,24 @@
if (rlang::is_named2(x)) {
force(x_arg)
x <- rlang::set_names(x, snakecase::to_snake_case)
if (any(names(x) %in% valid_names)) {
x <- as.list(x)[names(x) %in% valid_names]
if (length(extra_names)) {
to_rename <- names(x) %in% names(extra_names)
names(x)[to_rename] <- extra_names[names(x)[to_rename]]
}
return(x)
ignored_names <- names(x)[!names(x) %in% valid_names]
x <- as.list(x)[names(x) %in% valid_names]
if (length(extra_names)) {
to_rename <- names(x) %in% names(extra_names)
names(x)[to_rename] <- extra_names[names(x)[to_rename]]
}
x <- x %|0|% NULL
if (length(ignored_names)) {
cli::cli_warn(
c(
"{.arg {x_arg}} expects names {.or {.val {valid_names}}}.",
"*" = "Ignored names: {.val {ignored_names}}."
),
class = "rapid_warning_extra_names"
)
}

return(x)
}

cli::cli_abort(
Expand Down
4 changes: 3 additions & 1 deletion R/components-security_scheme.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ S7::method(as_security_scheme, class_list) <- function(x) {
if (!length(x) || !any(lengths(x))) {
return(NULL)
}
switch(snakecase::to_snake_case(x$type),
type <- snakecase::to_snake_case(x$type)
x$type <- NULL
switch(type,
api_key = as_api_key_security_scheme(x),
# http = as_http_security_scheme(x),
# mutual_tls = as_mutual_tls_security_scheme(x),
Expand Down
3 changes: 3 additions & 0 deletions R/servers.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ S7::method(as_servers, class_list) <- function(x) {
)
}
)
if (!any(lengths(x))) {
x <- NULL
}

servers(
url = purrr::map_chr(x, "url"),
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/components-security_scheme-api_key.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
@ parameter_name: chr "parm1"
@ location : chr "query"

# as_api_key_security_scheme() errors for un/misnamed input
# as_api_key_security_scheme() errors for unnamed input

Code
as_api_key_security_scheme(list(a = "Jon", b = "[email protected]"))
as_api_key_security_scheme(list("Jon", "[email protected]"))
Condition
Error:
! `x` must have names "parameter_name", "location", "in", or "name".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
@ authorization_url: chr "https://auth.ebay.com/oauth2/authorize"
@ token_url : chr "https://api.ebay.com/identity/v1/oauth2/token"

# as_oauth2_authorization_code_flow() errors for un/misnamed input
# as_oauth2_authorization_code_flow() errors for unnamed input

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

---

Code
as_oauth2_authorization_code_flow(list(a = "Jon", b = "[email protected]"))
Condition
Error:
! `x` must have names "refresh_url", "scopes", "authorization_url", or "token_url".
* Any other names are ignored.

# as_oauth2_authorization_code_flow() errors for bad classes

Code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@
.. @ description: chr [1:2] "View and manage your account settings" ...
@ authorization_url: chr "https://auth.ebay.com/oauth2/authorize"

# as_oauth2_implicit_flow() errors for unnamed or misnamed input
# as_oauth2_implicit_flow() errors for unnamed input

Code
as_oauth2_implicit_flow(list(a = "Jon", b = "[email protected]"))
as_oauth2_implicit_flow(list("Jon", "[email protected]"))
Condition
Error:
! `x` must have names "refresh_url", "scopes", or "authorization_url".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@
.. @ description: chr [1:2] "View and manage your account settings" ...
@ token_url : chr "https://auth.ebay.com/oauth2/token"

# as_oauth2_token_flow() errors for unnamed or misnamed input
# as_oauth2_token_flow() errors for unnamed input

Code
as_oauth2_token_flow(list(a = "Jon", b = "[email protected]"))
as_oauth2_token_flow(list("Jon", "[email protected]"))
Condition
Error:
! `x` must have names "refresh_url", "scopes", or "token_url".
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/components-security_scheme-oauth2.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@
.. @ authorization_url: chr(0)
.. @ token_url : chr(0)

# as_oauth2_security_scheme() errors for unnamed or misnamed input
# as_oauth2_security_scheme() errors for unnamed input

Code
as_oauth2_security_scheme(list(a = "Jon", b = "[email protected]"))
as_oauth2_security_scheme(list("Jon", "[email protected]"))
Condition
Error:
! `x` must contain a named flows object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
@ details : <rapid::security_scheme_details> list()
@ description: chr(0)

# as_security_scheme_collection() errors for un/misnamed input
# as_security_scheme_collection() errors for unnamed input

Code
as_security_scheme_collection(as.list(letters))
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
.. @ details : <rapid::security_scheme_details> list()
.. @ description: chr(0)

# as_component_collection() errors for unnamed or misnamed input
# as_component_collection() errors for unnamed input

Code
as_component_collection(as.list(letters))
Expand All @@ -22,7 +22,7 @@
---

Code
as_component_collection(list(a = "My Cool API"))
as_component_collection(list("My Cool API"))
Condition
Error:
! `x` must have names "security_schemes".
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/_snaps/info-contact.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
@ email: chr(0)
@ url : chr(0)

# as_contact() errors informatively for unnamed or misnamed input
# as_contact() errors informatively for unnamed input

Code
as_contact(letters)
Expand All @@ -76,7 +76,7 @@
---

Code
as_contact(list(a = "Jon", b = "[email protected]"))
as_contact(list("Jon", "[email protected]"))
Condition
Error:
! `x` must have names "name", "email", or "url".
Expand All @@ -85,7 +85,7 @@
---

Code
as_contact(c(a = "Jon", b = "[email protected]"))
as_contact(c("Jon", "[email protected]"))
Condition
Error:
! `x` must have names "name", "email", or "url".
Expand Down
11 changes: 1 addition & 10 deletions tests/testthat/_snaps/info-license.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
@ identifier: chr "technically these have a fancy required format"
@ url : chr(0)

# as_license() errors informatively for unnamed or misnamed input
# as_license() errors informatively for unnamed input

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

---

Code
as_license(list(a = "Apache 2.0", b = "https://opensource.org/license/apache-2-0/"))
Condition
Error:
! `x` must have names "name", "identifier", or "url".
* Any other names are ignored.

# as_license() errors informatively for bad classes

Code
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/info.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
@ summary : chr(0)
@ terms_of_service: chr(0)

# as_info() errors informatively for unnamed or misnamed input
# as_info() errors informatively for unnamed input

Code
as_info(letters)
Expand All @@ -62,7 +62,7 @@
---

Code
as_info(list(a = "My Cool API"))
as_info(list("My Cool API"))
Condition
Error:
! `x` must have names "title", "version", "contact", "description", "license", "summary", or "terms_of_service".
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/_snaps/servers-string_replacements.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
.. $ : NULL
@ description: chr [1:3] "The active user's folder." NA NA

# as_string_replacements() errors for un/misnamed input
# as_string_replacements() errors for unnamed input

Code
as_string_replacements(letters)
Expand All @@ -135,7 +135,7 @@
---

Code
as_string_replacements(list(a = "Jon", b = "[email protected]"))
as_string_replacements(list("Jon", "[email protected]"))
Condition
Error in `purrr::map_chr()`:
i In index: 1.
Expand All @@ -145,7 +145,7 @@
---

Code
as_string_replacements(c(a = "Jon", b = "[email protected]"))
as_string_replacements(c("Jon", "[email protected]"))
Condition
Error:
! Can't coerce `x` <character> to <string_replacements>.
Expand Down
13 changes: 1 addition & 12 deletions tests/testthat/_snaps/servers.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@ description: chr(0)
@ variables : <rapid::server_variables> list()

# as_servers() errors informatively for unnamed or misnamed input
# as_servers() errors informatively for unnamed input

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

---

Code
as_servers(list(list(a = "https://example.com", b = "A cool server.")))
Condition
Error in `purrr::map()`:
i In index: 1.
Caused by error in `as_servers()`:
! `x[[i]]` must have names "url", "description", or "variables".
* Any other names are ignored.

# as_servers() errors informatively for bad classes

Code
Expand Down
65 changes: 50 additions & 15 deletions tests/testthat/_snaps/zz-rapid.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
Error:
! Can't coerce `x` <logical> to <rapid>.

# as_rapid() errors informatively for unnamed or misnamed input
# as_rapid() errors informatively for unnamed input

Code
as_rapid(list(letters))
Expand All @@ -105,30 +105,19 @@
---

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

# as_rapid() fails gracefully for unsupported urls

Code
as_rapid(url("https://api.apis.guru/v2/openapi.yaml"))
Condition
Error:
! `x` must be comprised of properly formed, supported elements.
Caused by error:
! `x` must have names "security_schemes".
* Any other names are ignored.

# as_rapid() works for urls

Code
as_rapid(url(
"https://api.apis.guru/v2/specs/amazonaws.com/AWSMigrationHub/2017-05-31/openapi.yaml"))
suppressWarnings(as_rapid(url(
"https://api.apis.guru/v2/specs/amazonaws.com/AWSMigrationHub/2017-05-31/openapi.yaml")))
Output
<rapid::rapid>
@ info : <rapid::info>
Expand Down Expand Up @@ -187,3 +176,49 @@
.. .. $ : chr(0)
.. @ rapid_class_requirement: chr "security_scheme"

# as_rapid() works for empty optional fields

Code
suppressWarnings(as_rapid(x))
Output
<rapid::rapid>
@ info : <rapid::info>
.. @ title : chr "OpenFEC"
.. @ version : chr "1.0"
.. @ contact : <rapid::contact>
.. .. @ name : chr(0)
.. .. @ email: chr(0)
.. .. @ url : chr(0)
.. @ description : chr "This application programming interface (API) allows you to explore the way candidates and committees fund their"| __truncated__
.. @ license : <rapid::license>
.. .. @ name : chr(0)
.. .. @ identifier: chr(0)
.. .. @ url : chr(0)
.. @ summary : chr(0)
.. @ terms_of_service: chr(0)
@ servers : <rapid::servers>
.. @ url : chr "/v1"
.. @ description: chr(0)
.. @ variables : <rapid::server_variables> list()
@ components: <rapid::component_collection>
.. @ security_schemes: <rapid::security_scheme_collection>
.. .. @ name : chr [1:3] "ApiKeyHeaderAuth" "ApiKeyQueryAuth" "apiKey"
.. .. @ details : <rapid::security_scheme_details> List of 3
.. .. .. $ : <rapid::api_key_security_scheme>
.. .. .. ..@ parameter_name: chr "X-Api-Key"
.. .. .. ..@ location : chr "header"
.. .. .. $ : <rapid::api_key_security_scheme>
.. .. .. ..@ parameter_name: chr "api_key"
.. .. .. ..@ location : chr "query"
.. .. .. $ : <rapid::api_key_security_scheme>
.. .. .. ..@ parameter_name: chr "api_key"
.. .. .. ..@ location : chr "query"
.. .. @ description: chr(0)
@ security : <rapid::security_requirements>
.. @ name : chr [1:3] "ApiKeyHeaderAuth" "ApiKeyQueryAuth" "apiKey"
.. @ required_scopes :List of 3
.. .. $ : chr(0)
.. .. $ : chr(0)
.. .. $ : chr(0)
.. @ rapid_class_requirement: chr "security_scheme"

Binary file added tests/testthat/fixtures/fec.rds
Binary file not shown.
Loading

0 comments on commit 129a52b

Please sign in to comment.