Skip to content

Commit

Permalink
Support rename in NewType (#964)
Browse files Browse the repository at this point in the history
* support rename in NewType

* add rename test with const
  • Loading branch information
ihor-rud authored Feb 23, 2025
1 parent 099ba43 commit 9770762
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
18 changes: 16 additions & 2 deletions poem-openapi-derive/src/newtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use quote::quote;
use syn::{Attribute, DeriveInput, Error, Generics, Type};

use crate::{
common_args::ExternalDocument,
common_args::{ExternalDocument, LitOrPath},
error::GeneratorResult,
utils::{
get_crate_name, get_summary_and_description, optional_literal, optional_literal_string,
Expand Down Expand Up @@ -39,6 +39,8 @@ struct NewTypeArgs {
external_docs: Option<ExternalDocument>,
#[darling(default)]
example: bool,
#[darling(default)]
rename: Option<LitOrPath<String>>,
}

const fn default_true() -> bool {
Expand Down Expand Up @@ -90,6 +92,18 @@ pub(crate) fn generate(args: DeriveInput) -> GeneratorResult<TokenStream> {
quote!(None)
};

let name = match args.rename {
Some(LitOrPath::Lit(name)) => quote! {
::std::borrow::Cow::from(#name)
},
Some(LitOrPath::Path(path)) => quote! {
::std::borrow::Cow::from(#path)
},
None => quote! {
<#inner_ty as #crate_name::types::Type>::name()
},
};

let schema_ref = quote! {
<#inner_ty as #crate_name::types::Type>::schema_ref().merge(#crate_name::registry::MetaSchema {
title: #summary,
Expand Down Expand Up @@ -182,7 +196,7 @@ pub(crate) fn generate(args: DeriveInput) -> GeneratorResult<TokenStream> {
type RawElementValueType = <#inner_ty as #crate_name::types::Type>::RawElementValueType;

fn name() -> ::std::borrow::Cow<'static, str> {
<#inner_ty as #crate_name::types::Type>::name()
#name
}

fn schema_ref() -> #crate_name::registry::MetaSchemaRef {
Expand Down
1 change: 1 addition & 0 deletions poem-openapi/src/docs/newtype.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Define a new type.
| to_header | Implement `ToHeader` trait. Default is `true` | bool | Y |
| external_docs | Specify a external resource for extended documentation | string | Y |
| example | Indicates that the type has implemented `Example` trait | bool | Y |
| rename | Rename the type | string | Y |

# Examples

Expand Down
20 changes: 20 additions & 0 deletions poem-openapi/tests/newtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,23 @@ async fn generic() {
Some("string")
);
}

#[tokio::test]
async fn rename_new_type() {
#[derive(NewType)]
#[oai(rename = "TYPE_A")]
struct TypeA(String);

assert_eq!(TypeA::name(), "TYPE_A");
}

#[tokio::test]
async fn rename_new_type_using_const() {
const NEW_NAME: &str = "NEW_NAME";

#[derive(NewType)]
#[oai(rename = NEW_NAME)]
struct TypeA(String);

assert_eq!(TypeA::name(), NEW_NAME);
}

0 comments on commit 9770762

Please sign in to comment.