Skip to content

Commit b29d532

Browse files
committed
Change debug.assert to throw arugment null and use the resource from the product codes
1 parent df86936 commit b29d532

35 files changed

+291
-93
lines changed

src/Microsoft.OpenApi/Properties/SRResource.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Microsoft.OpenApi/Properties/SRResource.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@
129129
<data name="IndentationLevelInvalid" xml:space="preserve">
130130
<value>Indentation level cannot be lower than 0.</value>
131131
</data>
132+
<data name="InputItemShouldBeType" xml:space="preserve">
133+
<value>The input item should be in type of '{0}'.</value>
134+
</data>
132135
<data name="ObjectScopeNeededForPropertyNameWriting" xml:space="preserve">
133136
<value>The active scope must be an object scope for property name '{0}' to be written.</value>
134137
</data>

src/Microsoft.OpenApi/Validations/Rules/OpenApiTagRules.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static class OpenApiTagRules
2424
if (String.IsNullOrEmpty(tag.Name))
2525
{
2626
ValidationError error = new ValidationError(ErrorReason.Required, context.PathString,
27-
String.Format(SRResource.Validation_FieldIsRequired, "name", "Tag"));
27+
String.Format(SRResource.Validation_FieldIsRequired, "name", "tag"));
2828
context.AddError(error);
2929
}
3030
context.Pop();

src/Microsoft.OpenApi/Validations/ValidationRule.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Licensed under the MIT license.
33

44
using System;
5-
using System.Diagnostics;
65
using Microsoft.OpenApi.Interfaces;
6+
using Microsoft.OpenApi.Properties;
77

88
namespace Microsoft.OpenApi.Validations
99
{
@@ -49,7 +49,21 @@ internal override Type ElementType
4949

5050
internal override void Evaluate(ValidationContext context, object item)
5151
{
52-
Debug.Assert(item is T, "item should be " + typeof(T));
52+
if (context == null)
53+
{
54+
throw Error.ArgumentNull(nameof(context));
55+
}
56+
57+
if (item == null)
58+
{
59+
return;
60+
}
61+
62+
if (!(item is T))
63+
{
64+
throw Error.Argument(string.Format(SRResource.InputItemShouldBeType, typeof(T).FullName));
65+
}
66+
5367
T typedItem = (T)item;
5468
this._validate(context, typedItem);
5569
}

src/Microsoft.OpenApi/Validations/Visitors/CallbackVisitor.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
using System.Diagnostics;
54
using Microsoft.OpenApi.Models;
65

76
namespace Microsoft.OpenApi.Validations.Visitors
@@ -18,8 +17,15 @@ internal class CallbackVisitor : VisitorBase<OpenApiCallback>
1817
/// <param name="callback">The <see cref="OpenApiCallback"/>.</param>
1918
protected override void Next(ValidationContext context, OpenApiCallback callback)
2019
{
21-
Debug.Assert(context != null);
22-
Debug.Assert(callback != null);
20+
if (context == null)
21+
{
22+
throw Error.ArgumentNull(nameof(context));
23+
}
24+
25+
if (callback == null)
26+
{
27+
throw Error.ArgumentNull(nameof(callback));
28+
}
2329

2430
foreach (var pathItem in callback.PathItems)
2531
{

src/Microsoft.OpenApi/Validations/Visitors/ComponentsVisitor.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
using System.Diagnostics;
54
using Microsoft.OpenApi.Models;
65

76
namespace Microsoft.OpenApi.Validations.Visitors
@@ -18,18 +17,34 @@ internal class ComponentsVisitor : VisitorBase<OpenApiComponents>
1817
/// <param name="components">The <see cref="OpenApiComponents"/>.</param>
1918
protected override void Next(ValidationContext context, OpenApiComponents components)
2019
{
21-
Debug.Assert(context != null);
22-
Debug.Assert(components != null);
20+
if (context == null)
21+
{
22+
throw Error.ArgumentNull(nameof(context));
23+
}
24+
25+
if (components == null)
26+
{
27+
throw Error.ArgumentNull(nameof(components));
28+
}
2329

2430
context.ValidateMap(components.Schemas);
31+
2532
context.ValidateMap(components.Responses);
33+
2634
context.ValidateMap(components.Parameters);
35+
2736
context.ValidateMap(components.Examples);
37+
2838
context.ValidateMap(components.RequestBodies);
39+
2940
context.ValidateMap(components.Headers);
41+
3042
context.ValidateMap(components.SecuritySchemes);
43+
3144
context.ValidateMap(components.Links);
45+
3246
context.ValidateMap(components.Callbacks);
47+
3348
base.Next(context, components);
3449
}
3550
}

src/Microsoft.OpenApi/Validations/Visitors/DocumentVisitor.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,31 @@ internal class DocumentVisitor : VisitorBase<OpenApiDocument>
1717
/// <param name="document">The <see cref="OpenApiDocument"/>.</param>
1818
protected override void Next(ValidationContext context, OpenApiDocument document)
1919
{
20+
if (context == null)
21+
{
22+
throw Error.ArgumentNull(nameof(context));
23+
}
24+
2025
if (document == null)
2126
{
22-
return;
27+
throw Error.ArgumentNull(nameof(document));
2328
}
2429

2530
context.Validate(document.Info);
2631

32+
context.ValidateCollection(document.Servers);
33+
34+
context.Validate(document.Paths);
35+
36+
context.Validate(document.Components);
37+
38+
context.ValidateCollection(document.SecurityRequirements);
39+
2740
context.ValidateCollection(document.Tags);
2841

29-
// add more.
42+
context.Validate(document.ExternalDocs);
43+
44+
base.Next(context, document);
3045
}
3146
}
3247
}

