From 1a40d1b06c1acb82aa00ae6776cf8c67f0b3874b Mon Sep 17 00:00:00 2001 From: Graham Esau Date: Mon, 6 May 2024 18:12:20 +0100 Subject: [PATCH] Fix deriving JsonSchema on enum without variants Fixes #287 --- schemars/tests/enum.rs | 8 ++++++++ schemars/tests/expected/no-variants.json | 6 ++++++ schemars_derive/src/schema_exprs.rs | 2 +- 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 schemars/tests/expected/no-variants.json diff --git a/schemars/tests/enum.rs b/schemars/tests/enum.rs index 3f60e843..1ed60462 100644 --- a/schemars/tests/enum.rs +++ b/schemars/tests/enum.rs @@ -136,3 +136,11 @@ enum SoundOfMusic { fn enum_unit_with_doc_comments() -> TestResult { test_default_generated_schema::("enum-unit-doc") } + +#[derive(JsonSchema)] +enum NoVariants {} + +#[test] +fn enum_no_variants() -> TestResult { + test_default_generated_schema::("no-variants") +} diff --git a/schemars/tests/expected/no-variants.json b/schemars/tests/expected/no-variants.json new file mode 100644 index 00000000..efe28535 --- /dev/null +++ b/schemars/tests/expected/no-variants.json @@ -0,0 +1,6 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "NoVariants", + "type": "string", + "enum": [] +} \ No newline at end of file diff --git a/schemars_derive/src/schema_exprs.rs b/schemars_derive/src/schema_exprs.rs index de9ca39b..b625114b 100644 --- a/schemars_derive/src/schema_exprs.rs +++ b/schemars_derive/src/schema_exprs.rs @@ -162,7 +162,7 @@ fn expr_for_external_tagged_enum<'a>( let unit_names = unit_variants.iter().map(|v| v.name()); let unit_schema = schema_object(quote! { instance_type: Some(schemars::schema::InstanceType::String.into()), - enum_values: Some([#(#unit_names),*].into_iter().map(|v| v.into()).collect()), + enum_values: Some(vec![#(#unit_names.into()),*]), }); if complex_variants.is_empty() {