Skip to content

Commit

Permalink
Fixed UUID generation in openapi examples (#3199)
Browse files Browse the repository at this point in the history
* fixed uuid generation in openapi examples

* fixed test
  • Loading branch information
c-reeder committed Nov 21, 2022
1 parent c9340fa commit f0db0ac
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 4 additions & 3 deletions expr/example.go
Expand Up @@ -6,6 +6,7 @@ import (
"regexp"
"time"

googleuuid "github.com/google/uuid"
regen "github.com/zach-klippenstein/goregen"
)

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion expr/example_test.go
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit f0db0ac

Please sign in to comment.