Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow hidi to transform an OpenAPI 3.1 document #1854

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

MaggieKimani1 marked this conversation as resolved.
Show resolved Hide resolved
// 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).");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add another check like the previous one to validate that the major version is between 2 and 3?

}

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
}
}
}
Loading