Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(oapi): Add support for serde skip in IntoParams derive #396

Merged
merged 6 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions crates/oapi-macros/src/parameter/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::feature::{
ReadOnly, Rename, RenameAll, SchemaWith, Style, ToTokensExt, WriteOnly, XmlAttr,
};
use crate::parameter::ParameterIn;
use crate::serde::{self, RenameRule, SerdeContainer};
use crate::serde::{self, RenameRule, SerdeContainer, SerdeValue};
use crate::type_tree::TypeTree;
use crate::{attribute, Array, FieldRename, Required, ResultExt};

Expand Down Expand Up @@ -128,9 +128,18 @@ impl ToTokens for ToParameters {
let params = self
.get_struct_fields(&names.as_ref())
.enumerate()
.map(|(index, field)| {
.filter_map(|(index, field)| {
let field_params = serde::parse_value(&field.attrs);
if matches!(&field_params, Some(params) if !params.skip) {
Some((index, field, field_params))
} else {
None
}
})
.map(|(index, field, field_serde_params)|{
Parameter {
field,
field_serde_params,
container_attributes: FieldParameterContainerAttributes {
rename_all: rename_all.as_ref().and_then(|feature| {
match feature {
Expand Down Expand Up @@ -323,6 +332,8 @@ impl Parse for FieldFeatures {
struct Parameter<'a> {
/// Field in the container used to create a single parameter.
field: &'a Field,
//// Field serde params parsed from field attributes.
field_serde_params: Option<SerdeValue>,
/// Attributes on the container which are relevant for this macro.
container_attributes: FieldParameterContainerAttributes<'a>,
/// Either serde rename all rule or to_parameters rename all rule if provided.
Expand Down Expand Up @@ -400,6 +411,7 @@ impl ToTokens for Parameter<'_> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let oapi = crate::oapi_crate();
let field = self.field;
let field_serde_params = &self.field_serde_params;
let ident = &field.ident;
let mut name = &*ident
.as_ref()
Expand All @@ -416,11 +428,10 @@ impl ToTokens for Parameter<'_> {
name = &name[2..];
}

let field_param_serde = serde::parse_value(&field.attrs);
let (schema_features, mut param_features) = self.resolve_field_features();

let rename = param_features.pop_rename_feature().map(|rename| rename.into_value());
let rename_to = field_param_serde
let rename_to = field_serde_params
.as_ref()
.and_then(|field_param_serde| field_param_serde.rename.as_deref().map(Cow::Borrowed))
.or_else(|| rename.map(Cow::Owned));
Expand Down Expand Up @@ -466,7 +477,7 @@ impl ToTokens for Parameter<'_> {
.unwrap_or(false);

let non_required = (component.is_option() && !required)
|| !crate::is_required(field_param_serde.as_ref(), self.serde_container);
|| !crate::is_required(field_serde_params.as_ref(), self.serde_container);
let required: Required = (!non_required).into();

tokens.extend(quote! {
Expand Down
6 changes: 5 additions & 1 deletion crates/oapi-macros/src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ impl SerdeValue {
let mut rest = *cursor;
while let Some((tt, next)) = rest.token_tree() {
match tt {
TokenTree::Ident(ident) if ident == "skip" || ident == "skip_serializing" => value.skip = true,
TokenTree::Ident(ident)
if ident == "skip" || ident == "skip_serializing" || ident == "skip_deserializing" =>
{
value.skip = true
}
TokenTree::Ident(ident) if ident == "skip_serializing_if" => value.skip_serializing_if = true,
TokenTree::Ident(ident) if ident == "flatten" => value.flatten = true,
TokenTree::Ident(ident) if ident == "rename" => {
Expand Down
3 changes: 3 additions & 0 deletions crates/oapi/docs/derive_to_parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ generated OpenAPI doc. The following attributes are currently supported:
* `default` Supported at the container level and field level according to [serde attributes].
* `skip_serializing_if = "..."` Supported **only** at the field level.
* `with = ...` Supported **only** at field level.
* `skip_serializing = "..."` Supported **only** at the field or variant level.
* `skip_deserializing = "..."` Supported **only** at the field or variant level.
* `skip = "..."` Supported **only** at the field level.

Other _`serde`_ attributes will impact the serialization but will not be reflected on the generated OpenAPI doc.

Expand Down
2 changes: 1 addition & 1 deletion crates/oapi/src/openapi/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,13 @@ pub enum KnownFormat {
/// **ulid** feature need to be enabled.
#[cfg(feature = "ulid")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "ulid")))]
Ulid,
/// Used with [`String`] values to indicate value is in UUID format.
///
/// **uuid** feature need to be enabled.
#[cfg(feature = "uuid")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "uuid")))]
Uuid,
Ulid,
}

#[cfg(test)]
Expand Down
Loading