-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathOpenApiDocumentFactory.cs
More file actions
89 lines (83 loc) · 3.52 KB
/
OpenApiDocumentFactory.cs
File metadata and controls
89 lines (83 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System.Net;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers;
namespace HttpGenerator.Core;
/// <summary>
/// Creates an <see cref="OpenApiDocument"/> from a specified path or URL.
/// </summary>
public static class OpenApiDocumentFactory
{
/// <summary>
/// Creates a new instance of the <see cref="OpenApiDocument"/> class asynchronously.
/// </summary>
/// <returns>A new instance of the <see cref="OpenApiDocument"/> class.</returns>
public static async Task<OpenApiDocument> CreateAsync(string openApiPath)
{
if (IsHttp(openApiPath))
{
var content = await GetHttpContent(openApiPath);
return await ParseOpenApiContent(content);
}
else
{
var content = File.ReadAllText(openApiPath);
return await ParseOpenApiContent(content);
}
}
private static async Task<OpenApiDocument> ParseOpenApiContent(string content)
{
// Try to parse with Microsoft.OpenApi first
try
{
using var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(content));
var reader = new OpenApiStreamReader();
var result = await reader.ReadAsync(stream, CancellationToken.None);
return result.OpenApiDocument;
}
catch (Exception ex) when (ex.Message.Contains("3.1.0") || ex.Message.Contains("not supported"))
{
// If OpenAPI 3.1 is detected, try to downgrade to 3.0 for parsing
var downgradedContent = DowngradeOpenApi31To30(content);
using var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(downgradedContent));
var reader = new OpenApiStreamReader();
var result = await reader.ReadAsync(stream, CancellationToken.None);
return result.OpenApiDocument;
}
}
private static string DowngradeOpenApi31To30(string content)
{
// Simple downgrade strategy: replace 3.1.0 with 3.0.3 and remove unsupported 3.1 features
return content
.Replace("\"openapi\": \"3.1.0\"", "\"openapi\": \"3.0.3\"")
.Replace("openapi: 3.1.0", "openapi: 3.0.3")
.Replace("openapi: \"3.1.0\"", "openapi: \"3.0.3\"")
// Remove webhooks section which is 3.1 specific
.Replace("\"webhooks\":", "\"x-webhooks\":")
.Replace("webhooks:", "x-webhooks:");
}
/// <summary>
/// Gets the content of the URI as a string and decompresses it if necessary.
/// </summary>
/// <returns>The content of the HTTP request.</returns>
private static async Task<string> GetHttpContent(string openApiPath)
{
var httpMessageHandler = new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true
};
using var http = new HttpClient(httpMessageHandler, disposeHandler: true);
var content = await http.GetStringAsync(openApiPath);
return content;
}
/// <summary>
/// Determines whether the specified path is an HTTP URL.
/// </summary>
/// <param name="path">The path to check.</param>
/// <returns>True if the path is an HTTP URL, otherwise false.</returns>
private static bool IsHttp(string path)
{
return path.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
path.StartsWith("https://", StringComparison.OrdinalIgnoreCase);
}
}