Skip to content

Commit d7a23a9

Browse files
[Rust-Axum] Fix compilation error when validate is used on Nullable values (#20100)
* Fix compilation error when validate is used on Nullable values * Update samples * Switch Nullable Into Option trait implement to From * Update samples from rebase
1 parent 2935247 commit d7a23a9

File tree

11 files changed

+1375
-0
lines changed

11 files changed

+1375
-0
lines changed

modules/openapi-generator/src/main/resources/rust-axum/types.mustache

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,131 @@ where
632632
}
633633
}
634634
635+
impl<T> validator::Validate for Nullable<T>
636+
where
637+
T: validator::Validate,
638+
{
639+
fn validate(&self) -> Result<(), validator::ValidationErrors> {
640+
match self {
641+
Self::Present(x) => x.validate(),
642+
Self::Null => Ok(()),
643+
}
644+
}
645+
}
646+
647+
impl<'a, T> validator::ValidateArgs<'a> for Nullable<T>
648+
where
649+
T: validator::ValidateArgs<'a>,
650+
{
651+
type Args = T::Args;
652+
fn validate_with_args(&self, args: Self::Args) -> Result<(), validator::ValidationErrors> {
653+
match self {
654+
Self::Present(x) => x.validate_with_args(args),
655+
Self::Null => Ok(()),
656+
}
657+
}
658+
}
659+
660+
impl<T> validator::ValidateEmail for Nullable<T>
661+
where
662+
T: validator::ValidateEmail,
663+
{
664+
fn as_email_string(&self) -> Option<std::borrow::Cow<str>> {
665+
match self {
666+
Self::Present(x) => x.as_email_string(),
667+
Self::Null => None,
668+
}
669+
}
670+
}
671+
672+
impl<T> validator::ValidateUrl for Nullable<T>
673+
where
674+
T: validator::ValidateUrl,
675+
{
676+
fn as_url_string(&self) -> Option<std::borrow::Cow<str>> {
677+
match self {
678+
Self::Present(x) => x.as_url_string(),
679+
Self::Null => None,
680+
}
681+
}
682+
}
683+
684+
impl<T> validator::ValidateContains for Nullable<T>
685+
where
686+
T: validator::ValidateContains,
687+
{
688+
fn validate_contains(&self, needle: &str) -> bool {
689+
match self {
690+
Self::Present(x) => x.validate_contains(needle),
691+
Self::Null => true,
692+
}
693+
}
694+
}
695+
696+
impl<T> validator::ValidateRequired for Nullable<T>
697+
where
698+
T: validator::ValidateRequired,
699+
{
700+
fn is_some(&self) -> bool {
701+
self.is_present()
702+
}
703+
}
704+
705+
impl<T> validator::ValidateRegex for Nullable<T>
706+
where
707+
T: validator::ValidateRegex,
708+
{
709+
fn validate_regex(&self, regex: impl validator::AsRegex) -> bool {
710+
match self {
711+
Self::Present(x) => x.validate_regex(regex),
712+
Self::Null => true,
713+
}
714+
}
715+
}
716+
717+
impl<T, I> validator::ValidateRange<I> for Nullable<T>
718+
where
719+
T: validator::ValidateRange<I>,
720+
{
721+
fn greater_than(&self, max: I) -> Option<bool> {
722+
use validator::ValidateRange;
723+
match self {
724+
Self::Present(x) => x.greater_than(max),
725+
Self::Null => None,
726+
}
727+
}
728+
fn less_than(&self, min: I) -> Option<bool> {
729+
use validator::ValidateRange;
730+
match self {
731+
Self::Present(x) => x.less_than(min),
732+
Self::Null => None,
733+
}
734+
}
735+
}
736+
737+
impl<T, I> validator::ValidateLength<I> for Nullable<T>
738+
where
739+
T: validator::ValidateLength<I>,
740+
I: PartialEq + PartialOrd,
741+
{
742+
fn length(&self) -> Option<I> {
743+
use validator::ValidateLength;
744+
match self {
745+
Self::Present(x) => x.length(),
746+
Self::Null => None,
747+
}
748+
}
749+
}
750+
751+
impl<T> From<Nullable<T>> for Option<T> {
752+
fn from(value: Nullable<T>) -> Option<T> {
753+
match value {
754+
Nullable::Present(x) => Some(x),
755+
Nullable::Null => None,
756+
}
757+
}
758+
}
759+
635760
#[inline(never)]
636761
#[cold]
637762
fn expect_failed(msg: &str) -> ! {

samples/server/petstore/rust-axum/output/apikey-auths/src/types.rs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,131 @@ where
632632
}
633633
}
634634

