From 12540af49b7b2c6964371ab6faae41c0c628c3af Mon Sep 17 00:00:00 2001 From: Pierre Fenoll Date: Tue, 31 May 2022 17:24:44 +0200 Subject: [PATCH] TestIssue430: fix racey behavior (#553) --- .github/workflows/go.yml | 1 + openapi3/schema_formats.go | 11 ++++++----- openapi3/schema_formats_test.go | 3 +++ 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 6c192f969..70b6db6d9 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -66,6 +66,7 @@ jobs: env: GOARCH: '386' - run: go test ./... + - run: go test -count=2 ./... - run: go test -v -run TestRaceyPatternSchema -race ./... env: CGO_ENABLED: '1' diff --git a/openapi3/schema_formats.go b/openapi3/schema_formats.go index 29fbd51fb..17b7564cf 100644 --- a/openapi3/schema_formats.go +++ b/openapi3/schema_formats.go @@ -12,18 +12,19 @@ const ( FormatOfStringForUUIDOfRFC4122 = `^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$` ) -//FormatCallback custom check on exotic formats -type FormatCallback func(Val string) error +// FormatCallback performs custom checks on exotic formats +type FormatCallback func(value string) error +// Format represents a format validator registered by either DefineStringFormat or DefineStringFormatCallback type Format struct { regexp *regexp.Regexp callback FormatCallback } -//SchemaStringFormats allows for validating strings format -var SchemaStringFormats = make(map[string]Format, 8) +// SchemaStringFormats allows for validating string formats +var SchemaStringFormats = make(map[string]Format, 4) -//DefineStringFormat Defines a new regexp pattern for a given format +// DefineStringFormat defines a new regexp pattern for a given format func DefineStringFormat(name string, pattern string) { re, err := regexp.Compile(pattern) if err != nil { diff --git a/openapi3/schema_formats_test.go b/openapi3/schema_formats_test.go index 14733c8a1..5cceb8cf0 100644 --- a/openapi3/schema_formats_test.go +++ b/openapi3/schema_formats_test.go @@ -13,6 +13,9 @@ func TestIssue430(t *testing.T) { NewStringSchema().WithFormat("ipv6"), ) + delete(SchemaStringFormats, "ipv4") + delete(SchemaStringFormats, "ipv6") + err := schema.Validate(context.Background()) require.NoError(t, err)