This repository was archived by the owner on Nov 11, 2025. It is now read-only.
forked from microsoft/OpenAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenApiSpecVersionHelper.cs
More file actions
46 lines (41 loc) · 1.55 KB
/
OpenApiSpecVersionHelper.cs
File metadata and controls
46 lines (41 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
namespace Microsoft.OpenApi.Hidi
{
public static class OpenApiSpecVersionHelper
{
public static OpenApiSpecVersion TryParseOpenApiSpecVersion(string value)
{
if (string.IsNullOrEmpty(value))
{
throw new InvalidOperationException("Please provide a version");
}
// Split the version string by the dot
var versionSegments = value.Split('.', StringSplitOptions.RemoveEmptyEntries);
if (!int.TryParse(versionSegments[0], out var majorVersion)
|| !int.TryParse(versionSegments[1], out var minorVersion))
{
throw new InvalidOperationException("Invalid version format. Please provide a valid OpenAPI version (e.g., 2.0, 3.0, 3.1).");
}
// Check for specific version matches
if (majorVersion == 2)
{
return OpenApiSpecVersion.OpenApi2_0;
}
else if (majorVersion == 3 && minorVersion == 0)
{
return OpenApiSpecVersion.OpenApi3_0;
}
else if (majorVersion == 3 && minorVersion == 1)
{
return OpenApiSpecVersion.OpenApi3_1;
}
else if (majorVersion == 3 && minorVersion == 2)
{
return OpenApiSpecVersion.OpenApi3_2;
}
return OpenApiSpecVersion.OpenApi3_2; // default
}
}
}