-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathopenapi-types.R
140 lines (129 loc) · 3.85 KB
/
openapi-types.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# calculate all OpenAPI Type information at once and use created information throughout package
apiTypesInfo <- list()
plumberToApiTypeMap <- list()
defaultApiType <- structure("string", default = TRUE)
defaultIsArray <- structure(FALSE, default = TRUE)
add_api_info_onLoad <- function() {
addApiInfo <- function(apiType, plumberTypes,
regex = NULL, converter = NULL,
format = NULL,
location = NULL,
realType = NULL) {
apiTypesInfo[[apiType]] <<-
list(
regex = regex,
converter = converter,
format = format,
location = location,
realType = ifelse(is.null(realType), apiType, realType),
# Q: Do we need to safe guard against special characters, such as `,`?
# https://github.com/rstudio/plumber/pull/532#discussion_r439584727
# A: https://swagger.io/docs/specification/serialization/
# > Additionally, the allowReserved keyword specifies whether the reserved
# > characters :/?#[]@!$&'()*+,;= in parameter values are allowed to be sent as they are,
# > or should be percent-encoded. By default, allowReserved is false, and reserved characters
# > are percent-encoded. For example, / is encoded as %2F (or %2f), so that the parameter
# > value quotes/h2g2.txt will be sent as quotes%2Fh2g2.txt
regexArray = paste0("(?:(?:", regex, "),?)+"),
converterArray = function(x) {converter(stri_split_fixed(x, ",")[[1]])}
)
for (plumberType in plumberTypes) {
plumberToApiTypeMap[[plumberType]] <<- apiType
}
# make sure it could be called again
plumberToApiTypeMap[[apiType]] <<- apiType
invisible(TRUE)
}
addApiInfo(
"boolean",
c("bool", "boolean", "logical"),
"[01tfTF]|true|false|TRUE|FALSE",
as.logical,
location = c("query", "path")
)
addApiInfo(
"number",
c("dbl", "double", "float", "number", "numeric"),
"-?\\\\d*\\\\.?\\\\d+",
as.numeric,
format = "double",
location = c("query", "path")
)
addApiInfo(
"integer",
c("int", "integer"),
"-?\\\\d+",
as.integer,
format = "int64",
location = c("query", "path")
)
addApiInfo(
"string",
c("chr", "str", "character", "string"),
"[^/]+",
as.character,
location = c("query", "path")
)
addApiInfo(
"date-time",
c("POSIXct", "POSIXt"),
"[^/]+",
lubridate::as_datetime,
format = "date-time",
location = c("query", "path"),
realType = "string"
)
addApiInfo(
"date",
c("Date"),
"[^/]+",
lubridate::as_date,
format = "date",
location = c("query", "path"),
realType = "string"
)
addApiInfo(
"object",
c("list", "data.frame", "df", "object"),
location = "requestBody"
)
addApiInfo(
"file",
c("file", "binary"),
location = "requestBody",
format = "binary",
realType = "string"
)
}
#' Parse the given plumber type and return the typecast value
#' @noRd
plumberToApiType <- function(type, inPath = FALSE) {
if (length(type) > 1) {
return(vapply(type, plumberToApiType, character(1), inPath, USE.NAMES = FALSE))
}
# default type is "string" type
if (is.na(type)) {
return(defaultApiType)
}
apiType <- plumberToApiTypeMap[[as.character(type)]]
if (is.null(apiType)) {
warning(
"Unrecognized type: ", type, ". Using type: ", defaultApiType,
call. = FALSE
)
apiType <- defaultApiType
}
if (inPath && !"path" %in% apiTypesInfo[[apiType]]$location) {
warning(
"Unsupported path parameter type: ", type, ". Using type: ", defaultApiType,
call. = FALSE
)
apiType <- defaultApiType
}
return(apiType)
}
#' Filter OpenAPI Types
#' @noRd
filterApiTypes <- function(matches, property) {
names(Filter(function(x) {any(matches %in% x[[property]])}, apiTypesInfo))
}