Skip to content

Commit 2a60c79

Browse files
committed
fix: Validate schema property is not null
1 parent 90b3966 commit 2a60c79

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

src/Microsoft.OpenApi/Properties/SRResource.Designer.cs

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Microsoft.OpenApi/Properties/SRResource.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@
219219
<data name="Validation_SchemaRequiredFieldListMustContainThePropertySpecifiedInTheDiscriminator" xml:space="preserve">
220220
<value>Schema {0} must contain property specified in the discriminator {1} in the required field list.</value>
221221
</data>
222+
<data name="Validation_SchemaPropertyObjectRequired" xml:space="preserve">
223+
<value>Schema {0} property {1} is null.</value>
224+
</data>
222225
<data name="Validation_StringMustBeEmailAddress" xml:space="preserve">
223226
<value>The string '{0}' MUST be in the format of an email address.</value>
224227
</data>

src/Microsoft.OpenApi/Validations/Rules/OpenApiSchemaRules.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,31 @@ namespace Microsoft.OpenApi
1111
[OpenApiRule]
1212
public static class OpenApiSchemaRules
1313
{
14+
/// <summary>
15+
/// Validates Schema Property has value
16+
/// </summary>
17+
public static ValidationRule<IOpenApiSchema> ValidateSchemaPropertyHasValue =>
18+
new(nameof(ValidateSchemaPropertyHasValue),
19+
(context, schema) =>
20+
{
21+
if (schema.Properties is not null)
22+
{
23+
foreach (var property in schema.Properties)
24+
{
25+
if (property.Value is null)
26+
{
27+
context.Enter(property.Key);
28+
context.CreateError(nameof(ValidateSchemaPropertyHasValue),
29+
string.Format(SRResource.Validation_SchemaPropertyObjectRequired,
30+
schema is OpenApiSchemaReference { Reference: not null } schemaReference
31+
? schemaReference.Reference.Id
32+
: string.Empty, property.Key));
33+
context.Exit();
34+
}
35+
}
36+
}
37+
});
38+
1439
/// <summary>
1540
/// Validates Schema Discriminator
1641
/// </summary>

test/Microsoft.OpenApi.Tests/Validations/OpenApiSchemaValidationTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,44 @@ public void ValidateDefaultShouldNotHaveDataTypeMismatchForComplexSchema()
174174
// Assert
175175
Assert.NotEmpty(validator.Warnings);
176176
}
177+
178+
[Fact]
179+
public void ValidateNullProperty()
180+
{
181+
// Arrange
182+
var components = new OpenApiComponents
183+
{
184+
Schemas = new Dictionary<string, IOpenApiSchema>
185+
{
186+
{
187+
"schema1",
188+
new OpenApiSchema
189+
{
190+
Properties = new Dictionary<string, IOpenApiSchema>
191+
{
192+
["property1"] = null,
193+
}
194+
}
195+
}
196+
}
197+
};
198+
199+
// Act
200+
var defaultRuleSet = ValidationRuleSet.GetDefaultRuleSet();
201+
defaultRuleSet.Add(typeof(IOpenApiSchema), OpenApiNonDefaultRules.SchemaMismatchedDataType);
202+
var validator = new OpenApiValidator(defaultRuleSet);
203+
var walker = new OpenApiWalker(validator);
204+
walker.Walk(components);
205+
206+
// Assert
207+
Assert.NotEmpty(validator.Errors);
208+
Assert.Equivalent(new List<OpenApiValidatorError>
209+
{
210+
new OpenApiValidatorError(nameof(OpenApiSchemaRules.ValidateSchemaPropertyHasValue),"#/schemas/schema1/property1",
211+
string.Format(SRResource.Validation_SchemaPropertyObjectRequired,
212+
string.Empty, "property1"))
213+
}, validator.Errors);
214+
}
177215

178216
[Fact]
179217
public void ValidateSchemaRequiredFieldListMustContainThePropertySpecifiedInTheDiscriminator()

0 commit comments

Comments
 (0)