Skip to content

Commit cabda49

Browse files
chore(client): fix multipart serialisation of Default() fields
1 parent dd77af4 commit cabda49

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

internal/apiform/encoder.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,14 @@ func (e *encoder) newStructTypeEncoder(t reflect.Type) encoderFunc {
265265
}
266266
return typeEncoderFn(key, value, writer)
267267
}
268+
} else if ptag.defaultValue != nil {
269+
typeEncoderFn := e.typeEncoder(field.Type)
270+
encoderFn = func(key string, value reflect.Value, writer *multipart.Writer) error {
271+
if value.IsZero() {
272+
return typeEncoderFn(key, reflect.ValueOf(ptag.defaultValue), writer)
273+
}
274+
return typeEncoderFn(key, value, writer)
275+
}
268276
} else {
269277
encoderFn = e.typeEncoder(field.Type)
270278
}

internal/apiform/form_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ type StructUnion struct {
123123
param.APIUnion
124124
}
125125

126+
type ConstantStruct struct {
127+
Anchor string `form:"anchor" default:"created_at"`
128+
Seconds int `form:"seconds"`
129+
}
130+
126131
type MultipartMarshalerParent struct {
127132
Middle MultipartMarshalerMiddleNext `form:"middle"`
128133
}
@@ -554,6 +559,37 @@ Content-Disposition: form-data; name="union"
554559
Union: UnionTime(time.Date(2010, 05, 23, 0, 0, 0, 0, time.UTC)),
555560
},
556561
},
562+
"constant_zero_value": {
563+
`--xxx
564+
Content-Disposition: form-data; name="anchor"
565+
566+
created_at
567+
--xxx
568+
Content-Disposition: form-data; name="seconds"
569+
570+
3600
571+
--xxx--
572+
`,
573+
ConstantStruct{
574+
Seconds: 3600,
575+
},
576+
},
577+
"constant_explicit_value": {
578+
`--xxx
579+
Content-Disposition: form-data; name="anchor"
580+
581+
created_at_override
582+
--xxx
583+
Content-Disposition: form-data; name="seconds"
584+
585+
3600
586+
--xxx--
587+
`,
588+
ConstantStruct{
589+
Anchor: "created_at_override",
590+
Seconds: 3600,
591+
},
592+
},
557593
"deeply-nested-struct,brackets": {
558594
`--xxx
559595
Content-Disposition: form-data; name="middle[middleNext][child]"

0 commit comments

Comments
 (0)