From c3a5c7c5f4a47e9c3ca9f9d987dfa8eff9341c10 Mon Sep 17 00:00:00 2001 From: Graham Esau Date: Thu, 23 May 2024 17:21:16 +0100 Subject: [PATCH] Fix "null" default not being set on schema Fixes #295 --- schemars/src/_private.rs | 2 +- schemars/tests/default.rs | 1 + schemars/tests/expected/default.json | 7 +++++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/schemars/src/_private.rs b/schemars/src/_private.rs index 657244b1..5b031780 100644 --- a/schemars/src/_private.rs +++ b/schemars/src/_private.rs @@ -161,7 +161,7 @@ pub mod metadata { add_metadata_fn!(add_deprecated, deprecated, bool); add_metadata_fn!(add_read_only, read_only, bool); add_metadata_fn!(add_write_only, write_only, bool); - add_metadata_fn!(add_default, default, Value); + add_metadata_fn!(add_default, default, Option); pub fn add_examples>(schema: Schema, examples: I) -> Schema { let mut schema_obj = schema.into_object(); diff --git a/schemars/tests/default.rs b/schemars/tests/default.rs index fbc1033b..ab489f5e 100644 --- a/schemars/tests/default.rs +++ b/schemars/tests/default.rs @@ -30,6 +30,7 @@ where struct MyStruct { my_int: i32, my_bool: bool, + my_optional_string: Option, #[serde(serialize_with = "custom_serialize")] my_struct2: MyStruct2, #[serde( diff --git a/schemars/tests/expected/default.json b/schemars/tests/expected/default.json index aefef83d..4155d53c 100644 --- a/schemars/tests/expected/default.json +++ b/schemars/tests/expected/default.json @@ -12,6 +12,13 @@ "default": false, "type": "boolean" }, + "my_optional_string": { + "default": null, + "type": [ + "string", + "null" + ] + }, "my_struct2": { "default": "i:0 b:false", "allOf": [