Skip to content

Commit

Permalink
Merge pull request #1854 from microsoft/mk/allow-hidi-to-emit-31
Browse files Browse the repository at this point in the history
Allow hidi to transform an OpenAPI 3.1 document
  • Loading branch information
MaggieKimani1 authored Oct 3, 2024
2 parents ed5c771 + 3ab3071 commit 7261ad9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/Microsoft.OpenApi.Hidi/OpenApiService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
Expand Down Expand Up @@ -79,7 +79,7 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l

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

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

// Create OpenAIPluginManifest from ApiDependency and OpenAPI document
var manifest = new OpenAIPluginManifest
Expand Down
28 changes: 20 additions & 8 deletions src/Microsoft.OpenApi.Hidi/OpenApiSpecVersionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license.

using System;
using System.Linq;

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

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

return OpenApiSpecVersion.OpenApi3_0; // default
// 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;
}

return OpenApiSpecVersion.OpenApi3_1; // default
}
}
}

0 comments on commit 7261ad9

Please sign in to comment.