Skip to content

Commit 7261ad9

Browse files
Merge pull request #1854 from microsoft/mk/allow-hidi-to-emit-31
Allow hidi to transform an OpenAPI 3.1 document
2 parents ed5c771 + 3ab3071 commit 7261ad9

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System;
@@ -79,7 +79,7 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l
7979

8080
// Default to yaml and OpenApiVersion 3 during csdl to OpenApi conversion
8181
var openApiFormat = options.OpenApiFormat ?? (!string.IsNullOrEmpty(options.OpenApi) ? GetOpenApiFormat(options.OpenApi, logger) : OpenApiFormat.Yaml);
82-
var openApiVersion = options.Version != null ? TryParseOpenApiSpecVersion(options.Version) : OpenApiSpecVersion.OpenApi3_0;
82+
var openApiVersion = options.Version != null ? TryParseOpenApiSpecVersion(options.Version) : OpenApiSpecVersion.OpenApi3_1;
8383

8484
// If ApiManifest is provided, set the referenced OpenAPI document
8585
var apiDependency = await FindApiDependency(options.FilterOptions.FilterByApiManifest, logger, cancellationToken).ConfigureAwait(false);
@@ -768,7 +768,7 @@ internal static async Task PluginManifest(HidiOptions options, ILogger logger, C
768768
// Write OpenAPI to Output folder
769769
options.Output = new(Path.Combine(options.OutputFolder, "openapi.json"));
770770
options.TerseOutput = true;
771-
WriteOpenApi(options, OpenApiFormat.Json, OpenApiSpecVersion.OpenApi3_0, document, logger);
771+
WriteOpenApi(options, OpenApiFormat.Json, OpenApiSpecVersion.OpenApi3_1, document, logger);
772772

773773
// Create OpenAIPluginManifest from ApiDependency and OpenAPI document
774774
var manifest = new OpenAIPluginManifest

src/Microsoft.OpenApi.Hidi/OpenApiSpecVersionHelper.cs

+20-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT license.
33

44
using System;
5-
using System.Linq;
65

76
namespace Microsoft.OpenApi.Hidi
87
{
@@ -14,17 +13,30 @@ public static OpenApiSpecVersion TryParseOpenApiSpecVersion(string value)
1413
{
1514
throw new InvalidOperationException("Please provide a version");
1615
}
17-
var res = value.Split('.', StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
16+
// Split the version string by the dot
17+
var versionSegments = value.Split('.', StringSplitOptions.RemoveEmptyEntries);
1818

19-
if (int.TryParse(res, out var result))
19+
if (!int.TryParse(versionSegments[0], out var majorVersion)
20+
|| !int.TryParse(versionSegments[1], out var minorVersion))
2021
{
21-
if (result is >= 2 and < 3)
22-
{
23-
return OpenApiSpecVersion.OpenApi2_0;
24-
}
22+
throw new InvalidOperationException("Invalid version format. Please provide a valid OpenAPI version (e.g., 2.0, 3.0, 3.1).");
2523
}
2624

27-
return OpenApiSpecVersion.OpenApi3_0; // default
25+
// Check for specific version matches
26+
if (majorVersion == 2)
27+
{
28+
return OpenApiSpecVersion.OpenApi2_0;
29+
}
30+
else if (majorVersion == 3 && minorVersion == 0)
31+
{
32+
return OpenApiSpecVersion.OpenApi3_0;
33+
}
34+
else if (majorVersion == 3 && minorVersion == 1)
35+
{
36+
return OpenApiSpecVersion.OpenApi3_1;
37+
}
38+
39+
return OpenApiSpecVersion.OpenApi3_1; // default
2840
}
2941
}
3042
}

0 commit comments

Comments
 (0)