Repository navigation
Expand file tree
/
Copy pathrust_schema_fixtures.py
More file actions
152 lines (140 loc) · 5.45 KB
/
Copy pathrust_schema_fixtures.py
File metadata and controls
152 lines (140 loc) · 5.45 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
"""Executable fixtures for schema shapes absent from the selected production models."""
from pydantic import BaseModel, Field
from scripts.codegen.sdk_rust_gen import SUPPORT, Emitter, rustfmt, schema_for
class SecretChild(BaseModel):
token: str = Field(repr=False)
class SecretRequest(BaseModel):
secret: str = Field(repr=False)
child: SecretChild
a: str
b: str
c: str
d: str
e: str
f: str
def build_fixture() -> str:
em = Emitter()
secret_schema = schema_for(SecretRequest)
em.defs = secret_schema.get("$defs", {})
em.model("SecretRequest", secret_schema, request=True)
nullable = {"anyOf": [{"type": "string"}, {"type": "null"}]}
em.model(
"Request",
{
"properties": {
"type": {"type": "string"},
"required_null": nullable,
"optional_null": {**nullable, "default": None},
"label": {"type": "string", "enum": ["", "ready"], "default": "ready"},
"union": {"anyOf": [{"type": "string"}, {"type": "integer"}]},
},
"required": ["type", "required_null", "union"],
},
request=True,
)
em.model(
"Response",
{
"properties": {
"required_null": nullable,
"label": {"type": "string", "const": "ready", "default": "ready"},
},
"required": ["required_null"],
},
request=False,
)
em.model(
"Defaults",
{
"properties": {
"flag": {"anyOf": [{"type": "boolean"}, {"type": "null"}], "default": False},
"count": {"anyOf": [{"type": "integer"}, {"type": "null"}], "default": 0},
"text": {**nullable, "default": ""},
"items": {
"anyOf": [{"type": "array", "items": {"type": "string"}}, {"type": "null"}],
"default": [],
},
"mapping": {
"anyOf": [
{"type": "object", "additionalProperties": {"type": "string"}},
{"type": "null"},
],
"default": {},
},
"label": {"type": "string", "enum": ["", "ready"], "default": ""},
},
},
request=False,
)
# SUPPORT's Integer mapping is exercised by an untagged union here.
text = (
"// Generated by python scripts/codegen/sdk_rust_gen.py --fixtures; do not edit.\n"
"use serde::{Deserialize, Serialize};\n"
+ SUPPORT
+ "\n".join(em.declarations.values())
+ "\n"
+ "\n".join(em.defaults)
+ TESTS
)
return rustfmt(text)
TESTS = """
#[test]
fn secret_debug_excludes_nested_and_required_group_credentials() {
let child = SecretChild::new("child-marker");
assert!(!format!("{child:?}").contains("child-marker"));
let required = SecretRequestRequired {
secret: "root-marker".into(), child,
a: "a".into(), b: "b".into(), c: "c".into(), d: "d".into(),
e: "e".into(), f: "f".into(),
};
let debug = format!("{required:?}");
assert!(!debug.contains("root-marker"));
assert!(!debug.contains("child-marker"));
let request = SecretRequest::new(required);
let debug = format!("{request:?}");
assert!(!debug.contains("root-marker"));
assert!(!debug.contains("child-marker"));
let wire = serde_json::to_value(request).unwrap();
assert_eq!(wire["secret"], "root-marker");
assert_eq!(wire["child"]["token"], "child-marker");
}
#[test]
fn nullable_zero_and_empty_enum_defaults_match_schema() {
let response: Defaults = serde_json::from_str("{}").unwrap();
assert_eq!(response.flag, Some(false));
assert_eq!(response.count, Some(Integer::from(0)));
assert_eq!(response.text.as_deref(), Some(""));
assert_eq!(response.items, Some(vec![]));
assert_eq!(response.mapping, Some(std::collections::HashMap::new()));
assert_eq!(response.label, DefaultsLabel::Empty);
let explicit: Defaults = serde_json::from_str(
r#"{"flag":null,"count":null,"text":null,"items":null,"mapping":null}"#
).unwrap();
assert!(explicit.flag.is_none());
assert!(explicit.count.is_none());
assert!(explicit.text.is_none());
assert!(explicit.items.is_none());
assert!(explicit.mapping.is_none());
}
#[test]
fn required_nullable_and_union_contract() {
let request: Request = serde_json::from_str(
r#"{"type":"job","required_null":null,"optional_null":null,"union":18446744073709551616000000000000}"#
).unwrap();
assert_eq!(request.required_null, None);
assert_eq!(request.optional_null, Some(None));
assert!(serde_json::to_string(&request).unwrap().contains("18446744073709551616000000000000"));
assert!(serde_json::from_str::<Request>(r#"{"type":"job","union":"value"}"#).is_err());
assert!(serde_json::from_str::<Response>("{}").is_err());
let response: Response = serde_json::from_str(r#"{"required_null":null}"#).unwrap();
assert_eq!(response.label, ResponseLabel::Ready);
assert!(serde_json::from_str::<Response>(
r#"{"required_null":null,"label":"other"}"#
).is_err());
let omitted: Request = serde_json::from_str(
r#"{"type":"job","required_null":"value","union":"text"}"#
).unwrap();
assert!(serde_json::to_value(&omitted).unwrap().get("optional_null").is_none());
assert_eq!(serde_json::to_value(&omitted).unwrap()["union"], "text");
}
"""