Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Wicpar committed Mar 25, 2024
2 parents d4d7e2d + 7e2d71e commit 8bd148a
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 83 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/typos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Typos

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
typos:
name: Check for typos
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: crate-ci/typos@v1.19.0
9 changes: 9 additions & 0 deletions .typos.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[default]
extend-ignore-re = [
"ff32ba0",
]

[files]
extend-exclude = [
"crates/aide/res/**/*.js",
]
1 change: 0 additions & 1 deletion crates/aide/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ serde_json = "1"
thiserror = "1"
tracing = "0"
aide-macros = { version = "0.7.0", path = "../aide-macros", optional = true }
derive_more = "0.99.17"

bytes = { version = "1", optional = true }
http = { version = "1.0.0", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/aide/src/axum/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ mod private {
pub trait Sealed {}
}

/// A trait that extens [`axum::handler::Handler`] with API operation
/// A trait that extends [`axum::handler::Handler`] with API operation
/// details.
///
/// Just like axum's `Handler`, it is automatically implemented
Expand Down
2 changes: 1 addition & 1 deletion crates/aide/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn infer_responses(infer: bool) {
});
}

/// Output all theoretically possbile error responses
/// Output all theoretically possible error responses
/// including framework-specific ones.
///
/// This is disabled by default.
Expand Down
56 changes: 37 additions & 19 deletions crates/aide/src/helpers/no_api.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,19 @@
use derive_more::{AsMut, AsRef, Deref, DerefMut, From};
use std::ops::{Deref, DerefMut};

use serde::{Deserialize, Serialize};

use crate::{OperationInput, OperationOutput};

/// Allows non [`OperationInput`] or [`OperationOutput`] types to be used in aide handlers with a default empty documentation.
/// Allows non [`OperationInput`] or [`OperationOutput`] types to be used in aide handlers with a default empty documentation.
///
/// For types that already implement [`OperationInput`] or [`OperationOutput`] it overrides the documentation and hides it.
/// ```ignore
/// pub async fn my_sqlx_tx_endpoint(
/// NoApi(mut tx): NoApi<Tx<sqlx::Any>> // allows usage of the TX
/// ) -> NoApi<Json<YourResult>> // Hides the API of the return type
/// # {}
/// ```
#[derive(
Copy,
Clone,
Debug,
Ord,
PartialOrd,
Eq,
PartialEq,
Hash,
Serialize,
Deserialize,
Deref,
DerefMut,
AsRef,
AsMut,
From,
)]
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct NoApi<T>(pub T);

impl<T> NoApi<T> {
Expand All @@ -37,6 +23,38 @@ impl<T> NoApi<T> {
}
}

impl<T> Deref for NoApi<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<T> DerefMut for NoApi<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl<T> AsRef<T> for NoApi<T> {
fn as_ref(&self) -> &T {
&self.0
}
}

impl<T> AsMut<T> for NoApi<T> {
fn as_mut(&mut self) -> &mut T {
&mut self.0
}
}

impl<T> From<T> for NoApi<T> {
fn from(value: T) -> Self {
Self(value)
}
}

impl<T> OperationInput for NoApi<T> {}

impl<T> OperationOutput for NoApi<T> {
Expand Down
71 changes: 39 additions & 32 deletions crates/aide/src/helpers/use_api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::marker::PhantomData;
use std::{
marker::PhantomData,
ops::{Deref, DerefMut},
};

use derive_more::{AsMut, AsRef, Deref, DerefMut};
use serde::{Deserialize, Serialize};

use crate::gen::GenContext;
Expand All @@ -24,37 +26,10 @@ impl<T> IntoApi for T {
}

/// Allows non [`OperationInput`] or [`OperationOutput`] types to be used in aide handlers with the api documentation of [A].
///
/// For types that already implement [`OperationInput`] or [`OperationOutput`] it overrides the documentation with the provided one.
#[derive(
Copy,
Clone,
Debug,
Ord,
PartialOrd,
Eq,
PartialEq,
Hash,
Serialize,
Deserialize,
Deref,
DerefMut,
AsRef,
AsMut,
)]
pub struct UseApi<T, A>(
#[as_ref]
#[as_mut]
#[deref]
#[deref_mut]
pub T,
pub PhantomData<A>,
);

