rstudio / plumber
1

2

3
# calculate all OpenAPI Type information at once and use created information throughout package
4
apiTypesInfo <- list()
5
plumberToApiTypeMap <- list()
6
defaultApiType <- structure("string", default = TRUE)
7
defaultIsArray <- structure(FALSE, default = TRUE)
8

9
add_api_info_onLoad <- function() {
10 0
  addApiInfo <- function(apiType, plumberTypes,
11 0
                         regex = NULL, converter = NULL,
12 0
                         format = NULL,
13 0
                         location = NULL,
14 0
                         realType = NULL) {
15 0
    apiTypesInfo[[apiType]] <<-
16 0
      list(
17 0
        regex = regex,
18 0
        converter = converter,
19 0
        format = format,
20 0
        location = location,
21 0
        realType = realType,
22
        # Q: Do we need to safe guard against special characters, such as `,`?
23
        # https://github.com/rstudio/plumber/pull/532#discussion_r439584727
24
        # A: https://swagger.io/docs/specification/serialization/
25
        # > Additionally, the allowReserved keyword specifies whether the reserved
26
        # > characters :/?#[]@!$&'()*+,;= in parameter values are allowed to be sent as they are,
27
        # > or should be percent-encoded. By default, allowReserved is false, and reserved characters
28
        # > are percent-encoded. For example, / is encoded as %2F (or %2f), so that the parameter
29
        # > value quotes/h2g2.txt will be sent as quotes%2Fh2g2.txt
30 0
        regexArray = paste0("(?:(?:", regex, "),?)+"),
31 0
        converterArray = function(x) {converter(stri_split_fixed(x, ",")[[1]])}
32
      )
33

34 0
    for (plumberType in plumberTypes) {
35 0
      plumberToApiTypeMap[[plumberType]] <<- apiType
36
    }
37
    # make sure it could be called again
38 0
    plumberToApiTypeMap[[apiType]] <<- apiType
39

40 0
    invisible(TRUE)
41
  }
42

43 0
  addApiInfo(
44 0
    "boolean",
45 0
    c("bool", "boolean", "logical"),
46 0
    "[01tfTF]|true|false|TRUE|FALSE",
47 0
    as.logical,
48 0
    location = c("query", "path")
49
  )
50 0
  addApiInfo(
51 0
    "number",
52 0
    c("dbl", "double", "float", "number", "numeric"),
53 0
    "-?\\\\d*\\\\.?\\\\d+",
54 0
    as.numeric,
55 0
    format = "double",
56 0
    location = c("query", "path")
57
  )
58 0
  addApiInfo(
59 0
    "integer",
60 0
    c("int", "integer"),
61 0
    "-?\\\\d+",
62 0
    as.integer,
63 0
    format = "int64",
64 0
    location = c("query", "path")
65
  )
66 0
  addApiInfo(
67 0
    "string",
68 0
    c("chr", "str", "character", "string"),
69
    "[^/]+",
70 0
    as.character,
71 0
    location = c("query", "path")
72
  )
73 0
  addApiInfo(
74 0
    "object",
75 0
    c("list", "data.frame", "df"),
76 0
    location = "requestBody"
77
  )
78 0
  addApiInfo(
79 0
    "file",
80 0
    c("file", "binary"),
81 0
    location = "requestBody",
82 0
    format = "binary",
83 0
    realType = "string"
84
  )
85
}
86

87

88
#' Parse the given plumber type and return the typecast value
89
#' @noRd
90
plumberToApiType <- function(type, inPath = FALSE) {
91 1
  if (length(type) > 1) {
92 1
    return(vapply(type, plumberToApiType, character(1), inPath, USE.NAMES = FALSE))
93
  }
94
  # default type is "string" type
95 1
  if (is.na(type)) {
96 1
    return(defaultApiType)
97
  }
98

99 1
  apiType <- plumberToApiTypeMap[[as.character(type)]]
100 1
  if (is.null(apiType)) {
101 1
    warning(
102 1
      "Unrecognized type: ", type, ". Using type: ", defaultApiType,
103 1
      call. = FALSE
104
    )
105 1
    apiType <- defaultApiType
106
  }
107 1
  if (inPath && !"path" %in% apiTypesInfo[[apiType]]$location) {
108 1
    warning(
109 1
      "Unsupported path parameter type: ", type, ". Using type: ", defaultApiType,
110 1
      call. = FALSE
111
    )
112 1
    apiType <- defaultApiType
113
  }
114

115 1
  return(apiType)
116
}
117

118
#' Filter OpenAPI Types
119
#' @noRd
120
filterApiTypes <- function(matches, property) {
121 1
  names(Filter(function(x) {any(matches %in% x[[property]])}, apiTypesInfo))
122
}

Read our documentation on viewing source code .

Loading