Implement automatic schema collection for requests #1066
Merged
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.
This PR implements functionality to recognize recursive schemas within types implementing
ToSchema
trait by (derive). These schema references will be collected to the single list of schemas which are added to the schema automatically from their usage point.If schema is registered at
schemas(MyType)
forOpenApi
then the type and it's schema references found recursively will be registered to theOpenApi
.If the type is defined in
#[utoipa::path(request_body = ...)]
the schema and it's recursively found schema references will be automatically added to the schema when the handler is registered to theOpenApi
.If the framework supports request body from http handler function arguments (like
actix_web
,axum
) then the schema will be added to theOpenApi
as well with recursively found schema references when the handler is registered to theOpenApi
.This commit also refactors the parsing for
#[utoipa::path(request_body = ...)]
Which enables the support for automatic schema reference recognition and collection. This also comes with the possibility to only addrequest_body(content_type = "my/type")
without the schema if wanted. More over from now on therequest_body
supports defining multiple content objects allowing users to define multiple request body content's like the responses support.Breaking changes
This
request_body
parsing is in over all breaking change and the changed spec need be checked from the docs or tests. In short thecontent_type = ...
does not support anymore array syntax. E.g.content_type = [...]
is not possible anymore. Instead to support multiple content types in request body on must define multiple content objects similar to responses.Example of supported syntax to demonstrate the automatic schema collecting from
test_collect_schemas
handler when registered to theOpenApi
. Same way ifPerson
was declared toOpenApi
viaschemas(Person)
thenAccount
would also get registered.Fixes #692
Relates #662 #591 #965 #779