Skip to content

Commit

Permalink
chore: migrates to OpenAPI.net 2.0
Browse files Browse the repository at this point in the history
feat: adds support for OAS 3.1

Signed-off-by: Vincent Biret <[email protected]>
  • Loading branch information
baywet committed Dec 23, 2024
1 parent 3b798b9 commit eb2c277
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
9 changes: 6 additions & 3 deletions src/lib/Helpers/ParsingHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Reader;
using Microsoft.OpenApi.Readers;
using System.Diagnostics;
using System.Text.Json;
Expand Down Expand Up @@ -150,12 +152,13 @@ internal static async Task<ReadResult> ParseOpenApiAsync(Uri openApiFileUri, boo

internal static async Task<ReadResult> ParseOpenApiAsync(Stream stream, Uri openApiFileUri, bool inlineExternal, CancellationToken cancellationToken)
{
ReadResult result = await new OpenApiStreamReader(new OpenApiReaderSettings
OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader());
OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yml, new OpenApiYamlReader());
ReadResult result = await OpenApiDocument.LoadAsync(stream, settings: new OpenApiReaderSettings
{
LoadExternalRefs = inlineExternal,
BaseUrl = openApiFileUri
}
).ReadAsync(stream, cancellationToken).ConfigureAwait(false);
}, cancellationToken: cancellationToken).ConfigureAwait(false);

return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/TypeExtensions/ApiManifestDocumentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static async Task<OpenAIPluginManifest> ToOpenAIPluginManifestAsync(this
var result = await ParsingHelpers.ParseOpenApiAsync(new Uri(apiDependency!.ApiDescriptionUrl), false, cancellationToken).ConfigureAwait(false);
if (string.IsNullOrWhiteSpace(openApiPath))
openApiPath = apiDependency.ApiDescriptionUrl;
return apiManifestDocument.ToOpenAIPluginManifest(result.OpenApiDocument, logoUrl, legalInfoUrl, openApiPath!);
return apiManifestDocument.ToOpenAIPluginManifest(result.Document, logoUrl, legalInfoUrl, openApiPath!);
}
}

Expand Down
8 changes: 2 additions & 6 deletions src/lib/apimanifest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.23" />
<PackageReference Include="Microsoft.OpenApi.Readers" Version="2.0.0-preview3" />
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.12.19">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<!-- The target application is the one which will resolve the correct version.
When the version range is updated to > 8.0.4 in the future, remove the GHSA suppression -->
<PackageReference Include="System.Text.Json" Version="[6.0,)" />
<NuGetAuditSuppress Include="https://github.com/advisories/GHSA-hh2w-p6rv-4g7w" />
<NuGetAuditSuppress Include="https://github.com/advisories/GHSA-8g4q-xg66-9fp4" />
<PackageReference Include="System.Text.Json" Version="[8.0.5,)" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions tests/ApiManifest.Tests/Helpers/ParsingHelpersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ public async Task ParseOpenApiAsync()
var testOpenApiFilePath = Path.Combine(".", "TestFiles", "testOpenApi.yaml");
using var stream = File.OpenRead(testOpenApiFilePath);
var results = await ParsingHelpers.ParseOpenApiAsync(stream, new Uri("https://contoso.com/openapi.yaml"), false, CancellationToken.None);
Assert.Empty(results.OpenApiDiagnostic.Errors);
Assert.NotNull(results.OpenApiDocument);
Assert.Empty(results.Diagnostic.Errors);
Assert.NotNull(results.Document);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public void ToApiManifestWithValidDocumentReturnsApiManifestDocument()
Assert.Equal("GraphAPI", apiManifest.ApiDependencies.First().Key);
Assert.Equal(apiDescriptionUrl, apiManifest.ApiDependencies.First().Value.ApiDescriptionUrl);
Assert.Equal(exampleDocument.Info.Version, apiManifest.ApiDependencies.First().Value.ApiDescriptionVersion);
Assert.Equal(exampleDocument.Servers.First().Url, apiManifest.ApiDependencies.First().Value.ApiDeploymentBaseUrl);
Assert.NotNull(exampleDocument.Servers);
Assert.NotEmpty(exampleDocument.Servers);
Assert.Equal(exampleDocument.Servers[0].Url, apiManifest.ApiDependencies.First().Value.ApiDeploymentBaseUrl);
Assert.Equal(exampleDocument.Paths.Count, apiManifest.ApiDependencies.First().Value.Requests.Count);
}

Expand All @@ -87,7 +89,9 @@ public void ToApiManifestWithValidDocumentAndApiDependencyNameReturnsApiManifest
Assert.Equal(apiDependencyName, apiManifest.ApiDependencies.First().Key);
Assert.Equal(apiDescriptionUrl, apiManifest.ApiDependencies.First().Value.ApiDescriptionUrl);
Assert.Equal(exampleDocument.Info.Version, apiManifest.ApiDependencies.First().Value.ApiDescriptionVersion);
Assert.Equal(exampleDocument.Servers.First().Url, apiManifest.ApiDependencies.First().Value.ApiDeploymentBaseUrl);
Assert.NotNull(exampleDocument.Servers);
Assert.NotEmpty(exampleDocument.Servers);
Assert.Equal(exampleDocument.Servers[0].Url, apiManifest.ApiDependencies.First().Value.ApiDeploymentBaseUrl);
Assert.Equal(exampleDocument.Paths.Count, apiManifest.ApiDependencies.First().Value.Requests.Count);
}

Expand Down

0 comments on commit eb2c277

Please sign in to comment.