sw4rm_rs/openapi_v3_0/
example.rs

1use std::collections::HashMap;
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4
5use crate::{
6    Spec,
7    reference::*,
8};
9
10/// Example Object
11#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
12#[serde(default, rename_all = "camelCase")]
13pub struct Example {
14    /// Short description for the example.
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub summary: Option<String>,
17    /// Long description for the example. CommonMark syntax MAY be used for rich text representation.
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub description: Option<String>,
20    /// Embedded literal example. The value field and externalValue field are mutually exclusive.
21    /// To represent examples of media types that cannot be naturally represented in JSON or YAML,
22    /// use a string value to contain the example, escaping where necessary.
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub value: Option<Value>,
25    /// A URL that points to the literal example. This provides the capability to reference
26    /// examples that cannot easily be included in JSON or YAML documents. The value field and
27    /// externalValue field are mutually exclusive.
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub external_value: Option<String>,
30
31    /// Allows extensions to the Swagger Schema. The field name MUST begin with x-, for example,
32    /// x-internal-id. The value can be null, a primitive, an array or an object. See Vendor
33    /// Extensions for further details.
34    #[serde(flatten, skip_serializing_if = "HashMap::is_empty")]
35    pub x_fields: HashMap<String, Value>,
36}
37
38impl Resolvable for Example {
39    fn resolve(spec: &Spec, path: &String) -> Result<Self, ResolveError> {
40        let path = path.clone();
41        let reference: Reference = path.clone().try_into().unwrap();
42
43        match reference.kind {
44            ReferenceType::Example => {
45                spec.components
46                    .as_ref()
47                    .ok_or_else(|| ResolveError::UnknownPathError(path.clone()))
48                    .and_then(|c| c.examples.get(&reference.name).ok_or_else(|| ResolveError::UnknownPathError(path)))
49                    .and_then(|p| p.resolve(spec))
50            },
51            _ => Err(ResolveError::UnknownPathError(path)),
52        }
53    }
54}