-
Notifications
You must be signed in to change notification settings - Fork 301
Add HTTP Response Compression Support #3003
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
6
commits into
main
Choose a base branch
from
copilot/add-compression-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+633
−1
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5aa5ce7
Initial plan
Copilot 5e5a865
Add response compression support with configuration, CLI, and tests
Copilot 1a8cd13
Add validation for invalid compression levels
Copilot c53261c
Address PR feedback: add unknown property skip, fix docs, update logg…
Copilot 6326ead
Code cleanup: remove redundant parameters and use async CopyTo
Copilot a832d08
Fix: make DecompressGzip async to avoid blocking on async operations
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/Config/Converters/CompressionOptionsConverterFactory.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using Azure.DataApiBuilder.Config.ObjectModel; | ||
|
|
||
| namespace Azure.DataApiBuilder.Config.Converters; | ||
|
|
||
| /// <summary> | ||
| /// Defines how DAB reads and writes the compression options (JSON). | ||
| /// </summary> | ||
| internal class CompressionOptionsConverterFactory : JsonConverterFactory | ||
| { | ||
| /// <inheritdoc/> | ||
| public override bool CanConvert(Type typeToConvert) | ||
| { | ||
| return typeToConvert.IsAssignableTo(typeof(CompressionOptions)); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options) | ||
| { | ||
| return new CompressionOptionsConverter(); | ||
| } | ||
|
|
||
| private class CompressionOptionsConverter : JsonConverter<CompressionOptions> | ||
| { | ||
| /// <summary> | ||
| /// Defines how DAB reads the compression options and defines which values are | ||
| /// used to instantiate CompressionOptions. | ||
| /// </summary> | ||
| public override CompressionOptions? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| { | ||
| if (reader.TokenType == JsonTokenType.Null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| if (reader.TokenType != JsonTokenType.StartObject) | ||
| { | ||
| throw new JsonException("Expected start of object."); | ||
| } | ||
|
|
||
| CompressionLevel level = CompressionOptions.DEFAULT_LEVEL; | ||
| bool userProvidedLevel = false; | ||
|
|
||
| while (reader.Read()) | ||
| { | ||
| if (reader.TokenType == JsonTokenType.EndObject) | ||
| { | ||
| break; | ||
| } | ||
|
|
||
| if (reader.TokenType == JsonTokenType.PropertyName) | ||
| { | ||
| string? propertyName = reader.GetString(); | ||
| reader.Read(); | ||
|
|
||
| if (string.Equals(propertyName, "level", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| string? levelStr = reader.GetString(); | ||
| if (levelStr is not null) | ||
| { | ||
| if (Enum.TryParse<CompressionLevel>(levelStr, ignoreCase: true, out CompressionLevel parsedLevel)) | ||
| { | ||
| level = parsedLevel; | ||
| userProvidedLevel = true; | ||
| } | ||
| else | ||
| { | ||
| throw new JsonException($"Invalid compression level: '{levelStr}'. Valid values are: optimal, fastest, none."); | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| // Skip unknown properties and their values (including objects/arrays) | ||
| reader.Skip(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return new CompressionOptions(level) with { UserProvidedLevel = userProvidedLevel }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// When writing the CompressionOptions back to a JSON file, only write the level | ||
| /// property and value when it was provided by the user. | ||
| /// </summary> | ||
| public override void Write(Utf8JsonWriter writer, CompressionOptions value, JsonSerializerOptions options) | ||
| { | ||
| writer.WriteStartObject(); | ||
|
|
||
| if (value is not null && value.UserProvidedLevel) | ||
| { | ||
| writer.WritePropertyName("level"); | ||
| writer.WriteStringValue(value.Level.ToString().ToLowerInvariant()); | ||
| } | ||
|
|
||
| writer.WriteEndObject(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace Azure.DataApiBuilder.Config.ObjectModel; | ||
|
|
||
| /// <summary> | ||
| /// Specifies the compression level for HTTP response compression. | ||
| /// </summary> | ||
| [JsonConverter(typeof(JsonStringEnumConverter))] | ||
| public enum CompressionLevel | ||
| { | ||
| /// <summary> | ||
| /// Provides the best compression ratio at the cost of speed. | ||
| /// </summary> | ||
| Optimal, | ||
|
|
||
| /// <summary> | ||
| /// Provides the fastest compression at the cost of compression ratio. | ||
| /// </summary> | ||
| Fastest, | ||
|
|
||
| /// <summary> | ||
| /// Disables compression. | ||
| /// </summary> | ||
| None | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace Azure.DataApiBuilder.Config.ObjectModel; | ||
|
|
||
| /// <summary> | ||
| /// Configuration options for HTTP response compression. | ||
| /// </summary> | ||
| public record CompressionOptions | ||
| { | ||
| /// <summary> | ||
| /// Default compression level is Optimal. | ||
| /// </summary> | ||
| public const CompressionLevel DEFAULT_LEVEL = CompressionLevel.Optimal; | ||
|
|
||
| /// <summary> | ||
| /// The compression level to use for HTTP response compression. | ||
| /// </summary> | ||
| [JsonPropertyName("level")] | ||
| public CompressionLevel Level { get; init; } = DEFAULT_LEVEL; | ||
|
|
||
| /// <summary> | ||
| /// Flag which informs CLI and JSON serializer whether to write Level | ||
| /// property and value to the runtime config file. | ||
| /// </summary> | ||
| [JsonIgnore(Condition = JsonIgnoreCondition.Always)] | ||
| public bool UserProvidedLevel { get; init; } = false; | ||
|
|
||
| [JsonConstructor] | ||
| public CompressionOptions(CompressionLevel Level = DEFAULT_LEVEL) | ||
| { | ||
| this.Level = Level; | ||
| this.UserProvidedLevel = true; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Default parameterless constructor for cases where no compression level is specified. | ||
| /// </summary> | ||
| public CompressionOptions() | ||
| { | ||
| this.Level = DEFAULT_LEVEL; | ||
| this.UserProvidedLevel = false; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The converter doesn't properly handle unknown properties with complex values (objects or arrays). After reading a property name on line 58, if the property is not "level", the code continues the loop without skipping the property value. This works for simple values but could cause parsing issues if an unknown property contains an object or array.
Add a default case to properly skip unknown properties:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the
elseblock to properly skip unknown properties including objects and arrays usingreader.Skip(). Fixed in commit c53261c.