Repository: swaggest/rest
Pull request #201 (2024-03-29 21:03:41)

Fix web.Handle with all available HTTP methods

github-actions[bot]: ### Lines Of Code

| Language  | Files |    Lines    |    Code     | Comments |  Blanks   | Complexity |     Bytes      |
|-----------|-------|-------------|-------------|----------|-----------|------------|----------------|
| Go        | 113   | 8744 (+10)  | 6306 (+9)   | 660      | 1778 (+1) | 1043 (+4)  | 213.7K (+237B) |
| Go (test) | 55    | 6680 (+2)   | 5125 (+1)   | 295      | 1260 (+1) | 188        | 182.4K (+61B)  |
| JSON      | 3     | 1999 (+139) | 1999 (+139) | 0        | 0         | 0          | 76K (+4.2K)    |
<!-- Sticky Pull Request CommentLOC -->
github-actions[bot]: ### Go API Changes

<pre>
# summary
Inferred base version: v0.2.65
Suggested version: v0.2.66
</pre>
<!-- Sticky Pull Request Commentgorelease -->
github-actions[bot]: ### Unit Test Coverage
total:								(statements)			81.2%
changed lines: (statements) 77.8%, coverage is less than 90.0%, consider testing the changes more thoroughly
<details><summary>Coverage of changed lines</summary>

|         File          |     Function      | Coverage |
|-----------------------|-------------------|----------|
| Total                 |                   | 77.8%    |
| nethttp/openapi.go    |                   | 77.8%    |
| nethttp/openapi.go:13 | OpenAPIMiddleware | 92.3%    |

</details>

<details><summary>Coverage diff with base branch</summary>

|        File        |     Function      | Base Coverage | Current Coverage |
|--------------------|-------------------|---------------|------------------|
| Total              |                   | 81.2%         | 81.2% (0.0%)     |
| nethttp/openapi.go | OpenAPIMiddleware | 80.0%         | 81.2% (+1.2%)    |

</details>
<!-- Sticky Pull Request Commentunit-test -->