635+
impl<T> validator::Validate for Nullable<T>
636+
where
637+
T: validator::Validate,
638+
{
639+
fn validate(&self) -> Result<(), validator::ValidationErrors> {
640+
match self {
641+
Self::Present(x) => x.validate(),
642+
Self::Null => Ok(()),
643+
}
644+
}
645+
}
646+
647+
impl<'a, T> validator::ValidateArgs<'a> for Nullable<T>
648+
where
649+
T: validator::ValidateArgs<'a>,
650+
{
651+
type Args = T::Args;
652+
fn validate_with_args(&self, args: Self::Args) -> Result<(), validator::ValidationErrors> {
653+
match self {
654+
Self::Present(x) => x.validate_with_args(args),
655+
Self::Null => Ok(()),
656+
}
657+
}
658+
}
659+
660+
impl<T> validator::ValidateEmail for Nullable<T>
661+
where
662+
T: validator::ValidateEmail,
663+
{
664+
fn as_email_string(&self) -> Option<std::borrow::Cow<str>> {
665+
match self {
666+
Self::Present(x) => x.as_email_string(),
667+
Self::Null => None,
668+
}
669+
}
670+
}
671+
672+
impl<T> validator::ValidateUrl for Nullable<T>
673+
where
674+
T: validator::ValidateUrl,
675+
{
676+
fn as_url_string(&self) -> Option<std::borrow::Cow<str>> {
677+
match self {
678+
Self::Present(x) => x.as_url_string(),
679+
Self::Null => None,
680+
}
681+
}
682+
}
683+
684+
impl<T> validator::ValidateContains for Nullable<T>
685+
where
686+
T: validator::ValidateContains,
687+
{
688+
fn validate_contains(&self, needle: &str) -> bool {
689+
match self {
690+
Self::Present(x) => x.validate_contains(needle),
691+
Self::Null => true,
692+
}
693+
}
694+
}
695+
696+
impl<T> validator::ValidateRequired for Nullable<T>
697+
where
698+
T: validator::ValidateRequired,
699+
{
700+
fn is_some(&self) -> bool {
701+
self.is_present()
702+
}
703+
}
704+
705+
impl<T> validator::ValidateRegex for Nullable<T>
706+
where
707+
T: validator::ValidateRegex,
708+
{
709+
fn validate_regex(&self, regex: impl validator::AsRegex) -> bool {
710+
match self {
711+
Self::Present(x) => x.validate_regex(regex),
712+
Self::Null => true,
713+
}
714+
}
715+
}
716+
717+
impl<T, I> validator::ValidateRange<I> for Nullable<T>
718+
where
719+
T: validator::ValidateRange<I>,
720+
{
721+
fn greater_than(&self, max: I) -> Option<bool> {
722+
use validator::ValidateRange;
723+
match self {
724+
Self::Present(x) => x.greater_than(max),
725+
Self::Null => None,
726+
}
727+
}
728+
fn less_than(&self, min: I) -> Option<bool> {
729+
use validator::ValidateRange;
730+
match self {
731+
Self::Present(x) => x.less_than(min),
732+
Self::Null => None,
733+
}
734+
}
735+
}
736+
737+
impl<T, I> validator::ValidateLength<I> for Nullable<T>
738+
where
739+
T: validator::ValidateLength<I>,
740+
I: PartialEq + PartialOrd,
741+
{
742+
fn length(&self) -> Option<I> {
743+
use validator::ValidateLength;
744+
match self {
745+
Self::Present(x) => x.length(),
746+
Self::Null => None,
747+
}
748+
}
749+
}
750+
751+
impl<T> From<Nullable<T>> for Option<T> {
752+
fn from(value: Nullable<T>) -> Option<T> {
753+
match value {
754+
Nullable::Present(x) => Some(x),
755+
Nullable::Null => None,
756+
}
757+
}
758+
}
759+
635760
#[inline(never)]
636761
#[cold]
637762
fn expect_failed(msg: &str) -> ! {

samples/server/petstore/rust-axum/output/multipart-v3/src/types.rs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,131 @@ where
632632
}
633633
}
634634

