Skip to content

Fix deserializing a schema without type, such as the empty schema {} - #1653

Open
Vladimir-Pirotskiy wants to merge 1 commit into
juhaku:masterfrom
Vladimir-Pirotskiy:fix/deserialize-schema-without-type
Open

Vladimir-Pirotskiy wants to merge 1 commit into
juhaku:masterfrom
Vladimir-Pirotskiy:fix/deserialize-schema-without-type

Conversation

@Vladimir-Pirotskiy

@Vladimir-Pirotskiy Vladimir-Pirotskiy commented Oct 31, 2026 •

Copy link
Copy Markdown

Problem

A Schema Object with no type and no oneOf / allOf / anyOf accepts any value. {} is the
usual spelling of "any value", and {"description": "..."} is what utoipa itself generates for a
serde_json::Value field with a doc comment. Such a schema cannot be deserialized:

data did not match any variant of untagged enum RefOr

Object::schema_type has no default, so Schema::Object rejects the input, and the other
variants need their own keys. Because Schema is nested everywhere (properties, items,
additionalProperties, requestBody content, ...), one such schema makes the whole
OpenApi document fail to parse, although utoipa produces or accepts the same shape when
serializing.

let _: RefOr<Schema> = serde_json::from_str("{}").unwrap(); // error before this change

Fix

Schema now deserializes through a private untagged enum (#[serde(from = "SchemaRepr")]) that
has the same variants in the same order as before, plus a last one that accepts a schema without
type as an Object with SchemaType::AnyValue (AnyValue is skipped on serialization, so
{} round-trips to {}).

I did not add #[serde(default)] to Object::schema_type: Object comes before OneOf,
AllOf and AnyOf in the untagged Schema, so a {"oneOf": [...]} without type would be
swallowed by Object and lose its items. A fallback that is tried last avoids changing how any
currently parseable input is deserialized. Serialization is unchanged.

Tests

Added in utoipa/src/openapi/schema.rs:

  • round trip (deserialize, serialize, same JSON) of {}, {"description": ...}, an untyped
    schema with properties, empty schemas in properties, items, additionalProperties and
    in oneOf / allOf / anyOf, and a full OpenApi document with "schema": {}
  • {} is Schema::Object with SchemaType::AnyValue
  • schemas with oneOf / allOf / anyOf and no type stay OneOf / AllOf / AnyOf
  • typed object and array schemas are still Object and Array

8 of the 9 new tests fail on master (the typed-schema guard passes both ways), and all of them
pass with the fix. cargo fmt --check and cargo test -p utoipa (default features and the
features from justfile) are green, as are the utoipa-gen, utoipa-axum and
utoipa-actix-web tests. cargo clippy -p utoipa reports the same 10 warnings as on master,
none in the new code.

Not covered

Boolean schemas ("schema": true, false, OpenAPI 3.1) are still not parsed. true could be
read as an any-value schema, but it would serialize back as {}, so I left it out of this change.

The Error struct from #1192 (an Option<serde_json::Value> field with a doc comment, which is
{"description": "description"}) now deserializes and serializes back to the same JSON. The second
example in that issue (struct User;, which is {"default": null}) now parses too, but
default: null is lost on the way back because default is an Option<Value>; that is a separate
limitation.

Fixes #1192

An OpenAPI schema with no `type` and no `oneOf`, `allOf` or `anyOf`,
for example `{}` or `{"description": "..."}`, accepts any value but
could not be deserialized into `Schema`. That made the whole
`RefOr<Schema>`, and any document containing one, fail to parse.

Deserialize such a schema as an `Object` with `SchemaType::AnyValue`,
which serializes back to the same JSON.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Schema type with AnyValue cannot be deserialized from RefOr<...>

1 participant