diff --git a/src/Microsoft.OpenApi/Models/OpenApiComponents.cs b/src/Microsoft.OpenApi/Models/OpenApiComponents.cs index 0db85ed77..5b43b5187 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiComponents.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiComponents.cs @@ -318,7 +318,7 @@ private void RenderComponents(IOpenApiWriter writer, Action schemas)) + if (loops.TryGetValue(typeof(OpenApiSchema), out var schemas)) { writer.WriteOptionalMap(OpenApiConstants.Schemas, Schemas, callback); } diff --git a/src/Microsoft.OpenApi/Models/OpenApiDocument.cs b/src/Microsoft.OpenApi/Models/OpenApiDocument.cs index 27296f47b..cf7e53cae 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiDocument.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiDocument.cs @@ -238,7 +238,7 @@ public void SerializeAsV2(IOpenApiWriter writer) { var loops = writer.GetSettings().LoopDetector.Loops; - if (loops.TryGetValue(typeof(OpenApiSchema), out List schemas)) + if (loops.TryGetValue(typeof(OpenApiSchema), out var schemas)) { var openApiSchemas = schemas.Cast().Distinct().ToList() .ToDictionary(k => k.Reference.Id); @@ -409,14 +409,15 @@ private static void WriteHostInfoV2(IOpenApiWriter writer, IList? return url; }) .Where( - u => Uri.Compare( + u => u is not null && + Uri.Compare( u, firstServerUrl, UriComponents.Host | UriComponents.Port | UriComponents.Path, UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase) == 0 && u.IsAbsoluteUri) - .Select(u => u.Scheme) + .Select(u => u!.Scheme) .Distinct() .ToList(); @@ -464,10 +465,12 @@ public async Task GetHashCodeAsync(CancellationToken cancellationToken = SerializeAsV3(openApiJsonWriter); await openApiJsonWriter.FlushAsync(cancellationToken).ConfigureAwait(false); +#if NET5_0_OR_GREATER + await cryptoStream.FlushFinalBlockAsync(cancellationToken).ConfigureAwait(false); +#else cryptoStream.FlushFinalBlock(); - var hash = sha.Hash; - - return ConvertByteArrayToString(hash); +#endif + return ConvertByteArrayToString(sha.Hash ?? []); } private static string ConvertByteArrayToString(byte[] hash) diff --git a/src/Microsoft.OpenApi/Models/OpenApiOperation.cs b/src/Microsoft.OpenApi/Models/OpenApiOperation.cs index 6182eda2b..f5fe32274 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiOperation.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiOperation.cs @@ -283,11 +283,11 @@ public void SerializeAsV2(IOpenApiWriter writer) { var produces = Responses .Where(static r => r.Value.Content != null) - .SelectMany(static r => r.Value.Content?.Keys) + .SelectMany(static r => r.Value.Content?.Keys ?? []) .Concat( Responses .Where(static r => r.Value.Reference is {HostDocument: not null}) - .SelectMany(static r => r.Value.Content?.Keys)) + .SelectMany(static r => r.Value.Content?.Keys ?? [])) .Distinct(StringComparer.OrdinalIgnoreCase) .ToArray(); diff --git a/src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs b/src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs index 31b939548..fea86f899 100644 --- a/src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs +++ b/src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs @@ -129,7 +129,11 @@ public static async Task LoadAsync(Stream input, string format = nul var result = await InternalLoadAsync(preparedStream, format, settings, cancellationToken).ConfigureAwait(false); if (!settings.LeaveStreamOpen) { +#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP || NET5_0_OR_GREATER + await input.DisposeAsync().ConfigureAwait(false); +#else input.Dispose(); +#endif } return result; }