Skip to content

fix: Validate schema property is not null #2468

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
merged 2 commits into from
Aug 20, 2025
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
11 changes: 10 additions & 1 deletion src/Microsoft.OpenApi/Properties/SRResource.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Microsoft.OpenApi/Properties/SRResource.resx
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@
<data name="Validation_SchemaRequiredFieldListMustContainThePropertySpecifiedInTheDiscriminator" xml:space="preserve">
<value>Schema {0} must contain property specified in the discriminator {1} in the required field list.</value>
</data>
<data name="Validation_SchemaPropertyObjectRequired" xml:space="preserve">
<value>Schema {0} property {1} is null.</value>
</data>
<data name="Validation_StringMustBeEmailAddress" xml:space="preserve">
<value>The string '{0}' MUST be in the format of an email address.</value>
</data>
Expand Down
25 changes: 25 additions & 0 deletions src/Microsoft.OpenApi/Validations/Rules/OpenApiSchemaRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,37 @@

namespace Microsoft.OpenApi
{
using System.Linq;

/// <summary>
/// The validation rules for <see cref="OpenApiSchema"/>.
/// </summary>
[OpenApiRule]
public static class OpenApiSchemaRules
{
/// <summary>
/// Validates Schema Property has value
/// </summary>
public static ValidationRule<IOpenApiSchema> ValidateSchemaPropertyHasValue =>
new(nameof(ValidateSchemaPropertyHasValue),
(context, schema) =>
{
if (schema.Properties is not null)
{
foreach (var property in schema.Properties
.Where(entry => entry.Value is null))
{
context.Enter(property.Key);
context.CreateError(nameof(ValidateSchemaPropertyHasValue),
string.Format(SRResource.Validation_SchemaPropertyObjectRequired,
schema is OpenApiSchemaReference { Reference: not null } schemaReference
? schemaReference.Reference.Id
: string.Empty, property.Key));
context.Exit();
}
}
});

/// <summary>
/// Validates Schema Discriminator
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,7 @@ namespace Microsoft.OpenApi
public static class OpenApiSchemaRules
{
public static Microsoft.OpenApi.ValidationRule<Microsoft.OpenApi.IOpenApiSchema> ValidateSchemaDiscriminator { get; }
public static Microsoft.OpenApi.ValidationRule<Microsoft.OpenApi.IOpenApiSchema> ValidateSchemaPropertyHasValue { get; }
public static bool TraverseSchemaElements(string discriminatorName, System.Collections.Generic.IList<Microsoft.OpenApi.IOpenApiSchema>? childSchema) { }
public static bool ValidateChildSchemaAgainstDiscriminator(Microsoft.OpenApi.IOpenApiSchema schema, string? discriminatorName) { }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,44 @@ public void ValidateDefaultShouldNotHaveDataTypeMismatchForComplexSchema()
// Assert
Assert.NotEmpty(validator.Warnings);
}

[Fact]
public void ValidateNullProperty()
{
// Arrange
var components = new OpenApiComponents
{
Schemas = new Dictionary<string, IOpenApiSchema>
{
{
"schema1",
new OpenApiSchema
{
Properties = new Dictionary<string, IOpenApiSchema>
{
["property1"] = null,
}
}
}
}
};

// Act
var defaultRuleSet = ValidationRuleSet.GetDefaultRuleSet();
defaultRuleSet.Add(typeof(IOpenApiSchema), OpenApiNonDefaultRules.SchemaMismatchedDataType);
var validator = new OpenApiValidator(defaultRuleSet);
var walker = new OpenApiWalker(validator);
walker.Walk(components);

// Assert
Assert.NotEmpty(validator.Errors);
Assert.Equivalent(new List<OpenApiValidatorError>
{
new OpenApiValidatorError(nameof(OpenApiSchemaRules.ValidateSchemaPropertyHasValue),"#/schemas/schema1/property1",
string.Format(SRResource.Validation_SchemaPropertyObjectRequired,
string.Empty, "property1"))
}, validator.Errors);
}

[Fact]
public void ValidateSchemaRequiredFieldListMustContainThePropertySpecifiedInTheDiscriminator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public void RuleSetConstructorsReturnsTheCorrectRules()
Assert.Empty(ruleSet_4.Rules);

// Update the number if you add new default rule(s).
Assert.Equal(20, ruleSet_1.Rules.Count);
Assert.Equal(20, ruleSet_2.Rules.Count);
Assert.Equal(21, ruleSet_1.Rules.Count);
Assert.Equal(21, ruleSet_2.Rules.Count);
Assert.Equal(3, ruleSet_3.Rules.Count);
}

Expand Down
Loading