Repository navigation
Fix deserializing a schema without type, such as the empty schema {} - #1653
Open
Vladimir-Pirotskiy wants to merge 1 commit into
Open
Vladimir-Pirotskiy wants to merge 1 commit into
Vladimir-Pirotskiy wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A Schema Object with no
typeand nooneOf/allOf/anyOfaccepts any value.{}is theusual spelling of "any value", and
{"description": "..."}is what utoipa itself generates for aserde_json::Valuefield with a doc comment. Such a schema cannot be deserialized:Object::schema_typehas no default, soSchema::Objectrejects the input, and the othervariants need their own keys. Because
Schemais nested everywhere (properties,items,additionalProperties,requestBodycontent, ...), one such schema makes the wholeOpenApidocument fail to parse, although utoipa produces or accepts the same shape whenserializing.
Fix
Schemanow deserializes through a private untagged enum (#[serde(from = "SchemaRepr")]) thathas the same variants in the same order as before, plus a last one that accepts a schema without
typeas anObjectwithSchemaType::AnyValue(AnyValueis skipped on serialization, so{}round-trips to{}).I did not add
#[serde(default)]toObject::schema_type:Objectcomes beforeOneOf,AllOfandAnyOfin the untaggedSchema, so a{"oneOf": [...]}withouttypewould beswallowed by
Objectand lose its items. A fallback that is tried last avoids changing how anycurrently parseable input is deserialized. Serialization is unchanged.
Tests
Added in
utoipa/src/openapi/schema.rs:{},{"description": ...}, an untypedschema with
properties, empty schemas inproperties,items,additionalPropertiesandin
oneOf/allOf/anyOf, and a fullOpenApidocument with"schema": {}{}isSchema::ObjectwithSchemaType::AnyValueoneOf/allOf/anyOfand notypestayOneOf/AllOf/AnyOfobjectandarrayschemas are stillObjectandArray8 of the 9 new tests fail on
master(the typed-schema guard passes both ways), and all of thempass with the fix.
cargo fmt --checkandcargo test -p utoipa(default features and thefeatures from
justfile) are green, as are theutoipa-gen,utoipa-axumandutoipa-actix-webtests.cargo clippy -p utoipareports the same 10 warnings as onmaster,none in the new code.
Not covered
Boolean schemas (
"schema": true,false, OpenAPI 3.1) are still not parsed.truecould beread as an any-value schema, but it would serialize back as
{}, so I left it out of this change.The
Errorstruct from #1192 (anOption<serde_json::Value>field with a doc comment, which is{"description": "description"}) now deserializes and serializes back to the same JSON. The secondexample in that issue (
struct User;, which is{"default": null}) now parses too, butdefault: nullis lost on the way back becausedefaultis anOption<Value>; that is a separatelimitation.
Fixes #1192