diff --git a/src/Microsoft.OpenApi/Validations/Rules/OpenApiHeaderRules.cs b/src/Microsoft.OpenApi/Validations/Rules/OpenApiHeaderRules.cs deleted file mode 100644 index 4bc5aa94a..000000000 --- a/src/Microsoft.OpenApi/Validations/Rules/OpenApiHeaderRules.cs +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT license. - -using Microsoft.OpenApi.Models; - -namespace Microsoft.OpenApi.Validations.Rules -{ - /// - /// The validation rules for . - /// - //Removed from Default Rules as this is not a MUST in OpenAPI - [OpenApiRule] - public static class OpenApiHeaderRules - { - /// - /// Validate the data matches with the given data type. - /// - public static ValidationRule HeaderMismatchedDataType => - new(nameof(HeaderMismatchedDataType), - (context, header) => - { - // example - context.Enter("example"); - - if (header.Example != null) - { - RuleHelpers.ValidateDataTypeMismatch(context, - nameof(HeaderMismatchedDataType), header.Example, header.Schema); - } - - context.Exit(); - - // examples - context.Enter("examples"); - - if (header.Examples != null) - { - foreach (var key in header.Examples.Keys) - { - if (header.Examples[key] != null) - { - context.Enter(key); - context.Enter("value"); - RuleHelpers.ValidateDataTypeMismatch(context, - nameof(HeaderMismatchedDataType), header.Examples[key]?.Value, header.Schema); - context.Exit(); - context.Exit(); - } - } - } - - context.Exit(); - }); - - // add more rule. - } -} diff --git a/src/Microsoft.OpenApi/Validations/Rules/OpenApiMediaTypeRules.cs b/src/Microsoft.OpenApi/Validations/Rules/OpenApiMediaTypeRules.cs deleted file mode 100644 index 7ac09cbbf..000000000 --- a/src/Microsoft.OpenApi/Validations/Rules/OpenApiMediaTypeRules.cs +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT license. - -using Microsoft.OpenApi.Models; - -namespace Microsoft.OpenApi.Validations.Rules -{ - /// - /// The validation rules for . - /// - /// - /// 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 referenced. 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. - /// - [OpenApiRule] - public static class OpenApiMediaTypeRules - { - /// - /// Validate the data matches with the given data type. - /// - public static ValidationRule MediaTypeMismatchedDataType => - new(nameof(MediaTypeMismatchedDataType), - (context, mediaType) => - { - // example - context.Enter("example"); - - if (mediaType.Example != null) - { - RuleHelpers.ValidateDataTypeMismatch(context, nameof(MediaTypeMismatchedDataType), mediaType.Example, mediaType.Schema); - } - - context.Exit(); - - // enum - context.Enter("examples"); - - if (mediaType.Examples != null) - { - foreach (var key in mediaType.Examples.Keys) - { - if (mediaType.Examples[key] != null) - { - context.Enter(key); - context.Enter("value"); - RuleHelpers.ValidateDataTypeMismatch(context, nameof(MediaTypeMismatchedDataType), mediaType.Examples[key]?.Value, mediaType.Schema); - context.Exit(); - context.Exit(); - } - } - } - - context.Exit(); - }); - - // add more rule. - } -} diff --git a/src/Microsoft.OpenApi/Validations/Rules/OpenApiNonDefaultRules.cs b/src/Microsoft.OpenApi/Validations/Rules/OpenApiNonDefaultRules.cs new file mode 100644 index 000000000..f02be33ee --- /dev/null +++ b/src/Microsoft.OpenApi/Validations/Rules/OpenApiNonDefaultRules.cs @@ -0,0 +1,123 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. + +using System.Collections.Generic; +using System.Linq; +using System.Text.Json.Nodes; +using Microsoft.OpenApi.Models; + +namespace Microsoft.OpenApi.Validations.Rules +{ + /// + /// Defines a non-default set of rules for validating examples in header, media type and parameter objects against the schema + /// + public static class OpenApiNonDefaultRules + { + /// + /// Validate the data matches with the given data type. + /// + public static ValidationRule HeaderMismatchedDataType => + new(nameof(HeaderMismatchedDataType), + (context, header) => + { + ValidateMismatchedDataType(context, nameof(HeaderMismatchedDataType), header.Example, header.Examples, header.Schema); + }); + + /// + /// Validate the data matches with the given data type. + /// + public static ValidationRule MediaTypeMismatchedDataType => + new(nameof(MediaTypeMismatchedDataType), + (context, mediaType) => + { + ValidateMismatchedDataType(context, nameof(MediaTypeMismatchedDataType), mediaType.Example, mediaType.Examples, mediaType.Schema); + }); + + /// + /// Validate the data matches with the given data type. + /// + public static ValidationRule ParameterMismatchedDataType => + new(nameof(ParameterMismatchedDataType), + (context, parameter) => + { + ValidateMismatchedDataType(context, nameof(ParameterMismatchedDataType), parameter.Example, parameter.Examples, parameter.Schema); + }); + + /// + /// Validate the data matches with the given data type. + /// + public static ValidationRule SchemaMismatchedDataType => + new(nameof(SchemaMismatchedDataType), + (context, schema) => + { + // default + context.Enter("default"); + + if (schema.Default != null) + { + RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Default, schema); + } + + context.Exit(); + + // example + context.Enter("example"); + + if (schema.Example != null) + { + RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Example, schema); + } + + context.Exit(); + + // enum + context.Enter("enum"); + + if (schema.Enum != null) + { + for (var i = 0; i < schema.Enum.Count; i++) + { + context.Enter(i.ToString()); + RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Enum[i], schema); + context.Exit(); + } + } + + context.Exit(); + }); + + private static void ValidateMismatchedDataType(IValidationContext context, + string ruleName, + JsonNode example, + IDictionary examples, + OpenApiSchema schema) + { + // example + context.Enter("example"); + + if (example != null) + { + RuleHelpers.ValidateDataTypeMismatch(context, ruleName, example, schema); + } + + context.Exit(); + + // enum + context.Enter("examples"); + + if (examples != null) + { + foreach (var key in examples.Keys.Where(k => examples[k] != null)) + { + context.Enter(key); + context.Enter("value"); + RuleHelpers.ValidateDataTypeMismatch(context, ruleName, examples[key]?.Value, schema); + context.Exit(); + context.Exit(); + } + } + + context.Exit(); + } + } +} diff --git a/src/Microsoft.OpenApi/Validations/Rules/OpenApiParameterRules.cs b/src/Microsoft.OpenApi/Validations/Rules/OpenApiParameterRules.cs index c6ad7835d..812bc7f12 100644 --- a/src/Microsoft.OpenApi/Validations/Rules/OpenApiParameterRules.cs +++ b/src/Microsoft.OpenApi/Validations/Rules/OpenApiParameterRules.cs @@ -58,45 +58,6 @@ public static class OpenApiParameterRules context.Exit(); }); - /// - /// Validate the data matches with the given data type. - /// - public static ValidationRule ParameterMismatchedDataType => - new(nameof(ParameterMismatchedDataType), - (context, parameter) => - { - // example - context.Enter("example"); - - if (parameter.Example != null) - { - RuleHelpers.ValidateDataTypeMismatch(context, nameof(ParameterMismatchedDataType), parameter.Example, parameter.Schema); - } - - context.Exit(); - - // examples - context.Enter("examples"); - - if (parameter.Examples != null) - { - foreach (var key in parameter.Examples.Keys) - { - if (parameter.Examples[key] != null) - { - context.Enter(key); - context.Enter("value"); - RuleHelpers.ValidateDataTypeMismatch(context, - nameof(ParameterMismatchedDataType), parameter.Examples[key]?.Value, parameter.Schema); - context.Exit(); - context.Exit(); - } - } - } - - context.Exit(); - }); - /// /// Validate that a path parameter should always appear in the path /// diff --git a/src/Microsoft.OpenApi/Validations/Rules/OpenApiSchemaRules.cs b/src/Microsoft.OpenApi/Validations/Rules/OpenApiSchemaRules.cs index e768e8d42..054c79c6b 100644 --- a/src/Microsoft.OpenApi/Validations/Rules/OpenApiSchemaRules.cs +++ b/src/Microsoft.OpenApi/Validations/Rules/OpenApiSchemaRules.cs @@ -13,49 +13,6 @@ namespace Microsoft.OpenApi.Validations.Rules [OpenApiRule] public static class OpenApiSchemaRules { - /// - /// Validate the data matches with the given data type. - /// - public static ValidationRule SchemaMismatchedDataType => - new(nameof(SchemaMismatchedDataType), - (context, schema) => - { - // default - context.Enter("default"); - - if (schema.Default != null) - { - RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Default, schema); - } - - context.Exit(); - - // example - context.Enter("example"); - - if (schema.Example != null) - { - RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Example, schema); - } - - context.Exit(); - - // enum - context.Enter("enum"); - - if (schema.Enum != null) - { - for (var i = 0; i < schema.Enum.Count; i++) - { - context.Enter(i.ToString()); - RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Enum[i], schema); - context.Exit(); - } - } - - context.Exit(); - }); - /// /// Validates Schema Discriminator /// diff --git a/src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs b/src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs index 9902360ec..097d61ace 100644 --- a/src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs +++ b/src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs @@ -1,10 +1,8 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. -using System; using System.Text.Json; using System.Text.Json.Nodes; -using Microsoft.OpenApi.Any; using Microsoft.OpenApi.Models; namespace Microsoft.OpenApi.Validations.Rules diff --git a/src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs b/src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs index 67b84f0be..3e38d65b2 100644 --- a/src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs +++ b/src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs @@ -1,5 +1,4 @@ - -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. using System; @@ -9,7 +8,6 @@ using Microsoft.OpenApi.Exceptions; using Microsoft.OpenApi.Properties; using Microsoft.OpenApi.Validations.Rules; -using System.Data; namespace Microsoft.OpenApi.Validations { @@ -329,17 +327,15 @@ internal static PropertyInfo[] GetValidationRuleTypes() ..typeof(OpenApiExternalDocsRules).GetProperties(BindingFlags.Static | BindingFlags.Public), ..typeof(OpenApiInfoRules).GetProperties(BindingFlags.Static | BindingFlags.Public), ..typeof(OpenApiLicenseRules).GetProperties(BindingFlags.Static | BindingFlags.Public), - ..typeof(OpenApiMediaTypeRules).GetProperties(BindingFlags.Static | BindingFlags.Public), ..typeof(OpenApiOAuthFlowRules).GetProperties(BindingFlags.Static | BindingFlags.Public), ..typeof(OpenApiServerRules).GetProperties(BindingFlags.Static | BindingFlags.Public), ..typeof(OpenApiResponseRules).GetProperties(BindingFlags.Static | BindingFlags.Public), ..typeof(OpenApiResponsesRules).GetProperties(BindingFlags.Static | BindingFlags.Public), ..typeof(OpenApiSchemaRules).GetProperties(BindingFlags.Static | BindingFlags.Public), - ..typeof(OpenApiHeaderRules).GetProperties(BindingFlags.Static | BindingFlags.Public), ..typeof(OpenApiTagRules).GetProperties(BindingFlags.Static | BindingFlags.Public), ..typeof(OpenApiPathsRules).GetProperties(BindingFlags.Static | BindingFlags.Public), - ..typeof(OpenApiParameterRules).GetProperties(BindingFlags.Static | BindingFlags.Public), - ]; + ..typeof(OpenApiParameterRules).GetProperties(BindingFlags.Static | BindingFlags.Public) + ]; } } } diff --git a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt index 3a7fdbd57..255717a65 100644 --- a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt +++ b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt @@ -1682,11 +1682,6 @@ namespace Microsoft.OpenApi.Validations.Rules public static Microsoft.OpenApi.Validations.ValidationRule UrlIsRequired { get; } } [Microsoft.OpenApi.Validations.Rules.OpenApiRule] - public static class OpenApiHeaderRules - { - public static Microsoft.OpenApi.Validations.ValidationRule HeaderMismatchedDataType { get; } - } - [Microsoft.OpenApi.Validations.Rules.OpenApiRule] public static class OpenApiInfoRules { public static Microsoft.OpenApi.Validations.ValidationRule InfoRequiredFields { get; } @@ -1696,10 +1691,12 @@ namespace Microsoft.OpenApi.Validations.Rules { public static Microsoft.OpenApi.Validations.ValidationRule LicenseRequiredFields { get; } } - [Microsoft.OpenApi.Validations.Rules.OpenApiRule] - public static class OpenApiMediaTypeRules + public static class OpenApiNonDefaultRules { + public static Microsoft.OpenApi.Validations.ValidationRule HeaderMismatchedDataType { get; } public static Microsoft.OpenApi.Validations.ValidationRule MediaTypeMismatchedDataType { get; } + public static Microsoft.OpenApi.Validations.ValidationRule ParameterMismatchedDataType { get; } + public static Microsoft.OpenApi.Validations.ValidationRule SchemaMismatchedDataType { get; } } [Microsoft.OpenApi.Validations.Rules.OpenApiRule] public static class OpenApiOAuthFlowRules @@ -1709,7 +1706,6 @@ namespace Microsoft.OpenApi.Validations.Rules [Microsoft.OpenApi.Validations.Rules.OpenApiRule] public static class OpenApiParameterRules { - public static Microsoft.OpenApi.Validations.ValidationRule ParameterMismatchedDataType { get; } public static Microsoft.OpenApi.Validations.ValidationRule ParameterRequiredFields { get; } public static Microsoft.OpenApi.Validations.ValidationRule PathParameterShouldBeInThePath { get; } public static Microsoft.OpenApi.Validations.ValidationRule RequiredMustBeTrueWhenInIsPath { get; } @@ -1739,7 +1735,6 @@ namespace Microsoft.OpenApi.Validations.Rules [Microsoft.OpenApi.Validations.Rules.OpenApiRule] public static class OpenApiSchemaRules { - public static Microsoft.OpenApi.Validations.ValidationRule SchemaMismatchedDataType { get; } public static Microsoft.OpenApi.Validations.ValidationRule ValidateSchemaDiscriminator { get; } public static bool TraverseSchemaElements(string discriminatorName, System.Collections.Generic.IList childSchema) { } public static bool ValidateChildSchemaAgainstDiscriminator(Microsoft.OpenApi.Models.OpenApiSchema schema, string discriminatorName) { } diff --git a/test/Microsoft.OpenApi.Tests/Validations/OpenApiHeaderValidationTests.cs b/test/Microsoft.OpenApi.Tests/Validations/OpenApiHeaderValidationTests.cs index bbc9dfe35..356a233a1 100644 --- a/test/Microsoft.OpenApi.Tests/Validations/OpenApiHeaderValidationTests.cs +++ b/test/Microsoft.OpenApi.Tests/Validations/OpenApiHeaderValidationTests.cs @@ -31,7 +31,10 @@ public void ValidateExampleShouldNotHaveDataTypeMismatchForSimpleSchema() }; // Act - var validator = new OpenApiValidator(ValidationRuleSet.GetDefaultRuleSet()); + var defaultRuleSet = ValidationRuleSet.GetDefaultRuleSet(); + defaultRuleSet.Add(typeof(OpenApiHeader), OpenApiNonDefaultRules.HeaderMismatchedDataType); + var validator = new OpenApiValidator(defaultRuleSet); + var walker = new OpenApiWalker(validator); walker.Walk(header); @@ -41,14 +44,6 @@ public void ValidateExampleShouldNotHaveDataTypeMismatchForSimpleSchema() // Assert result.Should().BeFalse(); - warnings.Select(e => e.Message).Should().BeEquivalentTo(new[] - { - RuleHelpers.DataTypeMismatchedErrorMessage - }); - warnings.Select(e => e.Pointer).Should().BeEquivalentTo(new[] - { - "#/example", - }); } [Fact] @@ -107,21 +102,7 @@ public void ValidateExamplesShouldNotHaveDataTypeMismatchForSimpleSchema() var result = !warnings.Any(); // Assert - result.Should().BeFalse(); - warnings.Select(e => e.Message).Should().BeEquivalentTo(new[] - { - RuleHelpers.DataTypeMismatchedErrorMessage, - RuleHelpers.DataTypeMismatchedErrorMessage, - RuleHelpers.DataTypeMismatchedErrorMessage, - }); - warnings.Select(e => e.Pointer).Should().BeEquivalentTo(new[] - { - // #enum/0 is not an error since the spec allows - // representing an object using a string. - "#/examples/example1/value/y", - "#/examples/example1/value/z", - "#/examples/example2/value" - }); + result.Should().BeTrue(); } } } diff --git a/test/Microsoft.OpenApi.Tests/Validations/OpenApiMediaTypeValidationTests.cs b/test/Microsoft.OpenApi.Tests/Validations/OpenApiMediaTypeValidationTests.cs index 9f42cb21b..29bd199e1 100644 --- a/test/Microsoft.OpenApi.Tests/Validations/OpenApiMediaTypeValidationTests.cs +++ b/test/Microsoft.OpenApi.Tests/Validations/OpenApiMediaTypeValidationTests.cs @@ -39,15 +39,7 @@ public void ValidateExampleShouldNotHaveDataTypeMismatchForSimpleSchema() var result = !warnings.Any(); // Assert - result.Should().BeFalse(); - warnings.Select(e => e.Message).Should().BeEquivalentTo(new[] - { - RuleHelpers.DataTypeMismatchedErrorMessage - }); - warnings.Select(e => e.Pointer).Should().BeEquivalentTo(new[] - { - "#/example", - }); + result.Should().BeTrue(); } [Fact] @@ -106,21 +98,7 @@ public void ValidateExamplesShouldNotHaveDataTypeMismatchForSimpleSchema() var result = !warnings.Any(); // Assert - result.Should().BeFalse(); - warnings.Select(e => e.Message).Should().BeEquivalentTo(new[] - { - RuleHelpers.DataTypeMismatchedErrorMessage, - RuleHelpers.DataTypeMismatchedErrorMessage, - RuleHelpers.DataTypeMismatchedErrorMessage, - }); - warnings.Select(e => e.Pointer).Should().BeEquivalentTo(new[] - { - // #enum/0 is not an error since the spec allows - // representing an object using a string. - "#/examples/example1/value/y", - "#/examples/example1/value/z", - "#/examples/example2/value" - }); + result.Should().BeTrue(); } } } diff --git a/test/Microsoft.OpenApi.Tests/Validations/OpenApiParameterValidationTests.cs b/test/Microsoft.OpenApi.Tests/Validations/OpenApiParameterValidationTests.cs index beac66d74..ef25808d2 100644 --- a/test/Microsoft.OpenApi.Tests/Validations/OpenApiParameterValidationTests.cs +++ b/test/Microsoft.OpenApi.Tests/Validations/OpenApiParameterValidationTests.cs @@ -88,15 +88,7 @@ public void ValidateExampleShouldNotHaveDataTypeMismatchForSimpleSchema() var result = !warnings.Any(); // Assert - result.Should().BeFalse(); - warnings.Select(e => e.Message).Should().BeEquivalentTo(new[] - { - RuleHelpers.DataTypeMismatchedErrorMessage - }); - warnings.Select(e => e.Pointer).Should().BeEquivalentTo(new[] - { - "#/{parameter1}/example", - }); + result.Should().BeTrue(); } [Fact] @@ -149,7 +141,10 @@ public void ValidateExamplesShouldNotHaveDataTypeMismatchForSimpleSchema() }; // Act - var validator = new OpenApiValidator(ValidationRuleSet.GetDefaultRuleSet()); + var defaultRuleSet = ValidationRuleSet.GetDefaultRuleSet(); + defaultRuleSet.Add(typeof(OpenApiParameter), OpenApiNonDefaultRules.ParameterMismatchedDataType); + + var validator = new OpenApiValidator(defaultRuleSet); validator.Enter("{parameter1}"); var walker = new OpenApiWalker(validator); walker.Walk(parameter); @@ -159,20 +154,6 @@ public void ValidateExamplesShouldNotHaveDataTypeMismatchForSimpleSchema() // Assert result.Should().BeFalse(); - warnings.Select(e => e.Message).Should().BeEquivalentTo(new[] - { - RuleHelpers.DataTypeMismatchedErrorMessage, - RuleHelpers.DataTypeMismatchedErrorMessage, - RuleHelpers.DataTypeMismatchedErrorMessage, - }); - warnings.Select(e => e.Pointer).Should().BeEquivalentTo(new[] - { - // #enum/0 is not an error since the spec allows - // representing an object using a string. - "#/{parameter1}/examples/example1/value/y", - "#/{parameter1}/examples/example1/value/z", - "#/{parameter1}/examples/example2/value" - }); } [Fact] diff --git a/test/Microsoft.OpenApi.Tests/Validations/OpenApiSchemaValidationTests.cs b/test/Microsoft.OpenApi.Tests/Validations/OpenApiSchemaValidationTests.cs index 5885377ed..5f4ba5d6b 100644 --- a/test/Microsoft.OpenApi.Tests/Validations/OpenApiSchemaValidationTests.cs +++ b/test/Microsoft.OpenApi.Tests/Validations/OpenApiSchemaValidationTests.cs @@ -39,15 +39,7 @@ public void ValidateDefaultShouldNotHaveDataTypeMismatchForSimpleSchema() var result = !warnings.Any(); // Assert - result.Should().BeFalse(); - warnings.Select(e => e.Message).Should().BeEquivalentTo(new[] - { - RuleHelpers.DataTypeMismatchedErrorMessage - }); - warnings.Select(e => e.Pointer).Should().BeEquivalentTo(new[] - { - "#/default", - }); + result.Should().BeTrue(); } [Fact] @@ -72,15 +64,7 @@ public void ValidateExampleAndDefaultShouldNotHaveDataTypeMismatchForSimpleSchem var expectedWarnings = warnings.Select(e => e.Message).ToList(); // Assert - result.Should().BeFalse(); - warnings.Select(e => e.Message).Should().BeEquivalentTo(new[] - { - RuleHelpers.DataTypeMismatchedErrorMessage - }); - warnings.Select(e => e.Pointer).Should().BeEquivalentTo(new[] - { - "#/example", - }); + result.Should().BeTrue(); } [Fact] @@ -122,21 +106,7 @@ public void ValidateEnumShouldNotHaveDataTypeMismatchForSimpleSchema() var result = !warnings.Any(); // Assert - result.Should().BeFalse(); - warnings.Select(e => e.Message).Should().BeEquivalentTo(new[] - { - RuleHelpers.DataTypeMismatchedErrorMessage, - RuleHelpers.DataTypeMismatchedErrorMessage, - RuleHelpers.DataTypeMismatchedErrorMessage, - }); - warnings.Select(e => e.Pointer).Should().BeEquivalentTo(new[] - { - // #enum/0 is not an error since the spec allows - // representing an object using a string. - "#/enum/1/y", - "#/enum/1/z", - "#/enum/2" - }); + result.Should().BeTrue(); } [Fact] @@ -204,7 +174,9 @@ public void ValidateDefaultShouldNotHaveDataTypeMismatchForComplexSchema() }; // Act - var validator = new OpenApiValidator(ValidationRuleSet.GetDefaultRuleSet()); + var defaultRuleSet = ValidationRuleSet.GetDefaultRuleSet(); + defaultRuleSet.Add(typeof(OpenApiSchema), OpenApiNonDefaultRules.SchemaMismatchedDataType); + var validator = new OpenApiValidator(defaultRuleSet); var walker = new OpenApiWalker(validator); walker.Walk(schema); @@ -213,18 +185,6 @@ public void ValidateDefaultShouldNotHaveDataTypeMismatchForComplexSchema() // Assert result.Should().BeFalse(); - warnings.Select(e => e.Message).Should().BeEquivalentTo(new[] - { - RuleHelpers.DataTypeMismatchedErrorMessage, - RuleHelpers.DataTypeMismatchedErrorMessage, - RuleHelpers.DataTypeMismatchedErrorMessage - }); - warnings.Select(e => e.Pointer).Should().BeEquivalentTo(new[] - { - "#/default/property1/2", - "#/default/property2/0", - "#/default/property2/1/z" - }); } [Fact] diff --git a/test/Microsoft.OpenApi.Tests/Validations/ValidationRuleSetTests.cs b/test/Microsoft.OpenApi.Tests/Validations/ValidationRuleSetTests.cs index 15ef6b07f..6b4a920cf 100644 --- a/test/Microsoft.OpenApi.Tests/Validations/ValidationRuleSetTests.cs +++ b/test/Microsoft.OpenApi.Tests/Validations/ValidationRuleSetTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. using System; @@ -55,8 +55,8 @@ public void RuleSetConstructorsReturnsTheCorrectRules() Assert.Empty(ruleSet_4.Rules); // Update the number if you add new default rule(s). - Assert.Equal(23, ruleSet_1.Rules.Count); - Assert.Equal(23, ruleSet_2.Rules.Count); + Assert.Equal(19, ruleSet_1.Rules.Count); + Assert.Equal(19, ruleSet_2.Rules.Count); Assert.Equal(3, ruleSet_3.Rules.Count); }