Skip to content

Commit 612dc4d

Browse files
authored
[BUG][Go] Remove "null" body value when body is empty #13927 (#13934)
* only write reponse body if not nil * update go samples * golang style convention
1 parent 09ff222 commit 612dc4d

4 files changed

Lines changed: 20 additions & 4 deletions

File tree

modules/openapi-generator/src/main/resources/go-server/routers.mustache

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,11 @@ func EncodeJSONResponse(i interface{}, status *int,{{#addResponseHeaders}} heade
108108
w.WriteHeader(http.StatusOK)
109109
}
110110

111-
return json.NewEncoder(w).Encode(i)
111+
if i != nil {
112+
return json.NewEncoder(w).Encode(i)
113+
}
114+
115+
return nil
112116
}
113117

114118
// ReadFormFileToTempFile reads file data from a request form and writes it to a temporary file

samples/server/petstore/go-api-server/go/routers.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ func EncodeJSONResponse(i interface{}, status *int, headers map[string][]string,
7676
w.WriteHeader(http.StatusOK)
7777
}
7878

79-
return json.NewEncoder(w).Encode(i)
79+
if i != nil {
80+
return json.NewEncoder(w).Encode(i)
81+
}
82+
83+
return nil
8084
}
8185

8286
// ReadFormFileToTempFile reads file data from a request form and writes it to a temporary file

samples/server/petstore/go-chi-server/go/routers.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ func EncodeJSONResponse(i interface{}, status *int, headers map[string][]string,
7272
w.WriteHeader(http.StatusOK)
7373
}
7474

75-
return json.NewEncoder(w).Encode(i)
75+
if i != nil {
76+
return json.NewEncoder(w).Encode(i)
77+
}
78+
79+
return nil
7680
}
7781

7882
// ReadFormFileToTempFile reads file data from a request form and writes it to a temporary file

samples/server/petstore/go-server-required/go/routers.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ func EncodeJSONResponse(i interface{}, status *int, headers map[string][]string,
7272
w.WriteHeader(http.StatusOK)
7373
}
7474

75-
return json.NewEncoder(w).Encode(i)
75+
if i != nil {
76+
return json.NewEncoder(w).Encode(i)
77+
}
78+
79+
return nil
7680
}
7781

7882
// ReadFormFileToTempFile reads file data from a request form and writes it to a temporary file

0 commit comments

Comments
 (0)