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

Setting Pattern for New Type Structs #1150

Closed
wyatt-herkamp opened this issue Oct 18, 2024 · 4 comments · Fixed by #1241
Closed

Setting Pattern for New Type Structs #1150

wyatt-herkamp opened this issue Oct 18, 2024 · 4 comments · Fixed by #1241
Labels
enhancement New feature or request

Comments

@wyatt-herkamp
Copy link
Contributor

wyatt-herkamp commented Oct 18, 2024

The ability to apply pattern to new types(Tuple structs with only one data type)

#[derive(ToSchema)]
#[schema(pattern = r#"^([a-zA-Z0-9_\-]{3,32}$)"#)]
struct Username(String);
impl utoipa::PartialSchema for Username {
    fn schema() -> utoipa::openapi::RefOr<utoipa::openapi::schema::Schema> {
        utoipa::openapi::ObjectBuilder::new()
            .schema_type(utoipa::openapi::schema::SchemaType::new(
                utoipa::openapi::schema::Type::String,
            ))
            .pattern(Some(r#"^([a-zA-Z0-9_\-]{3,32}$)"#))
            .into()
    }
}
impl utoipa::ToSchema for Username {
    fn name() -> std::borrow::Cow<'static, str> {
        std::borrow::Cow::Borrowed("Username")
    }
}

I am willing to add this feature. But I won't be able to look at it til next week.

@juhaku
Copy link
Owner

juhaku commented Oct 20, 2024

Okay, I am not quite sure what are you after with this? This seems to be quite special case and is not something that is generic for all users of utoipa?

@wyatt-herkamp
Copy link
Contributor Author

wyatt-herkamp commented Oct 20, 2024 via email

@wyatt-herkamp
Copy link
Contributor Author

So for example

#[derive(ToSchema)]
#[schema(pattern = r#"^([a-zA-Z0-9_\-]{3,32}$)"#)]
struct Username(String);

It would generate the code like the above.

P.S I noticed that the way I worded my original issue was confusing.

@juhaku
Copy link
Owner

juhaku commented Oct 22, 2024

Right, now I get it. thanks 🙂 This seems duplicate for this #572.

If you find time please go ahead and submit a PR. This should be quite simple to do. It would need the unnamed struct features to parse some of those features currently parsed by named field features. It would be beneficial to add not just pattern, but other useful features as well.

@juhaku juhaku added the enhancement New feature or request label Oct 22, 2024
@wyatt-herkamp wyatt-herkamp changed the title Generating Schema for a new type string Setting Pattern for New Type Structs Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants