Skip to content

[Rust-Axum] Fix compilation error when validate is used on Nullable values #20100

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

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
125 changes: 125 additions & 0 deletions modules/openapi-generator/src/main/resources/rust-axum/types.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,131 @@ where
}
}

impl<T> validator::Validate for Nullable<T>
where
T: validator::Validate,
{
fn validate(&self) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate(),
Self::Null => Ok(()),
}
}
}

impl<'a, T> validator::ValidateArgs<'a> for Nullable<T>
where
T: validator::ValidateArgs<'a>,
{
type Args = T::Args;
fn validate_with_args(&self, args: Self::Args) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate_with_args(args),
Self::Null => Ok(()),
}
}
}

impl<T> validator::ValidateEmail for Nullable<T>
where
T: validator::ValidateEmail,
{
fn as_email_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_email_string(),
Self::Null => None,
}
}
}

impl<T> validator::ValidateUrl for Nullable<T>
where
T: validator::ValidateUrl,
{
fn as_url_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_url_string(),
Self::Null => None,
}
}
}

impl<T> validator::ValidateContains for Nullable<T>
where
T: validator::ValidateContains,
{
fn validate_contains(&self, needle: &str) -> bool {
match self {
Self::Present(x) => x.validate_contains(needle),
Self::Null => true,
}
}
}

impl<T> validator::ValidateRequired for Nullable<T>
where
T: validator::ValidateRequired,
{
fn is_some(&self) -> bool {
self.is_present()
}
}

impl<T> validator::ValidateRegex for Nullable<T>
where
T: validator::ValidateRegex,
{
fn validate_regex(&self, regex: impl validator::AsRegex) -> bool {
match self {
Self::Present(x) => x.validate_regex(regex),
Self::Null => true,
}
}
}

impl<T, I> validator::ValidateRange<I> for Nullable<T>
where
T: validator::ValidateRange<I>,
{
fn greater_than(&self, max: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.greater_than(max),
Self::Null => None,
}
}
fn less_than(&self, min: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.less_than(min),
Self::Null => None,
}
}
}

impl<T, I> validator::ValidateLength<I> for Nullable<T>
where
T: validator::ValidateLength<I>,
I: PartialEq + PartialOrd,
{
fn length(&self) -> Option<I> {
use validator::ValidateLength;
match self {
Self::Present(x) => x.length(),
Self::Null => None,
}
}
}

impl<T> From<Nullable<T>> for Option<T> {
fn from(value: Nullable<T>) -> Option<T> {
match value {
Nullable::Present(x) => Some(x),
Nullable::Null => None,
}
}
}

#[inline(never)]
#[cold]
fn expect_failed(msg: &str) -> ! {
Expand Down
125 changes: 125 additions & 0 deletions samples/server/petstore/rust-axum/output/apikey-auths/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,131 @@ where
}
}

impl<T> validator::Validate for Nullable<T>
where
T: validator::Validate,
{
fn validate(&self) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate(),
Self::Null => Ok(()),
}
}
}

impl<'a, T> validator::ValidateArgs<'a> for Nullable<T>
where
T: validator::ValidateArgs<'a>,
{
type Args = T::Args;
fn validate_with_args(&self, args: Self::Args) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate_with_args(args),
Self::Null => Ok(()),
}
}
}

impl<T> validator::ValidateEmail for Nullable<T>
where
T: validator::ValidateEmail,
{
fn as_email_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_email_string(),
Self::Null => None,
}
}
}

impl<T> validator::ValidateUrl for Nullable<T>
where
T: validator::ValidateUrl,
{
fn as_url_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_url_string(),
Self::Null => None,
}
}
}

impl<T> validator::ValidateContains for Nullable<T>
where
T: validator::ValidateContains,
{
fn validate_contains(&self, needle: &str) -> bool {
match self {
Self::Present(x) => x.validate_contains(needle),
Self::Null => true,
}
}
}

impl<T> validator::ValidateRequired for Nullable<T>
where
T: validator::ValidateRequired,
{
fn is_some(&self) -> bool {
self.is_present()
}
}

impl<T> validator::ValidateRegex for Nullable<T>
where
T: validator::ValidateRegex,
{
fn validate_regex(&self, regex: impl validator::AsRegex) -> bool {
match self {
Self::Present(x) => x.validate_regex(regex),
Self::Null => true,
}
}
}

impl<T, I> validator::ValidateRange<I> for Nullable<T>
where
T: validator::ValidateRange<I>,
{
fn greater_than(&self, max: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.greater_than(max),
Self::Null => None,
}
}
fn less_than(&self, min: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.less_than(min),
Self::Null => None,
}
}
}

impl<T, I> validator::ValidateLength<I> for Nullable<T>
where
T: validator::ValidateLength<I>,
I: PartialEq + PartialOrd,
{
fn length(&self) -> Option<I> {
use validator::ValidateLength;
match self {
Self::Present(x) => x.length(),
Self::Null => None,
}
}
}

impl<T> From<Nullable<T>> for Option<T> {
fn from(value: Nullable<T>) -> Option<T> {
match value {
Nullable::Present(x) => Some(x),
Nullable::Null => None,
}
}
}

#[inline(never)]
#[cold]
fn expect_failed(msg: &str) -> ! {
Expand Down
125 changes: 125 additions & 0 deletions samples/server/petstore/rust-axum/output/multipart-v3/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,131 @@ where
}
}

impl<T> validator::Validate for Nullable<T>
where
T: validator::Validate,
{
fn validate(&self) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate(),
Self::Null => Ok(()),
}
}
}

impl<'a, T> validator::ValidateArgs<'a> for Nullable<T>
where
T: validator::ValidateArgs<'a>,
{
type Args = T::Args;
fn validate_with_args(&self, args: Self::Args) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate_with_args(args),
Self::Null => Ok(()),
}
}
}

impl<T> validator::ValidateEmail for Nullable<T>
where
T: validator::ValidateEmail,
{
fn as_email_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_email_string(),
Self::Null => None,
}
}
}

impl<T> validator::ValidateUrl for Nullable<T>
where
T: validator::ValidateUrl,
{
fn as_url_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_url_string(),
Self::Null => None,
}
}
}

impl<T> validator::ValidateContains for Nullable<T>
where
T: validator::ValidateContains,
{
fn validate_contains(&self, needle: &str) -> bool {
match self {
Self::Present(x) => x.validate_contains(needle),
Self::Null => true,
}
}
}

impl<T> validator::ValidateRequired for Nullable<T>
where
T: validator::ValidateRequired,
{
fn is_some(&self) -> bool {
self.is_present()
}
}

impl<T> validator::ValidateRegex for Nullable<T>
where
T: validator::ValidateRegex,
{
fn validate_regex(&self, regex: impl validator::AsRegex) -> bool {
match self {
Self::Present(x) => x.validate_regex(regex),
Self::Null => true,
}
}
}

impl<T, I> validator::ValidateRange<I> for Nullable<T>
where
T: validator::ValidateRange<I>,
{
fn greater_than(&self, max: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.greater_than(max),
Self::Null => None,
}
}
fn less_than(&self, min: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.less_than(min),
Self::Null => None,
}
}
}

impl<T, I> validator::ValidateLength<I> for Nullable<T>
where
T: validator::ValidateLength<I>,
I: PartialEq + PartialOrd,
{
fn length(&self) -> Option<I> {
use validator::ValidateLength;
match self {
Self::Present(x) => x.length(),
Self::Null => None,
}
}
}

impl<T> From<Nullable<T>> for Option<T> {
fn from(value: Nullable<T>) -> Option<T> {
match value {
Nullable::Present(x) => Some(x),
Nullable::Null => None,
}
}
}

#[inline(never)]
#[cold]
fn expect_failed(msg: &str) -> ! {
Expand Down
Loading
Loading