Skip to content

Commit e0477ce

Browse files
andiikaaAndrélerenn
authored
fix: fix MarshallJSON for maps with complex types (#277)
Co-authored-by: André <andre.kuehnert@wandelbots.com> Co-authored-by: Louis FRADIN <louis.fradin@gmail.com>
1 parent 56c9dcb commit e0477ce

File tree

5 files changed

+776
-5
lines changed

5 files changed

+776
-5
lines changed

pkg/codegen/generators/v3/templates/marshaling/additional_properties.tmpl

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,22 @@ func (t {{ .Name }}) MarshalJSON() ([]byte, error) {
1313
// Remove the end of the json (i.e. '}')
1414
b = b[:len(b)-1]
1515

16-
// Add additional properties
17-
for k, v := range t.AdditionalProperties {
18-
b = append(b,[]byte(fmt.Sprintf(",%q:%q", k, v))...)
19-
}
16+
// When there are no properties, we cant start with a separator
17+
needSeparator := len(b) > 1
18+
19+
// Add additional properties
20+
for k, v := range t.AdditionalProperties {
21+
if needSeparator {
22+
b = append(b, ',')
23+
}
24+
needSeparator = true
25+
26+
vBytes, err := json.Marshal(v)
27+
if err != nil {
28+
return nil, err
29+
}
30+
b = append(b, fmt.Sprintf("%q:%s", k, vBytes)...)
31+
}
2032

2133
// Close JSON and return
2234
return append(b, []byte("}")...) , nil

test/v3/issues/164/asyncapi.gen.go

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)