Skip to content

Commit 901a2dc

Browse files
committed
update samples
1 parent b86af83 commit 901a2dc

5 files changed

Lines changed: 372 additions & 13 deletions

File tree

modules/openapi-generator/src/test/resources/3_0/petstore.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,4 +738,4 @@ components:
738738
type:
739739
type: string
740740
message:
741-
type: string
741+
type: string

modules/openapi-generator/src/test/resources/3_0/rust-axum/rust-axum-oneof.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,14 @@ components:
7676
additionalProperties: false
7777
discriminator:
7878
propertyName: op
79+
mapping:
80+
yo: "#/components/schemas/YoMessage"
7981
oneOf:
8082
- "$ref": "#/components/schemas/Hello"
8183
- "$ref": "#/components/schemas/Greeting"
8284
- "$ref": "#/components/schemas/Goodbye"
8385
- "$ref": "#/components/schemas/SomethingCompletelyDifferent"
86+
- "$ref": "#/components/schemas/YoMessage"
8487
title: Message
8588
Hello:
8689
type: object
@@ -141,3 +144,17 @@ components:
141144
type: object
142145
type: array
143146
- type: object
147+
YoMessage:
148+
type: object
149+
title: Yo
150+
properties:
151+
d:
152+
type: object
153+
properties:
154+
nickname:
155+
type: string
156+
required:
157+
- nickname
158+
required:
159+
- op
160+
- d

samples/server/petstore/rust-axum/output/rust-axum-oneof/src/models.rs

Lines changed: 316 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,10 +983,16 @@ impl std::convert::TryFrom<HeaderValue> for header::IntoHeaderValue<HelloD> {
983983
#[serde(tag = "op")]
984984
#[allow(non_camel_case_types, clippy::large_enum_variant)]
985985
pub enum Message {
986+
#[serde(alias = "Hello")]
986987
Hello(models::Hello),
988+
#[serde(alias = "Greeting")]
987989
Greeting(models::Greeting),
990+
#[serde(alias = "Goodbye")]
988991
Goodbye(models::Goodbye),
992+
#[serde(alias = "SomethingCompletelyDifferent")]
989993
SomethingCompletelyDifferent(models::SomethingCompletelyDifferent),
994+
#[serde(alias = "yo")]
995+
YoMessage(models::YoMessage),
990996
}
991997

992998
impl validator::Validate for Message {
@@ -996,6 +1002,7 @@ impl validator::Validate for Message {
9961002
Self::Greeting(v) => v.validate(),
9971003
Self::Goodbye(v) => v.validate(),
9981004
Self::SomethingCompletelyDifferent(v) => v.validate(),
1005+
Self::YoMessage(v) => v.validate(),
9991006
}
10001007
}
10011008
}
@@ -1021,6 +1028,7 @@ impl serde::Serialize for Message {
10211028
Self::Greeting(x) => x.serialize(serializer),
10221029
Self::Goodbye(x) => x.serialize(serializer),
10231030
Self::SomethingCompletelyDifferent(x) => x.serialize(serializer),
1031+
Self::YoMessage(x) => x.serialize(serializer),
10241032
}
10251033
}
10261034
}
@@ -1045,6 +1053,11 @@ impl From<models::SomethingCompletelyDifferent> for Message {
10451053
Self::SomethingCompletelyDifferent(value)
10461054
}
10471055
}
1056+
impl From<models::YoMessage> for Message {
1057+
fn from(value: models::YoMessage) -> Self {
1058+
Self::YoMessage(value)
1059+
}
1060+
}
10481061

