Skip to content

Commit

Permalink
Add support for schema(ignore) and param(ignore) (#1090)
Browse files Browse the repository at this point in the history
Add support for `#[schema(ignore)]` for named field struct types
implementing `ToSchema` trait. Add support for `param(ignore)` for named
field structs implementing `IntoParams` trait. The ignored field will
not get serialized to the OpenAPI schema.

```rust
 #[derive(ToSchema)]
 struct SchemaIgnoredField {
     value: String,
     #[schema(ignore)]
     __this_is_private: String,
 }

 #[derive(IntoParams)]
 #[into_params(parameter_in = Query)]
 struct Params {
     name: String,
     #[param(ignore)]
     __this_is_private: String,
 }
```

Closes #795
  • Loading branch information
juhaku authored Oct 4, 2024
1 parent a282141 commit 22ea68d
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 124 deletions.
1 change: 1 addition & 0 deletions utoipa-gen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Added

* Add support for `schema(ignore)` and `param(ignore)` (https://github.com/juhaku/utoipa/pull/1090)
* Add support for `property_names` for object (https://github.com/juhaku/utoipa/pull/1084)
* Add `bound` attribute for customizing generic impl bounds. (https://github.com/juhaku/utoipa/pull/1079)
* Add auto collect schemas for utoipa-axum (https://github.com/juhaku/utoipa/pull/1072)
Expand Down
6 changes: 6 additions & 0 deletions utoipa-gen/src/component/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub enum Feature {
ContentMediaType(attributes::ContentMediaType),
Discriminator(attributes::Discriminator),
Bound(attributes::Bound),
Ignore(attributes::Ignore),
MultipleOf(validation::MultipleOf),
Maximum(validation::Maximum),
Minimum(validation::Minimum),
Expand Down Expand Up @@ -239,6 +240,7 @@ impl ToTokensDiagnostics for Feature {
let name = <attributes::Required as FeatureLike>::get_name();
quote! { .#name(#required) }
}
Feature::Ignore(_) => return Err(Diagnostics::new("Feature::Ignore does not support ToTokens")),
};

tokens.extend(feature);
Expand Down Expand Up @@ -300,6 +302,7 @@ impl Display for Feature {
Feature::ContentMediaType(content_media_type) => content_media_type.fmt(f),
Feature::Discriminator(discriminator) => discriminator.fmt(f),
Feature::Bound(bound) => bound.fmt(f),
Feature::Ignore(ignore) => ignore.fmt(f),
}
}
}
Expand Down Expand Up @@ -349,6 +352,7 @@ impl Validatable for Feature {
Feature::ContentMediaType(content_media_type) => content_media_type.is_validatable(),
Feature::Discriminator(discriminator) => discriminator.is_validatable(),
Feature::Bound(bound) => bound.is_validatable(),
Feature::Ignore(ignore) => ignore.is_validatable(),
}
}
}
Expand Down Expand Up @@ -396,6 +400,7 @@ is_validatable! {
attributes::ContentMediaType,
attributes::Discriminator,
attributes::Bound,
attributes::Ignore,
validation::MultipleOf = true,
validation::Maximum = true,
validation::Minimum = true,
Expand Down Expand Up @@ -624,6 +629,7 @@ impl_feature_into_inner! {
attributes::AdditionalProperties,
attributes::Discriminator,
attributes::Bound,
attributes::Ignore,
validation::MultipleOf,
validation::Maximum,
validation::Minimum,
Expand Down
22 changes: 22 additions & 0 deletions utoipa-gen/src/component/features/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -982,3 +982,25 @@ impl From<Bound> for Feature {
Feature::Bound(value)
}
}

// Nothing to parse, it will be parsed true via `parse_features!` when defined as `ignore`
impl_feature! {
#[derive(Clone)]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct Ignore;
}

impl Parse for Ignore {
fn parse(_: ParseStream, _: Ident) -> syn::Result<Self>
where
Self: std::marker::Sized,
{
Ok(Self)
}
}

impl From<Ignore> for Feature {
fn from(value: Ignore) -> Self {
Self::Ignore(value)
}
}
Loading

0 comments on commit 22ea68d

Please sign in to comment.