@@ -72,13 +72,48 @@ public static class OpenApiSchemaRules
7272 {
7373 if ( ! schema . Required . Contains ( schema . Discriminator ? . PropertyName ) )
7474 {
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+ }
7894 }
79- }
95+ }
8096
8197 context . Exit ( ) ;
8298 } ) ;
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+ }
83118 }
84119}
0 commit comments