Skip to content

Commit 07f8f08

Browse files
committed
Add string extension methods to validate spec versions for easy reuse
1 parent deae283 commit 07f8f08

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
namespace Microsoft.OpenApi.Readers
5+
{
6+
/// <summary>
7+
/// Generates custom extension methods for the version string type
8+
/// </summary>
9+
public static class OpenApiVersionExtensionMethods
10+
{
11+
/// <summary>
12+
/// Extension method for Spec version 2.0
13+
/// </summary>
14+
/// <param name="version"></param>
15+
/// <returns></returns>
16+
public static bool is2_0(this string version)
17+
{
18+
bool result = false;
19+
if (version.Equals("2.0"))
20+
{
21+
result = true;
22+
}
23+
24+
return result;
25+
}
26+
27+
/// <summary>
28+
/// Extension method for Spec version 3.0
29+
/// </summary>
30+
/// <param name="version"></param>
31+
/// <returns></returns>
32+
public static bool is3_0(this string version)
33+
{
34+
bool result = false;
35+
if (version.StartsWith("3.0"))
36+
{
37+
result = true;
38+
}
39+
40+
return result;
41+
}
42+
43+
/// <summary>
44+
/// Extension method for Spec version 3.1
45+
/// </summary>
46+
/// <param name="version"></param>
47+
/// <returns></returns>
48+
public static bool is3_1(this string version)
49+
{
50+
bool result = false;
51+
if (version.StartsWith("3.1"))
52+
{
53+
result = true;
54+
}
55+
56+
return result;
57+
}
58+
}
59+
}

src/Microsoft.OpenApi.Readers/ParsingContext.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ internal OpenApiDocument Parse(YamlDocument yamlDocument)
5959

6060
switch (inputVersion)
6161
{
62-
case string version when version == "2.0":
62+
case string version when version.is2_0():
6363
VersionService = new OpenApiV2VersionService(Diagnostic);
6464
doc = VersionService.LoadDocument(RootNode);
6565
this.Diagnostic.SpecificationVersion = OpenApiSpecVersion.OpenApi2_0;
6666
ValidateRequiredFields(doc, version);
6767
break;
6868

69-
case string version when version.StartsWith("3.0") || version.StartsWith("3.1"):
69+
case string version when version.is3_0() || version.is3_1():
7070
VersionService = new OpenApiV3VersionService(Diagnostic);
7171
doc = VersionService.LoadDocument(RootNode);
7272
this.Diagnostic.SpecificationVersion = OpenApiSpecVersion.OpenApi3_0;
@@ -248,12 +248,12 @@ public void PopLoop(string loopid)
248248

249249
private void ValidateRequiredFields(OpenApiDocument doc, string version)
250250
{
251-
if ((version == "2.0" || version.StartsWith("3.0")) && (doc.Paths == null || !doc.Paths.Any()))
251+
if ((version.is2_0() || version.is3_0()) && (doc.Paths == null || !doc.Paths.Any()))
252252
{
253253
// paths is a required field in OpenAPI 3.0 but optional in 3.1
254254
RootNode.Context.Diagnostic.Errors.Add(new OpenApiError("", $"Paths is a REQUIRED field at {RootNode.Context.GetLocation()}"));
255255
}
256-
else if (version.StartsWith("3.1") && (doc.Paths == null || !doc.Paths.Any()) && (doc.Webhooks == null || !doc.Webhooks.Any()))
256+
else if (version.is3_1() && (doc.Paths == null || !doc.Paths.Any()) && (doc.Webhooks == null || !doc.Webhooks.Any()))
257257
{
258258
RootNode.Context.Diagnostic.Errors.Add(new OpenApiError(
259259
"", $"The document MUST contain either a Paths or Webhooks field at {RootNode.Context.GetLocation()}"));

0 commit comments

Comments
 (0)