schemars_derive 1.2.1

Macros for #[derive(JsonSchema)], for use with schemars
Documentation
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
mod custom_meta;
mod doc;
mod parse_meta;
mod schemars_to_serde;
mod validation;

use parse_meta::{
    parse_extensions, parse_name_value_expr, parse_name_value_lit_str, require_name_value_lit_str,
    require_path_only,
};
use proc_macro2::TokenStream;
use quote::ToTokens;
use serde_derive_internals::Ctxt;
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::{Attribute, Expr, ExprLit, Ident, Lit, LitStr, Path, Type};
use validation::ValidationAttrs;

use crate::ast::Data;
use crate::idents::SCHEMA;
use crate::schema_exprs::SchemaExpr;

pub use custom_meta::*;
pub use schemars_to_serde::process_serde_attrs;

#[derive(Default)]
pub struct CommonAttrs {
    pub doc: Option<Expr>,
    pub deprecated: bool,
    pub title: Option<Expr>,
    pub description: Option<Expr>,
    pub examples: Vec<Expr>,
    pub extensions: Vec<(String, TokenStream)>,
    pub transforms: Vec<Expr>,
}

#[derive(Default)]
pub struct FieldAttrs {
    pub common: CommonAttrs,
    pub with: Option<WithAttr>,
    pub validation: ValidationAttrs,
}

#[derive(Default)]
pub struct ContainerAttrs {
    pub common: CommonAttrs,
    pub repr: Option<Type>,
    pub crate_name: Option<Path>,
    // The actual parsing of this is done in `get_rename_format_type_params()`,
    // because it depends on the type's generic params.
    pub rename_format_string: Option<LitStr>,
    pub inline: bool,
    pub ref_variants: bool,
    pub with: Option<WithAttr>,
}

#[derive(Default)]
pub struct VariantAttrs {
    pub common: CommonAttrs,
    pub with: Option<WithAttr>,
}

pub enum WithAttr {
    Type(Type),
    Function(Path),
}

impl CommonAttrs {
    fn populate(
        &mut self,
        attrs: &[Attribute],
        schemars_cx: &mut AttrCtxt,
        serde_cx: &mut AttrCtxt,
    ) {
        self.process_attr(schemars_cx);
        self.process_attr(serde_cx);

        self.doc = doc::get_doc(attrs);
        self.deprecated = attrs.iter().any(|a| a.path().is_ident("deprecated"));
    }

    fn process_attr(&mut self, cx: &mut AttrCtxt) {
        cx.parse_meta(|m, n, c| self.process_meta(m, n, c));
    }

    fn process_meta(
        &mut self,
        meta: CustomMeta,
        meta_name: &str,
        cx: &AttrCtxt,
    ) -> Result<(), CustomMeta> {
        match meta_name {
            "title" => match self.title {
                Some(_) => cx.duplicate_error(&meta),
                None => self.title = parse_name_value_expr(meta, cx).ok(),
            },

            "description" => match self.description {
                Some(_) => cx.duplicate_error(&meta),
                None => self.description = parse_name_value_expr(meta, cx).ok(),
            },

            "example" => {
                if let Ok(expr) = parse_name_value_expr(meta, cx) {
                    if let Expr::Lit(ExprLit {
                        lit: Lit::Str(lit_str),
                        ..
                    }) = &expr
                    {
                        if lit_str.parse::<Path>().is_ok() {
                            let lit_str_value = lit_str.value();
                            cx.error_spanned_by(&expr, format_args!(
                                "`example` value must be an expression, and string literals that may be interpreted as function paths are currently disallowed to avoid migration errors \
                                 (this restriction may be relaxed in a future version of schemars).\n\
                                If you want to use the result of a function, use `#[schemars(example = {lit_str_value}())]`.\n\
                                Or to use the string literal value, use `#[schemars(example = &\"{lit_str_value}\")]`."));
                        }
                    }

                    self.examples.push(expr);
                }
            }

            "extend" => {
                for ex in parse_extensions(&meta, cx).into_iter().flatten() {
                    // This is O(n^2) but should be fine with the typically small number of
                    // extensions. If this does become a problem, it can be changed to use
                    // IndexMap, or a separate Map with cloned keys.
                    if self.extensions.iter().any(|e| e.0 == ex.key_str) {
                        cx.error_spanned_by(
                            ex.key_lit,
                            format_args!("Duplicate extension key '{}'", ex.key_str),
                        );
                    } else {
                        self.extensions.push((ex.key_str, ex.value));
                    }
                }
            }

            "transform" => {
                if let Ok(expr) = parse_name_value_expr(meta, cx) {
                    if let Expr::Lit(ExprLit {
                        lit: Lit::Str(lit_str),
                        ..
                    }) = &expr
                    {
                        if lit_str.parse::<Expr>().is_ok() {
                            cx.error_spanned_by(
                                &expr,
                                format_args!(
                                    "Expected a `fn(&mut Schema)` or other value implementing `schemars::transform::Transform`, found `&str`.\nDid you mean `#[schemars(transform = {})]`?",
                                    lit_str.value()
                                ),
                            );
                        }
                    } else {
                        self.transforms.push(expr);
                    }
                }
            }

            _ => return Err(meta),
        }

        Ok(())
    }

