Skip to content

Commit c4f3b7f

Browse files
committedApr 1, 2025·
fix: adding middleware with options errors with "unsupported option type"
1 parent 592eea4 commit c4f3b7f

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed
 

‎compression_handler.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ var compressKey = abstractions.RequestOptionKey{Key: "CompressionHandler"}
3232

3333
// NewCompressionHandler creates an instance of a compression middleware
3434
func NewCompressionHandler() *CompressionHandler {
35-
options := NewCompressionOptions(true)
36-
return NewCompressionHandlerWithOptions(options)
35+
options := NewCompressionOptionsReference(true)
36+
return NewCompressionHandlerWithOptions(*options)
3737
}
3838

3939
// NewCompressionHandlerWithOptions creates an instance of the compression middleware with
@@ -43,10 +43,22 @@ func NewCompressionHandlerWithOptions(option CompressionOptions) *CompressionHan
4343
}
4444

4545
// NewCompressionOptions creates a configuration object for the CompressionHandler
46+
//
47+
// Deprecated: This function is deprecated, and superseded by NewCompressionOptionsReference,
48+
// which returns a pointer instead of plain value.
4649
func NewCompressionOptions(enableCompression bool) CompressionOptions {
4750
return CompressionOptions{enableCompression: enableCompression}
4851
}
4952

53+
// NewCompressionOptionsReference creates a configuration object for the CompressionHandler.
54+
//
55+
// This function supersedes the NewCompressionOptions function and returns a pointer,
56+
// which is expected by GetDefaultMiddlewaresWithOptions.
57+
func NewCompressionOptionsReference(enableCompression bool) *CompressionOptions {
58+
options := CompressionOptions{enableCompression: enableCompression}
59+
return &options
60+
}
61+
5062
// GetKey returns CompressionOptions unique name in context object
5163
func (o CompressionOptions) GetKey() abstractions.RequestOptionKey {
5264
return compressKey

‎kiota_client_factory.go

+3
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ func GetDefaultMiddlewaresWithOptions(requestOptions ...abs.RequestOption) ([]Mi
9898
middlewareMap[redirectKeyValue] = NewRedirectHandlerWithOptions(*v)
9999
case *CompressionOptions:
100100
middlewareMap[compressKey] = NewCompressionHandlerWithOptions(*v)
101+
case CompressionOptions:
102+
println("deprecation notice: function GetDefaultMiddlewaresWithOptions expects a pointer to CompressionOptions. Use the NewCompressionOptionsReference convenience function.")
103+
middlewareMap[compressKey] = NewCompressionHandlerWithOptions(v)
101104
case *ParametersNameDecodingOptions:
102105
middlewareMap[parametersNameDecodingKeyValue] = NewParametersNameDecodingHandlerWithOptions(*v)
103106
case *UserAgentHandlerOptions:

‎kiota_client_factory_test.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestGetDefaultMiddleWareWithMultipleOptions(t *testing.T) {
2020
return true
2121
},
2222
}
23-
compressionOptions := NewCompressionOptions(false)
23+
compressionOptions := NewCompressionOptionsReference(false)
2424
parametersNameDecodingOptions := ParametersNameDecodingOptions{
2525
Enable: true,
2626
ParametersToDecode: []byte{'-', '.', '~', '$'},
@@ -36,7 +36,7 @@ func TestGetDefaultMiddleWareWithMultipleOptions(t *testing.T) {
3636
}
3737
options, err := GetDefaultMiddlewaresWithOptions(&retryOptions,
3838
&redirectHandlerOptions,
39-
&compressionOptions,
39+
compressionOptions,
4040
&parametersNameDecodingOptions,
4141
&userAgentHandlerOptions,
4242
&headersInspectionOptions,
@@ -67,19 +67,28 @@ func TestGetDefaultMiddleWareWithInvalidOption(t *testing.T) {
6767
}
6868

6969
func TestGetDefaultMiddleWareWithOptions(t *testing.T) {
70+
compression := NewCompressionOptionsReference(false)
71+
options, err := GetDefaultMiddlewaresWithOptions(compression)
72+
verifyMiddlewareWithDisabledCompression(t, options, err)
73+
}
74+
75+
func TestGetDefaultMiddleWareWithOptionsDeprecated(t *testing.T) {
7076
compression := NewCompressionOptions(false)
71-
options, err := GetDefaultMiddlewaresWithOptions(&compression)
77+
options, err := GetDefaultMiddlewaresWithOptions(compression)
78+
verifyMiddlewareWithDisabledCompression(t, options, err)
79+
}
80+
81+
func verifyMiddlewareWithDisabledCompression(t *testing.T, options []Middleware, err error) {
7282
if err != nil {
7383
t.Errorf(err.Error())
7484
}
7585
if len(options) != 6 {
7686
t.Errorf("expected 6 middleware, got %v", len(options))
7787
}
78-
7988
for _, element := range options {
8089
switch v := element.(type) {
8190
case *CompressionHandler:
82-
assert.Equal(t, v.options.ShouldCompress(), compression.ShouldCompress())
91+
assert.Equal(t, v.options.ShouldCompress(), false)
8392
}
8493
}
8594
}

0 commit comments

Comments
 (0)
Please sign in to comment.