-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathschemas.rs
483 lines (430 loc) · 18.8 KB
/
schemas.rs
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use anyhow::bail;
use clients_schema::{
Body, Enum, Interface, LiteralValueValue, PropertiesBody, Property, Request, Response, TypeAlias,
TypeAliasVariants, TypeDefinition, TypeName, ValueOf,
};
use indexmap::IndexMap;
use openapiv3::{
AdditionalProperties, ArrayType, Discriminator, ExternalDocumentation, NumberType, ObjectType, ReferenceOr, Schema,
SchemaData, SchemaKind, StringType, Type,
};
use openapiv3::SchemaKind::AnyOf;
use crate::components::TypesAndComponents;
use crate::utils::{IntoSchema, ReferenceOrBoxed, SchemaName};
// A placeholder in components.schema to handle recursive types
const SCHEMA_PLACEHOLDER: ReferenceOr<Schema> = ReferenceOr::Reference {
reference: String::new(),
};
/// Convert `schema.json` type and value definitions to OpenAPI schemas:
///
/// The `convert_*` functions return a concrete schema and not a reference and do not store them in
/// the OpenAPI `components.schema`. This is the role of `for_type_name` hat creates and stores the
/// schema and returns a reference.
impl<'a> TypesAndComponents<'a> {
/// Convert a value. Returns a schema reference and not a concrete schema, as values can
/// be simple references to types.
pub fn convert_value_of(&mut self, value_of: &ValueOf) -> anyhow::Result<ReferenceOr<Schema>> {
Ok(match value_of {
// Instance_of
ValueOf::InstanceOf(instance) => {
// Do not care about generics, we work with an expanded schema
self.for_type_name(&instance.typ)?
}
// Array
ValueOf::ArrayOf(array) => ReferenceOr::Item(Schema {
schema_data: Default::default(),
schema_kind: SchemaKind::Type(Type::Array(ArrayType {
items: Some(self.convert_value_of(&array.value)?.boxed()),
min_items: None,
max_items: None,
unique_items: false,
})),
}),
// Union
ValueOf::UnionOf(union) => {
let mut items = Vec::new();
for item in &union.items {
items.push(self.convert_value_of(item)?)
}
ReferenceOr::Item(Schema {
schema_data: Default::default(),
schema_kind: SchemaKind::OneOf { one_of: items },
})
}
// Dictionary
// See https://swagger.io/docs/specification/data-models/dictionaries/
ValueOf::DictionaryOf(dict) => {
ObjectType {
properties: Default::default(),
required: vec![],
additional_properties: Some(AdditionalProperties::Schema(Box::new(
self.convert_value_of(&dict.value)?,
))),
// Single key dictionaries have exactly one property
min_properties: if dict.single_key { Some(1) } else { None },
max_properties: if dict.single_key { Some(1) } else { None },
}
.into_schema_ref()
}
// User defined value
ValueOf::UserDefinedValue(_) => {
ReferenceOr::Item(Schema {
schema_data: Default::default(),
// FIXME: not the right way to represent an arbitrary value
schema_kind: SchemaKind::Type(Type::Object(ObjectType::default())),
})
}
// Literal value
ValueOf::LiteralValue(literal) => {
let str_value = match &literal.value {
LiteralValueValue::String(s) => s.clone(),
LiteralValueValue::Number(n) => n.to_string(),
LiteralValueValue::Boolean(b) => b.to_string(),
};
ReferenceOr::Item(Schema {
// Note: the enclosing property will add "required: true"
schema_data: Default::default(),
schema_kind: SchemaKind::Type(Type::String(StringType {
format: Default::default(),
pattern: None,
enumeration: vec![Some(str_value)],
min_length: None,
max_length: None,
})),
})
}
})
}
/// Return the reference for a type name, registering it if needed
pub fn for_type_name(&mut self, type_name: &TypeName) -> anyhow::Result<ReferenceOr<Schema>> {
let schema_name = type_name.schema_name();
if self.components.schemas.contains_key(&schema_name) {
// Has already been processed
return Ok(type_name.schema_ref());
}
// Builtin types
if type_name.namespace == "_builtins" {
return match type_name.name.as_str() {
"string" => Ok(Type::String(StringType {
format: Default::default(),
pattern: None,
enumeration: vec![],
min_length: None,
max_length: None,
})
.into_schema_ref()),
"boolean" => Ok(Type::Boolean {}.into_schema_ref()),
"number" => Ok(Type::Number(NumberType::default()).into_schema_ref()),
"void" => {
// Empty object
Ok(ObjectType::default().into_schema_ref())
}
"null" => {
// Note that there is no null type; instead, the nullable attribute is used as a modifier of the
// base type. https://swagger.io/docs/specification/data-models/data-types/
// FIXME: null should be handled in unions by setting "nullable" to the resulting schema
Ok(Type::String(StringType {
format: Default::default(),
pattern: None,
enumeration: vec![],
min_length: None,
max_length: None,
})
.into_schema_ref_with_data_fn(|data| {
data.nullable = true;
}))
}
"binary" => {
// FIXME: must be handled in requests and responses
Ok(ObjectType::default().into_schema_ref())
}
_ => bail!("unknown builtin type: {}", type_name),
};
}
if type_name.namespace == "_types" {
match type_name.name.as_str() {
"double" | "long" | "integer" | "float" => {
return Ok(Type::Number(NumberType::default()).into_schema_ref());
}
_ => {}
}
}
// Store a placeholder, it will avoid infinite loops with recursive types
self.components.schemas.insert(schema_name, SCHEMA_PLACEHOLDER);
let typedef = self.model.get_type(type_name)?;
use TypeDefinition::*;
let schema = match typedef {
// Request and response may not have a body (and a schema) and so have dedicated methods below
Request(_) => bail!("Requests should be handled using for_request"),
Response(_) => bail!("Responses should be handled using for_request"),
Enum(enumm) => self.convert_enum(enumm)?.into_schema_ref(),
Interface(itf) => self.convert_interface_definition(itf)?.into_schema_ref(),
TypeAlias(alias) => self.convert_type_alias(alias)?.into_schema_ref(),
};
Ok(self.add_schema(type_name, schema))
}
// Returns the schema, if any, for a request body
pub fn convert_request(&mut self, request: &Request) -> anyhow::Result<Option<ReferenceOr<Schema>>> {
self.for_body(&request.body)
}
pub fn convert_response(&mut self, response: &Response) -> anyhow::Result<Option<ReferenceOr<Schema>>> {
self.for_body(&response.body)
}
pub fn convert_external_docs(&self, obj: &impl clients_schema::Documented) -> Option<ExternalDocumentation> {
// FIXME: does the model contain resolved doc_id?
obj.doc_url().map(|url| {
let branch: &str = self
.model
.info
.as_ref()
.and_then(|i| i.version.as_deref())
.unwrap_or("current");
ExternalDocumentation {
description: None,
url: url.trim().replace("{branch}", branch),
extensions: Default::default(),
}
})
}
fn for_body(&mut self, body: &Body) -> anyhow::Result<Option<ReferenceOr<Schema>>> {
let result = match body {
Body::NoBody(_) => None,
Body::Value(value_body) => Some(self.convert_value_of(&value_body.value)?), // TODO codegen_name?
Body::Properties(PropertiesBody { properties }) => Some(
ObjectType {
properties: self.convert_properties(properties.iter())?,
required: self.required_properties(properties.iter()),
additional_properties: None,
min_properties: None,
max_properties: None,
}
.into_schema_ref(),
),
};
Ok(result)
}
fn convert_property(&mut self, prop: &Property) -> anyhow::Result<ReferenceOr<Schema>> {
let mut result = self.convert_value_of(&prop.typ)?;
// TODO: how can we just wrap a reference so that we can add docs?
if let ReferenceOr::Item(ref mut schema) = &mut result {
self.fill_data_with_prop(&mut schema.schema_data, prop);
}
Ok(result)
}
fn convert_properties<'b>(
&mut self,
props: impl Iterator<Item = &'b Property>,
) -> anyhow::Result<IndexMap<String, ReferenceOr<Box<Schema>>>> {
let mut result = IndexMap::new();
for prop in props {
result.insert(prop.name.clone(), self.convert_property(prop)?.boxed());
}
Ok(result)
}
fn required_properties<'b>(&mut self, props: impl Iterator<Item = &'b Property>) -> Vec<String> {
props
.filter(|&prop| prop.required).map(|prop| prop.name.clone())
.collect()
}
/// Convert an interface definition into a schema
fn convert_interface_definition(&mut self, itf: &Interface) -> anyhow::Result<Schema> {
if !itf.generics.is_empty() {
bail!(
"Interface definition {} has generic parameters. Expand generics before conversion",
itf.base.name
);
}
let mut schema = if let Some(container) = &itf.variants {
// TODO: interface definition container.non_exhaustive
let _non_exhaustive = container.non_exhaustive;
// Split container properties and variants
let container_props = itf
.properties
.iter()
.filter(|p| p.container_property)
.collect::<Vec<_>>();
let variant_props = itf
.properties
.iter()
.filter(|p| !p.container_property)
.collect::<Vec<_>>();
// A container is represented by an object will all optional properties and exactly one that
// needs to be set.
let mut schema = ObjectType {
properties: self.convert_properties(variant_props.iter().copied())?,
required: vec![],
additional_properties: None,
min_properties: Some(1),
max_properties: Some(1),
}
.into_schema();
if !container_props.is_empty() {
// Create a schema for the container property, and group it in an "allOf" with variants
let container_props_schema = ObjectType {
properties: self.convert_properties(container_props.iter().copied())?,
required: self.required_properties(container_props.iter().copied()),
additional_properties: None,
min_properties: None,
max_properties: None,
}
.into_schema_ref();
schema = SchemaKind::AllOf {
all_of: vec![container_props_schema, schema.into_schema_ref()],
}
.into_schema();
}
self.fill_schema_with_base(&mut schema, &itf.base);
schema
} else {
let schema = ObjectType {
properties: self.convert_properties(itf.properties.iter())?,
required: self.required_properties(itf.properties.iter()),
additional_properties: None,
min_properties: None,
max_properties: None,
}
.into_schema();
schema
};
// Inheritance
if let Some(inherit) = &itf.inherits {
schema = SchemaKind::AllOf {
all_of: vec![self.for_type_name(&inherit.typ)?, schema.into_schema_ref()],
}
.into_schema();
}
// Behaviors
for bh in &itf.implements {
match bh.typ.name.as_str() {
name @ ("AdditionalProperty" | "AdditionalProperties") => {
let single = name == "AdditionalProperty";
let value_schema = self.convert_value_of(&bh.generics[1])?;
schema = ObjectType {
properties: Default::default(),
required: vec![],
additional_properties: Some(AdditionalProperties::Schema(Box::new(value_schema))),
min_properties: if single { Some(1) } else { None },
max_properties: if single { Some(1) } else { None },
}
.into_schema();
}
_ => bail!("Unknown behavior {}", &bh.typ),
}
}
Ok(schema)
}
/// Creates alias an alias that references another type.
fn convert_type_alias(&mut self, alias: &TypeAlias) -> anyhow::Result<Schema> {
if !alias.generics.is_empty() {
bail!(
"Type alias {} has generic parameters. Expand generics before conversion",
alias.base.name
);
}
let mut schema = self
.convert_value_of(&alias.typ)?
.into_schema_with_base(self, &alias.base);
match &alias.variants {
None => {}
Some(TypeAliasVariants::ExternalTag(_tag)) => {
// TODO: typed-keys: add an extension to identify it?
}
Some(TypeAliasVariants::InternalTag(tag)) => {
// TODO: add tag.default_tag as an extension
schema.schema_data.discriminator = Some(Discriminator {
property_name: tag.tag.clone(),
mapping: Default::default(),
extensions: Default::default(),
});
}
Some(TypeAliasVariants::Untagged(_tag)) => {
}
};
Ok(schema)
}
/// Register an enumeration and return the schema reference.
fn convert_enum(&mut self, enumm: &Enum) -> anyhow::Result<Schema> {
// Collect all members and their aliases
let enum_values = enumm.members.iter()
.flat_map(|m|
std::iter::once(m.name.clone())
.chain(m.aliases.iter().cloned())
)
.map(Some) // enumeration below is a Vec<Option<String>>
.collect::<Vec<_>>();
let result = StringType {
format: Default::default(),
pattern: None,
enumeration: enum_values,
min_length: None,
max_length: None,
};
// Open enumeration: keep the value list for reference and also allow any string
let mut schema = if enumm.is_open {
AnyOf {
any_of: vec![
result.into_schema_ref(),
StringType::default().into_schema_ref(),
]
}.into_schema()
} else {
result.into_schema()
};
self.fill_data_with_base(&mut schema.schema_data, &enumm.base);
Ok(schema)
}
fn fill_schema_with_base(&self, schema: &mut Schema, base: &clients_schema::BaseType) {
self.fill_data_with_base(&mut schema.schema_data, base);
}
pub fn fill_data_with_base(&self, data: &mut SchemaData, base: &clients_schema::BaseType) {
// SchemaData {
// nullable: false,
// read_only: false,
// write_only: false,
// deprecated: false,
// external_docs: Default::default(),
// example: None,
// title: None,
// description: base.description.clone(),
// discriminator: None,
// default: None,
// extensions: Default::default(),
// }
data.external_docs = self.convert_external_docs(base);
data.deprecated = base.deprecation.is_some();
data.description = base.description.clone();
// TODO: base.deprecation as extension
// TODO: base.spec_location as extension?
// TODO: base.doc_id as extension
// TODO: base.variant_name as extension? (used for external_variants)
// TODO: base.codegen_names as extension?
}
fn fill_data_with_prop(&self, data: &mut SchemaData, prop: &Property) {
data.external_docs = self.convert_external_docs(prop);
data.deprecated = prop.deprecation.is_some();
data.description = prop.description.clone();
// TODO: prop.aliases as extensions
// TODO: prop.server_default as extension
// TODO: prop.availability as extension
// TODO: prop.doc_id as extension (new representation of since and stability)
// TODO: prop.es_quirk as extension?
// TODO: prop.codegen_name as extension?
// TODO: prop.deprecation as extension
}
}