Skip to content

Commit 5fbb568

Browse files
committed
Cast string to OpenApiSpecVersion enum value
1 parent 5034952 commit 5fbb568

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using Microsoft.OpenApi.Services;
2323
using Microsoft.OpenApi.Validations;
2424
using Microsoft.OpenApi.Writers;
25+
using static Microsoft.OpenApi.Hidi.OpenApiSpecVersionExtension;
2526

2627
namespace Microsoft.OpenApi.Hidi
2728
{
@@ -31,7 +32,7 @@ public static async Task ProcessOpenApiDocument(
3132
string openapi,
3233
string csdl,
3334
FileInfo output,
34-
OpenApiSpecVersion? version,
35+
string? version,
3536
OpenApiFormat? format,
3637
LogLevel loglevel,
3738
bool inline,
@@ -83,13 +84,14 @@ string filterbycollection
8384
Stream stream;
8485
OpenApiDocument document;
8586
OpenApiFormat openApiFormat;
87+
OpenApiSpecVersion? openApiVersion = null;
8688
var stopwatch = new Stopwatch();
8789

8890
if (!string.IsNullOrEmpty(csdl))
8991
{
9092
// Default to yaml and OpenApiVersion 3 during csdl to OpenApi conversion
9193
openApiFormat = format ?? GetOpenApiFormat(csdl, logger);
92-
version ??= OpenApiSpecVersion.OpenApi3_0;
94+
openApiVersion = version.TryParseOpenApiSpecVersion();
9395

9496
stream = await GetStream(csdl, logger);
9597
document = await ConvertCsdlToOpenApi(stream);
@@ -128,7 +130,7 @@ string filterbycollection
128130
}
129131

130132
openApiFormat = format ?? GetOpenApiFormat(openapi, logger);
131-
version ??= result.OpenApiDiagnostic.SpecificationVersion;
133+
openApiVersion ??= result.OpenApiDiagnostic.SpecificationVersion;
132134
}
133135

134136
Func<string, OperationType?, OpenApiOperation, bool> predicate;
@@ -185,14 +187,14 @@ string filterbycollection
185187
logger.LogTrace("Serializing to OpenApi document using the provided spec version and writer");
186188

187189
stopwatch.Start();
188-
document.Serialize(writer, (OpenApiSpecVersion)version);
190+
document.Serialize(writer, (OpenApiSpecVersion)openApiVersion);
189191
stopwatch.Stop();
190192

191193
logger.LogTrace($"Finished serializing in {stopwatch.ElapsedMilliseconds}ms");
192194

193195
textWriter.Flush();
194196
}
195-
197+
196198
/// <summary>
197199
/// Converts CSDL to OpenAPI
198200
/// </summary>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using System.Linq;
6+
7+
namespace Microsoft.OpenApi.Hidi
8+
{
9+
public static class OpenApiSpecVersionExtension
10+
{
11+
public static OpenApiSpecVersion TryParseOpenApiSpecVersion(this string value)
12+
{
13+
if (string.IsNullOrEmpty(value))
14+
{
15+
throw new InvalidOperationException("Please provide a version");
16+
}
17+
var res = value.Split('.', StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
18+
19+
if (int.TryParse(res, out int result))
20+
{
21+
22+
if (result >= 2 || result <= 3)
23+
{
24+
return (OpenApiSpecVersion)result;
25+
}
26+
}
27+
28+
return OpenApiSpecVersion.OpenApi3_0; // default
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)