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
//! Derive for JSON schema on Relay protocol types.

#![doc(
    html_logo_url = "https://raw.githubusercontent.com/getsentry/relay/master/artwork/relay-icon.png",
    html_favicon_url = "https://raw.githubusercontent.com/getsentry/relay/master/artwork/relay-icon.png"
)]
#![warn(missing_docs)]
#![recursion_limit = "256"]

use proc_macro2::TokenStream;
use quote::quote;
use syn::{parse_quote, Attribute, Lit, Meta, MetaNameValue, NestedMeta, Visibility};
use synstructure::decl_derive;

decl_derive!(
    [JsonSchema, attributes(metastructure)] =>
    /// Derives `schemars::JsonSchema` from an `Annotated` structure.
    derive_jsonschema
);

fn derive_jsonschema(mut s: synstructure::Structure<'_>) -> TokenStream {
    let _ = s.add_bounds(synstructure::AddBounds::Generics);

    let mut arms = Vec::new();

    let is_single_variant = s.variants().len() == 1;

    for variant in s.variants() {
        // parse_variant_attributes currently cannot deal with struct attributes. Structs are
        // represented as single-variant enums in synstructure.
        if !is_single_variant {
            let variant_attrs = parse_variant_attributes(variant.ast().attrs);

            if variant_attrs.omit_from_schema {
                continue;
            }
        }

        let mut fields = Vec::new();

        let mut is_tuple_struct = false;

        for (index, bi) in variant.bindings().iter().enumerate() {
            let field_attrs = parse_field_attributes(index, bi.ast(), &mut is_tuple_struct);
            let name = field_attrs.field_name;

            let mut ast = bi.ast().clone();
            ast.vis = Visibility::Inherited;
            transform_attributes(&mut ast.attrs);

            ast.attrs.push(parse_quote!(#[schemars(rename = #name)]));

            if !field_attrs.required.unwrap_or(false) {
                ast.attrs
                    .push(parse_quote!(#[schemars(default = "__schemars_null")]));
            }

            if field_attrs.additional_properties || field_attrs.omit_from_schema {
                ast.attrs.push(parse_quote!(#[schemars(skip)]));
            }

            fields.push(ast);
        }

        let ident = variant.ast().ident;

        let arm = if is_tuple_struct {
            quote!( #ident( #(#fields),* ) )
        } else {
            quote!( #ident { #(#fields),* } )
        };

        arms.push(arm);
    }

    let ident = &s.ast().ident;
    let mut attrs = s.ast().attrs.clone();
    transform_attributes(&mut attrs);

    s.gen_impl(quote! {
        // Massive hack to tell schemars that fields are nullable. Causes it to emit {"default":
        // null} even though Option<()> is not a valid instance of T.
        fn __schemars_null() -> Option<()> {
            None
        }

        #[automatically_derived]
        gen impl schemars::JsonSchema for @Self {
            fn schema_name() -> String {
                stringify!(#ident).to_owned()
            }

            fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
                #[derive(schemars::JsonSchema)]
                #[cfg_attr(feature = "jsonschema", schemars(untagged))]
                #[cfg_attr(feature = "jsonschema", schemars(deny_unknown_fields))]
                #(#attrs)*
                enum Helper {
                    #(#arms),*
                }

                Helper::json_schema(gen)
            }
        }
    })
}

/// Take an attribute set from the original struct and:
///
/// 1. Filter out all attibutes but `schemars` and `doc`.
/// 2. Replace #[doc = "foo"] with #[schemars(description = "foo")]. While schemars already uses
///    docstrings for its jsonschema description by default, it applies line-wrapping logic that
///    destroys markdown. Explicitly setting description bypasses that.
///
fn transform_attributes(attrs: &mut Vec<Attribute>) {
    let mut description = String::new();

    attrs.retain(|attr| {
        if attr.path.is_ident("doc") {
            if let Ok(Meta::NameValue(MetaNameValue {
                lit: Lit::Str(s), ..
            })) = attr.parse_meta()
            {
                if !description.is_empty() {
                    description.push('\n');
                }
                description.push_str(&s.value());
                return false;
            }
        }

        attr.path.is_ident("schemars")
    });

    if !description.is_empty() {
        attrs.push(parse_quote!(#[schemars(description = #description)]));
    }
}

#[derive(Default)]
struct FieldAttrs {
    additional_properties: bool,
    omit_from_schema: bool,
    field_name: String,
    required: Option<bool>,
}

fn parse_field_attributes(
    index: usize,
    bi_ast: &syn::Field,
    is_tuple_struct: &mut bool,
) -> FieldAttrs {
    if bi_ast.ident.is_none() {
        *is_tuple_struct = true;
    } else if *is_tuple_struct {
        panic!("invalid tuple struct");
    }

    let mut rv = FieldAttrs {
        field_name: bi_ast
            .ident
            .as_ref()
            .map(ToString::to_string)
            .unwrap_or_else(|| index.to_string()),
        ..Default::default()
    };

    for attr in &bi_ast.attrs {
        let meta = match attr.parse_meta() {
            Ok(meta) => meta,
            Err(_) => continue,
        };

        let ident = match meta.path().get_ident() {
            Some(x) => x,
            None => continue,
        };

        if ident != "metastructure" {
            continue;
        }

        if let Meta::List(metalist) = meta {
            for nested_meta in metalist.nested {
                if let NestedMeta::Meta(meta) = nested_meta {
                    match meta {
                        Meta::Path(path) => {
                            let ident = path.get_ident().expect("Unexpected path");

                            if ident == "additional_properties" {
                                rv.additional_properties = true;
                            } else if ident == "omit_from_schema" {
                                rv.omit_from_schema = true;
                            } else {
                                panic!("Unknown attribute {ident}");
                            }
                        }
                        Meta::NameValue(name_value) => {
                            let ident = name_value.path.get_ident().expect("Unexpected path");
                            if ident == "field" {
                                match name_value.lit {
                                    Lit::Str(litstr) => {
                                        rv.field_name = litstr.value();
                                    }
                                    _ => {
                                        panic!("Got non string literal for field");
                                    }
                                }
                            } else if ident == "required" {
                                match name_value.lit {
                                    Lit::Str(litstr) => match litstr.value().as_str() {
                                        "true" => rv.required = Some(true),
                                        "false" => rv.required = Some(false),
                                        other => panic!("Unknown value {other}"),
                                    },
                                    _ => {
                                        panic!("Got non string literal for required");
                                    }
                                }
                            }
                        }
                        _ => (),
                    }
                }
            }
        }
    }
    rv
}

#[derive(Default)]
struct VariantAttrs {
    omit_from_schema: bool,
}

fn parse_variant_attributes(attrs: &[syn::Attribute]) -> VariantAttrs {
    let mut rv = VariantAttrs::default();
    for attr in attrs {
        let meta = match attr.parse_meta() {
            Ok(meta) => meta,
            Err(_) => continue,
        };
        let ident = match meta.path().get_ident() {
            Some(x) => x,
            None => continue,
        };

        if ident != "metastructure" {
            continue;
        }

        if let Meta::List(metalist) = meta {
            for nested_meta in metalist.nested {
                if let NestedMeta::Meta(Meta::Path(path)) = nested_meta {
                    let ident = path.get_ident().expect("Unexpected path");
                    if ident == "omit_from_schema" {
                        rv.omit_from_schema = true;
                    }
                }
            }
        }
    }
    rv
}