-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathendpoint.mbt
More file actions
307 lines (287 loc) · 8.98 KB
/
Copy pathendpoint.mbt
File metadata and controls
307 lines (287 loc) · 8.98 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
///|
/// Where a parameter is carried, the OpenAPI `in` locations: the path, the query
/// string, a header, or a cookie.
pub(all) enum ParamLoc {
InPath
InQuery
InHeader
InCookie
} derive(Eq)
///|
/// The OpenAPI `in` string for a location.
fn loc_str(l : ParamLoc) -> String {
match l {
InPath => "path"
InQuery => "query"
InHeader => "header"
InCookie => "cookie"
}
}
///|
/// A single request parameter descriptor: its `name`, `loc`ation, scalar
/// `schema`, whether it is `required`, an optional `description`, its value
/// `constraints`, the `default` that stands in when the request omits it, and
/// the `alias` it travels under on the wire. Path parameters are always required
/// (OpenAPI requires it); the constructor keeps the caller's value but
/// validation treats path params as mandatory.
pub(all) struct Param {
name : String
loc : ParamLoc
schema : Schema
required : Bool
description : String
constraints : Array[Constraint]
default : Json?
alias_ : String
} derive(Eq)
///|
/// Build a parameter descriptor. `schema` defaults to a string and `required` to
/// `true`. `constraints` are the Pydantic-style bounds FastAPI reads off a
/// `Query(ge=…, max_length=…)` — emitted into the parameter's documented schema
/// *and* enforced on the inbound value. `default` is emitted and makes the
/// parameter optional, since a value that has one is never missing. `alias` is
/// the name the parameter travels under when that differs from the one the code
/// calls it (← `Query(alias="item-query")`).
pub fn Param::new(
name : String,
loc : ParamLoc,
schema? : Schema = SStr,
required? : Bool = true,
description? : String = "",
constraints? : Array[Constraint] = [],
default? : Json,
alias_? : String = "",
) -> Param {
{ name, loc, schema, required, description, constraints, default, alias_, }
}
///|
/// The name this parameter travels under: its `alias` when it has one, else its
/// own name. This is what is read off the request, what the document calls it,
/// and what a validation error's `loc` points at — all three being the client's
/// view of the parameter.
pub fn Param::key(self : Param) -> String {
if self.alias_ == "" {
self.name
} else {
self.alias_
}
}
///|
/// Whether a request must carry this parameter. One with a default never must —
/// its absence is answered by the default, exactly as a Python default argument
/// is.
pub fn Param::is_required(self : Param) -> Bool {
self.required && self.default is None
}
///|
/// A single response descriptor: the HTTP `status`, a human `description`, and an
/// optional body `schema` (`None` for an empty body, e.g. `204`).
pub(all) struct ResponseSpec {
status : Int
description : String
body : Schema?
} derive(Eq)
///|
/// Build a response descriptor. `description` defaults to `"OK"` and there is no
/// body unless one is given.
pub fn ResponseSpec::new(
status : Int,
description? : String = "OK",
body? : Schema,
) -> ResponseSpec {
{ status, description, body, }
}
///|
/// The runtime endpoint descriptor a route can carry — the one first-class value
/// that replaces FastAPI reading a handler's signature. Walked once for the
/// OpenAPI operation (parameters + `requestBody` + `responses`, with every named
/// object hoisted into `components/schemas`) and for request validation.
pub(all) struct Endpoint {
params : Array[Param]
request_body : Schema?
request_required : Bool
responses : Array[ResponseSpec]
} derive(Eq)
///|
/// Build an endpoint descriptor. Everything is optional: a bare `Endpoint::new()`
/// describes an endpoint with no parameters, no body, and (on emit) a default
/// `200 OK` response.
pub fn Endpoint::new(
params? : Array[Param] = [],
request_body? : Schema,
request_required? : Bool = true,
responses? : Array[ResponseSpec] = [],
) -> Endpoint {
{ params, request_body, request_required, responses, }
}
// -- OpenAPI emission for an endpoint -----------------------------------------
///|
/// One parameter as its OpenAPI parameter object for `version`. In Swagger 2.0 a
/// scalar type sits inline; in OpenAPI 3.x it lives under `schema`.
fn param_json(
p : Param,
defs : Map[String, Json],
version : OpenApiVersion,
) -> Json {
let m : Map[String, Json] = Map([
("name", p.key().to_json()),
("in", loc_str(p.loc).to_json()),
("required", p.is_required().to_json()),
])
if p.description != "" {
m["description"] = p.description.to_json()
}
match version {
// Swagger 2.0 puts a scalar's type and its constraint keywords on the
// parameter object itself — there is no `schema` to hang them off.
Swagger20 =>
match scalar_name(p.schema) {
Some(t) => {
m["type"] = t.to_json()
merge_into(m, param_facets(p, version))
}
None =>
m["schema"] = param_facets_on(
emit_schema(p.schema, defs, version),
p,
version,
)
}
_ =>
m["schema"] = param_facets_on(
emit_schema(p.schema, defs, version),
p,
version,
)
}
m.to_json()
}
///|
/// `j` with the parameter's constraints and default written into it.
fn param_facets_on(j : Json, p : Param, version : OpenApiVersion) -> Json {
let with_c = with_constraints(j, p.constraints, version)
match p.default {
Some(d) => with_key(with_c, "default", d)
None => with_c
}
}
///|
/// Just the keywords a parameter's constraints and default contribute, as a bare
/// object — for the Swagger 2.0 shape, which merges them into the parameter.
fn param_facets(p : Param, version : OpenApiVersion) -> Json {
let empty : Map[String, Json] = Map([])
param_facets_on(empty.to_json(), p, version)
}
///|
/// Attach the request body of an endpoint to an operation object `op`. OpenAPI
/// 3.x uses a `requestBody` with a media-type map; Swagger 2.0 uses an
/// `in: body` parameter, so it is appended to `op`'s `parameters`.
fn attach_request_body(
op : Map[String, Json],
body : Schema,
required : Bool,
defs : Map[String, Json],
version : OpenApiVersion,
) -> Unit {
match version {
Swagger20 => {
let bp : Map[String, Json] = Map([
("name", "body".to_json()),
("in", "body".to_json()),
("required", required.to_json()),
("schema", emit_schema(body, defs, version)),
])
let existing : Array[Json] = match op.get("parameters") {
Some(Array(a)) => a
_ => []
}
existing.push(bp.to_json())
op["parameters"] = existing.to_json()
}
_ => {
let media : Map[String, Json] = Map([
("schema", emit_schema(body, defs, version)),
])
let content : Map[String, Json] = Map([
("application/json", media.to_json()),
])
let rb : Map[String, Json] = Map([
("required", required.to_json()),
("content", content.to_json()),
])
op["requestBody"] = rb.to_json()
}
}
}
///|
/// One response as its OpenAPI response object. OpenAPI 3.x nests the body schema
/// under `content."application/json".schema`; Swagger 2.0 puts it directly under
/// `schema`.
fn response_json(
rs : ResponseSpec,
defs : Map[String, Json],
version : OpenApiVersion,
) -> Json {
let m : Map[String, Json] = Map([("description", rs.description.to_json())])
match rs.body {
Some(body) =>
match version {
Swagger20 => m["schema"] = emit_schema(body, defs, version)
_ => {
let media : Map[String, Json] = Map([
("schema", emit_schema(body, defs, version)),
])
let content : Map[String, Json] = Map([
("application/json", media.to_json()),
])
m["content"] = content.to_json()
}
}
None => ()
}
m.to_json()
}
///|
/// Fill the operation object `op` from an endpoint descriptor for `version`,
/// hoisting named schemas into `defs`. Emits `parameters`, `requestBody` (or a
/// body parameter in 2.0), and `responses`; an endpoint with no declared
/// responses gets a default `OK` under `ok_status`, which is the route's
/// `status_code` or `200`.
fn emit_endpoint(
op : Map[String, Json],
ep : Endpoint,
defs : Map[String, Json],
version : OpenApiVersion,
ok_status : Int,
) -> Unit {
let params : Array[Json] = []
for p in ep.params {
params.push(param_json(p, defs, version))
}
if params.length() > 0 {
op["parameters"] = params.to_json()
}
match ep.request_body {
Some(body) =>
attach_request_body(op, body, ep.request_required, defs, version)
None => ()
}
let specs = if ep.responses.length() > 0 {
ep.responses
} else {
[ResponseSpec::new(ok_status)]
}
let responses : Map[String, Json] = Map([])
for rs in specs {
responses[rs.status.to_string()] = response_json(rs, defs, version)
}
op["responses"] = responses.to_json()
}
///|
pub extend ParamLoc with Eq::{not_equal, equal}
///|
pub extend Param with Eq::{not_equal, equal}
///|
pub extend ResponseSpec with Eq::{not_equal, equal}
///|
pub extend Endpoint with Eq::{not_equal, equal}