openapi_nexus/spec/oas32/spec/
request_body.rs1use std::collections::BTreeMap;
2
3use serde::{Deserialize, Serialize};
4
5use super::{ErrorRef, FromRef, MediaType, OpenApiV32Spec, Ref, RefType};
6
7#[derive(Debug, Clone, Default, PartialEq, Deserialize, Serialize)]
9pub struct RequestBody {
10 #[serde(skip_serializing_if = "Option::is_none")]
11 pub description: Option<String>,
12
13 pub content: BTreeMap<String, MediaType>,
14
15 #[serde(skip_serializing_if = "Option::is_none")]
16 pub required: Option<bool>,
17}
18
19impl FromRef for RequestBody {
20 fn from_ref(spec: &OpenApiV32Spec, path: &str) -> Result<Self, ErrorRef> {
21 let refpath = path.parse::<Ref>()?;
22
23 match refpath.kind {
24 RefType::RequestBody => spec
25 .components
26 .as_ref()
27 .and_then(|cs| cs.request_bodies.get(&refpath.name))
28 .ok_or_else(|| ErrorRef::Unresolvable {
29 path: path.to_owned(),
30 })
31 .and_then(|oor| oor.resolve(spec)),
32
33 typ => Err(ErrorRef::MismatchedType {
34 expected: typ,
35 actual: RefType::RequestBody,
36 }),
37 }
38 }
39}