Skip to content

Commit

Permalink
fix: Fixed schema refs not generating when requested in `finish_api_w…
Browse files Browse the repository at this point in the history
…ith`
  • Loading branch information
Wicpar committed Jan 15, 2024
1 parent be3b6d7 commit 079a891
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
22 changes: 14 additions & 8 deletions crates/aide/src/axum/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ where
/// As opposed to [`route`](crate::axum::ApiRouter::route), this method only accepts an [`ApiMethodRouter`].
///
/// See [`axum::Router::route`] for details.
#[tracing::instrument(skip_all, fields(%path))]
#[tracing::instrument(skip_all, fields(% path))]
pub fn api_route(mut self, path: &str, mut method_router: ApiMethodRouter<S>) -> Self {
in_context(|ctx| {
let new_path_item = method_router.take_path_item();
Expand Down Expand Up @@ -355,12 +355,17 @@ where
where
F: FnOnce(TransformOpenApi) -> TransformOpenApi,
{
self.merge_api(api);
let _ = transform(TransformOpenApi::new(api));
self.merge_api_with(api, transform);
self.router
}

fn merge_api(&mut self, api: &mut OpenApi) {
self.merge_api_with(api, |x| x)
}
fn merge_api_with<F>(&mut self, api: &mut OpenApi, transform: F)
where
F: FnOnce(TransformOpenApi) -> TransformOpenApi,
{
if api.paths.is_none() {
api.paths = Some(Default::default());
}
Expand All @@ -377,17 +382,15 @@ where
})
.collect();

let _ = transform(TransformOpenApi::new(api));

let needs_reset =
in_context(|ctx| {
if !ctx.extract_schemas {
return false;
}

if api.components.is_none() {
api.components = Some(Components::default());
}

let components = api.components.as_mut().unwrap();
let components = api.components.get_or_insert_with(Default::default);

components
.schemas
Expand Down Expand Up @@ -626,6 +629,7 @@ impl<S> From<ApiRouter<S>> for Router<S> {
/// that implement [`IntoResponse`] and [`OperationOutput`],
/// it should not be implemented manually.
pub trait IntoApiResponse: IntoResponse + OperationOutput {}

impl<T> IntoApiResponse for T where T: IntoResponse + OperationOutput {}

/// Convenience extension trait for [`axum::Router`].
Expand Down Expand Up @@ -736,7 +740,9 @@ mod tests {
use axum::extract::State;

async fn test_handler1(State(_): State<TestState>) {}

async fn test_handler2(State(_): State<u8>) {}

async fn test_handler3() {}

#[derive(Clone, Copy)]
Expand Down
8 changes: 1 addition & 7 deletions crates/aide/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,15 +644,9 @@ impl<'t> TransformOperation<'t> {
R: OperationOutput,
F: FnOnce(TransformResponse<R::Inner>) -> TransformResponse<R::Inner>,
{
if self.operation.responses.is_none() {
self.operation.responses = Some(Default::default());
} else {
tracing::trace!("operation already has a default response");
}

in_context(|ctx| {
if let Some(mut res) = R::operation_response(ctx, self.operation) {
let responses = self.operation.responses.as_mut().unwrap();
let responses = self.operation.responses.get_or_insert_with(Default::default);
if responses.default.is_none() {
let t = transform(TransformResponse::new(&mut res));

Expand Down

0 comments on commit 079a891

Please sign in to comment.