Skip to content

Commit

Permalink
Merge pull request #588 from /issues/407
Browse files Browse the repository at this point in the history
Removed DataTypeMismatch as default rule
  • Loading branch information
darrelmiller authored May 15, 2021
2 parents d4a0b8e + 429c6e4 commit afe0675
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
11 changes: 10 additions & 1 deletion src/Microsoft.OpenApi/Validations/Rules/OpenApiMediaTypeRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ namespace Microsoft.OpenApi.Validations.Rules
/// <summary>
/// The validation rules for <see cref="OpenApiMediaType"/>.
/// </summary>
[OpenApiRule]
/// <remarks>
/// Removed this in v1.3 as a default rule as the OpenAPI specification does not require that example
/// values validate against the schema. Validating examples against the schema is particularly difficult
/// as it requires parsing of the example using the schema as a guide. This is not possible when the schema
/// is ref'd. Even if we fix this issue, this rule should be treated as a warning, not an error
/// Future versions of the validator should make that distinction.
/// Future versions of the example parsers should not try an infer types.
/// Example validation should be done as a separate post reading step so all schemas can be fully available.
/// </remarks>
//[OpenApiRule]
public static class OpenApiMediaTypeRules
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,6 @@ namespace Microsoft.OpenApi.Validations.Rules
{
public static Microsoft.OpenApi.Validations.ValidationRule<Microsoft.OpenApi.Models.OpenApiLicense> LicenseRequiredFields { get; }
}
[Microsoft.OpenApi.Validations.Rules.OpenApiRule]
public static class OpenApiMediaTypeRules
{
public static Microsoft.OpenApi.Validations.ValidationRule<Microsoft.OpenApi.Models.OpenApiMediaType> MediaTypeMismatchedDataType { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public void ValidateExampleShouldNotHaveDataTypeMismatchForSimpleSchema()
};

// Act
var validator = new OpenApiValidator(ValidationRuleSet.GetDefaultRuleSet());
var ruleset = ValidationRuleSet.GetDefaultRuleSet();
ruleset.Add(OpenApiMediaTypeRules.MediaTypeMismatchedDataType);
var validator = new OpenApiValidator(ruleset);
var walker = new OpenApiWalker(validator);
walker.Walk(mediaType);

Expand Down Expand Up @@ -102,7 +104,9 @@ public void ValidateExamplesShouldNotHaveDataTypeMismatchForSimpleSchema()
};

// Act
var validator = new OpenApiValidator(ValidationRuleSet.GetDefaultRuleSet());
var ruleset = ValidationRuleSet.GetDefaultRuleSet();
ruleset.Add(OpenApiMediaTypeRules.MediaTypeMismatchedDataType);
var validator = new OpenApiValidator(ruleset);
var walker = new OpenApiWalker(validator);
walker.Walk(mediaType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void DefaultRuleSetPropertyReturnsTheCorrectRules()
Assert.NotEmpty(rules);

// Update the number if you add new default rule(s).
Assert.Equal(21, rules.Count);
Assert.Equal(20, rules.Count);
}
}
}

0 comments on commit afe0675

Please sign in to comment.