From a502b2178cec8ff147dc8a60edd71603b5b5c57f Mon Sep 17 00:00:00 2001 From: John Soo Date: Wed, 18 Dec 2024 07:56:47 -0700 Subject: [PATCH] [utoipa-gen] MediaTypeAttr: Make parse_named_attributes a method (#1236) --------- Co-authored-by: Juha Kukkonen --- utoipa-gen/CHANGELOG.md | 1 + utoipa-gen/src/path/media_type.rs | 6 +++--- utoipa-gen/src/path/request_body.rs | 13 +++++-------- utoipa-gen/src/path/response.rs | 11 +++++------ 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/utoipa-gen/CHANGELOG.md b/utoipa-gen/CHANGELOG.md index f836ba87..e170f1b4 100644 --- a/utoipa-gen/CHANGELOG.md +++ b/utoipa-gen/CHANGELOG.md @@ -8,6 +8,7 @@ ### Changed +* Make `parse_named_attributes` a method of `MediaTypeAttr` (https://github.com/juhaku/utoipa/pull/1236) * Use a re-exported `serde_json` dependency in macros instead of implicitly requiring it as dependency in end projects (https://github.com/juhaku/utoipa/pull/1243) * Simplified `ToTokensDiagnostics` for `request_body` (https://github.com/juhaku/utoipa/pull/1235) * `Info::from_env()` sets `License::identifier` (https://github.com/juhaku/utoipa/pull/1233) diff --git a/utoipa-gen/src/path/media_type.rs b/utoipa-gen/src/path/media_type.rs index fd54b58e..31cd9440 100644 --- a/utoipa-gen/src/path/media_type.rs +++ b/utoipa-gen/src/path/media_type.rs @@ -90,7 +90,7 @@ impl<'m> MediaTypeAttr<'m> { } pub fn parse_named_attributes( - media_type: &mut MediaTypeAttr, + &mut self, input: ParseStream, attribute: &Ident, ) -> syn::Result<()> { @@ -98,12 +98,12 @@ impl<'m> MediaTypeAttr<'m> { match name { "example" => { - media_type.example = Some(parse_utils::parse_next(input, || { + self.example = Some(parse_utils::parse_next(input, || { AnyValue::parse_any(input) })?) } "examples" => { - media_type.examples = parse_utils::parse_comma_separated_within_parenthesis(input)? + self.examples = parse_utils::parse_comma_separated_within_parenthesis(input)? } // // TODO implement encoding support // "encoding" => (), diff --git a/utoipa-gen/src/path/request_body.rs b/utoipa-gen/src/path/request_body.rs index d8570451..37251a58 100644 --- a/utoipa-gen/src/path/request_body.rs +++ b/utoipa-gen/src/path/request_body.rs @@ -174,14 +174,11 @@ impl Parse for RequestBodyAttr<'_> { request_body_attr.description = Some(parse::description(&group)?); } _ => { - MediaTypeAttr::parse_named_attributes( - request_body_attr - .content - .get_mut(0) - .expect("parse request body named attributes must have media type"), - &group, - &ident, - )?; + request_body_attr + .content + .get_mut(0) + .expect("parse request body named attributes must have media type") + .parse_named_attributes(&group, &ident)?; } } diff --git a/utoipa-gen/src/path/response.rs b/utoipa-gen/src/path/response.rs index 6ad97da8..177738af 100644 --- a/utoipa-gen/src/path/response.rs +++ b/utoipa-gen/src/path/response.rs @@ -323,13 +323,12 @@ impl<'r> ResponseValue<'r> { self.links = parse_utils::parse_comma_separated_within_parenthesis(input)?; } _ => { - MediaTypeAttr::parse_named_attributes( - self.content.get_mut(0).expect( + self.content + .get_mut(0) + .expect( "parse named attributes response value must have one media type by default", - ), - input, - attribute, - )?; + ) + .parse_named_attributes(input, attribute)?; } } Ok(())