Skip to content

Commit 31b967a

Browse files
authored
Fix tyme and date as object parameter field (#522)
1 parent d6bedad commit 31b967a

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

styleparam.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,12 @@ func stylePrimitive(style string, explode bool, paramName string, paramLocation
348348
func primitiveToString(value interface{}) (string, error) {
349349
var output string
350350

351+
// sometimes time and date used like primitive types
352+
// it can happen if paramether is object and has time or date as field
353+
if res, ok := marshalDateTimeValue(value); ok {
354+
return res, nil
355+
}
356+
351357
// Values may come in by pointer for optionals, so make sure to dereferene.
352358
v := reflect.Indirect(reflect.ValueOf(value))
353359
t := v.Type()

styleparam_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,4 +520,23 @@ func TestStyleParam(t *testing.T) {
520520
result, err = StyleParamWithLocation("simple", false, "id", ParamLocationQuery, object2)
521521
assert.NoError(t, err)
522522
assert.EqualValues(t, "firstName,Alex", result)
523+
524+
// Test handling of time and date inside objects
525+
type testObject3 struct {
526+
TimeField time.Time `json:"time_field"`
527+
DateField types.Date `json:"date_field"`
528+
}
529+
timeVal := time.Date(1996, time.March, 19, 0, 0, 0, 0, time.UTC)
530+
dateVal := types.Date{
531+
Time: timeVal,
532+
}
533+
534+
object3 := testObject3{
535+
TimeField: timeVal,
536+
DateField: dateVal,
537+
}
538+
539+
result, err = StyleParamWithLocation("simple", false, "id", ParamLocationQuery, object3)
540+
assert.NoError(t, err)
541+
assert.EqualValues(t, "date_field,1996-03-19,time_field,1996-03-19T00%3A00%3A00Z", result)
523542
}

0 commit comments

Comments
 (0)