Add SetSingleExample visitor for openapi 3.0
This commit is contained in:
parent
ca7c66eb08
commit
8f85178a7a
2 changed files with 29 additions and 0 deletions
|
@ -79,6 +79,9 @@ impl SchemaSettings {
|
|||
Box::new(ReplaceBoolSchemas {
|
||||
skip_additional_properties: true,
|
||||
}),
|
||||
Box::new(SetSingleExample {
|
||||
retain_examples: false,
|
||||
}),
|
||||
],
|
||||
_hidden: (),
|
||||
}
|
||||
|
|
|
@ -93,6 +93,7 @@ fn visit_single_or_vec<V: Visitor + ?Sized>(v: &mut V, target: &mut Option<Singl
|
|||
/// TODO document
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ReplaceBoolSchemas {
|
||||
/// TODO document
|
||||
pub skip_additional_properties: bool,
|
||||
}
|
||||
|
||||
|
@ -146,3 +147,28 @@ impl Visitor for RemoveRefSiblings {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO document
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SetSingleExample {
|
||||
/// TODO document
|
||||
pub retain_examples: bool,
|
||||
}
|
||||
|
||||
impl Visitor for SetSingleExample {
|
||||
fn visit_schema_object(&mut self, schema: &mut SchemaObject) {
|
||||
visit_schema_object(self, schema);
|
||||
|
||||
let first_example = schema.metadata.as_mut().and_then(|m| {
|
||||
if self.retain_examples {
|
||||
m.examples.first().cloned()
|
||||
} else {
|
||||
m.examples.drain(..).next()
|
||||
}
|
||||
});
|
||||
|
||||
if let Some(example) = first_example {
|
||||
schema.extensions.insert("example".to_owned(), example);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue