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
use std::collections::{BTreeMap, BTreeSet};
use schemars::{
schema::{InstanceType, RootSchema, SingleOrVec},
JsonSchema,
};
use thiserror::Error;
pub use cosmwasm_schema_derive::QueryResponses;
pub trait QueryResponses: JsonSchema {
fn response_schemas() -> Result<BTreeMap<String, RootSchema>, IntegrityError> {
let response_schemas = Self::response_schemas_impl();
let queries: BTreeSet<_> = response_schemas.keys().cloned().collect();
check_api_integrity::<Self>(queries)?;
Ok(response_schemas)
}
fn response_schemas_impl() -> BTreeMap<String, RootSchema>;
}
fn check_api_integrity<T: QueryResponses + ?Sized>(
generated_queries: BTreeSet<String>,
) -> Result<(), IntegrityError> {
let schema = crate::schema_for!(T);
let schema_queries: BTreeSet<_> = match schema.schema.subschemas {
Some(subschemas) => subschemas
.one_of
.ok_or(IntegrityError::InvalidQueryMsgSchema)?
.into_iter()
.map(|s| {
let s = s.into_object();
if let Some(SingleOrVec::Single(ty)) = s.instance_type {
match *ty {
InstanceType::Object => s
.object
.ok_or(IntegrityError::InvalidQueryMsgSchema)?
.required
.into_iter()
.next()
.ok_or(IntegrityError::InvalidQueryMsgSchema),
InstanceType::String => {
let values =
s.enum_values.ok_or(IntegrityError::InvalidQueryMsgSchema)?;
if values.len() != 1 {
return Err(IntegrityError::InvalidQueryMsgSchema);
}
values[0]
.as_str()
.map(String::from)
.ok_or(IntegrityError::InvalidQueryMsgSchema)
}
_ => Err(IntegrityError::InvalidQueryMsgSchema),
}
} else {
Err(IntegrityError::InvalidQueryMsgSchema)
}
})
.collect::<Result<_, _>>()?,
None => BTreeSet::new(),
};
if schema_queries != generated_queries {
return Err(IntegrityError::InconsistentQueries {
query_msg: schema_queries,
responses: generated_queries,
});
}
Ok(())
}
#[derive(Debug, Error, PartialEq, Eq)]
pub enum IntegrityError {
#[error("the structure of the QueryMsg schema was unexpected")]
InvalidQueryMsgSchema,
#[error(
"inconsistent queries - QueryMsg schema has {query_msg:?}, but query responses have {responses:?}"
)]
InconsistentQueries {
query_msg: BTreeSet<String>,
responses: BTreeSet<String>,
},
}
#[cfg(test)]
mod tests {
use schemars::schema_for;
use super::*;
#[derive(Debug, JsonSchema)]
#[serde(rename_all = "snake_case")]
#[allow(dead_code)]
pub enum GoodMsg {
BalanceFor { account: String },
AccountIdFor(String),
Supply {},
Liquidity,
AccountCount(),
}
impl QueryResponses for GoodMsg {
fn response_schemas_impl() -> BTreeMap<String, RootSchema> {
BTreeMap::from([
("balance_for".to_string(), schema_for!(u128)),
("account_id_for".to_string(), schema_for!(u128)),
("supply".to_string(), schema_for!(u128)),
("liquidity".to_string(), schema_for!(u128)),
("account_count".to_string(), schema_for!(u128)),
])
}
}
#[test]
fn good_msg_works() {
let response_schemas = GoodMsg::response_schemas().unwrap();
assert_eq!(
response_schemas,
BTreeMap::from([
("balance_for".to_string(), schema_for!(u128)),
("account_id_for".to_string(), schema_for!(u128)),
("supply".to_string(), schema_for!(u128)),
("liquidity".to_string(), schema_for!(u128)),
("account_count".to_string(), schema_for!(u128))
])
);
}
#[derive(Debug, JsonSchema)]
#[serde(rename_all = "snake_case")]
#[allow(dead_code)]
pub enum EmptyMsg {}
impl QueryResponses for EmptyMsg {
fn response_schemas_impl() -> BTreeMap<String, RootSchema> {
BTreeMap::from([])
}
}
#[test]
fn empty_msg_works() {
let response_schemas = EmptyMsg::response_schemas().unwrap();
assert_eq!(response_schemas, BTreeMap::from([]));
}
#[derive(Debug, JsonSchema)]
#[serde(rename_all = "kebab-case")]
#[allow(dead_code)]
pub enum BadMsg {
BalanceFor { account: String },
}
impl QueryResponses for BadMsg {
fn response_schemas_impl() -> BTreeMap<String, RootSchema> {
BTreeMap::from([("balance_for".to_string(), schema_for!(u128))])
}
}
#[test]
fn bad_msg_fails() {
let err = BadMsg::response_schemas().unwrap_err();
assert_eq!(
err,
IntegrityError::InconsistentQueries {
query_msg: BTreeSet::from(["balance-for".to_string()]),
responses: BTreeSet::from(["balance_for".to_string()])
}
);
}
}