Skip to content

Commit

Permalink
[utoipa-gen] request_body: Simplify ToTokensDiagnostics (#1235)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoo1 authored Dec 18, 2024
1 parent a01e194 commit fa227d7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
4 changes: 4 additions & 0 deletions utoipa-gen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Changed

* Simplified `ToTokensDiagnostics` for `request_body` (https://github.com/juhaku/utoipa/pull/1235)

### Fixed

* Fix tagged enum with flatten fields (https://github.com/juhaku/utoipa/pull/1208)
Expand Down
48 changes: 20 additions & 28 deletions utoipa-gen/src/path/request_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,42 +213,34 @@ impl Parse for RequestBodyAttr<'_> {

impl ToTokensDiagnostics for RequestBodyAttr<'_> {
fn to_tokens(&self, tokens: &mut TokenStream) -> Result<(), Diagnostics> {
let media_types = self
.content
.iter()
.map(|media_type| {
let default_content_type_result = media_type.schema.get_default_content_type();
let type_tree = media_type.schema.get_type_tree();

match (default_content_type_result, type_tree) {
(Ok(content_type), Ok(type_tree)) => Ok((content_type, media_type, type_tree)),
(Err(diagnostics), _) => Err(diagnostics),
(_, Err(diagnostics)) => Err(diagnostics),
}
})
.collect::<Result<Vec<_>, Diagnostics>>()?;

let any_required = media_types.iter().any(|(_, _, type_tree)| {
type_tree
.as_ref()
.map(|type_tree| !type_tree.is_option())
.unwrap_or(false)
});

tokens.extend(quote! {
utoipa::openapi::request_body::RequestBodyBuilder::new()
});
for (content_type, media_type, _) in media_types {
let content_type_tokens = media_type
.content_type
.as_ref()
.map(|content_type| content_type.to_token_stream())
.unwrap_or_else(|| content_type.to_token_stream());

let mut any_required = false;

for media_type in self.content.iter() {
let content_type_tokens = match media_type.content_type.as_ref() {
Some(ct) => ct.to_token_stream(),
None => media_type
.schema
.get_default_content_type()?
.to_token_stream(),
};

let content_tokens = media_type.try_to_token_stream()?;

tokens.extend(quote! {
.content(#content_type_tokens, #content_tokens)
});

any_required = any_required
|| media_type
.schema
.get_type_tree()?
.as_ref()
.map(|t| !t.is_option())
.unwrap_or(false);
}

if any_required {
Expand Down

0 comments on commit fa227d7

Please sign in to comment.