8000 Adding support for Content in Parameters by Gaelik-git · Pull Request #1399 · juhaku/utoipa · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Adding support for Content in Parameters #1399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions utoipa/src/openapi/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
use crate::Path;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::BTreeMap;

use super::{
builder,
extensions::Extensions,
request_body::RequestBody,
response::{Response, Responses},
security::SecurityRequirement,
set_value, Deprecated, ExternalDocs, RefOr, Required, Schema, Server,
set_value, Content, Deprecated, ExternalDocs, RefOr, Required, Schema, Server,
};

#[cfg(not(feature = "preserve_path_order"))]
Expand Down Expand Up @@ -705,6 +706,10 @@ builder! {
#[serde(skip_serializing_if = "Option::is_none")]
pub schema: Option<RefOr<Schema>>,

/// For more complex scenarios, the content property can define the media type and schema of the parameter.
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
pub content: BTreeMap<String, Content>,

/// Describes how [`Parameter`] is being serialized depending on [`Parameter::schema`] (type of a content).
/// Default value is based on [`ParameterIn`].
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -791,6 +796,12 @@ impl ParameterBuilder {
set_value!(self schema component.map(|component| component.into()))
}

/// Add a content for a specific media type
pub fn content<K: Into<String>, V: Into<Content>>(mut self, media_type: K, content: V) -> Self {
self.content.insert(media_type.into(), content.into());
self
}

/// Add or change serialization style of [`Parameter`].
pub fn style(mut self, style: Option<ParameterStyle>) -> Self {
set_value!(self style style)
Expand Down Expand Up @@ -871,8 +882,11 @@ pub enum ParameterStyle {

#[cfg(test)]
mod tests {
use super::{HttpMethod, Operation, OperationBuilder};
use crate::openapi::{security::SecurityRequirement, server::Server, PathItem, PathsBuilder};
use super::{HttpMethod, Operation, OperationBuilder, ParameterBuilder};
use crate::openapi::{
schema::RefBuilder, security::SecurityRequirement, server::Server, ContentBuilder,
PathItem, PathsBuilder,
};

#[test]
fn test_path_order() {
Expand Down Expand Up @@ -1012,4 +1026,30 @@ mod tests {

assert!(operation.servers.is_some());
}

#[test]
fn parameter_with_content() {
let param = ParameterBuilder::new()
.name("filter")
.parameter_in(super::ParameterIn::Query)
.required(crate::openapi::Required::True)
.content(
"application/json",
ContentBuilder::new()
.schema(Some(
RefBuilder::new()
.ref_location_from_schema_name("Filter")
.build(),
))
.build(),
)
.build();

let param = serde_json::to_string(&param).unwrap();

assert_eq!(
param,
r###"{"name":"filter","in":"query","required":true,"content":{"application/json":{"schema":{"$ref":"#/components/schemas/Filter"}}}}"###
)
}
}
0