Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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
Loading