| | | 1 | | using Microsoft.OpenApi; |
| | | 2 | | |
| | | 3 | | namespace Kestrun.OpenApi; |
| | | 4 | | /// <summary> |
| | | 5 | | /// Extension methods for OpenApiSpecVersion enum. |
| | | 6 | | /// </summary> |
| | | 7 | | public static class OpenApiSpecVersionExtensions |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Converts a string representation of an OpenAPI spec version to the corresponding enum value. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <param name="version">The string representation of the OpenAPI spec version.</param> |
| | | 13 | | /// <returns>The corresponding <see cref="OpenApiSpecVersion"/> enum value.</returns> |
| | | 14 | | /// <exception cref="ArgumentException">Thrown when the provided version string is not recognized.</exception> |
| | | 15 | | public static OpenApiSpecVersion ParseOpenApiSpecVersion(this string version) |
| | | 16 | | { |
| | 0 | 17 | | var normalized = version.TrimStart('v', 'V'); // e.g. "2.0", "3.0", "3.1" |
| | 0 | 18 | | return normalized switch |
| | 0 | 19 | | { |
| | 0 | 20 | | "2.0" => OpenApiSpecVersion.OpenApi2_0, |
| | 0 | 21 | | "3.0" or "3.0.0" or "3.0.1" or "3.0.2" or "3.0.3" or "3.0.4" => OpenApiSpecVersion.OpenApi3_0, |
| | 0 | 22 | | "3.1" or "3.1.0" or "3.1.1" => OpenApiSpecVersion.OpenApi3_1, |
| | 0 | 23 | | _ => throw new ArgumentException($"Unsupported OpenAPI spec version: {version}", nameof(version)), |
| | 0 | 24 | | }; |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Converts an <see cref="OpenApiSpecVersion"/> enum value to its string representation. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="specVersion">The <see cref="OpenApiSpecVersion"/> enum value.</param> |
| | | 31 | | /// <returns>The string representation of the OpenAPI spec version.</returns> |
| | | 32 | | /// <exception cref="ArgumentOutOfRangeException">Thrown when the provided enum value is not recognized.</exception> |
| | | 33 | | public static string ToVersionString(this OpenApiSpecVersion specVersion) |
| | | 34 | | { |
| | 0 | 35 | | return specVersion switch |
| | 0 | 36 | | { |
| | 0 | 37 | | OpenApiSpecVersion.OpenApi2_0 => "2.0", |
| | 0 | 38 | | OpenApiSpecVersion.OpenApi3_0 => "3.0.1", |
| | 0 | 39 | | OpenApiSpecVersion.OpenApi3_1 => "3.1.0", |
| | 0 | 40 | | _ => throw new ArgumentOutOfRangeException(nameof(specVersion), specVersion, null) |
| | 0 | 41 | | }; |
| | | 42 | | } |
| | | 43 | | } |