Skip to content

Commit

Permalink
Implement OperationOutput for axum::response::NoContent
Browse files Browse the repository at this point in the history
  • Loading branch information
jplatte committed Jan 29, 2025
1 parent 96f2df4 commit f095162
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
22 changes: 20 additions & 2 deletions crates/aide/src/axum/outputs.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::openapi::{MediaType, Operation, Response, SchemaObject};
use crate::{
openapi::{MediaType, Operation, Response, SchemaObject},
util::no_content_response,
};
#[cfg(feature = "axum-form")]
use axum::extract::rejection::FormRejection;
#[cfg(feature = "axum-json")]
use axum::extract::rejection::JsonRejection;
use axum::response::{Html, Redirect};
use axum::response::{Html, NoContent, Redirect};
#[cfg(any(feature = "axum-json", feature = "axum-form"))]
use http::StatusCode;
use indexmap::IndexMap;
Expand All @@ -13,6 +16,21 @@ use schemars::JsonSchema;

use crate::{generate::GenContext, operation::OperationOutput};

impl OperationOutput for NoContent {
type Inner = ();

fn operation_response(_ctx: &mut GenContext, _operation: &mut Operation) -> Option<Response> {
Some(no_content_response())
}

fn inferred_responses(
_ctx: &mut GenContext,
_operation: &mut Operation,
) -> Vec<(Option<u16>, Response)> {
vec![(Some(204), no_content_response())]
}
}

#[cfg(feature = "axum-json")]
impl<T> OperationOutput for axum::Json<T>
where
Expand Down
6 changes: 2 additions & 4 deletions crates/aide/src/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{borrow::Cow, convert::Infallible, rc::Rc, sync::Arc};
use crate::{
openapi::{MediaType, Operation, RequestBody, Response},
operation::set_body,
util::no_content_response,
OperationInput,
};
use indexmap::IndexMap;
Expand Down Expand Up @@ -290,10 +291,7 @@ impl OperationOutput for () {
_ctx: &mut crate::generate::GenContext,
_operation: &mut Operation,
) -> Option<crate::openapi::Response> {
Some(Response {
description: "no content".to_string(),
..Default::default()
})
Some(no_content_response())
}

fn inferred_responses(
Expand Down
9 changes: 8 additions & 1 deletion crates/aide/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{
generate::GenContext,
openapi::{Operation, PathItem},
openapi::{Operation, PathItem, Response},
Error,
};

Expand Down Expand Up @@ -135,6 +135,13 @@ pub(crate) fn merge_paths(ctx: &mut GenContext, path: &str, target: &mut PathIte
target.extensions.extend(from.extensions);
}

pub(crate) fn no_content_response() -> Response {
Response {
description: "no content".to_string(),
..Default::default()
}
}

// FIXME: remove the code below when the upstream openapiv3 3.1 is available.
pub(crate) use spec::*;
mod spec {
Expand Down

0 comments on commit f095162

Please sign in to comment.