Repository: goadesign/goa
Pull request #3290 (2023-05-19 12:13:10)

Fix OpenAPI responses when it is Empty
Fixes: https://github.com/goadesign/goa/issues/3289

raphael: This is great, thank you!

Patch:
diff --git a/http/codegen/openapi/v3/response.go b/http/codegen/openapi/v3/response.go
index 7ae0c24e..dad77bf9 100644
--- a/http/codegen/openapi/v3/response.go
+++ b/http/codegen/openapi/v3/response.go
@@ -4,6 +4,7 @@ import (
 	"fmt"
 	"net/http"
 
+	"goa.design/goa/v3/eval"
 	"goa.design/goa/v3/expr"
 	"goa.design/goa/v3/http/codegen/openapi"
 )
@@ -45,7 +46,8 @@ func responseFromExpr(r *expr.HTTPResponseExpr, bodies map[int][]*openapi.Schema
 				Extensions: openapi.ExtensionsFromExpr(r.Body.Meta),
 			}
 			initExamples(content[ct], r.Body, rand)
-		} else if r.StatusCode != expr.StatusNoContent {
+		} else if r.StatusCode != expr.StatusNoContent &&
+			isSkipResponseBodyEncodeDecode(r.Parent) {
 			// When SkipResponseBodyEncodeDecode is declared, the response type
 			// is Empty, but the response code is not 204 and has content.
 			content = make(map[string]*MediaType)
@@ -69,3 +71,8 @@ func responseFromExpr(r *expr.HTTPResponseExpr, bodies map[int][]*openapi.Schema
 		Extensions:  openapi.ExtensionsFromExpr(r.Meta),
 	}
 }
+
+func isSkipResponseBodyEncodeDecode(parent eval.Expression) bool {
+	ee, ok := parent.(*expr.HTTPEndpointExpr)
+	return ok && ee.SkipResponseBodyEncodeDecode
+}


Test patch:
diff --git a/http/codegen/openapi/v3/testdata/golden/skip-response-body-encode-decode_file0.golden b/http/codegen/openapi/v3/testdata/golden/skip-response-body-encode-decode_file0.golden
index 2ecc865b..92a9abd6 100644
--- a/http/codegen/openapi/v3/testdata/golden/skip-response-body-encode-decode_file0.golden
+++ b/http/codegen/openapi/v3/testdata/golden/skip-response-body-encode-decode_file0.golden
@@ -1,1 +1,1 @@
-{"openapi":"3.0.3","info":{"title":"Goa API","version":"1.0"},"servers":[{"url":"http://localhost:80","description":"Default server for test api"}],"paths":{"/binary":{"get":{"tags":["testService"],"summary":"binary testService","operationId":"testService#binary","responses":{"200":{"description":"OK response.","content":{"image/png":{"schema":{"type":"string","format":"binary"}}}}}}},"/empty":{"get":{"tags":["testService"],"summary":"empty testService","operationId":"testService#empty","responses":{"204":{"description":"No Content response."}}}}},"components":{},"tags":[{"name":"testService"}]}
\ No newline at end of file
+{"openapi":"3.0.3","info":{"title":"Goa API","version":"1.0"},"servers":[{"url":"http://localhost:80","description":"Default server for test api"}],"paths":{"/binary":{"get":{"tags":["testService"],"summary":"binary testService","operationId":"testService#binary","responses":{"200":{"description":"OK response.","content":{"image/png":{"schema":{"type":"string","format":"binary"}}}}}}},"/empty":{"get":{"tags":["testService"],"summary":"empty testService","operationId":"testService#empty","responses":{"204":{"description":"No Content response."}}}},"/empty/ok":{"get":{"tags":["testService"],"summary":"empty_ok testService","operationId":"testService#empty_ok","responses":{"200":{"description":"OK response."}}}}},"components":{},"tags":[{"name":"testService"}]}
\ No newline at end of file
diff --git a/http/codegen/openapi/v3/testdata/golden/skip-response-body-encode-decode_file1.golden b/http/codegen/openapi/v3/testdata/golden/skip-response-body-encode-decode_file1.golden
index a10a174c..8e2f090f 100644
--- a/http/codegen/openapi/v3/testdata/golden/skip-response-body-encode-decode_file1.golden
+++ b/http/codegen/openapi/v3/testdata/golden/skip-response-body-encode-decode_file1.golden
@@ -29,6 +29,15 @@ paths:
             responses:
                 "204":
                     description: No Content response.
+    /empty/ok:
+        get:
+            tags:
+                - testService
+            summary: empty_ok testService
+            operationId: testService#empty_ok
+            responses:
+                "200":
+                    description: OK response.
 components: {}
 tags:
     - name: testService
diff --git a/http/codegen/testdata/openapi_dsls.go b/http/codegen/testdata/openapi_dsls.go
index ea85b964..29ded6c7 100644
--- a/http/codegen/testdata/openapi_dsls.go
+++ b/http/codegen/testdata/openapi_dsls.go
@@ -697,6 +697,14 @@ var SkipResponseBodyEncodeDecodeDSL = func() {
 				GET("/empty")
 			})
 		})
+		Method("empty_ok", func() {
+			Payload(Empty)
+			Result(Empty)
+			HTTP(func() {
+				GET("/empty/ok")
+				Response(StatusOK)
+			})
+		})
 		Method("binary", func() {
 			Payload(Empty)
 			Result(Empty)