Skip to content

Commit

Permalink
Fix tyme and date as object parameter field (#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
vagruchi committed Apr 19, 2022
1 parent 0c49c75 commit dc6ec1b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/runtime/styleparam.go
Expand Up @@ -348,6 +348,12 @@ func stylePrimitive(style string, explode bool, paramName string, paramLocation
func primitiveToString(value interface{}) (string, error) {
var output string

// sometimes time and date used like primitive types
// it can happen if paramether is object and has time or date as field
if res, ok := marshalDateTimeValue(value); ok {
return res, nil
}

// Values may come in by pointer for optionals, so make sure to dereferene.
v := reflect.Indirect(reflect.ValueOf(value))
t := v.Type()
Expand Down
19 changes: 19 additions & 0 deletions pkg/runtime/styleparam_test.go
Expand Up @@ -520,4 +520,23 @@ func TestStyleParam(t *testing.T) {
result, err = StyleParamWithLocation("simple", false, "id", ParamLocationQuery, object2)
assert.NoError(t, err)
assert.EqualValues(t, "firstName,Alex", result)

// Test handling of time and date inside objects
type testObject3 struct {
TimeField time.Time `json:"time_field"`
DateField types.Date `json:"date_field"`
}
timeVal := time.Date(1996, time.March, 19, 0, 0, 0, 0, time.UTC)
dateVal := types.Date{
Time: timeVal,
}

object3 := testObject3{
TimeField: timeVal,
DateField: dateVal,
}

result, err = StyleParamWithLocation("simple", false, "id", ParamLocationQuery, object3)
assert.NoError(t, err)
assert.EqualValues(t, "date_field,1996-03-19,time_field,1996-03-19T00%3A00%3A00Z", result)
}

0 comments on commit dc6ec1b

Please sign in to comment.