    pub fn is_default(&self) -> bool {
        matches!(
            self,
            Self {
                title: None,
                description: None,
                doc: None,
                deprecated: false,
                examples,
                extensions,
                transforms,
            } if examples.is_empty() && extensions.is_empty() && transforms.is_empty()
        )
    }

    pub fn add_mutators(&self, expr: &mut SchemaExpr) {
        let mutators = &mut expr.mutators;

        let mut title = self.title.as_ref().map(ToTokens::to_token_stream);
        let mut description = self.description.as_ref().map(ToTokens::to_token_stream);
        if let Some(doc) = &self.doc {
            title.get_or_insert_with(|| {
                quote!({
                    const TITLE: &str = schemars::_private::get_title_and_description(#doc).0;
                    TITLE
                })
            });
            description.get_or_insert_with(|| {
                quote!({
                    const DESCRIPTION: &str = schemars::_private::get_title_and_description(#doc).1;
                    DESCRIPTION
                })
            });
        }
        if let Some(title) = title {
            mutators.push(quote! {
                schemars::_private::insert_metadata_property_if_nonempty(&mut #SCHEMA, "title", #title);
            });
        }
        if let Some(description) = description {
            mutators.push(quote! {
                schemars::_private::insert_metadata_property_if_nonempty(&mut #SCHEMA, "description", #description);
            });
        }

        if self.deprecated {
            mutators.push(quote! {
                #SCHEMA.insert("deprecated".into(), true.into());
            });
        }

        if !self.examples.is_empty() {
            let examples = self.examples.iter().map(|eg| {
                quote! {
                    schemars::_private::serde_json::value::to_value(#eg)
                }
            });
            mutators.push(quote! {
                #SCHEMA.insert("examples".into(), schemars::_private::serde_json::Value::Array([#(#examples),*].into_iter().flatten().collect()));
            });
        }

        // Post-Mutators - extensions and transforms will be applied after all other mutators

        for (k, v) in &self.extensions {
            expr.post_mutators.push(quote! {
                #SCHEMA.insert(#k.into(), schemars::_private::serde_json::json!(#v));
            });
        }

        for transform in &self.transforms {
            expr.post_mutators.push(quote_spanned! {transform.span()=>
                schemars::transform::Transform::transform(&mut #transform, &mut #SCHEMA);
            });
        }
    }
}

impl FieldAttrs {
    pub fn new(attrs: &[Attribute], cx: &Ctxt) -> Self {
        let mut result = Self::default();
        result.populate(attrs, cx);
        result
    }

    fn populate(&mut self, attrs: &[Attribute], cx: &Ctxt) {
        let schemars_cx = &mut AttrCtxt::new(cx, attrs, "schemars");
        let serde_cx = &mut AttrCtxt::new(cx, attrs, "serde");
        let validate_cx = &mut AttrCtxt::new(cx, attrs, "validate");
        let garde_cx = &mut AttrCtxt::new(cx, attrs, "garde");

        self.common.populate(attrs, schemars_cx, serde_cx);
        self.validation.populate(schemars_cx, validate_cx, garde_cx);
        self.process_attr(schemars_cx);
        self.process_attr(serde_cx);
    }

    fn process_attr(&mut self, cx: &mut AttrCtxt) {
        cx.parse_meta(|m, n, c| self.process_meta(m, n, c));
    }

    fn process_meta(
        &mut self,
        meta: CustomMeta,
        meta_name: &str,
        cx: &AttrCtxt,
    ) -> Result<(), CustomMeta> {
        match meta_name {
            "with" => match self.with {
                Some(WithAttr::Type(_)) => cx.duplicate_error(&meta),
                Some(WithAttr::Function(_)) => cx.mutual_exclusive_error(&meta, "schema_with"),
                None => self.with = parse_name_value_lit_str(meta, cx).ok().map(WithAttr::Type),
            },
            "schema_with" if cx.attr_type == "schemars" => match self.with {
                Some(WithAttr::Function(_)) => cx.duplicate_error(&meta),
                Some(WithAttr::Type(_)) => cx.mutual_exclusive_error(&meta, "with"),
                None => {
                    self.with = parse_name_value_lit_str(meta, cx)
                        .ok()
                        .map(WithAttr::Function);
                }
            },

            _ => return Err(meta),
        }

        Ok(())
    }

    pub fn is_default(&self) -> bool {
        matches!(
            self,
            Self {
                common,
                validation,
                with: None,
            } if common.is_default() && validation.is_default())
    }
}

impl ContainerAttrs {
    pub fn new(attrs: &[Attribute], data: &Data, cx: &Ctxt) -> Self {
        let mut result = Self::default();
        result.populate(attrs, data, cx);
        result
    }

    fn populate(&mut self, attrs: &[Attribute], data: &Data, cx: &Ctxt) {
        let schemars_cx = &mut AttrCtxt::new(cx, attrs, "schemars");
        let serde_cx = &mut AttrCtxt::new(cx, attrs, "serde");

        self.common.populate(attrs, schemars_cx, serde_cx);
        self.process_attr(data, schemars_cx);
        self.process_attr(data, serde_cx);

        self.repr = attrs
            .iter()
            .find(|a| a.path().is_ident("repr"))
            .and_then(|a| a.parse_args().ok());
    }

    fn process_attr(&mut self, data: &Data, cx: &mut AttrCtxt) {
        cx.parse_meta(|m, n, c| self.process_meta(m, n, data, c));
    }

    fn process_meta(
        &mut self,
        meta: CustomMeta,
        meta_name: &str,
        data: &Data,
        cx: &AttrCtxt,
    ) -> Result<(), CustomMeta> {
        match meta_name {
            "crate" => match self.crate_name {
                Some(_) => cx.duplicate_error(&meta),
                None => self.crate_name = parse_name_value_lit_str(meta, cx).ok(),
            },

            "rename" if cx.attr_type == "schemars" => match self.rename_format_string {
                Some(_) => cx.duplicate_error(&meta),
                None => self.rename_format_string = require_name_value_lit_str(meta, cx).ok(),
            },

            "inline" => {
                if self.inline {
                    cx.duplicate_error(&meta);
                } else if require_path_only(&meta, cx).is_ok() {
                    self.inline = true;
                }
            }

            "with" if cx.attr_type == "schemars" => match self.with {
                Some(WithAttr::Type(_)) => cx.duplicate_error(&meta),
                Some(WithAttr::Function(_)) => cx.mutual_exclusive_error(&meta, "schema_with"),
                None => self.with = parse_name_value_lit_str(meta, cx).ok().map(WithAttr::Type),
            },
            "schema_with" if cx.attr_type == "schemars" => match self.with {
                Some(WithAttr::Function(_)) => cx.duplicate_error(&meta),
                Some(WithAttr::Type(_)) => cx.mutual_exclusive_error(&meta, "with"),
                None => {
                    self.with = parse_name_value_lit_str(meta, cx)
                        .ok()
                        .map(WithAttr::Function);
                }
            },

            "_unstable_ref_variants" if cx.attr_type == "schemars" => {
                if !matches!(data, Data::Enum(_)) {
                    cx.error_spanned_by(
                        meta.path(),
                        "`_unstable_ref_variants` can only be used on enums",
                    );
                } else if self.ref_variants {
                    cx.duplicate_error(&meta);
                } else if require_path_only(&meta, cx).is_ok() {
                    self.ref_variants = true;
                }
            }

            _ => return Err(meta),
        }

        Ok(())
    }
}

impl VariantAttrs {
    pub fn new(attrs: &[Attribute], cx: &Ctxt) -> Self {
        let mut result = Self::default();
        result.populate(attrs, cx);
        result
    }

    fn populate(&mut self, attrs: &[Attribute], cx: &Ctxt) {
        let schemars_cx = &mut AttrCtxt::new(cx, attrs, "schemars");
        let serde_cx = &mut AttrCtxt::new(cx, attrs, "serde");

        self.common.populate(attrs, schemars_cx, serde_cx);
        self.process_attr(schemars_cx);
        self.process_attr(serde_cx);
    }

    fn process_attr(&mut self, cx: &mut AttrCtxt) {
        cx.parse_meta(|m, n, c| self.process_meta(m, n, c));
    }

    fn process_meta(
        &mut self,
        meta: CustomMeta,
        meta_name: &str,
        cx: &AttrCtxt,
    ) -> Result<(), CustomMeta> {
        match meta_name {
            "with" => match self.with {
                Some(WithAttr::Type(_)) => cx.duplicate_error(&meta),
                Some(WithAttr::Function(_)) => cx.mutual_exclusive_error(&meta, "schema_with"),
                None => self.with = parse_name_value_lit_str(meta, cx).ok().map(WithAttr::Type),
            },
            "schema_with" if cx.attr_type == "schemars" => match self.with {
                Some(WithAttr::Function(_)) => cx.duplicate_error(&meta),
                Some(WithAttr::Type(_)) => cx.mutual_exclusive_error(&meta, "with"),
                None => {
                    self.with = parse_name_value_lit_str(meta, cx)
                        .ok()
                        .map(WithAttr::Function);
                }
            },

            _ => return Err(meta),
        }

        Ok(())
    }

    pub fn is_default(&self) -> bool {
        matches!(
            self,
            Self {
                common,
                with: None,
            } if common.is_default()
        )
    }
}

fn get_meta_items(attrs: &[Attribute], attr_type: &'static str, cx: &Ctxt) -> Vec<CustomMeta> {
    let mut result = vec![];

    for attr in attrs.iter().filter(|a| a.path().is_ident(attr_type)) {
        match attr.parse_args_with(Punctuated::<CustomMeta, Token![,]>::parse_terminated) {
            Ok(list) => result.extend(list),
            Err(err) => {
                if attr_type == "schemars" {
                    cx.syn_error(err);
                }
            }
        }
    }

    result
}

fn path_str(path: &Path) -> String {
    path.get_ident().map_or_else(
        || path.into_token_stream().to_string().replace(' ', ""),
        Ident::to_string,
    )
}

pub struct AttrCtxt<'a> {
    inner: &'a Ctxt,
    attr_type: &'static str,
    metas: Vec<CustomMeta>,
}

impl<'a> AttrCtxt<'a> {
    pub fn new(inner: &'a Ctxt, attrs: &'a [Attribute], attr_type: &'static str) -> Self {
        Self {
            inner,
            attr_type,
            metas: get_meta_items(attrs, attr_type, inner),
        }
    }

    pub fn new_nested_meta(&self, metas: Vec<CustomMeta>) -> Self {
        Self { metas, ..*self }
    }

    pub fn parse_meta(
        &mut self,
        mut handle: impl FnMut(CustomMeta, &str, &Self) -> Result<(), CustomMeta>,
    ) {
        let metas = std::mem::take(&mut self.metas);
        self.metas = metas
            .into_iter()
            .filter_map(|meta| match meta.path().get_ident().map(Ident::to_string) {
                Some(ident) => handle(meta, &ident, self).err(),
                _ => Some(meta),
            })
            .collect();
    }

    pub fn error_spanned_by<A: ToTokens, T: std::fmt::Display>(&self, obj: A, msg: T) {
        self.inner.error_spanned_by(obj, msg);
    }

    pub fn syn_error(&self, err: syn::Error) {
        self.inner.syn_error(err);
    }

    pub fn mutual_exclusive_error(&self, meta: &CustomMeta, other_attr: &str) {
        if self.attr_type == "schemars" {
            self.error_spanned_by(
                meta,
                format_args!(
                    "schemars attribute cannot contain both `{}` and `{}`",
                    path_str(meta.path()),
                    other_attr,
                ),
            );
        }
    }

    pub fn duplicate_error(&self, meta: &CustomMeta) {
        if self.attr_type == "schemars" {
            self.error_spanned_by(
                meta,
                format_args!(
                    "duplicate schemars attribute item `{}`",
                    path_str(meta.path())
                ),
            );
        }
    }
}

impl Drop for AttrCtxt<'_> {
    fn drop(&mut self) {
        if self.attr_type == "schemars" {
            for unhandled_meta in self.metas.iter().filter(|m| !is_schemars_serde_keyword(m)) {
                self.error_spanned_by(
                    unhandled_meta.path(),
                    format_args!(
                        "unknown schemars attribute `{}`",
                        path_str(unhandled_meta.path())
                    ),
                );
            }
        }
    }
}

fn is_schemars_serde_keyword(meta: &CustomMeta) -> bool {
    let known_keywords = schemars_to_serde::SCHEMARS_KEYWORDS_PARSED_BY_SERDE;
    meta.path()
        .get_ident()
        .is_some_and(|i| known_keywords.contains(&i.to_string().as_str()))
}