Skip to content

Commit d101fc3

Browse files
Merge pull request #2472 from martincostello/gh-2454
feat: add Validation Rule for path operations to not have a request body
1 parent 808397b commit d101fc3

19 files changed

+437
-125
lines changed

src/Microsoft.OpenApi/Services/OpenApiVisitorBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public abstract class OpenApiVisitorBase
2222
public CurrentKeys CurrentKeys { get; } = new();
2323

2424
/// <summary>
25-
/// Allow Rule to indicate validation error occured at a deeper context level.
25+
/// Allow Rule to indicate validation error occurred at a deeper context level.
2626
/// </summary>
2727
/// <param name="segment">Identifier for context</param>
2828
public virtual void Enter(string segment)

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

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

4-
using System;
5-
64
namespace Microsoft.OpenApi
75
{
86
/// <summary>
@@ -18,14 +16,13 @@ public static class OpenApiContactRules
1816
new(nameof(EmailMustBeEmailFormat),
1917
(context, item) =>
2018
{
21-
context.Enter("email");
2219
if (item is {Email: not null} && !item.Email.IsEmailAddress())
2320
{
21+
context.Enter("email");
2422
context.CreateError(nameof(EmailMustBeEmailFormat),
25-
String.Format(SRResource.Validation_StringMustBeEmailAddress, item.Email));
23+
string.Format(SRResource.Validation_StringMustBeEmailAddress, item.Email));
24+
context.Exit();
2625
}
27-
context.Exit();
2826
});
29-
3027
}
3128
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ public static class OpenApiDocumentRules
1919
(context, item) =>
2020
{
2121
// info
22-
context.Enter("info");
2322
if (item.Info == null)
2423
{
24+
context.Enter("info");
2525
context.CreateError(nameof(OpenApiDocumentFieldIsMissing),
2626
string.Format(SRResource.Validation_FieldIsRequired, "info", "document"));
27+
context.Exit();
2728
}
28-
context.Exit();
2929
});
3030

3131
/// <summary>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ public static class OpenApiExtensibleRules
1919
new(nameof(ExtensionNameMustStartWithXDash),
2020
(context, item) =>
2121
{
22-
context.Enter("extensions");
2322
if (item.Extensions is not null)
2423
{
24+
context.Enter("extensions");
2525
foreach (var extensible in item.Extensions.Keys.Where(static x => !x.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase)))
2626
{
2727
context.CreateError(nameof(ExtensionNameMustStartWithXDash),
2828
string.Format(SRResource.Validation_ExtensionNameMustBeginWithXDash, extensible, context.PathString));
29-
}
29+
}
30+
context.Exit();
3031
}
31-
context.Exit();
3232
});
3333
}
3434
}

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

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

4-
using System;
5-
64
namespace Microsoft.OpenApi
75
{
86
/// <summary>
@@ -19,15 +17,13 @@ public static class OpenApiExternalDocsRules
1917
(context, item) =>
2018
{
2119
// url
22-
context.Enter("url");
2320
if (item.Url == null)
2421
{
22+
context.Enter("url");
2523
context.CreateError(nameof(UrlIsRequired),
26-
String.Format(SRResource.Validation_FieldIsRequired, "url", "External Documentation"));
24+
string.Format(SRResource.Validation_FieldIsRequired, "url", "External Documentation"));
25+
context.Exit();
2726
}
28-
context.Exit();
2927
});
30-
31-
// add more rule.
3228
}
3329
}
Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
using System;
5-
64
namespace Microsoft.OpenApi
75
{
86
/// <summary>
@@ -19,25 +17,22 @@ public static class OpenApiInfoRules
1917
(context, item) =>
2018
{
2119
// title
22-
context.Enter("title");
2320
if (item.Title == null)
2421
{
22+
context.Enter("title");
2523
context.CreateError(nameof(InfoRequiredFields),
26-
String.Format(SRResource.Validation_FieldIsRequired, "title", "info"));
24+
string.Format(SRResource.Validation_FieldIsRequired, "title", "info"));
25+
context.Exit();
2726
}
28-
context.Exit();
2927

3028
// version
31-
context.Enter("version");
3229
if (item.Version == null)
3330
{
31+
context.Enter("version");
3432
context.CreateError(nameof(InfoRequiredFields),
35-
String.Format(SRResource.Validation_FieldIsRequired, "version", "info"));
33+
string.Format(SRResource.Validation_FieldIsRequired, "version", "info"));
34+
context.Exit();
3635
}
37-
context.Exit();
38-
3936
});
40-
41-
// add more rule.
4237
}
4338
}

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

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

4-
using System;
5-
64
namespace Microsoft.OpenApi
75
{
86
/// <summary>
@@ -18,15 +16,13 @@ public static class OpenApiLicenseRules
1816
new(nameof(LicenseRequiredFields),
1917
(context, license) =>
2018
{
21-
context.Enter("name");
2219
if (license.Name == null)
2320
{
21+
context.Enter("name");
2422
context.CreateError(nameof(LicenseRequiredFields),
25-
String.Format(SRResource.Validation_FieldIsRequired, "name", "license"));
23+
string.Format(SRResource.Validation_FieldIsRequired, "name", "license"));
24+
context.Exit();
2625
}
27-
context.Exit();
2826
});
29-
30-
// add more rules
3127
}
3228
}

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

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static class OpenApiNonDefaultRules
3636
/// Validate the data matches with the given data type.
3737
/// </summary>
3838
public static ValidationRule<OpenApiParameter> ParameterMismatchedDataType =>
39-
new(nameof(ParameterMismatchedDataType),
39+
new(nameof(ParameterMismatchedDataType),
4040
(context, parameter) =>
4141
{
4242
ValidateMismatchedDataType(context, nameof(ParameterMismatchedDataType), parameter.Example, parameter.Examples, parameter.Schema);
@@ -50,39 +50,33 @@ public static class OpenApiNonDefaultRules
5050
(context, schema) =>
5151
{
5252
// default
53-
context.Enter("default");
54-
5553
if (schema.Default != null)
5654
{
55+
context.Enter("default");
5756
RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Default, schema);
57+
context.Exit();
5858
}
5959

60-
context.Exit();
61-
6260
// example
63-
context.Enter("example");
64-
6561
if (schema.Example != null)
6662
{
63+
context.Enter("example");
6764
RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Example, schema);
65+
context.Exit();
6866
}
6967

70-
context.Exit();
71-
7268
// enum
73-
context.Enter("enum");
74-
7569
if (schema.Enum != null)
7670
{
71+
context.Enter("enum");
7772
for (var i = 0; i < schema.Enum.Count; i++)
7873
{
7974
context.Enter(i.ToString());
8075
RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Enum[i], schema);
8176
context.Exit();
8277
}
78+
context.Exit();
8379
}
84-
85-
context.Exit();
8680
});
8781