Patch:
diff --git a/_examples/go.mod b/_examples/go.mod
index db07f3a..b8666b0 100644
--- a/_examples/go.mod
+++ b/_examples/go.mod
@@ -17,8 +17,8 @@ require (
 	github.com/rs/cors v1.10.1
 	github.com/stretchr/testify v1.9.0
 	github.com/swaggest/assertjson v1.9.0
-	github.com/swaggest/jsonschema-go v0.3.69
-	github.com/swaggest/openapi-go v0.2.49
+	github.com/swaggest/jsonschema-go v0.3.70
+	github.com/swaggest/openapi-go v0.2.50
 	github.com/swaggest/rest v0.0.0-00010101000000-000000000000
 	github.com/swaggest/swgui v1.8.0
 	github.com/swaggest/usecase v1.3.1
diff --git a/_examples/go.sum b/_examples/go.sum
index a6211c5..ba9d761 100644
--- a/_examples/go.sum
+++ b/_examples/go.sum
@@ -123,10 +123,10 @@ github.com/swaggest/assertjson v1.9.0 h1:dKu0BfJkIxv/xe//mkCrK5yZbs79jL7OVf9Ija7
 github.com/swaggest/assertjson v1.9.0/go.mod h1:b+ZKX2VRiUjxfUIal0HDN85W0nHPAYUbYH5WkkSsFsU=
 github.com/swaggest/form/v5 v5.1.1 h1:ct6/rOQBGrqWUQ0FUv3vW5sHvTUb31AwTUWj947N6cY=
 github.com/swaggest/form/v5 v5.1.1/go.mod h1:X1hraaoONee20PMnGNLQpO32f9zbQ0Czfm7iZThuEKg=
-github.com/swaggest/jsonschema-go v0.3.69 h1:BNEajhoQjnEQzxZqPmjD1Pcs1pxnjx/X9L5KWllLHxo=
-github.com/swaggest/jsonschema-go v0.3.69/go.mod h1:7N43/CwdaWgPUDfYV70K7Qm79tRqe/al7gLSt9YeGIE=
-github.com/swaggest/openapi-go v0.2.49 h1:WxAfdde6DlfQPQayHWTb64CywK9+vpToi7iN17iDPO8=
-github.com/swaggest/openapi-go v0.2.49/go.mod h1:MK5O26lG289kFgMOyXK1VXDoTZ89KJ8Vt0v0ic23zZw=
+github.com/swaggest/jsonschema-go v0.3.70 h1:8Vx5nm5t/6DBFw2+WC0/Vp1ZVe9/4mpuA0tuAe0wwCI=
+github.com/swaggest/jsonschema-go v0.3.70/go.mod h1:7N43/CwdaWgPUDfYV70K7Qm79tRqe/al7gLSt9YeGIE=
+github.com/swaggest/openapi-go v0.2.50 h1:5yQ7N/IhMK9bQSk2yFAEbB75DvoXzyEmji3Q2iS++is=
+github.com/swaggest/openapi-go v0.2.50/go.mod h1:5R2TWYBz0U7P3vwIwN0ytwSxqONXZnbiAaa+DQ3Sq1k=
 github.com/swaggest/refl v1.3.0 h1:PEUWIku+ZznYfsoyheF97ypSduvMApYyGkYF3nabS0I=
 github.com/swaggest/refl v1.3.0/go.mod h1:3Ujvbmh1pfSbDYjC6JGG7nMgPvpG0ehQL4iNonnLNbg=
 github.com/swaggest/swgui v1.8.0 h1:dPu8TsYIOraaObAkyNdoiLI8mu7nOqQ6SU7HOv254rM=
diff --git a/go.mod b/go.mod
index f05ba3b..716faeb 100644
--- a/go.mod
+++ b/go.mod
@@ -13,8 +13,8 @@ require (
 	github.com/stretchr/testify v1.8.2
 	github.com/swaggest/assertjson v1.9.0
 	github.com/swaggest/form/v5 v5.1.1
-	github.com/swaggest/jsonschema-go v0.3.69
-	github.com/swaggest/openapi-go v0.2.49
+	github.com/swaggest/jsonschema-go v0.3.70
+	github.com/swaggest/openapi-go v0.2.50
 	github.com/swaggest/refl v1.3.0
 	github.com/swaggest/usecase v1.3.1
 )
diff --git a/go.sum b/go.sum
index 9857b9a..4d05bee 100644
--- a/go.sum
+++ b/go.sum
@@ -78,11 +78,10 @@ github.com/swaggest/assertjson v1.9.0 h1:dKu0BfJkIxv/xe//mkCrK5yZbs79jL7OVf9Ija7
 github.com/swaggest/assertjson v1.9.0/go.mod h1:b+ZKX2VRiUjxfUIal0HDN85W0nHPAYUbYH5WkkSsFsU=
 github.com/swaggest/form/v5 v5.1.1 h1:ct6/rOQBGrqWUQ0FUv3vW5sHvTUb31AwTUWj947N6cY=
 github.com/swaggest/form/v5 v5.1.1/go.mod h1:X1hraaoONee20PMnGNLQpO32f9zbQ0Czfm7iZThuEKg=
-github.com/swaggest/jsonschema-go v0.3.66/go.mod h1:7N43/CwdaWgPUDfYV70K7Qm79tRqe/al7gLSt9YeGIE=
-github.com/swaggest/jsonschema-go v0.3.69 h1:BNEajhoQjnEQzxZqPmjD1Pcs1pxnjx/X9L5KWllLHxo=
-github.com/swaggest/jsonschema-go v0.3.69/go.mod h1:7N43/CwdaWgPUDfYV70K7Qm79tRqe/al7gLSt9YeGIE=
-github.com/swaggest/openapi-go v0.2.49 h1:WxAfdde6DlfQPQayHWTb64CywK9+vpToi7iN17iDPO8=
-github.com/swaggest/openapi-go v0.2.49/go.mod h1:MK5O26lG289kFgMOyXK1VXDoTZ89KJ8Vt0v0ic23zZw=
+github.com/swaggest/jsonschema-go v0.3.70 h1:8Vx5nm5t/6DBFw2+WC0/Vp1ZVe9/4mpuA0tuAe0wwCI=
+github.com/swaggest/jsonschema-go v0.3.70/go.mod h1:7N43/CwdaWgPUDfYV70K7Qm79tRqe/al7gLSt9YeGIE=
+github.com/swaggest/openapi-go v0.2.50 h1:5yQ7N/IhMK9bQSk2yFAEbB75DvoXzyEmji3Q2iS++is=
+github.com/swaggest/openapi-go v0.2.50/go.mod h1:5R2TWYBz0U7P3vwIwN0ytwSxqONXZnbiAaa+DQ3Sq1k=
 github.com/swaggest/refl v1.3.0 h1:PEUWIku+ZznYfsoyheF97ypSduvMApYyGkYF3nabS0I=
 github.com/swaggest/refl v1.3.0/go.mod h1:3Ujvbmh1pfSbDYjC6JGG7nMgPvpG0ehQL4iNonnLNbg=
 github.com/swaggest/usecase v1.3.1 h1:JdKV30MTSsDxAXxkldLNcEn8O2uf565khyo6gr5sS+w=
diff --git a/nethttp/openapi.go b/nethttp/openapi.go
index abc7ad6..1c083ec 100644
--- a/nethttp/openapi.go
+++ b/nethttp/openapi.go
@@ -25,14 +25,26 @@ func OpenAPIMiddleware(s *openapi.Collector) func(http.Handler) http.Handler {
 			return h
 		}
 
-		err := s.CollectUseCase(
-			withRoute.RouteMethod(),
-			withRoute.RoutePattern(),
-			handler.UseCase(),
-			handler.HandlerTrait,
-		)
-		if err != nil {
-			panic(err)
+		var methods []string
+
+		method := withRoute.RouteMethod()
+
+		if method == "" {
+			methods = []string{"get", "put", "post", "delete", "options", "head", "patch", "trace"}
+		} else {
+			methods = []string{method}
+		}
+
+		for _, m := range methods {
+			err := s.CollectUseCase(
+				m,
+				withRoute.RoutePattern(),
+				handler.UseCase(),
+				handler.HandlerTrait,
+			)
+			if err != nil {
+				panic(err)
+			}
 		}
 
 		return h
diff --git a/response/gzip/middleware.go b/response/gzip/middleware.go
index 106ce18..d91a583 100644
--- a/response/gzip/middleware.go
+++ b/response/gzip/middleware.go
@@ -4,12 +4,14 @@ import (
 	"bufio"
 	"compress/flate"
 	"compress/gzip"
+	"errors"
 	"fmt"
 	"io"
 	"net"
 	"net/http"
 	"strings"
 	"sync"
+	"syscall"
 
 	gz "github.com/swaggest/rest/gzip"
 )
@@ -30,7 +32,7 @@ func Middleware(next http.Handler) http.Handler {
 		if closer, ok := w.(io.Closer); ok {
 			defer func() {
 				err := closer.Close()
-				if err != nil {
+				if err != nil && !errors.Is(err, syscall.EPIPE) {
 					panic(fmt.Sprintf("BUG: cannot close gzip writer: %s", err))
 				}
 			}()


Test patch:
diff --git a/web/_testdata/openapi.json b/web/_testdata/openapi.json
index 6ab353f..2164161 100644
--- a/web/_testdata/openapi.json
+++ b/web/_testdata/openapi.json
@@ -6,6 +6,145 @@
     "version":"v1.0.0"
   },
   "paths":{
+    "/a/{id}":{
+      "delete":{
+        "tags":["Album"],"summary":"Album By ID",
+        "operationId":"rest/web_test.albumByID7",
+        "parameters":[
+          {"name":"locale","in":"query","schema":{"type":"string"}},
+          {
+            "name":"id","in":"path","required":true,"schema":{"type":"integer"}
+          }
+        ],
+        "responses":{
+          "200":{
+            "description":"OK",
+            "content":{
+              "application/json":{"schema":{"$ref":"#/components/schemas/WebTestAlbum"}}
+            }
+          }
+        }
+      },
+      "get":{
+        "tags":["Album"],"summary":"Album By ID",
+        "operationId":"rest/web_test.albumByID4",
+        "parameters":[
+          {"name":"locale","in":"query","schema":{"type":"string"}},
+          {
+            "name":"id","in":"path","required":true,"schema":{"type":"integer"}
+          }
+        ],
+        "responses":{
+          "200":{
+            "description":"OK",
+            "content":{
+              "application/json":{"schema":{"$ref":"#/components/schemas/WebTestAlbum"}}
+            }
+          }
+        }
+      },
+      "head":{
+        "tags":["Album"],"summary":"Album By ID",
+        "operationId":"rest/web_test.albumByID9",
+        "parameters":[
+          {"name":"locale","in":"query","schema":{"type":"string"}},
+          {
+            "name":"id","in":"path","required":true,"schema":{"type":"integer"}
+          }
+        ],
+        "responses":{"200":{"description":"OK"}}
+      },
+      "options":{
+        "tags":["Album"],"summary":"Album By ID",
+        "operationId":"rest/web_test.albumByID8",
+        "parameters":[
+          {"name":"locale","in":"query","schema":{"type":"string"}},
+          {
+            "name":"id","in":"path","required":true,"schema":{"type":"integer"}
+          }
+        ],
+        "responses":{
+          "200":{
+            "description":"OK",
+            "content":{
+              "application/json":{"schema":{"$ref":"#/components/schemas/WebTestAlbum"}}
+            }
+          }
+        }
+      },
+      "patch":{
+        "tags":["Album"],"summary":"Album By ID",
+        "operationId":"rest/web_test.albumByID10",
+        "parameters":[
+          {"name":"locale","in":"query","schema":{"type":"string"}},
+          {
+            "name":"id","in":"path","required":true,"schema":{"type":"integer"}
+          }
+        ],
+        "responses":{
+          "200":{
+            "description":"OK",
+            "content":{
+              "application/json":{"schema":{"$ref":"#/components/schemas/WebTestAlbum"}}
+            }
+          }
+        }
+      },
+      "post":{
+        "tags":["Album"],"summary":"Album By ID",
+        "operationId":"rest/web_test.albumByID6",
+        "parameters":[
+          {"name":"locale","in":"query","schema":{"type":"string"}},
+          {
+            "name":"id","in":"path","required":true,"schema":{"type":"integer"}
+          }
+        ],
+        "responses":{
+          "200":{
+            "description":"OK",
+            "content":{
+              "application/json":{"schema":{"$ref":"#/components/schemas/WebTestAlbum"}}
+            }
+          }
+        }
+      },
+      "put":{
+        "tags":["Album"],"summary":"Album By ID",
+        "operationId":"rest/web_test.albumByID5",
+        "parameters":[
+          {"name":"locale","in":"query","schema":{"type":"string"}},
+          {
+            "name":"id","in":"path","required":true,"schema":{"type":"integer"}
+          }
+        ],
+        "responses":{
+          "200":{
+            "description":"OK",
+            "content":{
+              "application/json":{"schema":{"$ref":"#/components/schemas/WebTestAlbum"}}
+            }
+          }
+        }
+      },
+      "trace":{
+        "tags":["Album"],"summary":"Album By ID",
+        "operationId":"rest/web_test.albumByID11",
+        "parameters":[
+          {"name":"locale","in":"query","schema":{"type":"string"}},
+          {
+            "name":"id","in":"path","required":true,"schema":{"type":"integer"}
+          }
+        ],
+        "responses":{
+          "200":{
+            "description":"OK",
+            "content":{
+              "application/json":{"schema":{"$ref":"#/components/schemas/WebTestAlbum"}}
+            }
+          }
+        }
+      }
+    },
     "/albums":{
       "options":{
         "tags":["Album"],"summary":"Post Albums",
@@ -154,4 +293,4 @@
       }
     }
   }
-}
+}
\ No newline at end of file
diff --git a/web/service_test.go b/web/service_test.go
index eb436f7..22ebd2e 100644
--- a/web/service_test.go
+++ b/web/service_test.go
@@ -76,6 +76,8 @@ func TestDefaultService(t *testing.T) {
 		}),
 	)
 
+	service.Handle("/a/{id}", nethttp.NewHandler(albumByID()))
+
 	rw := httptest.NewRecorder()
 	r, err := http.NewRequest(http.MethodGet, "http://localhost/docs/openapi.json", nil)
 	require.NoError(t, err)