635+
impl<T> validator::Validate for Nullable<T>
636+
where
637+
T: validator::Validate,
638+
{
639+
fn validate(&self) -> Result<(), validator::ValidationErrors> {
640+
match self {
641+
Self::Present(x) => x.validate(),
642+
Self::Null => Ok(()),
643+
}
644+
}
645+
}
646+
647+
impl<'a, T> validator::ValidateArgs<'a> for Nullable<T>
648+
where
649+
T: validator::ValidateArgs<'a>,
650+
{
651+
type Args = T::Args;
652+
fn validate_with_args(&self, args: Self::Args) -> Result<(), validator::ValidationErrors> {
653+
match self {
654+
Self::Present(x) => x.validate_with_args(args),
655+
Self::Null => Ok(()),
656+
}
657+
}
658+
}
659+
660+
impl<T> validator::ValidateEmail for Nullable<T>
661+
where
662+
T: validator::ValidateEmail,
663+
{
664+
fn as_email_string(&self) -> Option<std::borrow::Cow<str>> {
665+
match self {
666+
Self::Present(x) => x.as_email_string(),
667+
Self::Null => None,
668+
}
669+
}
670+
}
671+
672+
impl<T> validator::ValidateUrl for Nullable<T>
673+
where
674+
T: validator::ValidateUrl,
675+
{
676+
fn as_url_string(&self) -> Option<std::borrow::Cow<str>> {
677+
match self {
678+
Self::Present(x) => x.as_url_string(),
679+
Self::Null => None,
680+
}
681+
}
682+
}
683+
684+
impl<T> validator::ValidateContains for Nullable<T>
685+
where
686+
T: validator::ValidateContains,
687+
{
688+
fn validate_contains(&self, needle: &str) -> bool {
689+
match self {
690+
Self::Present(x) => x.validate_contains(needle),
691+
Self::Null => true,
692+
}
693+
}
694+
}
695+
696+
impl<T> validator::ValidateRequired for Nullable<T>
697+
where
698+
T: validator::ValidateRequired,
699+
{
700+
fn is_some(&self) -> bool {
701+
self.is_present()
702+
}
703+
}
704+
705+
impl<T> validator::ValidateRegex for Nullable<T>
706+
where
707+
T: validator::ValidateRegex,
708+
{
709+
fn validate_regex(&self, regex: impl validator::AsRegex) -> bool {
710+
match self {
711+
Self::Present(x) => x.validate_regex(regex),
712+
Self::Null => true,
713+
}
714+
}
715+
}
716+
717+
impl<T, I> validator::ValidateRange<I> for Nullable<T>
718+
where
719+
T: validator::ValidateRange<I>,
720+
{
721+
fn greater_than(&self, max: I) -> Option<bool> {
722+
use validator::ValidateRange;
723+
match self {
724+
Self::Present(x) => x.greater_than(max),
725+
Self::Null => None,
726+
}
727+
}
728+
fn less_than(&self, min: I) -> Option<bool> {
729+
use validator::ValidateRange;
730+
match self {
731+
Self::Present(x) => x.less_than(min),
732+
Self::Null => None,
733+
}
734+
}
735+
}
736+
737+
impl<T, I> validator::ValidateLength<I> for Nullable<T>
738+
where
739+
T: validator::ValidateLength<I>,
740+
I: PartialEq + PartialOrd,
741+
{
742+
fn length(&self) -> Option<I> {
743+
use validator::ValidateLength;
744+
match self {
745+
Self::Present(x) => x.length(),
746+
Self::Null => None,
747+
}
748+
}
749+
}
750+
751+
impl<T> From<Nullable<T>> for Option<T> {
752+
fn from(value: Nullable<T>) -> Option<T> {
753+
match value {
754+
Nullable::Present(x) => Some(x),
755+
Nullable::Null => None,
756+
}
757+
}
758+
}
759+
635760
#[inline(never)]
636761
#[cold]
637762
fn expect_failed(msg: &str) -> ! {

0 commit comments

Comments
 (0)