From 0ad8b87b4bcc7414bd04ecd498ecb9b5addf3798 Mon Sep 17 00:00:00 2001 From: HNO3Miracle Date: Tue, 21 Jul 2026 11:37:28 +0800 Subject: [PATCH 1/5] tests: wait for the local fixture server TestJSONSchemaSuite started the HTTP fixture server in a goroutine and immediately ran parallel tests, allowing clients to connect before the listener was ready. Create the listener synchronously and close the server during cleanup to remove the race. Signed-off-by: HNO3Miracle --- jsonschema_test.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/jsonschema_test.go b/jsonschema_test.go index faf547a..ca2a847 100644 --- a/jsonschema_test.go +++ b/jsonschema_test.go @@ -5,6 +5,8 @@ package validate import ( "encoding/json" + "errors" + "net" "net/http" "os" "path/filepath" @@ -97,15 +99,18 @@ func isExtendedEnabled(nm string) bool { func TestJSONSchemaSuite(t *testing.T) { // Internal local server to serve remote $ref + svr := &http.Server{ + ReadTimeout: 5 * time.Second, + WriteTimeout: 10 * time.Second, + Handler: http.FileServer(http.Dir(jsonSchemaFixturesPath + "/remotes")), + } + listener, err := net.Listen("tcp", "localhost:1234") + require.NoError(t, err) + t.Cleanup(func() { + require.NoError(t, svr.Close()) + }) go func() { - svr := &http.Server{ - ReadTimeout: 5 * time.Second, - WriteTimeout: 10 * time.Second, - Addr: "localhost:1234", - Handler: http.FileServer(http.Dir(jsonSchemaFixturesPath + "/remotes")), - } - err := svr.ListenAndServe() - if err != nil { + if err := svr.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) { panic(err.Error()) } }() From 3a2f55a9ee9af715925bb641c7e1dec1a4717b7b Mon Sep 17 00:00:00 2001 From: HNO3Miracle Date: Tue, 21 Jul 2026 13:24:06 +0800 Subject: [PATCH 2/5] tests: use the bundled Petstore example fixture The documentation examples should not depend on petstore.swagger.io. Load the repository fixture instead so the examples remain deterministic and work in offline build environments. Signed-off-by: HNO3Miracle --- doc_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/doc_test.go b/doc_test.go index 53bcb13..30e92fb 100644 --- a/doc_test.go +++ b/doc_test.go @@ -41,9 +41,9 @@ func ExampleSpec() { func ExampleSpec_second() { // Example with high level spec validation call, without showing warnings - // Example with online spec URL: - url := "http://petstore.swagger.io/v2/swagger.json" - doc, err := loads.JSONSpec(url) + // Example with the bundled Petstore spec: + path := "fixtures/go-swagger/canary/petstore/swagger.json" + doc, err := loads.JSONSpec(path) if err == nil { validate.SetContinueOnErrors(true) // Set global options errs := validate.Spec(doc, strfmt.Default) // Validates spec with default Swagger 2.0 format definitions @@ -96,9 +96,9 @@ func ExampleSpecValidator_Validate() { func ExampleSpecValidator_Validate_url() { // Example of spec validation call with full result - // Example with online spec URL: - url := "http://petstore.swagger.io/v2/swagger.json" - doc, err := loads.JSONSpec(url) + // Example with the bundled Petstore spec: + path := "fixtures/go-swagger/canary/petstore/swagger.json" + doc, err := loads.JSONSpec(path) if err == nil { validator := validate.NewSpecValidator(doc.Schema(), strfmt.Default) validator.SetContinueOnErrors(true) // Set option for this validator From cda9a58d89252a6f56443c549d93800c2a1be448 Mon Sep 17 00:00:00 2001 From: HNO3Miracle Date: Thu, 23 Jul 2026 02:28:01 +0800 Subject: [PATCH 3/5] tests: use a context-aware fixture listener Signed-off-by: HNO3Miracle --- jsonschema_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsonschema_test.go b/jsonschema_test.go index ca2a847..55ba1db 100644 --- a/jsonschema_test.go +++ b/jsonschema_test.go @@ -104,7 +104,7 @@ func TestJSONSchemaSuite(t *testing.T) { WriteTimeout: 10 * time.Second, Handler: http.FileServer(http.Dir(jsonSchemaFixturesPath + "/remotes")), } - listener, err := net.Listen("tcp", "localhost:1234") + listener, err := new(net.ListenConfig).Listen(t.Context(), "tcp", "localhost:1234") require.NoError(t, err) t.Cleanup(func() { require.NoError(t, svr.Close()) From 9e0d804875730d977825d009e68327c6e7ffd956 Mon Sep 17 00:00:00 2001 From: HNO3Miracle Date: Thu, 23 Jul 2026 16:36:31 +0800 Subject: [PATCH 4/5] docs: retain the URL loading example Signed-off-by: HNO3Miracle --- doc_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/doc_test.go b/doc_test.go index 30e92fb..0d3f5b6 100644 --- a/doc_test.go +++ b/doc_test.go @@ -42,6 +42,7 @@ func ExampleSpec_second() { // Example with high level spec validation call, without showing warnings // Example with the bundled Petstore spec: + // Also works with URL, e.g. http://petstore.swagger.io/v2/swagger.json path := "fixtures/go-swagger/canary/petstore/swagger.json" doc, err := loads.JSONSpec(path) if err == nil { From 7053c02498d8eb4325607b3128d4cdcdd83d0f7b Mon Sep 17 00:00:00 2001 From: HNO3Miracle Date: Thu, 23 Jul 2026 16:46:33 +0800 Subject: [PATCH 5/5] docs: retain URL note in validator example Signed-off-by: HNO3Miracle --- doc_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/doc_test.go b/doc_test.go index 0d3f5b6..8d51eb9 100644 --- a/doc_test.go +++ b/doc_test.go @@ -98,6 +98,7 @@ func ExampleSpecValidator_Validate_url() { // Example of spec validation call with full result // Example with the bundled Petstore spec: + // Also works with URL, e.g. http://petstore.swagger.io/v2/swagger.json path := "fixtures/go-swagger/canary/petstore/swagger.json" doc, err := loads.JSONSpec(path) if err == nil {