Skip to content

Commit

Permalink
oapi: Add support for inlined enum variants (#859)
Browse files Browse the repository at this point in the history
* oapi: Add support for inlined enum variants

* Format Rust code using rustfmt

* fix ci

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
chrislearn and github-actions[bot] authored Aug 11, 2024
1 parent fea0dc0 commit 828f51a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
13 changes: 10 additions & 3 deletions crates/oapi-macros/src/schema/struct_schemas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use syn::{punctuated::Punctuated, spanned::Spanned, Attribute, Field, Generics,
use crate::component::{ComponentDescription, ComponentSchemaProps};
use crate::doc_comment::CommentAttributes;
use crate::feature::{
pop_feature, pop_feature_as_inner, Alias, Bound, Feature, FeaturesExt, IsSkipped, Name, RenameAll, SkipBound,
TryToTokensExt,
parse_features, pop_feature, pop_feature_as_inner, Alias, Bound, Feature, FeaturesExt, IsSkipped, Name, RenameAll,
SkipBound, TryToTokensExt,
};
use crate::schema::{Description, Inline};
use crate::type_tree::TypeTree;
Expand All @@ -18,7 +18,7 @@ use crate::{
};

use super::{
feature::{FromAttributes, NamedFieldFeatures},
feature::{parse_schema_features_with, FromAttributes, NamedFieldFeatures},
is_flatten, is_not_skipped, ComponentSchema, FieldRename, FlattenedMapSchema, Property,
};

Expand Down Expand Up @@ -354,6 +354,13 @@ impl TryToTokens for UnnamedStructSchema<'_> {

if fields_len == 1 {
if let Some(ref mut features) = unnamed_struct_features {
let inline = parse_schema_features_with(&first_field.attrs, |input| {
Ok(parse_features!(input as crate::feature::Inline))
})?
.unwrap_or_default();

features.extend(inline);

if pop_feature!(features => Feature::Default(crate::feature::Default(None))).is_some() {
let struct_ident = format_ident!("{}", &self.struct_name);
let index: syn::Index = 0.into();
Expand Down
24 changes: 24 additions & 0 deletions crates/oapi/docs/derive_to_schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,30 @@ In addition to the variant type specific configuration options enum variants sup
_`rename`_ attribute. It behaves similarly to serde's _`rename`_ attribute. If both _serde_
_`rename`_ and _schema_ _`rename`_ are defined __serde__ will take precedence.

## Enum Unnamed Variant Field Configuration Options

* `inline` If the type of this field implements [`ToSchema`][to_schema], then the schema definition
will be inlined. **warning:** Don't use this for recursive data types!

_**Inline unnamed field variant schemas.**_
```
use salvo_oapi::ToSchema;
#[derive(ToSchema)]
enum Number {
One,
}
#[derive(ToSchema)]
enum Color {
Spade,
}
#[derive(ToSchema)]
enum Card {
Number(#[salvo(schema(inline))] Number),
Color(#[salvo(schema(inline))] Color),
}
```

# Unnamed Field Struct Optional Configuration Options for `#[salvo(schema(...))]`

* `description = ...` Can be literal string or Rust expression e.g. _`const`_ reference or
Expand Down

0 comments on commit 828f51a

Please sign in to comment.