-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathschema_impl.rs
More file actions
386 lines (349 loc) · 12.6 KB
/
schema_impl.rs
File metadata and controls
386 lines (349 loc) · 12.6 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//! Schema derive macro implementation.
//!
//! This module implements the `#[derive(Schema)]` derive macro that registers
//! types for `OpenAPI` schema generation.
//!
//! # Overview
//!
//! The `#[derive(Schema)]` macro registers a struct or enum for inclusion in the `OpenAPI` spec.
//! It stores metadata about the type which is later used by the `vespera!` macro to generate
//! the `OpenAPI` components/schemas section.
//!
//! # Global Schema Storage
//!
//! This module uses a global [`SCHEMA_STORAGE`] mutex to collect all schema types across
//! a crate at compile time. This is necessary because proc-macros are invoked independently,
//! so we need a shared location to gather all types before generating the final `OpenAPI` spec.
//!
//! # Custom Schema Names
//!
//! By default, the `OpenAPI` schema name matches the struct name. You can customize it:
//!
//! ```ignore
//! #[derive(Schema)]
//! #[schema(name = "CustomSchemaName")]
//! pub struct MyType { ... }
//! ```
//!
//! # Key Functions
//!
//! - [`extract_schema_name_attr`] - Extract custom name from `#[schema]` attribute
//! - [`process_derive_schema`] - Process the derive macro input and register the type
use std::{
collections::HashMap,
sync::{LazyLock, Mutex},
};
use crate::metadata::StructMetadata;
pub static SCHEMA_STORAGE: LazyLock<Mutex<HashMap<String, StructMetadata>>> =
LazyLock::new(|| Mutex::new(HashMap::new()));
/// Extract custom schema name from #[schema(name = "...")] attribute
pub fn extract_schema_name_attr(attrs: &[syn::Attribute]) -> Option<String> {
for attr in attrs {
if attr.path().is_ident("schema") {
let mut custom_name = None;
let _ = attr.parse_nested_meta(|meta| {
if meta.path.is_ident("name") {
let value = meta.value()?;
let lit: syn::LitStr = value.parse()?;
custom_name = Some(lit.value());
}
Ok(())
});
if custom_name.is_some() {
return custom_name;
}
}
}
None
}
/// Process derive input and return metadata + expanded code
pub fn process_derive_schema(
input: &syn::DeriveInput,
) -> (StructMetadata, proc_macro2::TokenStream) {
let name = &input.ident;
// Check for custom schema name from #[schema(name = "...")] attribute
let schema_name = extract_schema_name_attr(&input.attrs).unwrap_or_else(|| name.to_string());
// Schema-derived types appear in OpenAPI spec (include_in_openapi: true)
let metadata = StructMetadata::new(schema_name, quote::quote!(#input).to_string());
(metadata, proc_macro2::TokenStream::new())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_process_derive_schema_struct() {
let input: syn::DeriveInput = syn::parse_quote! {
struct User {
name: String,
age: u32,
}
};
let (metadata, _expanded) = process_derive_schema(&input);
assert_eq!(metadata.name, "User");
assert!(metadata.definition.contains("struct User"));
}
#[test]
fn test_process_derive_schema_enum() {
let input: syn::DeriveInput = syn::parse_quote! {
enum Status {
Active,
Inactive,
}
};
let (metadata, _expanded) = process_derive_schema(&input);
assert_eq!(metadata.name, "Status");
assert!(metadata.definition.contains("enum Status"));
}
#[test]
fn test_process_derive_schema_generic() {
let input: syn::DeriveInput = syn::parse_quote! {
struct Container<T> {
value: T,
}
};
let (metadata, _expanded) = process_derive_schema(&input);
assert_eq!(metadata.name, "Container");
}
#[test]
fn test_extract_schema_name_attr_with_name() {
let attrs: Vec<syn::Attribute> = syn::parse_quote! {
#[schema(name = "CustomName")]
};
let result = extract_schema_name_attr(&attrs);
assert_eq!(result, Some("CustomName".to_string()));
}
#[test]
fn test_extract_schema_name_attr_without_name() {
let attrs: Vec<syn::Attribute> = syn::parse_quote! {
#[derive(Debug)]
};
let result = extract_schema_name_attr(&attrs);
assert_eq!(result, None);
}
#[test]
fn test_extract_schema_name_attr_empty_schema() {
let attrs: Vec<syn::Attribute> = syn::parse_quote! {
#[schema]
};
let result = extract_schema_name_attr(&attrs);
assert_eq!(result, None);
}
#[test]
fn test_extract_schema_name_attr_with_other_attrs() {
let attrs: Vec<syn::Attribute> = syn::parse_quote! {
#[derive(Clone)]
#[schema(name = "MySchema")]
#[serde(rename_all = "camelCase")]
};
let result = extract_schema_name_attr(&attrs);
assert_eq!(result, Some("MySchema".to_string()));
}
#[test]
fn test_process_derive_schema_simple() {
let input: syn::DeriveInput = syn::parse_quote! {
struct User {
id: i32,
name: String,
}
};
let (metadata, _tokens) = process_derive_schema(&input);
assert_eq!(metadata.name, "User");
assert!(metadata.definition.contains("User"));
}
#[test]
fn test_process_derive_schema_with_custom_name() {
let input: syn::DeriveInput = syn::parse_quote! {
#[schema(name = "CustomUserSchema")]
struct User {
id: i32,
}
};
let (metadata, _) = process_derive_schema(&input);
assert_eq!(metadata.name, "CustomUserSchema");
}
#[test]
fn test_process_derive_schema_with_generics() {
let input: syn::DeriveInput = syn::parse_quote! {
struct Container<T> {
value: T,
}
};
let (metadata, _tokens) = process_derive_schema(&input);
assert_eq!(metadata.name, "Container");
}
#[test]
fn test_extract_schema_name_attr_non_name_meta_key() {
// #[schema(other = "foo")] — has schema attr but no "name" key
let attrs: Vec<syn::Attribute> = syn::parse_quote! {
#[schema(other = "foo")]
};
let result = extract_schema_name_attr(&attrs);
assert_eq!(result, None);
}
#[test]
fn test_extract_schema_name_attr_multiple_schema_attrs() {
// Two #[schema] attrs — first one with name wins
let attrs: Vec<syn::Attribute> = syn::parse_quote! {
#[schema(name = "First")]
#[schema(name = "Second")]
};
let result = extract_schema_name_attr(&attrs);
assert_eq!(result, Some("First".to_string()));
}
#[test]
fn test_extract_schema_name_attr_schema_with_unknown_key_value() {
// #[schema(other = "x", name = "MyName")] — parse_nested_meta bails on unhandled
// key=value (other = "x") since the value isn't consumed. Error is silently ignored.
let attrs: Vec<syn::Attribute> = syn::parse_quote! {
#[schema(other = "x", name = "MyName")]
};
let result = extract_schema_name_attr(&attrs);
// parse_nested_meta fails at `other = "x"` (value not consumed), so `name` is never reached
assert_eq!(result, None);
}
#[test]
fn test_extract_schema_name_attr_name_before_unknown() {
// name comes FIRST, so it's extracted before the unknown key causes a bail
let attrs: Vec<syn::Attribute> = syn::parse_quote! {
#[schema(name = "Found", other = "x")]
};
let result = extract_schema_name_attr(&attrs);
// name is parsed successfully; parse_nested_meta may error on `other` but name is already set
assert_eq!(result, Some("Found".to_string()));
}
// ========== Coverage: process_derive_schema struct variants ==========
#[test]
fn test_process_derive_schema_unit_struct() {
let input: syn::DeriveInput = syn::parse_quote! {
struct Unit;
};
let (metadata, tokens) = process_derive_schema(&input);
assert_eq!(metadata.name, "Unit");
assert!(metadata.definition.contains("Unit"));
assert!(tokens.is_empty(), "Token stream should be empty");
}
#[test]
fn test_process_derive_schema_tuple_struct() {
let input: syn::DeriveInput = syn::parse_quote! {
struct Pair(i32, String);
};
let (metadata, tokens) = process_derive_schema(&input);
assert_eq!(metadata.name, "Pair");
assert!(metadata.definition.contains("Pair"));
assert!(tokens.is_empty());
}
#[test]
fn test_process_derive_schema_empty_struct() {
let input: syn::DeriveInput = syn::parse_quote! {
struct Empty {}
};
let (metadata, _) = process_derive_schema(&input);
assert_eq!(metadata.name, "Empty");
}
#[test]
fn test_process_derive_schema_with_lifetime() {
let input: syn::DeriveInput = syn::parse_quote! {
struct Ref<'a> {
data: &'a str,
}
};
let (metadata, _) = process_derive_schema(&input);
assert_eq!(metadata.name, "Ref");
}
#[test]
fn test_process_derive_schema_with_serde_attrs() {
let input: syn::DeriveInput = syn::parse_quote! {
#[serde(rename_all = "camelCase")]
struct UserResponse {
user_name: String,
#[serde(skip)]
internal_id: u64,
}
};
let (metadata, _) = process_derive_schema(&input);
assert_eq!(metadata.name, "UserResponse");
assert!(metadata.definition.contains("camelCase"));
assert!(metadata.definition.contains("skip"));
}
// ========== Coverage: metadata field verification ==========
#[test]
fn test_process_derive_schema_include_in_openapi_true() {
let input: syn::DeriveInput = syn::parse_quote! {
struct Visible { x: i32 }
};
let (metadata, _) = process_derive_schema(&input);
assert!(
metadata.include_in_openapi,
"Schema-derived types must have include_in_openapi=true"
);
}
#[test]
fn test_process_derive_schema_definition_contains_fields() {
let input: syn::DeriveInput = syn::parse_quote! {
struct WithFields {
id: u64,
name: String,
active: bool,
}
};
let (metadata, _) = process_derive_schema(&input);
assert!(metadata.definition.contains("id"));
assert!(metadata.definition.contains("u64"));
assert!(metadata.definition.contains("name"));
assert!(metadata.definition.contains("active"));
assert!(metadata.definition.contains("bool"));
}
// ========== Coverage: SCHEMA_STORAGE direct usage ==========
#[test]
fn test_schema_storage_insert_and_get() {
let storage = SCHEMA_STORAGE.lock().unwrap();
let key = "__test_coverage_type__".to_string();
// Clean up if previous test left data
drop(storage);
{
let mut storage = SCHEMA_STORAGE.lock().unwrap();
storage.insert(
key.clone(),
StructMetadata::new(key.clone(), "struct __test_coverage_type__ {}".to_string()),
);
}
{
let storage = SCHEMA_STORAGE.lock().unwrap();
let meta = storage.get(&key);
assert!(meta.is_some(), "Inserted metadata should be retrievable");
let meta = meta.unwrap();
assert_eq!(meta.name, key);
assert!(meta.include_in_openapi);
}
// Cleanup
{
let mut storage = SCHEMA_STORAGE.lock().unwrap();
storage.remove(&key);
}
}
#[test]
fn test_schema_storage_overwrite() {
let key = "__test_overwrite_type__".to_string();
{
let mut storage = SCHEMA_STORAGE.lock().unwrap();
storage.insert(
key.clone(),
StructMetadata::new(key.clone(), "struct V1 {}".to_string()),
);
storage.insert(
key.clone(),
StructMetadata::new(key.clone(), "struct V2 {}".to_string()),
);
}
{
let storage = SCHEMA_STORAGE.lock().unwrap();
let meta = storage.get(&key).unwrap();
assert!(meta.definition.contains("V2"), "Last insert should win");
}
// Cleanup
{
let mut storage = SCHEMA_STORAGE.lock().unwrap();
storage.remove(&key);
}
}
}