Skip to content

Commit

Permalink
enable decompression methods during http get operations (#1621)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmanning authored Sep 26, 2023
1 parent dbc1d05 commit cbc0f24
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/NJsonSchema.Yaml.Tests/References/YamlReferencesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task When_yaml_OpenAPI_spec_has_external_schema_refs_they_are_resol
OpenApiResponse Unauthorized = responses["401"].ActualResponse;

////Assert

// Header schema loaded correctly from headers.yaml
Assert.True(OKheaders.ContainsKey(header));
Assert.NotNull(OKheaders[header]);
Expand All @@ -64,6 +64,20 @@ public async Task When_yaml_OpenAPI_spec_has_external_schema_refs_they_are_resol
Assert.NotNull(Unauthorized.Schema);
}

[Theory]
[InlineData("https://www.zuora.com/developer/yaml/swagger.yaml", "https://rest.zuora.com/")]
public async Task When_yaml_OpenAPI_spec_is__served_with_gzip_compression__it_works(string inputYamlUrl, string expectedBaseUrl)
{
//// Act
OpenApiDocument doc = await OpenApiYamlDocument.FromUrlAsync(inputYamlUrl);

////Assert
Assert.NotNull(doc);
Assert.NotNull(doc.Paths);
Assert.NotEmpty(doc.Paths);
Assert.Equal(expectedBaseUrl, doc.BaseUrl);
}

[Theory]
[InlineData("/References/YamlReferencesTest/subdir_spec/yaml_spec_with_yaml_schema_with_relative_subdir_refs.yaml")]
public async Task When_yaml_OpenAPI_spec_has_relative_external_schema_refs_in_subdirs__they_are_resolved(string relativePath)
Expand Down
27 changes: 27 additions & 0 deletions src/NJsonSchema/Infrastructure/DynamicApis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static class DynamicApis
private static readonly Type FileType;
private static readonly Type DirectoryType;
private static readonly Type PathType;
private static readonly Type DecompressionMethodsType;
private static readonly Type HttpClientHandlerType;
private static readonly Type HttpClientType;

Expand All @@ -34,6 +35,10 @@ static DynamicApis()
"System.Xml.XPath.Extensions, System.Xml.XPath.XDocument",
"System.Xml.XPath.Extensions, System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

DecompressionMethodsType = TryLoadType(
"System.Net.DecompressionMethods, System.Net.Primitives",
"System.Net.DecompressionMethods, System.Net.Primitives, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");

HttpClientHandlerType = TryLoadType(
"System.Net.Http.HttpClientHandler, System.Net.Http",
"System.Net.Http.HttpClientHandler, System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
Expand Down Expand Up @@ -79,12 +84,34 @@ public static async Task<string> HttpGetAsync(string url, CancellationToken canc
{
handler.UseDefaultCredentials = true;

// enable all decompression methods
var calculatedAllValue = GenerateAllDecompressionMethodsEnumValue();
var allDecompressionMethodsValue = Enum.ToObject(DecompressionMethodsType, calculatedAllValue);
handler.AutomaticDecompression = (dynamic)allDecompressionMethodsValue;

var response = await client.GetAsync(url, cancellationToken).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
}

// see https://learn.microsoft.com/en-us/dotnet/api/system.net.decompressionmethods?view=net-7.0
private static int GenerateAllDecompressionMethodsEnumValue()
{
// calculate the set of all possible values (the set will depend on which version of the enum was loaded)
// NOTE: we can't use All or -1 since those weren't in the 4.0 version of the enum, but we
// still want to enable additional decompression methods like Brotli (and potentially
// additional ones in the future) if the loaded httpclient supports it.
// while the existing values would allow doing a Sum, we still bitwise or to be defensive about
// potential additions in the future of values like "GZipOrDeflate"
var calculatedAllValue = Enum.GetValues(DecompressionMethodsType)
.Cast<int>()
.Where(val => val > 0) // filter to only positive so we're not including All or None
.Aggregate(0, (accumulated, newValue) => accumulated | newValue);

return calculatedAllValue;
}

/// <summary>Gets the current working directory.</summary>
/// <returns>The directory path.</returns>
/// <exception cref="NotSupportedException">The System.IO.Directory API is not available on this platform.</exception>
Expand Down

0 comments on commit cbc0f24

Please sign in to comment.