This repository has been archived by the owner on Jul 16, 2021. It is now read-only.
forked from softprops/openapi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
reference.rs
69 lines (64 loc) · 2.25 KB
/
reference.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
use crate::*;
use serde::{Deserialize, Serialize};
/// https://swagger.io/docs/specification/using-ref/
/// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#referenceObject
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(untagged)]
pub enum ReferenceOr<T> {
Reference {
#[serde(rename = "$ref")]
reference: String,
// $ref with sibling elements are not OpenAPI spec compliant
// https://github.com/ctaggart/autorust_openapi/issues/13
#[serde(skip_serializing_if = "Option::is_none")]
title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
// specifying "type" feels like a bug in the spec
#[serde(rename = "type", skip_serializing_if = "Option::is_none")]
type_: Option<DataType>,
#[serde(rename = "readOnly", skip_serializing_if = "Option::is_none")]
read_only: Option<bool>,
/// flattens client model property or parameter
/// https://github.com/Azure/autorest/blob/master/docs/extensions/readme.md#x-ms-client-flatten
#[serde(rename = "x-ms-client-flatten", skip_serializing_if = "Option::is_none")]
x_ms_client_flatten: Option<bool>,
},
Item(T),
}
#[cfg(test)]
mod tests {
use crate::{Parameter, ReferenceOr};
use serde_json;
#[test]
fn deserializes() {
let json = r#"{"$ref":"foo/bar"}"#;
assert_eq!(
serde_json::from_str::<ReferenceOr<Parameter>>(&json).unwrap(),
ReferenceOr::<Parameter>::Reference {
reference: "foo/bar".into(),
title: None,
description: None,
x_ms_client_flatten: None,
type_: None,
read_only: None
}
);
}
#[test]
fn serializes() {
let json = r#"{"$ref":"foo/bar"}"#;
assert_eq!(
json,
serde_json::to_string(&ReferenceOr::<Parameter>::Reference {
reference: "foo/bar".into(),
title: None,
description: None,
x_ms_client_flatten: None,
type_: None,
read_only: None
})
.unwrap()
);
}
}