Skip to content
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

Remove DataTypeMismatch validation rule from the default ruleset #1888

Merged
merged 7 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
57 changes: 0 additions & 57 deletions src/Microsoft.OpenApi/Validations/Rules/OpenApiHeaderRules.cs

This file was deleted.

63 changes: 0 additions & 63 deletions src/Microsoft.OpenApi/Validations/Rules/OpenApiMediaTypeRules.cs

This file was deleted.

125 changes: 125 additions & 0 deletions src/Microsoft.OpenApi/Validations/Rules/OpenApiNonDefaultRules.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System.Collections.Generic;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.Models;

namespace Microsoft.OpenApi.Validations.Rules
{
/// <summary>
/// Defines a non-default set of rules for validating examples in header, media type and parameter objects against the schema
/// </summary>
public static class OpenApiNonDefaultRules
{
/// <summary>
/// Validate the data matches with the given data type.
/// </summary>
public static ValidationRule<OpenApiHeader> HeaderMismatchedDataType =>
new(nameof(HeaderMismatchedDataType),
(context, header) =>
{
ValidateMismatchedDataType(context, nameof(HeaderMismatchedDataType), header.Example, header.Examples, header.Schema);
});

/// <summary>
/// Validate the data matches with the given data type.
/// </summary>
public static ValidationRule<OpenApiMediaType> MediaTypeMismatchedDataType =>
new(nameof(MediaTypeMismatchedDataType),
(context, mediaType) =>
{
ValidateMismatchedDataType(context, nameof(MediaTypeMismatchedDataType), mediaType.Example, mediaType.Examples, mediaType.Schema);
});

/// <summary>
/// Validate the data matches with the given data type.
/// </summary>
public static ValidationRule<OpenApiParameter> ParameterMismatchedDataType =>
new(nameof(ParameterMismatchedDataType),
(context, parameter) =>
{
ValidateMismatchedDataType(context, nameof(ParameterMismatchedDataType), parameter.Example, parameter.Examples, parameter.Schema);
});

/// <summary>
/// Validate the data matches with the given data type.
/// </summary>
public static ValidationRule<OpenApiSchema> 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<string, OpenApiExample> 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)
{
if (examples[key] != null)
{
context.Enter(key);
context.Enter("value");
RuleHelpers.ValidateDataTypeMismatch(context, ruleName, examples[key]?.Value, schema);
context.Exit();
context.Exit();
}
}
Fixed Show fixed Hide fixed
}

context.Exit();
}
}
}
39 changes: 0 additions & 39 deletions src/Microsoft.OpenApi/Validations/Rules/OpenApiParameterRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,45 +58,6 @@ public static class OpenApiParameterRules
context.Exit();
});

/// <summary>
/// Validate the data matches with the given data type.
/// </summary>
public static ValidationRule<OpenApiParameter> 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();
});

/// <summary>
/// Validate that a path parameter should always appear in the path
/// </summary>
Expand Down
43 changes: 0 additions & 43 deletions src/Microsoft.OpenApi/Validations/Rules/OpenApiSchemaRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,6 @@ namespace Microsoft.OpenApi.Validations.Rules
[OpenApiRule]
public static class OpenApiSchemaRules
{
/// <summary>
/// Validate the data matches with the given data type.
/// </summary>
public static ValidationRule<OpenApiSchema> 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();
});

/// <summary>
/// Validates Schema Discriminator
/// </summary>
Expand Down
2 changes: 0 additions & 2 deletions src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 3 additions & 7 deletions src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,7 +8,6 @@
using Microsoft.OpenApi.Exceptions;
using Microsoft.OpenApi.Properties;
using Microsoft.OpenApi.Validations.Rules;
using System.Data;

namespace Microsoft.OpenApi.Validations
{
Expand Down Expand Up @@ -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)
];
}
}
}
Loading
Loading