Skip to content

Commit 96f060b

Browse files
committed
Code cleanup
1 parent a676e37 commit 96f060b

File tree

3 files changed

+6
-113
lines changed

3 files changed

+6
-113
lines changed

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 1 addition & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -356,57 +356,7 @@ public static OpenApiDocument FixReferences(OpenApiDocument document)
356356

357357
return doc;
358358
}
359-
360-
private static async Task<Stream> GetStream(string input, ILogger logger)
361-
{
362-
var stopwatch = new Stopwatch();
363-
stopwatch.Start();
364-
365-
Stream stream;
366-
if (input.StartsWith("http"))
367-
{
368-
try
369-
{
370-
var httpClientHandler = new HttpClientHandler()
371-
{
372-
SslProtocols = System.Security.Authentication.SslProtocols.Tls12,
373-
};
374-
using var httpClient = new HttpClient(httpClientHandler)
375-
{
376-
DefaultRequestVersion = HttpVersion.Version20
377-
};
378-
stream = await httpClient.GetStreamAsync(input);
379-
}
380-
catch (HttpRequestException ex)
381-
{
382-
logger.LogError($"Could not download the file at {input}, reason{ex}");
383-
return null;
384-
}
385-
}
386-
else
387-
{
388-
try
389-
{
390-
var fileInput = new FileInfo(input);
391-
stream = fileInput.OpenRead();
392-
}
393-
catch (Exception ex) when (ex is FileNotFoundException ||
394-
ex is PathTooLongException ||
395-
ex is DirectoryNotFoundException ||
396-
ex is IOException ||
397-
ex is UnauthorizedAccessException ||
398-
ex is SecurityException ||
399-
ex is NotSupportedException)
400-
{
401-
logger.LogError($"Could not open the file at {input}, reason: {ex.Message}");
402-
return null;
403-
}
404-
}
405-
stopwatch.Stop();
406-
logger.LogTrace("{timestamp}ms: Read file {input}", stopwatch.ElapsedMilliseconds, input);
407-
return stream;
408-
}
409-
359+
410360
/// <summary>
411361
/// Takes in a file stream, parses the stream into a JsonDocument and gets a list of paths and Http methods
412362
/// </summary>
@@ -462,34 +412,6 @@ private static Dictionary<string, List<string>> EnumerateJsonDocument(JsonElemen
462412
return paths;
463413
}
464414

465-
/// <summary>
466-
/// Fixes the references in the resulting OpenApiDocument.
467-
/// </summary>
468-
/// <param name="document"> The converted OpenApiDocument.</param>
469-
/// <returns> A valid OpenApiDocument instance.</returns>
470-
// private static OpenApiDocument FixReferences2(OpenApiDocument document)
471-
// {
472-
// // This method is only needed because the output of ConvertToOpenApi isn't quite a valid OpenApiDocument instance.
473-
// // So we write it out, and read it back in again to fix it up.
474-
475-
// OpenApiDocument document;
476-
// logger.LogTrace("Parsing the OpenApi file");
477-
// var result = await new OpenApiStreamReader(new OpenApiReaderSettings
478-
// {
479-
// RuleSet = ValidationRuleSet.GetDefaultRuleSet(),
480-
// BaseUrl = new Uri(openapi)
481-
// }
482-
// ).ReadAsync(stream);
483-
484-
// document = result.OpenApiDocument;
485-
// var context = result.OpenApiDiagnostic;
486-
// var sb = new StringBuilder();
487-
// document.SerializeAsV3(new OpenApiYamlWriter(new StringWriter(sb)));
488-
// var doc = new OpenApiStringReader().Read(sb.ToString(), out _);
489-
490-
// return doc;
491-
// }
492-
493415
/// <summary>
494416
/// Reads stream from file system or makes HTTP request depending on the input string
495417
/// </summary>

src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,6 @@ public OpenApiDocument Read(Stream input, out OpenApiDiagnostic diagnostic)
4545
var reader = new StreamReader(input);
4646
var result = new OpenApiTextReaderReader(_settings).Read(reader, out diagnostic);
4747

48-
//HashAlgorithm sha = SHA512.Create();
49-
//byte[] data = sha.ComputeHash(input);
50-
//StringBuilder sb = new StringBuilder();
51-
//for (int i = 0; i < data.Length; i++)
52-
//{
53-
// sb.Append(data[i].ToString("X2"));
54-
//}
55-
56-
57-
//diagnostic.HashCode = sb.ToString();
5848
diagnostic.HashCode = OpenApiDocument.GenerateHashValue(input);
5949

6050
if (!_settings.LeaveStreamOpen)

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Collections.Generic;
66
using System.IO;
77
using System.Linq;
8-
using System.Runtime.Serialization.Formatters.Binary;
98
using System.Security.Cryptography;
109
using System.Text;
1110
using Microsoft.OpenApi.Exceptions;
@@ -66,8 +65,6 @@ public class OpenApiDocument : IOpenApiSerializable, IOpenApiExtensible
6665
/// </summary>
6766
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
6867

69-
private static readonly object locker = new();
70-
7168
/// <summary>
7269
/// Parameter-less constructor
7370
/// </summary>
@@ -381,27 +378,11 @@ public IOpenApiReferenceable ResolveReference(OpenApiReference reference)
381378
return ResolveReference(reference, false);
382379
}
383380

384-
///// <summary>
385-
///// Computes the hash code for an OpenApiDocument and its property values.
386-
///// </summary>
387-
///// <returns> The hash code.</returns>
388-
//public override int GetHashCode()
389-
//{
390-
// // select two random prime numbers e.g 1 and 3 and use them to compute hash codes
391-
// int hash = 1;
392-
// hash = hash * 3 + (Workspace == null ? 0 : Workspace.GetHashCode());
393-
// hash = hash * 3 + (Info == null ? 0 : Info.GetHashCode());
394-
// hash = hash * 3 + (Servers == null ? 0 : Servers.GetHashCode());
395-
// hash = hash * 3 + (Paths == null ? 0 : Paths.GetHashCode());
396-
// hash = hash * 3 + (Components == null ? 0 : Components.GetHashCode());
397-
// hash = hash * 3 + (SecurityRequirements == null ? 0 : SecurityRequirements.GetHashCode());
398-
// hash = hash * 3 + (Tags == null ? 0 : Tags.GetHashCode());
399-
// hash = hash * 3 + (ExternalDocs == null ? 0 : ExternalDocs.GetHashCode());
400-
// hash = hash * 3 + (Extensions == null ? 0 : Extensions.GetHashCode());
401-
402-
// return hash;
403-
//}
404-
381+
/// <summary>
382+
/// Uses the stream input to generate the hash value of an OpenApi document
383+
/// </summary>
384+
/// <param name="input">Stream containing OpenAPI description to hash.</param>
385+
/// <returns>The hash value.</returns>
405386
public static string GenerateHashValue(Stream input)
406387
{
407388
HashAlgorithm sha = SHA512.Create();

0 commit comments

Comments
 (0)