From afff057f00131370db87d7fb03e3356286ef1ee0 Mon Sep 17 00:00:00 2001 From: "Joshua M. Clulow" Date: Wed, 15 Sep 2021 06:41:31 +0000 Subject: [PATCH] omit missing optionals from JSON, rather than sending null --- src/main.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main.rs b/src/main.rs index 3cbe1a4..1f74846 100644 --- a/src/main.rs +++ b/src/main.rs @@ -505,6 +505,15 @@ impl TypeSpace { } } + fn is_optional(&self, tid: &TypeId) -> bool { + if let Some(te) = self.id_to_entry.get(&tid) { + if let TypeDetails::Optional(_) = &te.details { + return true; + } + } + false + } + /** * Emit a human-readable diagnostic description for this type ID. */ @@ -949,6 +958,16 @@ fn gen(api: &OpenAPI, ts: &mut TypeSpace) -> Result { te.name.as_deref().unwrap() )); for (name, tid) in omap.iter() { + if ts.is_optional(tid) { + /* + * Omit missing optionals from the document entirely; do + * not inject a "null" value. + */ + a( + " #[serde(skip_serializing_if = \ + \"Option::is_none\")]" + ); + } a(&format!( " pub {}: {},", name,