impl<T, A> From<T> for UseApi<T, A> {
fn from(value: T) -> Self {
Self(value, Default::default())
}
}
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct UseApi<T, A>(pub T, pub PhantomData<A>);

impl<T, A> UseApi<T, A> {
/// Unwraps [Self] into its inner type
Expand All @@ -63,6 +38,38 @@ impl<T, A> UseApi<T, A> {
}
}

impl<T, A> Deref for UseApi<T, A> {
type Target = T;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<T, A> DerefMut for UseApi<T, A> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl<T, A> AsRef<T> for UseApi<T, A> {
fn as_ref(&self) -> &T {
&self.0
}
}

impl<T, A> AsMut<T> for UseApi<T, A> {
fn as_mut(&mut self) -> &mut T {
&mut self.0
}
}

impl<T, A> From<T> for UseApi<T, A> {
fn from(value: T) -> Self {
Self(value, Default::default())
}
}

impl<T, A> OperationInput for UseApi<T, A>
where
A: OperationInput,
Expand Down
76 changes: 48 additions & 28 deletions crates/aide/src/helpers/with_api.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use std::marker::PhantomData;
use std::{
marker::PhantomData,
ops::{Deref, DerefMut},
};

use derive_more::{AsMut, AsRef, Deref, DerefMut};
use serde::{Deserialize, Serialize};

use crate::gen::GenContext;
use crate::openapi::{Operation, Response};
use crate::{OperationInput, OperationOutput};

/// Trait that allows implementing a custom Api definition for any type.
///
/// Two approaches are possible:
///
/// 1. Simple Type override for concrete types
Expand Down Expand Up @@ -58,37 +61,16 @@ use crate::{OperationInput, OperationOutput};
/// }
/// ```
pub trait ApiOverride {
/// The type that is being overriden
/// The type that is being overridden
type Target;
}

/// Allows non [`OperationInput`] or [`OperationOutput`] types to be used in aide handlers with a provided documentation.
/// Allows non [`OperationInput`] or [`OperationOutput`] types to be used in aide handlers with a provided documentation.
///
/// For types that already implement [`OperationInput`] or [`OperationOutput`] it overrides the documentation with the provided one.
/// See [`ApiOverride`] on how to implement such an override
#[derive(
Copy,
Clone,
Debug,
Ord,
PartialOrd,
Eq,
PartialEq,
Hash,
Serialize,
Deserialize,
Deref,
DerefMut,
AsRef,
AsMut,
)]
pub struct WithApi<T>(
#[as_ref]
#[as_mut]
#[deref]
#[deref_mut]
pub T::Target,
pub PhantomData<T>,
)
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct WithApi<T>(pub T::Target, pub PhantomData<T>)
where
T: ApiOverride;

Expand All @@ -102,6 +84,44 @@ where
}
}

impl<T> Deref for WithApi<T>
where
T: ApiOverride,
{
type Target = T::Target;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<T> DerefMut for WithApi<T>
where
T: ApiOverride,
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl<T> AsRef<T::Target> for WithApi<T>
where
T: ApiOverride,
{
fn as_ref(&self) -> &T::Target {
&self.0
}
}

impl<T> AsMut<T::Target> for WithApi<T>
where
T: ApiOverride,
{
fn as_mut(&mut self) -> &mut T::Target {
&mut self.0
}
}

impl<T> OperationInput for WithApi<T>
where
T: OperationInput + ApiOverride,
Expand Down
2 changes: 1 addition & 1 deletion crates/axum-jsonschema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ impl From<JsonSchemaRejection> for JsonSchemaErrorResponse {
error: "deserialization failed".to_string(),
extra: AdditionalError::Deserialization(DeserializationResponse {
deserialization_error: VecDeque::from([PathError {
// keys and index seperated by a '/'
// keys and index separated by a '/'
// enum is ignored because it doesn't exist in json
instance_location: std::iter::once(String::new())
.chain(s.path().iter().map(|s| match s {
Expand Down

0 comments on commit 8bd148a

Please sign in to comment.