Skip to content

Commit

Permalink
add workaround for golang/go#27589
Browse files Browse the repository at this point in the history
  • Loading branch information
randycylonix committed Sep 4, 2024
1 parent ab585f7 commit 81968c5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/codegen/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions pkg/codegen/templates/strict/strict-interface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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}}))
Expand Down

0 comments on commit 81968c5

Please sign in to comment.