From 457ed25ede58e528f0a08ed166a9326f683aa90c Mon Sep 17 00:00:00 2001 From: ToniAdreal <184365500+ToniAdreal@users.noreply.github.com> Date: Mon, 26 Oct 2026 04:30:47 -0700 Subject: [PATCH 1/2] Support the OpenAPI 3.1 forms of nullability The codegen only understood the OpenAPI 3.0 spelling, `nullable: true`. Fed a spec that uses the correct 3.1 forms it silently dropped most of the API: a field typed `"type": ["string", "null"]` made its whole containing type unsupported, and an `anyOf`-wrapped $ref query parameter made the whole operation unsupported. Add a normalisation step in the schema conversion that rewrites the 3.1 forms before dispatch: - `"type": [T, "null"]` becomes `"type": T`, nullable - `anyOf`/`oneOf` with a `{"type": "null"}` variant unwraps to the other variant (carrying over siblings like `description`/`default`), nullable The legacy `nullable: true` keeps working, and `Field.nullable` is now set from either spelling, so the templates need no changes. Path/header parameter validation accepts the array form too. Verified with the debug IR dump against svix-webhooks' codegen/lib-openapi.json (678 nullable sites) converted to the 3.1 forms with the jq recipe from svix/svix-webhooks#2551: before, the IR kept 36 of 252 types and 118 of 141 operations; with this change it is byte-identical to the IR produced from the current (3.0-spelling) spec, and the output for the current spec is unchanged. --- src/api/resources.rs | 9 +++- src/api/types.rs | 126 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 116 insertions(+), 19 deletions(-) diff --git a/src/api/resources.rs b/src/api/resources.rs index 231b41e..a4930d9 100644 --- a/src/api/resources.rs +++ b/src/api/resources.rs @@ -407,7 +407,14 @@ fn enforce_string_parameter(parameter_data: &openapi::ParameterData) -> anyhow:: .json_schema .get("type") .context("missing path parameter type")?; - if ty != "string" { + // The 3.1 spelling of a string type may be an array (`["string", "null"]` + // for a nullable parameter); accept any form that includes "string". + let is_string = match ty { + crate::JsonValue::String(s) => s == "string", + crate::JsonValue::Array(types) => types.iter().any(|t| t.as_str() == Some("string")), + _ => false, + }; + if !is_string { bail!("unsupported path parameter type `{ty:?}`"); } diff --git a/src/api/types.rs b/src/api/types.rs index 1ef6fd2..2a48867 100644 --- a/src/api/types.rs +++ b/src/api/types.rs @@ -21,6 +21,75 @@ use super::{ /// Intermediate representation of (some) `components` from the spec. pub type Types = BTreeMap; +/// Whether a schema is exactly a `{"type": "null"}` schema. +fn is_null_schema(schema: &JsonValue) -> bool { + schema.get("type").and_then(|t| t.as_str()) == Some("null") +} + +/// Normalise the OpenAPI 3.1 (JSON Schema 2020-12) spellings of nullability +/// into a shape the rest of this module understands. +/// +/// Returns the normalised schema plus whether it is nullable. Two correct +/// 3.1 forms are handled, in addition to the legacy 3.0 `nullable: true`: +/// +/// - `"type": ["string", "null"]` becomes `"type": "string"`, nullable. +/// - `"anyOf": [{"$ref": "…"}, {"type": "null"}]` (or the `oneOf` spelling) +/// unwraps to the non-null variant, nullable. Sibling keys of the wrapper +/// (e.g. `description`, `default`) are carried over to the unwrapped schema. +/// +/// Anything that is not a plain null-union is returned unchanged, so +/// unsupported shapes keep hitting the existing error paths. +pub(crate) fn normalize_nullable_schema(schema: &JsonValue) -> (Cow<'_, JsonValue>, bool) { + let mut nullable = schema + .get("nullable") + .and_then(|v| v.as_bool()) + .unwrap_or(false); + + // `"type": [T, "null"]` + if let Some(types) = schema.get("type").and_then(|t| t.as_array()) { + let mut non_null = types.iter().filter(|t| t.as_str() != Some("null")); + if types.iter().any(|t| t.as_str() == Some("null")) { + nullable = true; + if let (Some(single), None) = (non_null.next(), non_null.next()) + && let Some(single) = single.as_str() + { + let mut obj = schema.as_object().cloned().unwrap_or_default(); + obj.insert("type".to_owned(), JsonValue::String(single.to_owned())); + return (Cow::Owned(JsonValue::Object(obj)), nullable); + } + } + return (Cow::Borrowed(schema), nullable); + } + + // `"anyOf" / "oneOf": [inner, {"type": "null"}]` + for key in ["anyOf", "oneOf"] { + let Some(variants) = schema.get(key).and_then(|v| v.as_array()) else { + continue; + }; + if variants.len() == 2 && variants.iter().any(is_null_schema) { + let inner = variants.iter().find(|v| !is_null_schema(v)).unwrap(); + nullable = true; + // The inner schema may itself use a 3.1 nullable form. + let (inner, inner_nullable) = normalize_nullable_schema(inner); + nullable |= inner_nullable; + let JsonValue::Object(mut obj) = inner.into_owned() else { + return (Cow::Borrowed(schema), nullable); + }; + // Carry over siblings of the wrapper that the inner schema lacks. + if let Some(outer) = schema.as_object() { + for (k, v) in outer { + if k != key && k != "nullable" && !obj.contains_key(k) { + obj.insert(k.clone(), v.clone()); + } + } + } + return (Cow::Owned(JsonValue::Object(obj)), nullable); + } + } + + (Cow::Borrowed(schema), nullable) +} + pub(crate) fn from_referenced_components( res: &Resources, mut schemas: IndexMap, @@ -75,6 +144,13 @@ impl Type { pub(crate) fn from_schema(name: String, schema: &JsonValue) -> anyhow::Result { ensure!(schema.is_object(), "schema must be an object"); + // A component type itself has no nullable slot (nullability lives on + // the referencing field), so the flag is discarded here; normalising + // still matters so that `type: [T, "null"]` and null-union wrappers + // are understood at all. + let (schema_cow, _) = normalize_nullable_schema(schema); + let schema = schema_cow.as_ref(); + let instance_type = match schema.get("type") { Some(JsonValue::String(ty)) => Some(ty.as_str()), Some(ty) => bail!("invalid / unsupported type `{ty:?}`"), @@ -87,19 +163,27 @@ impl Type { .filter_map(|v| v.as_array()) .flatten() { - if let Some(ty) = &variant.get("type") { - match ty { - JsonValue::String(ty) => { - if let Some(res_ty) = result { - ensure!( - res_ty == ty, - "unsupported: oneOf schemas with different types" - ); - } else { - result = Some(ty.as_str()); - } - } - _ => bail!("invalid / unsupported type `{ty:?}` in oneOf"), + // A variant may itself spell its type the 3.1 way, + // `"type": ["string", "null"]`; read through that. + let variant_ty: Option<&str> = match variant.get("type") { + Some(JsonValue::String(ty)) => Some(ty.as_str()), + Some(JsonValue::Array(types)) => types + .iter() + .filter_map(|t| t.as_str()) + .find(|t| *t != "null"), + Some(ty) => { + bail!("invalid / unsupported type `{ty:?}` in oneOf") + } + None => None, + }; + if let Some(ty) = variant_ty { + if let Some(res_ty) = result { + ensure!( + res_ty == ty, + "unsupported: oneOf schemas with different types" + ); + } else { + result = Some(ty); } } } @@ -358,14 +442,11 @@ impl Field { positional: bool, ) -> anyhow::Result { let example = schema.get("example").cloned(); - let nullable = schema - .get("nullable") - .and_then(|v| v.as_bool()) - .unwrap_or(false); + let (normalized, nullable) = normalize_nullable_schema(schema); Ok(Self { name, - r#type: FieldType::from_schema(schema)?, + r#type: FieldType::from_schema(&normalized)?, default: schema.get("default").cloned(), description: schema["description"].as_str().map(ToOwned::to_owned), required, @@ -461,6 +542,15 @@ impl FieldType { fn from_schema(schema: &JsonValue) -> anyhow::Result { ensure!(schema.is_object(), "schema must be an object"); + // Unwrap the 3.1 nullable forms (`"type": [T, "null"]`, + // `anyOf`/`oneOf` with a `{"type": "null"}` variant) up front; the + // nullability flag itself is tracked by the caller (`Field`), this + // function only needs the underlying type. Recursion below passes + // sub-schemas through this function again, so nested nullable forms + // (array items, map values, …) are handled too. + let (schema_cow, _) = normalize_nullable_schema(schema); + let schema = schema_cow.as_ref(); + let result = match schema.get("type") { Some(JsonValue::String(ty)) => { match ty.as_str() { From 551139ff3b43caaedf6163d0840bdb853fb53b24 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 26 Oct 2026 13:04:00 +0100 Subject: [PATCH 2/2] Make comment a bit clearer --- src/api/resources.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api/resources.rs b/src/api/resources.rs index a4930d9..831483a 100644 --- a/src/api/resources.rs +++ b/src/api/resources.rs @@ -407,8 +407,8 @@ fn enforce_string_parameter(parameter_data: &openapi::ParameterData) -> anyhow:: .json_schema .get("type") .context("missing path parameter type")?; - // The 3.1 spelling of a string type may be an array (`["string", "null"]` - // for a nullable parameter); accept any form that includes "string". + // The OpenAPI 3.1 spelling of a nullable string is an array + // (`["string", "null"]`); accept any form that includes "string". let is_string = match ty { crate::JsonValue::String(s) => s == "string", crate::JsonValue::Array(types) => types.iter().any(|t| t.as_str() == Some("string")),