From 9bf3f0869166631840759c906abf5112c8505ae2 Mon Sep 17 00:00:00 2001 From: Maggiekimani1 Date: Wed, 23 Oct 2024 17:36:08 +0300 Subject: [PATCH] remove depracated validation rule --- .../Validations/Rules/OpenApiHeaderRules.cs | 57 ---- .../Rules/OpenApiMediaTypeRules.cs | 63 ----- .../Rules/OpenApiParameterRules.cs | 39 --- .../Validations/Rules/OpenApiSchemaRules.cs | 43 --- .../Validations/Rules/RuleHelpers.cs | 249 ------------------ .../Validations/ValidationRuleSet.cs | 2 - 6 files changed, 453 deletions(-) delete mode 100644 src/Microsoft.OpenApi/Validations/Rules/OpenApiHeaderRules.cs delete mode 100644 src/Microsoft.OpenApi/Validations/Rules/OpenApiMediaTypeRules.cs 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/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..f6891c043 100644 --- a/src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs +++ b/src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs @@ -1,18 +1,10 @@ // 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 { internal static class RuleHelpers { - internal const string DataTypeMismatchedErrorMessage = "Data and type mismatch found."; - /// /// Input string must be in the format of an email address /// @@ -40,246 +32,5 @@ public static bool IsEmailAddress(this string input) return true; } - - public static void ValidateDataTypeMismatch( - IValidationContext context, - string ruleName, - JsonNode value, - OpenApiSchema schema) - { - if (schema == null) - { - return; - } - - // convert value to JsonElement and access the ValueKind property to determine the type. - var jsonElement = JsonDocument.Parse(JsonSerializer.Serialize(value)).RootElement; - - var type = (string)schema.Type; - var format = schema.Format; - var nullable = schema.Nullable; - - // Before checking the type, check first if the schema allows null. - // If so and the data given is also null, this is allowed for any type. - if (nullable && jsonElement.ValueKind is JsonValueKind.Null) - { - return; - } - - if (type == "object") - { - // It is not against the spec to have a string representing an object value. - // To represent examples of media types that cannot naturally be represented in JSON or YAML, - // a string value can contain the example with escaping where necessary - if (jsonElement.ValueKind is JsonValueKind.String) - { - return; - } - - // If value is not a string and also not an object, there is a data mismatch. - if (value is not JsonObject anyObject) - { - context.CreateWarning( - ruleName, - DataTypeMismatchedErrorMessage); - return; - } - - foreach (var kvp in anyObject) - { - var key = kvp.Key; - context.Enter(key); - - if (schema.Properties != null && - schema.Properties.TryGetValue(key, out var property)) - { - ValidateDataTypeMismatch(context, ruleName, anyObject[key], property); - } - else - { - ValidateDataTypeMismatch(context, ruleName, anyObject[key], schema.AdditionalProperties); - } - - context.Exit(); - } - - return; - } - - if (type == "array") - { - // It is not against the spec to have a string representing an array value. - // To represent examples of media types that cannot naturally be represented in JSON or YAML, - // a string value can contain the example with escaping where necessary - if (jsonElement.ValueKind is JsonValueKind.String) - { - return; - } - - // If value is not a string and also not an array, there is a data mismatch. - if (value is not JsonArray anyArray) - { - context.CreateWarning( - ruleName, - DataTypeMismatchedErrorMessage); - return; - } - - for (var i = 0; i < anyArray.Count; i++) - { - context.Enter(i.ToString()); - - ValidateDataTypeMismatch(context, ruleName, anyArray[i], schema.Items); - - context.Exit(); - } - - return; - } - - if (type == "integer" && format == "int32") - { - if (jsonElement.ValueKind is not JsonValueKind.Number) - { - context.CreateWarning( - ruleName, - DataTypeMismatchedErrorMessage); - } - - return; - } - - if (type == "integer" && format == "int64") - { - if (jsonElement.ValueKind is not JsonValueKind.Number) - { - context.CreateWarning( - ruleName, - DataTypeMismatchedErrorMessage); - } - - return; - } - - if (type == "integer" && jsonElement.ValueKind is not JsonValueKind.Number) - { - if (jsonElement.ValueKind is not JsonValueKind.Number) - { - context.CreateWarning( - ruleName, - DataTypeMismatchedErrorMessage); - } - - return; - } - - if (type == "number" && format == "float") - { - if (jsonElement.ValueKind is not JsonValueKind.Number) - { - context.CreateWarning( - ruleName, - DataTypeMismatchedErrorMessage); - } - - return; - } - - if (type == "number" && format == "double") - { - if (jsonElement.ValueKind is not JsonValueKind.Number) - { - context.CreateWarning( - ruleName, - DataTypeMismatchedErrorMessage); - } - - return; - } - - if (type == "number") - { - if (jsonElement.ValueKind is not JsonValueKind.Number) - { - context.CreateWarning( - ruleName, - DataTypeMismatchedErrorMessage); - } - - return; - } - - if (type == "string" && format == "byte") - { - if (jsonElement.ValueKind is not JsonValueKind.String) - { - context.CreateWarning( - ruleName, - DataTypeMismatchedErrorMessage); - } - - return; - } - - if (type == "string" && format == "date") - { - if (jsonElement.ValueKind is not JsonValueKind.String) - { - context.CreateWarning( - ruleName, - DataTypeMismatchedErrorMessage); - } - - return; - } - - if (type == "string" && format == "date-time") - { - if (jsonElement.ValueKind is not JsonValueKind.String) - { - context.CreateWarning( - ruleName, - DataTypeMismatchedErrorMessage); - } - - return; - } - - if (type == "string" && format == "password") - { - if (jsonElement.ValueKind is not JsonValueKind.String) - { - context.CreateWarning( - ruleName, - DataTypeMismatchedErrorMessage); - } - - return; - } - - if (type == "string") - { - if (jsonElement.ValueKind is not JsonValueKind.String) - { - context.CreateWarning( - ruleName, - DataTypeMismatchedErrorMessage); - } - - return; - } - - if (type == "boolean") - { - if (jsonElement.ValueKind is not JsonValueKind.True && jsonElement.ValueKind is not JsonValueKind.False) - { - context.CreateWarning( - ruleName, - DataTypeMismatchedErrorMessage); - } - - return; - } - } } } diff --git a/src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs b/src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs index 67b84f0be..c818e0d6b 100644 --- a/src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs +++ b/src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs @@ -329,13 +329,11 @@ 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),