Skip to content

Commit 520cac2

Browse files
committed
(codegen) Allow #[deprecated] on field in impl_object
1 parent db0d595 commit 520cac2

File tree

2 files changed

+78
-14
lines changed

2 files changed

+78
-14
lines changed

juniper/src/macros/tests/field.rs

+48
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ impl Root {
3333
0
3434
}
3535

36+
#[deprecated]
37+
fn deprecated_outer() -> bool {
38+
true
39+
}
40+
41+
#[deprecated(note = "Deprecation Reason")]
42+
fn deprecated_outer_with_reason() -> bool {
43+
true
44+
}
45+
3646
#[graphql(deprecated = "Deprecation reason")]
3747
fn deprecated() -> i32 {
3848
0
@@ -275,6 +285,44 @@ fn introspect_interface_field_description() {
275285
});
276286
}
277287

288+
#[test]
289+
fn introspect_object_field_deprecated_outer() {
290+
run_field_info_query("Root", "deprecatedOuter", |field| {
291+
assert_eq!(
292+
field.get_field_value("name"),
293+
Some(&Value::scalar("deprecatedOuter"))
294+
);
295+
assert_eq!(field.get_field_value("description"), Some(&Value::null()));
296+
assert_eq!(
297+
field.get_field_value("isDeprecated"),
298+
Some(&Value::scalar(true))
299+
);
300+
assert_eq!(
301+
field.get_field_value("deprecationReason"),
302+
Some(&Value::null()),
303+
);
304+
});
305+
}
306+
307+
#[test]
308+
fn introspect_object_field_deprecated_outer_with_reason() {
309+
run_field_info_query("Root", "deprecatedOuterWithReason", |field| {
310+
assert_eq!(
311+
field.get_field_value("name"),
312+
Some(&Value::scalar("deprecatedOuterWithReason"))
313+
);
314+
assert_eq!(field.get_field_value("description"), Some(&Value::null()));
315+
assert_eq!(
316+
field.get_field_value("isDeprecated"),
317+
Some(&Value::scalar(true))
318+
);
319+
assert_eq!(
320+
field.get_field_value("deprecationReason"),
321+
Some(&Value::scalar("Deprecation Reason")),
322+
);
323+
});
324+
}
325+
278326
#[test]
279327
fn introspect_object_field_deprecated() {
280328
run_field_info_query("Root", "deprecated", |field| {

juniper_codegen/src/util.rs

+30-14
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,23 @@ pub fn get_deprecated(attrs: &Vec<Attribute>) -> Option<DeprecationAttr> {
7676
fn get_deprecated_meta_list(list: &MetaList) -> DeprecationAttr {
7777
for meta in &list.nested {
7878
match meta {
79-
&NestedMeta::Meta(Meta::NameValue(ref nv)) if nv.ident == "note" => match &nv.lit {
80-
&Lit::Str(ref strlit) => {
81-
return DeprecationAttr {
82-
reason: Some(strlit.value().to_string()),
83-
};
79+
&NestedMeta::Meta(Meta::NameValue(ref nv)) => {
80+
if nv.ident == "note" {
81+
match &nv.lit {
82+
&Lit::Str(ref strlit) => {
83+
return DeprecationAttr {
84+
reason: Some(strlit.value()),
85+
};
86+
}
87+
_ => panic!("deprecated attribute note value only has string literal"),
88+
}
89+
} else {
90+
panic!(
91+
"Unrecognized setting on #[deprecated(..)] attribute: {}",
92+
nv.ident
93+
);
8494
}
85-
_ => panic!("deprecated attribute note value only has string literal"),
86-
},
95+
}
8796
_ => {}
8897
}
8998
}
@@ -434,7 +443,7 @@ pub enum FieldAttributeParseMode {
434443
enum FieldAttribute {
435444
Name(syn::LitStr),
436445
Description(syn::LitStr),
437-
Deprecation(Option<syn::LitStr>),
446+
Deprecation(DeprecationAttr),
438447
Skip(syn::Ident),
439448
Arguments(HashMap<String, FieldAttributeArgument>),
440449
}
@@ -466,11 +475,13 @@ impl parse::Parse for FieldAttribute {
466475
"deprecated" | "deprecation" => {
467476
let reason = if input.peek(Token![=]) {
468477
input.parse::<Token![=]>()?;
469-
Some(input.parse()?)
478+
Some(input.parse::<syn::LitStr>()?.value())
470479
} else {
471480
None
472481
};
473-
Ok(FieldAttribute::Deprecation(reason))
482+
Ok(FieldAttribute::Deprecation(DeprecationAttr {
483+
reason: reason,
484+
}))
474485
}
475486
"skip" => Ok(FieldAttribute::Skip(ident)),
476487
"arguments" => {
@@ -525,10 +536,8 @@ impl parse::Parse for FieldAttributes {
525536
FieldAttribute::Description(name) => {
526537
output.description = Some(name.value());
527538
}
528-
FieldAttribute::Deprecation(reason_opt) => {
529-
output.deprecation = Some(DeprecationAttr {
530-
reason: reason_opt.map(|val| val.value()),
531-
});
539+
FieldAttribute::Deprecation(attr) => {
540+
output.deprecation = Some(attr);
532541
}
533542
FieldAttribute::Skip(_) => {
534543
output.skip = true;
@@ -553,6 +562,7 @@ impl FieldAttributes {
553562
_mode: FieldAttributeParseMode,
554563
) -> syn::parse::Result<Self> {
555564
let doc_comment = get_doc_comment(&attrs);
565+
let deprecation = get_deprecated(&attrs);
556566

557567
let attr_opt = attrs
558568
.into_iter()
@@ -562,9 +572,15 @@ impl FieldAttributes {
562572
Some(attr) => syn::parse(attr.tts.into())?,
563573
None => Self::default(),
564574
};
575+
576+
// Check for regular doc comment.
565577
if output.description.is_none() {
566578
output.description = doc_comment;
567579
}
580+
if output.deprecation.is_none() {
581+
output.deprecation = deprecation;
582+
}
583+
568584
Ok(output)
569585
}
570586

0 commit comments

Comments
 (0)