Skip to content
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

Fix tyme and date as object parameter field #522

Merged
merged 1 commit into from Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}