Skip to content

Commit 9415233

Browse files
committed
Update
1 parent fdbd1e8 commit 9415233

3 files changed

Lines changed: 252 additions & 15 deletions

File tree

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ paths:
1919
"application/json":
2020
schema:
2121
"$ref": "#/components/schemas/Message"
22-
"/issue21143":
22+
"/issue21143_1":
2323
post:
24-
operationId: i21143
24+
operationId: i21143_1
2525
requestBody:
2626
required: true
2727
content:
@@ -37,6 +37,38 @@ paths:
3737
"application/json":
3838
schema:
3939
"$ref": "#/components/schemas/Message"
40+
"/issue21143_2":
41+
post:
42+
operationId: i21143_2
43+
requestBody:
44+
required: true
45+
content:
46+
"application/json":
47+
schema:
48+
type: string
49+
responses:
50+
"200":
51+
description: Re-serialize and echo the request data
52+
content:
53+
"application/json":
54+
schema:
55+
"$ref": "#/components/schemas/Message"
56+
"/issue21143_3":
57+
post:
58+
operationId: i21143_3
59+
requestBody:
60+
required: true
61+
content:
62+
"application/json":
63+
schema:
64+
type: integer
65+
responses:
66+
"200":
67+
description: Re-serialize and echo the request data
68+
content:
69+
"application/json":
70+
schema:
71+
"$ref": "#/components/schemas/Message"
4072
components:
4173
schemas:
4274
Message:

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

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,23 @@ pub enum FooResponse {
1818
#[derive(Debug, PartialEq, Serialize, Deserialize)]
1919
#[must_use]
2020
#[allow(clippy::large_enum_variant)]
21-
pub enum I21143Response {
21+
pub enum I211431Response {
22+
/// Re-serialize and echo the request data
23+
Status200_Re(models::Message),
24+
}
25+
26+
#[derive(Debug, PartialEq, Serialize, Deserialize)]
27+
#[must_use]
28+
#[allow(clippy::large_enum_variant)]
29+
pub enum I211432Response {
30+
/// Re-serialize and echo the request data
31+
Status200_Re(models::Message),
32+
}
33+
34+
#[derive(Debug, PartialEq, Serialize, Deserialize)]
35+
#[must_use]
36+
#[allow(clippy::large_enum_variant)]
37+
pub enum I211433Response {
2238
/// Re-serialize and echo the request data
2339
Status200_Re(models::Message),
2440
}
@@ -37,13 +53,33 @@ pub trait Default<E: std::fmt::Debug + Send + Sync + 'static = ()>: super::Error
3753
body: &models::Message,
3854
) -> Result<FooResponse, E>;
3955

40-
/// I21143 - POST /issue21143
41-
async fn i21143(
56+
/// I211431 - POST /issue21143_1
57+
async fn i211431(
4258
&self,
4359

4460
method: &Method,
4561
host: &Host,
4662
cookies: &CookieJar,
4763
body: &Vec<i32>,
48-
) -> Result<I21143Response, E>;
64+
) -> Result<I211431Response, E>;
65+
66+
/// I211432 - POST /issue21143_2
67+
async fn i211432(
68+
&self,
69+
70+
method: &Method,
71+
host: &Host,
72+
cookies: &CookieJar,
73+
body: &String,
74+
) -> Result<I211432Response, E>;
75+
76+
/// I211433 - POST /issue21143_3
77+
async fn i211433(
78+
&self,
79+
80+
method: &Method,
81+
host: &Host,
82+
cookies: &CookieJar,
83+
body: &i32,
84+
) -> Result<I211433Response, E>;
4985
}

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

Lines changed: 178 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ where
2828
// build our application with a route
2929
Router::new()
3030
.route("/", post(foo::<I, A, E>))
31-
.route("/issue21143", post(i21143::<I, A, E>))
31+
.route("/issue21143_1", post(i211431::<I, A, E>))
32+
.route("/issue21143_2", post(i211432::<I, A, E>))
33+
.route("/issue21143_3", post(i211433::<I, A, E>))
3234
.with_state(api_impl)
3335
}
3436

