@@ -72,13 +72,48 @@ public static class OpenApiSchemaRules
72
72
{
73
73
if ( ! schema . Required . Contains ( schema . Discriminator ? . PropertyName ) )
74
74
{
75
- context . CreateError ( nameof ( ValidateSchemaDiscriminator ) ,
76
- string . Format ( SRResource . Validation_SchemaRequiredFieldListMustContainThePropertySpecifiedInTheDiscriminator ,
77
- schema . Reference . Id , schema . Discriminator . PropertyName ) ) ;
75
+ // check schema.OneOf, schema.AnyOf or schema.AllOf
76
+ if ( schema . OneOf . Count != 0 )
77
+ {
78
+ ValidateDiscriminatorAgainstChildSchema ( schema . OneOf , schema , context ) ;
79
+ }
80
+ else if ( schema . AnyOf . Count != 0 )
81
+ {
82
+ ValidateDiscriminatorAgainstChildSchema ( schema . AnyOf , schema , context ) ;
83
+ }
84
+ else if ( schema . AllOf . Count != 0 )
85
+ {
86
+ ValidateDiscriminatorAgainstChildSchema ( schema . AllOf , schema , context ) ;
87
+ }
88
+ else
89
+ {
90
+ context . CreateError ( nameof ( ValidateSchemaDiscriminator ) ,
91
+ string . Format ( SRResource . Validation_SchemaRequiredFieldListMustContainThePropertySpecifiedInTheDiscriminator ,
92
+ schema . Reference . Id , schema . Discriminator . PropertyName ) ) ;
93
+ }
78
94
}
79
- }
95
+ }
80
96
81
97
context . Exit ( ) ;
82
98
} ) ;
99
+
100
+ /// <summary>
101
+ /// Validates the property name in the discriminator against the ones present in the children schema
102
+ /// </summary>
103
+ /// <param name="childSchema">The derived schema.</param>
104
+ /// <param name="schema">The parent schema.</param>
105
+ /// <param name="context">A validation context.</param>
106
+ public static void ValidateDiscriminatorAgainstChildSchema ( IList < OpenApiSchema > childSchema , OpenApiSchema schema , IValidationContext context )
107
+ {
108
+ foreach ( var schemaItem in childSchema )
109
+ {
110
+ if ( ! schemaItem . Properties . Keys . Contains ( schema . Discriminator ? . PropertyName ) )
111
+ {
112
+ context . CreateError ( nameof ( ValidateSchemaDiscriminator ) ,
113
+ string . Format ( SRResource . Validation_SchemaRequiredFieldListMustContainThePropertySpecifiedInTheDiscriminator ,
114
+ schema . Reference . Id , schema . Discriminator . PropertyName ) ) ;
115
+ }
116
+ }
117
+ }
83
118
}
84
119
}
0 commit comments