Skip to content

Commit

Permalink
feat: add rename attribute to oasgen macro
Browse files Browse the repository at this point in the history
  • Loading branch information
markhilb committed Jan 27, 2025
1 parent d0b3e0a commit a463206
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions macro/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct FieldAttributes {
/// By default, oasgen will use references when possible
/// If you want to inline the schema, use `#[oasgen(inline)]`
pub inline: bool,
pub rename: Option<LitStr>,
}

impl FieldAttributes {
Expand Down
16 changes: 11 additions & 5 deletions macro/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ pub fn impl_OaSchema_schema(fields: &[Field], docstring: Option<String>) -> Toke
return quote! {};
}

let name = f.attrs.name().deserialize_name();
let rename = attr.rename.map(|v| v.value());
let name = rename
.as_deref()
.unwrap_or_else(|| f.attrs.name().deserialize_name());
let ty = f.ty;
let schema = quote! {
<#ty as ::oasgen::OaSchema>::schema()
Expand Down Expand Up @@ -124,14 +127,17 @@ pub fn derive_oaschema_enum(
tag: &TagType,
_docstring: Option<String>,
) -> TokenStream {
let variants = variants.iter().filter(|v| {
let variants = variants.iter().filter_map(|v| {
let openapi_attrs = FieldAttributes::try_from(&v.original.attrs).unwrap();
!openapi_attrs.skip
(!openapi_attrs.skip).then_some((v, openapi_attrs))
});
let mut complex_variants = vec![];
let mut str_variants = vec![];
for v in variants {
let name = v.attrs.name().deserialize_name();
for (v, attrs) in variants {
let rename = attrs.rename.map(|v| v.value());
let name = rename
.as_deref()
.unwrap_or_else(|| v.attrs.name().deserialize_name());
if v.fields.is_empty() {
str_variants.push(quote! { #name.to_string(), });
} else {
Expand Down

0 comments on commit a463206

Please sign in to comment.