diff --git a/pkg/codegen/operations.go b/pkg/codegen/operations.go index 7ec702a0c..a9b908bc9 100644 --- a/pkg/codegen/operations.go +++ b/pkg/codegen/operations.go @@ -511,6 +511,15 @@ func (r ResponseContentDefinition) IsJSON() bool { return util.IsMediaTypeJson(r.ContentType) } +// __BEGIN_CYLONIX_MOD__ +// IsArray returns whether this is an array type response. +func (r ResponseContentDefinition) IsArray() bool { + return r.Schema.ArrayType != nil || + (r.Schema.OAPISchema != nil && r.Schema.OAPISchema.Type != nil && + r.Schema.OAPISchema.Type.Is("array")) +} +// __END_CYLONIX_MOD__ + type ResponseHeaderDefinition struct { Name string GoName string diff --git a/pkg/codegen/templates/strict/strict-interface.tmpl b/pkg/codegen/templates/strict/strict-interface.tmpl index 74c20d829..2406aa214 100644 --- a/pkg/codegen/templates/strict/strict-interface.tmpl +++ b/pkg/codegen/templates/strict/strict-interface.tmpl @@ -89,6 +89,16 @@ {{$hasBodyVar := or ($hasHeaders) (not $fixedStatusCode) (not .IsSupported)}} {{if .IsJSON -}} {{$hasUnionElements := ne 0 (len .Schema.UnionElements)}} + {{/* __BEGIN_CYLONIX_MOD__ */}} + {{/* Workaround for golang json encoding nil slice or array to 'null' value: */}} + {{/* https://github.com/golang/go/issues/27589 */}} + {{/* */}} + {{if .IsArray -}} + if response == nil { + response = {{$receiverTypeName}}{} + } + {{end -}} + {{/* __END_CYLONIX_MOD__ */}} return json.NewEncoder(w).Encode(response{{if $hasBodyVar}}.Body{{end}}{{if $hasUnionElements}}.union{{end}}) {{else if eq .NameTag "Text" -}} _, err := w.Write([]byte({{if $hasBodyVar}}response.Body{{else}}response{{end}}))