Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a UUID format #546

Merged
merged 2 commits into from Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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

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)
}