8882
private static void ValidateMismatchedDataType(IValidationContext context,
@@ -92,20 +86,17 @@ private static void ValidateMismatchedDataType(IValidationContext context,
9286
IOpenApiSchema? schema)
9387
{
9488
// example
95-
context.Enter("example");
96-
9789
if (example != null)
9890
{
91+
context.Enter("example");
9992
RuleHelpers.ValidateDataTypeMismatch(context, ruleName, example, schema);
93+
context.Exit();
10094
}
10195

102-
context.Exit();
103-
10496
// enum
105-
context.Enter("examples");
106-
10797
if (examples != null)
10898
{
99+
context.Enter("examples");
109100
foreach (var key in examples.Keys.Where(k => examples[k] != null))
110101
{
111102
context.Enter(key);
@@ -114,9 +105,8 @@ private static void ValidateMismatchedDataType(IValidationContext context,
114105
context.Exit();
115106
context.Exit();
116107
}
108+
context.Exit();
117109
}
118-
119-
context.Exit();
120110
}
121111
}
122112
}
Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
using System;
5-
64
namespace Microsoft.OpenApi
75
{
86
/// <summary>
@@ -19,33 +17,31 @@ public static class OpenApiOAuthFlowRules
1917
(context, flow) =>
2018
{
2119
// authorizationUrl
22-
context.Enter("authorizationUrl");
2320
if (flow.AuthorizationUrl == null)
2421
{
22+
context.Enter("authorizationUrl");
2523
context.CreateError(nameof(OAuthFlowRequiredFields),
26-
String.Format(SRResource.Validation_FieldIsRequired, "authorizationUrl", "OAuth Flow"));
24+
string.Format(SRResource.Validation_FieldIsRequired, "authorizationUrl", "OAuth Flow"));
25+
context.Exit();
2726
}
28-
context.Exit();
2927

3028
// tokenUrl
31-
context.Enter("tokenUrl");
3229
if (flow.TokenUrl == null)
3330
{
31+
context.Enter("tokenUrl");
3432
context.CreateError(nameof(OAuthFlowRequiredFields),
35-
String.Format(SRResource.Validation_FieldIsRequired, "tokenUrl", "OAuth Flow"));
33+
string.Format(SRResource.Validation_FieldIsRequired, "tokenUrl", "OAuth Flow"));
34+
context.Exit();
3635
}
37-
context.Exit();
3836

3937
// scopes
40-
context.Enter("scopes");
4138
if (flow.Scopes == null)
4239
{
40+
context.Enter("scopes");
4341
context.CreateError(nameof(OAuthFlowRequiredFields),
44-
String.Format(SRResource.Validation_FieldIsRequired, "scopes", "OAuth Flow"));
42+
string.Format(SRResource.Validation_FieldIsRequired, "scopes", "OAuth Flow"));
43+
context.Exit();
4544
}
46-
context.Exit();
4745
});
48-
49-
// add more rule.
5046
}
5147
}

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

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

4-
using System;
5-
64
namespace Microsoft.OpenApi
75
{
86
/// <summary>
@@ -19,22 +17,22 @@ public static class OpenApiParameterRules
1917
(context, item) =>
2018
{
2119
// name
22-
context.Enter("name");
2320
if (item.Name == null)
2421
{
22+
context.Enter("name");
2523
context.CreateError(nameof(ParameterRequiredFields),
26-
String.Format(SRResource.Validation_FieldIsRequired, "name", "parameter"));
24+
string.Format(SRResource.Validation_FieldIsRequired, "name", "parameter"));
25+
context.Exit();
2726
}
28-
context.Exit();
2927

3028
// in
31-
context.Enter("in");
3229
if (item.In == null)
3330
{
31+
context.Enter("in");
3432
context.CreateError(nameof(ParameterRequiredFields),
35-
String.Format(SRResource.Validation_FieldIsRequired, "in", "parameter"));
33+
string.Format(SRResource.Validation_FieldIsRequired, "in", "parameter"));
34+
context.Exit();
3635
}
37-
context.Exit();
3836
});
3937

4038
/// <summary>
@@ -45,15 +43,14 @@ public static class OpenApiParameterRules
4543
(context, item) =>
4644
{
4745
// required
48-
context.Enter("required");
4946
if (item.In == ParameterLocation.Path && !item.Required)
5047
{
48+
context.Enter("required");
5149
context.CreateError(
5250
nameof(RequiredMustBeTrueWhenInIsPath),
5351
"\"required\" must be true when parameter location is \"path\"");
52+
context.Exit();
5453
}
55-
56-
context.Exit();
5754
});
5855

5956
/// <summary>

0 commit comments

Comments
 (0)