src/Microsoft.OpenApi/Validations/Visitors/EncodingVisitor.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
using System.Diagnostics;
54
using Microsoft.OpenApi.Models;
65

76
namespace Microsoft.OpenApi.Validations.Visitors
@@ -18,8 +17,15 @@ internal class EncodingVisitor : VisitorBase<OpenApiEncoding>
1817
/// <param name="encoding">The <see cref="OpenApiEncoding"/>.</param>
1918
protected override void Next(ValidationContext context, OpenApiEncoding encoding)
2019
{
21-
Debug.Assert(context != null);
22-
Debug.Assert(encoding != null);
20+
if (context == null)
21+
{
22+
throw Error.ArgumentNull(nameof(context));
23+
}
24+
25+
if (encoding == null)
26+
{
27+
throw Error.ArgumentNull(nameof(encoding));
28+
}
2329

2430
context.ValidateMap(encoding.Headers);
2531

src/Microsoft.OpenApi/Validations/Visitors/HeaderVisitor.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
using System.Diagnostics;
54
using Microsoft.OpenApi.Models;
65

76
namespace Microsoft.OpenApi.Validations.Visitors
@@ -18,8 +17,15 @@ internal class HeaderVisitor : VisitorBase<OpenApiHeader>
1817
/// <param name="header">The <see cref="OpenApiHeader"/>.</param>
1918
protected override void Next(ValidationContext context, OpenApiHeader header)
2019
{
21-
Debug.Assert(context != null);
22-
Debug.Assert(header != null);
20+
if (context == null)
21+
{
22+
throw Error.ArgumentNull(nameof(context));
23+
}
24+
25+
if (header == null)
26+
{
27+
throw Error.ArgumentNull(nameof(header));
28+
}
2329

2430
context.Validate(header.Schema);
2531

src/Microsoft.OpenApi/Validations/Visitors/InfoVisitor.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
using System.Diagnostics;
54
using Microsoft.OpenApi.Models;
65

76
namespace Microsoft.OpenApi.Validations.Visitors
@@ -18,8 +17,15 @@ internal class InfoVisitor : VisitorBase<OpenApiInfo>
1817
/// <param name="info">The <see cref="OpenApiInfo"/>.</param>
1918
protected override void Next(ValidationContext context, OpenApiInfo info)
2019
{
21-
Debug.Assert(context != null);
22-
Debug.Assert(info != null);
20+
if (context == null)
21+
{
22+
throw Error.ArgumentNull(nameof(context));
23+
}
24+
25+
if (info == null)
26+
{
27+
throw Error.ArgumentNull(nameof(info));
28+
}
2329

2430
context.Validate(info.Contact);
2531

0 commit comments

Comments
 (0)