@@ -117,20 +119,20 @@ where
117119

118120
#[derive(validator::Validate)]
119121
#[allow(dead_code)]
120-
struct I21143BodyValidator<'a> {
122+
struct I211431BodyValidator<'a> {
121123
body: &'a Vec<i32>,
122124
}
123125

124126
#[tracing::instrument(skip_all)]
125-
fn i21143_validation(body: Vec<i32>) -> std::result::Result<(Vec<i32>,), ValidationErrors> {
126-
let b = I21143BodyValidator { body: &body };
127+
fn i211431_validation(body: Vec<i32>) -> std::result::Result<(Vec<i32>,), ValidationErrors> {
128+
let b = I211431BodyValidator { body: &body };
127129
b.validate()?;
128130

129131
Ok((body,))
130132
}
131-
/// I21143 - POST /issue21143
133+
/// I211431 - POST /issue21143_1
132134
#[tracing::instrument(skip_all)]
133-
async fn i21143<I, A, E>(
135+
async fn i211431<I, A, E>(
134136
method: Method,
135137
host: Host,
136138
cookies: CookieJar,
@@ -143,7 +145,7 @@ where
143145
E: std::fmt::Debug + Send + Sync + 'static,
144146
{
145147
#[allow(clippy::redundant_closure)]
146-
let validation = tokio::task::spawn_blocking(move || i21143_validation(body))
148+
let validation = tokio::task::spawn_blocking(move || i211431_validation(body))
147149
.await
148150
.unwrap();
149151

@@ -156,14 +158,181 @@ where
156158

157159
let result = api_impl
158160
.as_ref()
159-
.i21143(&method, &host, &cookies, &body)
161+
.i211431(&method, &host, &cookies, &body)
160162
.await;
161163

162164
let mut response = Response::builder();
163165

164166
let resp = match result {
165167
Ok(rsp) => match rsp {
166-
apis::default::I21143Response::Status200_Re(body) => {
168+
apis::default::I211431Response::Status200_Re(body) => {
169+
let mut response = response.status(200);
170+
{
171+
let mut response_headers = response.headers_mut().unwrap();
172+
response_headers
173+
.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
174+
}
175+
176+
let body_content = tokio::task::spawn_blocking(move || {
177+
serde_json::to_vec(&body).map_err(|e| {
178+
error!(error = ?e);
179+
StatusCode::INTERNAL_SERVER_ERROR
180+
})
181+
})
182+
.await
183+
.unwrap()?;
184+
response.body(Body::from(body_content))
185+
}
186+
},
187+
Err(why) => {
188+
// Application code returned an error. This should not happen, as the implementation should
189+
// return a valid response.
190+
return api_impl
191+
.as_ref()
192+
.handle_error(&method, &host, &cookies, why)
193+
.await;
194+
}
195+
};
196+
197+
resp.map_err(|e| {
198+
error!(error = ?e);
199+
StatusCode::INTERNAL_SERVER_ERROR
200+
})
201+
}
202+
203+
#[derive(validator::Validate)]
204+
#[allow(dead_code)]
205+
struct I211432BodyValidator<'a> {
206+
#[validate(custom(function = "check_xss_string"))]
207+
body: &'a String,
208+
}
209+
210+
#[tracing::instrument(skip_all)]
211+
fn i211432_validation(body: String) -> std::result::Result<(String,), ValidationErrors> {
212+
let b = I211432BodyValidator { body: &body };
213+
b.validate()?;
214+
215+
Ok((body,))
216+
}
217+
/// I211432 - POST /issue21143_2
218+
#[tracing::instrument(skip_all)]
219+
async fn i211432<I, A, E>(
220+
method: Method,
221+
host: Host,
222+
cookies: CookieJar,
223+
State(api_impl): State<I>,
224+
Json(body): Json<String>,
225+
) -> Result<Response, StatusCode>
226+
where
227+
I: AsRef<A> + Send + Sync,
228+
A: apis::default::Default<E> + Send + Sync,
229+
E: std::fmt::Debug + Send + Sync + 'static,
230+
{
231+
#[allow(clippy::redundant_closure)]
232+
let validation = tokio::task::spawn_blocking(move || i211432_validation(body))
233+
.await
234+
.unwrap();
235+
236+
let Ok((body,)) = validation else {
237+
return Response::builder()
238+
.status(StatusCode::BAD_REQUEST)
239+
.body(Body::from(validation.unwrap_err().to_string()))
240+
.map_err(|_| StatusCode::BAD_REQUEST);
241+
};
242+
243+
let result = api_impl
244+
.as_ref()
245+
.i211432(&method, &host, &cookies, &body)
246+
.await;
247+
248+
let mut response = Response::builder();
249+
250+
let resp = match result {
251+
Ok(rsp) => match rsp {
252+
apis::default::I211432Response::Status200_Re(body) => {
253+
let mut response = response.status(200);
254+
{
255+
let mut response_headers = response.headers_mut().unwrap();
256+
response_headers
257+
.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
258+
}
259+
260+
let body_content = tokio::task::spawn_blocking(move || {
261+
serde_json::to_vec(&body).map_err(|e| {
262+
error!(error = ?e);
263+
StatusCode::INTERNAL_SERVER_ERROR
264+
})
265+
})
266+
.await
267+
.unwrap()?;
268+
response.body(Body::from(body_content))
269+
}
270+
},
271+
Err(why) => {
272+
// Application code returned an error. This should not happen, as the implementation should
273+
// return a valid response.
274+
return api_impl
275+
.as_ref()
276+
.handle_error(&method, &host, &cookies, why)
277+
.await;
278+
}
279+
};
280+
281+
resp.map_err(|e| {
282+
error!(error = ?e);
283+
StatusCode::INTERNAL_SERVER_ERROR
284+
})
285+
}
286+
287+
#[derive(validator::Validate)]
288+
#[allow(dead_code)]
289+
struct I211433BodyValidator<'a> {
290+
body: &'a i32,
291+
}
292+
293+
#[tracing::instrument(skip_all)]
294+
fn i211433_validation(body: i32) -> std::result::Result<(i32,), ValidationErrors> {
295+
let b = I211433BodyValidator { body: &body };
296+
b.validate()?;
297+
298+
Ok((body,))
299+
}
300+
/// I211433 - POST /issue21143_3
301+
#[tracing::instrument(skip_all)]
302+
async fn i211433<I, A, E>(
303+
method: Method,
304+
host: Host,
305+
cookies: CookieJar,
306+
State(api_impl): State<I>,
307+
Json(body): Json<i32>,
308+
) -> Result<Response, StatusCode>
309+
where
310+
I: AsRef<A> + Send + Sync,
311+
A: apis::default::Default<E> + Send + Sync,
312+
E: std::fmt::Debug + Send + Sync + 'static,
313+
{
314+
#[allow(clippy::redundant_closure)]
315+
let validation = tokio::task::spawn_blocking(move || i211433_validation(body))
316+
.await
317+
.unwrap();
318+
319+
let Ok((body,)) = validation else {
320+
return Response::builder()
321+
.status(StatusCode::BAD_REQUEST)
322+
.body(Body::from(validation.unwrap_err().to_string()))
323+
.map_err(|_| StatusCode::BAD_REQUEST);
324+
};
325+
326+
let result = api_impl
327+
.as_ref()
328+
.i211433(&method, &host, &cookies, &body)
329+
.await;
330+
331+
let mut response = Response::builder();
332+
333+
let resp = match result {
334+
Ok(rsp) => match rsp {
335+
apis::default::I211433Response::Status200_Re(body) => {
167336
let mut response = response.status(200);
168337
{
169338
let mut response_headers = response.headers_mut().unwrap();

0 commit comments

Comments
 (0)