Skip to content

Commit

Permalink
Adding a UUID format (#546)
Browse files Browse the repository at this point in the history
* Adding a UUID format

* Removing indirect (hopefully build passes now)

Co-authored-by: Chrusty <>
  • Loading branch information
chrusty committed Apr 19, 2022
1 parent 5ea7d77 commit aeb264f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -8,6 +8,7 @@ require (
github.com/go-playground/validator/v10 v10.9.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golangci/lint-1 v0.0.0-20181222135242-d2cdd8c08219
github.com/google/uuid v1.3.0
github.com/json-iterator/go v1.1.12 // indirect
github.com/labstack/echo/v4 v4.6.3
github.com/lestrrat-go/jwx v1.2.7
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -45,6 +45,8 @@ github.com/golangci/lint-1 v0.0.0-20181222135242-d2cdd8c08219/go.mod h1:/X8TswGS
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand Down
2 changes: 2 additions & 0 deletions pkg/codegen/schema.go
Expand Up @@ -392,6 +392,8 @@ func resolveType(schema *openapi3.Schema, path []string, outSchema *Schema) erro
case "json":
outSchema.GoType = "json.RawMessage"
outSchema.SkipOptionalPointer = true
case "uuid":
outSchema.GoType = "openapi_types.UUID"
default:
// All unrecognized formats are simply a regular string.
outSchema.GoType = "string"
Expand Down
29 changes: 29 additions & 0 deletions pkg/types/uuid.go
@@ -0,0 +1,29 @@
package types

import (
"encoding/json"
"errors"

"github.com/google/uuid"
)

type UUID string

This comment has been minimized.

Copy link
@elri

elri Apr 20, 2022

Wouldn't it be nicer if UUID was an alias of string rather than a 'new' type? (so type UUID = string instead of type UUID string). Then casting to/from string wouldn't be necessary


func (u UUID) MarshalJSON() ([]byte, error) {
if _, err := uuid.Parse(string(u)); err != nil {
return nil, errors.New("uuid: failed to pass validation")
}
return json.Marshal(string(u))
}

func (u *UUID) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
if _, err := uuid.Parse(s); err != nil {
return errors.New("uuid: failed to pass validation")
}
*u = UUID(s)
return nil
}
51 changes: 51 additions & 0 deletions pkg/types/uuid_test.go
@@ -0,0 +1,51 @@
package types

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

func TestUUID_MarshalJSON_Fail(t *testing.T) {
testUUID := "this-is-not-a-uuid"
b := struct {
UUIDField UUID `json:"uuid"`
}{
UUIDField: UUID(testUUID),
}
_, err := json.Marshal(b)
assert.Error(t, err)
}

func TestUUID_MarshalJSON_Pass(t *testing.T) {
testUUID := "9cb14230-b640-11ec-b909-0242ac120002"
b := struct {
UUIDField UUID `json:"uuid"`
}{
UUIDField: UUID(testUUID),
}
jsonBytes, err := json.Marshal(b)
assert.NoError(t, err)
assert.JSONEq(t, `{"uuid":"9cb14230-b640-11ec-b909-0242ac120002"}`, string(jsonBytes))
}

func TestUUID_UnmarshalJSON_Fail(t *testing.T) {
jsonStr := `{"uuid":"this-is-not-a-uuid"}`
b := struct {
UUIDField UUID `json:"uuid"`
}{}
err := json.Unmarshal([]byte(jsonStr), &b)
assert.Error(t, err)
}

func TestUUID_UnmarshalJSON_Pass(t *testing.T) {
testUUID := UUID("9cb14230-b640-11ec-b909-0242ac120002")
jsonStr := `{"uuid":"9cb14230-b640-11ec-b909-0242ac120002"}`
b := struct {
UUIDField UUID `json:"uuid"`
}{}
err := json.Unmarshal([]byte(jsonStr), &b)
assert.NoError(t, err)
assert.Equal(t, testUUID, b.UUIDField)
}

0 comments on commit aeb264f

Please sign in to comment.