Skip to content

Commit

Permalink
- adds support for enum path parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
baywet committed Nov 20, 2023
1 parent 0348011 commit 5ae9659
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
47 changes: 33 additions & 14 deletions request_information.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ type RequestInformation struct {
Headers *RequestHeaders
// The Query Parameters of the request.
// Deprecated: use QueryParametersAny instead
QueryParameters map[string]string
QueryParameters map[string]string
// The Query Parameters of the request.
QueryParametersAny map[string]any
// The Request Body.
Content []byte
// The path parameters to use for the URL template when generating the URI.
// Deprecated: use PathParametersAny instead
PathParameters map[string]string
// The path parameters to use for the URL template when generating the URI.
PathParametersAny map[string]any
// The Url template for the current request.
UrlTemplate string
options map[string]RequestOption
Expand All @@ -49,6 +53,7 @@ func NewRequestInformation() *RequestInformation {
QueryParametersAny: make(map[string]any),
options: make(map[string]RequestOption),
PathParameters: make(map[string]string),
PathParametersAny: make(map[string]any),
}
}

Expand Down Expand Up @@ -103,6 +108,9 @@ func (request *RequestInformation) GetUri() (*u.URL, error) {
for key, value := range request.PathParameters {
substitutions[key] = value
}
for key, value := range request.PathParametersAny {
substitutions[key] = request.normalizeEnumParameters(reflect.ValueOf(value), value, false)
}
for key, value := range request.QueryParameters {
substitutions[key] = value
}
Expand Down Expand Up @@ -513,21 +521,32 @@ func (request *RequestInformation) AddQueryParameters(source any) {
if arr, ok := value.([]any); ok && len(arr) > 0 {
request.QueryParametersAny[fieldName] = arr
}
if valueOfValue.Kind() == reflect.Slice && valueOfValue.Len() > 0 {
//type assertions to "enums" don't work if you don't know the enum type in advance, we need to use reflection
enumArr := valueOfValue.Slice(0, valueOfValue.Len())
strRepresentations := make([]string, valueOfValue.Len())
if _, ok := enumArr.Index(0).Interface().(kiotaEnum); ok {
// testing the first value is an enum to avoid iterating over the whole array if it's not
for i := range strRepresentations {
strRepresentations[i] = enumArr.Index(i).Interface().(kiotaEnum).String()
}
request.QueryParametersAny[fieldName] = strRepresentations
}
normalizedEnumValue := request.normalizeEnumParameters(valueOfValue, value, true)
if normalizedEnumValue != nil {
request.QueryParametersAny[fieldName] = normalizedEnumValue
}
if enum, ok := value.(kiotaEnum); ok {
request.QueryParameters[fieldName] = enum.String()
}
}
func (request *RequestInformation) normalizeEnumParameters(valueOfValue reflect.Value, value any, returnNilIfNotEnum bool) any {
if valueOfValue.Kind() == reflect.Slice && valueOfValue.Len() > 0 {
//type assertions to "enums" don't work if you don't know the enum type in advance, we need to use reflection
enumArr := valueOfValue.Slice(0, valueOfValue.Len())
strRepresentations := make([]string, valueOfValue.Len())
if _, ok := enumArr.Index(0).Interface().(kiotaEnum); ok {
// testing the first value is an enum to avoid iterating over the whole array if it's not
for i := range strRepresentations {
strRepresentations[i] = enumArr.Index(i).Interface().(kiotaEnum).String()
}
return strRepresentations
}
} else if enum, ok := value.(kiotaEnum); ok {
return enum.String()
}

if returnNilIfNotEnum {
return nil
} else {
return value
}
}

Expand Down
28 changes: 28 additions & 0 deletions request_information_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,34 @@ func TestItSetsEnumValuesInQueryParameters(t *testing.T) {
assert.Equal(t, "http://localhost/users?statuses=active,suspended", resultUri.String())
}

func TestItSetsEnumValueInPathParameters(t *testing.T) {
requestInformation := NewRequestInformation()
requestInformation.UrlTemplate = "{+baseurl}/{status}"

status := internal.ACTIVE
requestInformation.PathParameters["baseurl"] = "http://localhost"
requestInformation.PathParametersAny["status"] = &status

resultUri, err := requestInformation.GetUri()
assert.Nil(t, err)
assert.Equal(t, "http://localhost/active", resultUri.String())
}

func TestItSetsEnumValuesInPathParameters(t *testing.T) {
requestInformation := NewRequestInformation()
requestInformation.UrlTemplate = "{+baseurl}/{statuses}"

statuses := make([]internal.PersonStatus, 2)
statuses[0] = internal.ACTIVE
statuses[1] = internal.SUSPENDED
requestInformation.PathParameters["baseurl"] = "http://localhost"
requestInformation.PathParametersAny["statuses"] = statuses

resultUri, err := requestInformation.GetUri()
assert.Nil(t, err)
assert.Equal(t, "http://localhost/active,suspended", resultUri.String())
}

func TestItSetsExplodedQueryParameters(t *testing.T) {
value := true
requestInformation := NewRequestInformation()
Expand Down

0 comments on commit 5ae9659

Please sign in to comment.