10491062
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
10501063
#[serde(untagged)]
@@ -1084,3 +1097,306 @@ impl From<crate::types::Object> for SomethingCompletelyDifferent {
10841097
Self::Object(value)
10851098
}
10861099
}
1100+
1101+
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, validator::Validate)]
1102+
#[cfg_attr(feature = "conversion", derive(frunk::LabelledGeneric))]
1103+
pub struct YoMessage {
1104+
#[serde(rename = "d")]
1105+
#[validate(nested)]
1106+
pub d: models::YoMessageD,
1107+
1108+
#[serde(default = "YoMessage::_name_for_op")]
1109+
#[serde(serialize_with = "YoMessage::_serialize_op")]
1110+
#[serde(rename = "op")]
1111+
pub op: String,
1112+
}
1113+
1114+
impl YoMessage {
1115+
fn _name_for_op() -> String {
1116+
String::from("yo")
1117+
}
1118+
1119+
fn _serialize_op<S>(_: &String, s: S) -> Result<S::Ok, S::Error>
1120+
where
1121+
S: serde::Serializer,
1122+
{
1123+
s.serialize_str(&Self::_name_for_op())
1124+
}
1125+
}
1126+
1127+
impl YoMessage {
1128+
#[allow(clippy::new_without_default, clippy::too_many_arguments)]
1129+
pub fn new(d: models::YoMessageD) -> YoMessage {
1130+
YoMessage {
1131+
d,
1132+
op: Self::_name_for_op(),
1133+
}
1134+
}
1135+
}
1136+
1137+
/// Converts the YoMessage value to the Query Parameters representation (style=form, explode=false)
1138+
/// specified in https://swagger.io/docs/specification/serialization/
1139+
/// Should be implemented in a serde serializer
1140+
impl std::fmt::Display for YoMessage {
1141+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1142+
let params: Vec<Option<String>> = vec![
1143+
// Skipping d in query parameter serialization
1144+
Some("op".to_string()),
1145+
Some(self.op.to_string()),
1146+
];
1147+
1148+
write!(
1149+
f,
1150+
"{}",
1151+
params.into_iter().flatten().collect::<Vec<_>>().join(",")
1152+
)
1153+
}
1154+
}
1155+
1156+
/// Converts Query Parameters representation (style=form, explode=false) to a YoMessage value
1157+
/// as specified in https://swagger.io/docs/specification/serialization/
1158+
/// Should be implemented in a serde deserializer
1159+
impl std::str::FromStr for YoMessage {
1160+
type Err = String;
1161+
1162+
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
1163+
/// An intermediate representation of the struct to use for parsing.
1164+
#[derive(Default)]
1165+
#[allow(dead_code)]
1166+
struct IntermediateRep {
1167+
pub d: Vec<models::YoMessageD>,
1168+
pub op: Vec<String>,
1169+
}
1170+
1171+
let mut intermediate_rep = IntermediateRep::default();
1172+
1173+
// Parse into intermediate representation
1174+
let mut string_iter = s.split(',');
1175+
let mut key_result = string_iter.next();
1176+
1177+
while key_result.is_some() {
1178+
let val = match string_iter.next() {
1179+
Some(x) => x,
1180+
None => {
1181+
return std::result::Result::Err(
1182+
"Missing value while parsing YoMessage".to_string(),
1183+
);
1184+
}
1185+
};
1186+
1187+
if let Some(key) = key_result {
1188+
#[allow(clippy::match_single_binding)]
1189+
match key {
1190+
#[allow(clippy::redundant_clone)]
1191+
"d" => intermediate_rep.d.push(
1192+
<models::YoMessageD as std::str::FromStr>::from_str(val)
1193+
.map_err(|x| x.to_string())?,
1194+
),
1195+
#[allow(clippy::redundant_clone)]
1196+
"op" => intermediate_rep.op.push(
1197+
<String as std::str::FromStr>::from_str(val).map_err(|x| x.to_string())?,
1198+
),
1199+
_ => {
1200+
return std::result::Result::Err(
1201+
"Unexpected key while parsing YoMessage".to_string(),
1202+
);
1203+
}
1204+
}
1205+
}
1206+
1207+
// Get the next key
1208+
key_result = string_iter.next();
1209+
}
1210+
1211+
// Use the intermediate representation to return the struct
1212+
std::result::Result::Ok(YoMessage {
1213+
d: intermediate_rep
1214+
.d
1215+
.into_iter()
1216+
.next()
1217+
.ok_or_else(|| "d missing in YoMessage".to_string())?,
1218+
op: intermediate_rep
1219+
.op
1220+
.into_iter()
1221+
.next()
1222+
.ok_or_else(|| "op missing in YoMessage".to_string())?,
1223+
})
1224+
}
1225+
}
1226+
1227+
// Methods for converting between header::IntoHeaderValue<YoMessage> and HeaderValue
1228+
1229+
#[cfg(feature = "server")]
1230+
impl std::convert::TryFrom<header::IntoHeaderValue<YoMessage>> for HeaderValue {
1231+
type Error = String;
1232+
1233+
fn try_from(
1234+
hdr_value: header::IntoHeaderValue<YoMessage>,
1235+
) -> std::result::Result<Self, Self::Error> {
1236+
let hdr_value = hdr_value.to_string();
1237+
match HeaderValue::from_str(&hdr_value) {
1238+
std::result::Result::Ok(value) => std::result::Result::Ok(value),
1239+
std::result::Result::Err(e) => std::result::Result::Err(format!(
1240+
r#"Invalid header value for YoMessage - value: {hdr_value} is invalid {e}"#
1241+
)),
1242+
}
1243+
}
1244+
}
1245+
1246+
#[cfg(feature = "server")]
1247+
impl std::convert::TryFrom<HeaderValue> for header::IntoHeaderValue<YoMessage> {
1248+
type Error = String;
1249+
1250+
fn try_from(hdr_value: HeaderValue) -> std::result::Result<Self, Self::Error> {
1251+
match hdr_value.to_str() {
1252+
std::result::Result::Ok(value) => {
1253+
match <YoMessage as std::str::FromStr>::from_str(value) {
1254+
std::result::Result::Ok(value) => {
1255+
std::result::Result::Ok(header::IntoHeaderValue(value))
1256+
}
1257+
std::result::Result::Err(err) => std::result::Result::Err(format!(
1258+
r#"Unable to convert header value '{value}' into YoMessage - {err}"#
1259+
)),
1260+
}
1261+
}
1262+
std::result::Result::Err(e) => std::result::Result::Err(format!(
1263+
r#"Unable to convert header: {hdr_value:?} to string: {e}"#
1264+
)),
1265+
}
1266+
}
1267+
}
1268+
1269+
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, validator::Validate)]
1270+
#[cfg_attr(feature = "conversion", derive(frunk::LabelledGeneric))]
1271+
pub struct YoMessageD {
1272+
#[serde(rename = "nickname")]
1273+
#[validate(custom(function = "check_xss_string"))]
1274+
pub nickname: String,
1275+
}
1276+
1277+
impl YoMessageD {
1278+
#[allow(clippy::new_without_default, clippy::too_many_arguments)]
1279+
pub fn new(nickname: String) -> YoMessageD {
1280+
YoMessageD { nickname }
1281+
}
1282+
}
1283+
1284+
/// Converts the YoMessageD value to the Query Parameters representation (style=form, explode=false)
1285+
/// specified in https://swagger.io/docs/specification/serialization/
1286+
/// Should be implemented in a serde serializer
1287+
impl std::fmt::Display for YoMessageD {
1288+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1289+
let params: Vec<Option<String>> = vec![
1290+
Some("nickname".to_string()),
1291+
Some(self.nickname.to_string()),
1292+
];
1293+
1294+
write!(
1295+
f,
1296+
"{}",
1297+
params.into_iter().flatten().collect::<Vec<_>>().join(",")
1298+
)
1299+
}
1300+
}
1301+
1302+
/// Converts Query Parameters representation (style=form, explode=false) to a YoMessageD value
1303+
/// as specified in https://swagger.io/docs/specification/serialization/
1304+
/// Should be implemented in a serde deserializer
1305+
impl std::str::FromStr for YoMessageD {
1306+
type Err = String;
1307+
1308+
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
1309+
/// An intermediate representation of the struct to use for parsing.
1310+
#[derive(Default)]
1311+
#[allow(dead_code)]
1312+
struct IntermediateRep {
1313+
pub nickname: Vec<String>,
1314+
}
1315+
1316+
let mut intermediate_rep = IntermediateRep::default();
1317+
1318+
// Parse into intermediate representation
1319+
let mut string_iter = s.split(',');
1320+
let mut key_result = string_iter.next();
1321+
1322+
while key_result.is_some() {
1323+
let val = match string_iter.next() {
1324+
Some(x) => x,
1325+
None => {
1326+
return std::result::Result::Err(
1327+
"Missing value while parsing YoMessageD".to_string(),
1328+
);
1329+
}
1330+
};
1331+
1332+
if let Some(key) = key_result {
1333+
#[allow(clippy::match_single_binding)]
1334+
match key {
1335+
#[allow(clippy::redundant_clone)]
1336+
"nickname" => intermediate_rep.nickname.push(
1337+
<String as std::str::FromStr>::from_str(val).map_err(|x| x.to_string())?,
1338+
),
1339+
_ => {
1340+
return std::result::Result::Err(
1341+
"Unexpected key while parsing YoMessageD".to_string(),
1342+
);
1343+
}
1344+
}
1345+
}
1346+
1347+
// Get the next key
1348+
key_result = string_iter.next();
1349+
}
1350+
1351+
// Use the intermediate representation to return the struct
1352+
std::result::Result::Ok(YoMessageD {
1353+
nickname: intermediate_rep
1354+
.nickname
1355+
.into_iter()
1356+
.next()
1357+
.ok_or_else(|| "nickname missing in YoMessageD".to_string())?,
1358+
})
1359+
}
1360+
}
1361+
1362+
// Methods for converting between header::IntoHeaderValue<YoMessageD> and HeaderValue
1363+
1364+
#[cfg(feature = "server")]
1365+
impl std::convert::TryFrom<header::IntoHeaderValue<YoMessageD>> for HeaderValue {
1366+
type Error = String;
1367+
1368+
fn try_from(
1369+
hdr_value: header::IntoHeaderValue<YoMessageD>,
1370+
) -> std::result::Result<Self, Self::Error> {
1371+
let hdr_value = hdr_value.to_string();
1372+
match HeaderValue::from_str(&hdr_value) {
1373+
std::result::Result::Ok(value) => std::result::Result::Ok(value),
1374+
std::result::Result::Err(e) => std::result::Result::Err(format!(
1375+
r#"Invalid header value for YoMessageD - value: {hdr_value} is invalid {e}"#
1376+
)),
1377+
}
1378+
}
1379+
}
1380+
1381+
#[cfg(feature = "server")]
1382+
impl std::convert::TryFrom<HeaderValue> for header::IntoHeaderValue<YoMessageD> {
1383+
type Error = String;
1384+
1385+
fn try_from(hdr_value: HeaderValue) -> std::result::Result<Self, Self::Error> {
1386+
match hdr_value.to_str() {
1387+
std::result::Result::Ok(value) => {
1388+
match <YoMessageD as std::str::FromStr>::from_str(value) {
1389+
std::result::Result::Ok(value) => {
1390+
std::result::Result::Ok(header::IntoHeaderValue(value))
1391+
}
1392+
std::result::Result::Err(err) => std::result::Result::Err(format!(
1393+
r#"Unable to convert header value '{value}' into YoMessageD - {err}"#
1394+
)),
1395+
}
1396+
}
1397+
std::result::Result::Err(e) => std::result::Result::Err(format!(
1398+
r#"Unable to convert header: {hdr_value:?} to string: {e}"#
1399+
)),
1400+
}
1401+
}
1402+
}

0 commit comments

Comments
 (0)