Repository: goadesign/goa
Pull request #3199 (2022-11-19 05:02:12)

Fixed UUID generation in openapi examples
The OpenAPI generation currently produces invalid UUIDs in the examples. This is problematic for any tool that pulls those in for testing (ex. Postman). 

Using the regular expression to generate the UUIDs was only acceptable on occasion because it failed to take into account the different UUID variants (reserved, backwards compatible, etc).

This change makes us use the same library dependency that we were _already_ using for checking/inspecting an incoming UUID in order to generate one.

Also the generic fallback was invalid because it had one character too many.


Patch:
diff --git a/expr/example.go b/expr/example.go
index 95a5849e..364f1ec1 100644
--- a/expr/example.go
+++ b/expr/example.go
@@ -6,6 +6,7 @@ import (
 	"regexp"
 	"time"
 
+	googleuuid "github.com/google/uuid"
 	regen "github.com/zach-klippenstein/goregen"
 )
 
@@ -206,11 +207,11 @@ func byFormat(a *AttributeExpr, r *ExampleGenerator) interface{} {
 		FormatRegexp:  r.Characters(3) + ".*",
 		FormatRFC1123: time.Unix(int64(r.Int())%1454957045, 0).UTC().Format(time.RFC1123), // to obtain a "fixed" rand
 		FormatUUID: func() string {
-			res, err := regen.Generate(`[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}`)
+			uuid, err := googleuuid.NewUUID()
 			if err != nil {
-				return "12345678-1234-1234-12324-123456789ABC"
+				return "12345678-1234-1234-9232-123456789ABC"
 			}
-			return res
+			return uuid.String()
 		}(),
 		FormatJSON: `{"name":"example","email":"mail@example.com"}`,
 	}[format]; ok {
diff --git a/go.mod b/go.mod
index 69904980..a28988ca 100644
--- a/go.mod
+++ b/go.mod
@@ -15,7 +15,7 @@ require (
 	github.com/zach-klippenstein/goregen v0.0.0-20160303162051-795b5e3961ea
 	golang.org/x/text v0.4.0
 	golang.org/x/tools v0.3.0
-	google.golang.org/grpc v1.50.1
+	google.golang.org/grpc v1.51.0
 	google.golang.org/protobuf v1.28.1
 	gopkg.in/yaml.v3 v3.0.1
 )
diff --git a/go.sum b/go.sum
index 99adc11a..90216105 100644
--- a/go.sum
+++ b/go.sum
@@ -98,8 +98,8 @@ golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df h1:5Pf6pFKu98ODmgnpvkJ3k
 golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
 google.golang.org/genproto v0.0.0-20220930163606-c98284e70a91 h1:Ezh2cpcnP5Rq60sLensUsFnxh7P6513NLvNtCm9iyJ4=
 google.golang.org/genproto v0.0.0-20220930163606-c98284e70a91/go.mod h1:3526vdqwhZAwq4wsRUaVG555sVgsNmIjRtO7t/JH29U=
-google.golang.org/grpc v1.50.1 h1:DS/BukOZWp8s6p4Dt/tOaJaTQyPyOoCcrjroHuCeLzY=
-google.golang.org/grpc v1.50.1/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI=
+google.golang.org/grpc v1.51.0 h1:E1eGv1FTqoLIdnBCZufiSHgKjlqG6fKFf6pPWtMTh8U=
+google.golang.org/grpc v1.51.0/go.mod h1:wgNDFcnuBGmxLKI/qn4T+m5BtEBYXJPvibbUPsAIPww=
 google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
 google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
 google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=


Test patch:
diff --git a/expr/example_test.go b/expr/example_test.go
index 26e5cc16..276eeb00 100644
--- a/expr/example_test.go
+++ b/expr/example_test.go
@@ -44,7 +44,7 @@ func TestByFormatUUID(t *testing.T) {
 	att := expr.AttributeExpr{Validation: val}
 	r := expr.NewRandom("test")
 	example := att.Example(r).(string)
-	if !regexp.MustCompile(`[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}`).MatchString(example) {
+	if !regexp.MustCompile(`[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}`).MatchString(example) {
 		t.Errorf("got %s, expected a match with `[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}`", example)
 	}
 }