Skip to content

Commit

Permalink
Fix deriving JsonSchema on enum without variants
Browse files Browse the repository at this point in the history
Fixes #287
  • Loading branch information
GREsau committed May 6, 2024
1 parent 4b90b6f commit 1a40d1b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
8 changes: 8 additions & 0 deletions schemars/tests/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,11 @@ enum SoundOfMusic {
fn enum_unit_with_doc_comments() -> TestResult {
test_default_generated_schema::<SoundOfMusic>("enum-unit-doc")
}

#[derive(JsonSchema)]
enum NoVariants {}

#[test]
fn enum_no_variants() -> TestResult {
test_default_generated_schema::<NoVariants>("no-variants")
}
6 changes: 6 additions & 0 deletions schemars/tests/expected/no-variants.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "NoVariants",
"type": "string",
"enum": []
}
2 changes: 1 addition & 1 deletion schemars_derive/src/schema_exprs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 1a40d1b

Please sign in to comment.