From 09e40f82cc95bdd2f05881d9859c08e9c3618af2 Mon Sep 17 00:00:00 2001 From: tdakkota Date: Fri, 23 Dec 2022 20:27:29 +0300 Subject: [PATCH] test: add `any` type custom format test --- _testdata/positive/custom_formats.json | 29 +++++++ .../integration/cmd/customformats/main.go | 4 + .../customformats/eventtype/eventtype.go | 60 +++++++++++++++ internal/integration/customformats_test.go | 76 +++++++++++++------ 4 files changed, 145 insertions(+), 24 deletions(-) create mode 100644 internal/integration/customformats/eventtype/eventtype.go diff --git a/_testdata/positive/custom_formats.json b/_testdata/positive/custom_formats.json index cc8201e1c..c1745d0e0 100644 --- a/_testdata/positive/custom_formats.json +++ b/_testdata/positive/custom_formats.json @@ -5,6 +5,32 @@ "version": "v0.1.0" }, "paths": { + "/event": { + "post": { + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Event" + } + } + } + }, + "responses": { + "200": { + "description": "description", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Event" + } + } + } + } + } + } + }, "/phone": { "get": { "parameters": [ @@ -75,6 +101,9 @@ } }, "schemas": { + "Event": { + "format": "x-my-event" + }, "User": { "type": "object", "required": [ diff --git a/internal/integration/cmd/customformats/main.go b/internal/integration/cmd/customformats/main.go index 117b50e83..1b74bad5e 100644 --- a/internal/integration/cmd/customformats/main.go +++ b/internal/integration/cmd/customformats/main.go @@ -11,6 +11,7 @@ import ( "github.com/ogen-go/ogen" "github.com/ogen-go/ogen/gen" "github.com/ogen-go/ogen/gen/genfs" + "github.com/ogen-go/ogen/internal/integration/customformats/eventtype" "github.com/ogen-go/ogen/internal/integration/customformats/hextype" "github.com/ogen-go/ogen/internal/integration/customformats/phonetype" "github.com/ogen-go/ogen/internal/integration/customformats/rgbatype" @@ -58,6 +59,9 @@ func run(specPath, targetDir string) error { "rgba": rgbatype.RGBAFormat, "hex": hextype.HexFormat, }, + jsonschema.Empty: { + "x-my-event": eventtype.EventFormat, + }, }, File: location.NewFile(fileName, specPath, data), Logger: l, diff --git a/internal/integration/customformats/eventtype/eventtype.go b/internal/integration/customformats/eventtype/eventtype.go new file mode 100644 index 000000000..27811bdc3 --- /dev/null +++ b/internal/integration/customformats/eventtype/eventtype.go @@ -0,0 +1,60 @@ +// Package eventtype defines a custom format for event types. +package eventtype + +import ( + "encoding/json" + + "github.com/go-faster/jx" + + "github.com/ogen-go/ogen/gen" +) + +type Event = any + +// EventFormat defines a custom format for Event. +var EventFormat = gen.CustomFormat[ + Event, + JSONEventEncoding, + TextEventEncoding, +]() + +// JSONEventEncoding defines a custom JSON encoding for hexadecimal numbers. +type JSONEventEncoding struct{} + +// EncodeJSON encodes a hexadecimal number as a JSON string. +func (JSONEventEncoding) EncodeJSON(e *jx.Encoder, v Event) { + b, err := json.Marshal(v) + if err != nil { + e.Null() + return + } + e.Raw(b) +} + +// DecodeJSON decodes a hexadecimal number from a JSON string. +func (JSONEventEncoding) DecodeJSON(d *jx.Decoder) (v Event, _ error) { + r, err := d.Raw() + if err != nil { + return v, err + } + err = json.Unmarshal(r, &v) + return v, err +} + +// TextEventEncoding defines a custom text encoding for hexadecimal numbers. +type TextEventEncoding struct{} + +// EncodeText encodes a hexadecimal number as a string. +func (TextEventEncoding) EncodeText(v Event) string { + b, err := json.Marshal(v) + if err != nil { + return "" + } + return string(b) +} + +// DecodeText decodes a hexadecimal number from a string. +func (TextEventEncoding) DecodeText(s string) (v Event, _ error) { + err := json.Unmarshal([]byte(s), &v) + return v, err +} diff --git a/internal/integration/customformats_test.go b/internal/integration/customformats_test.go index 934bdcfe6..229e483b6 100644 --- a/internal/integration/customformats_test.go +++ b/internal/integration/customformats_test.go @@ -5,6 +5,7 @@ import ( "net/http/httptest" "testing" + "github.com/go-faster/errors" "github.com/stretchr/testify/require" "github.com/ogen-go/ogen/internal/integration/customformats/phonetype" @@ -14,6 +15,13 @@ import ( type testCustomFormats struct{} +func (t testCustomFormats) EventPost(ctx context.Context, req any) (any, error) { + if req == nil { + return nil, errors.New("empty request") + } + return req, nil +} + func (t testCustomFormats) PhoneGet(ctx context.Context, req *api.User, params api.PhoneGetParams) (*api.User, error) { req.HomePhone.SetTo(params.Phone) if v, ok := params.Color.Get(); ok { @@ -26,41 +34,61 @@ func (t testCustomFormats) PhoneGet(ctx context.Context, req *api.User, params a } func TestCustomFormats(t *testing.T) { - a := require.New(t) ctx := context.Background() srv, err := api.NewServer(testCustomFormats{}) - a.NoError(err) + require.NoError(t, err) s := httptest.NewServer(srv) defer s.Close() client, err := api.NewClient(s.URL, api.WithClient(s.Client())) - a.NoError(err) + require.NoError(t, err) - var ( - homePhone = phonetype.Phone("+1234567890") - backgroundColor = rgbatype.RGBA{R: 255, G: 0, B: 0, A: 255} - hex = int64(100) + t.Run("EventPost", func(t *testing.T) { + a := require.New(t) - u = &api.User{ - ID: 10, - Phone: "+1234567890", - ProfileColor: rgbatype.RGBA{R: 0, G: 0, B: 0, A: 255}, + for _, val := range []any{ + true, + float64(42), + "string", + []any{float64(1), float64(2), float64(3)}, + map[string]any{ + "key": []any{"value", "value2"}, + }, + } { + result, err := client.EventPost(ctx, val) + a.NoError(err) + a.Equal(val, result) } - ) + }) + t.Run("Phone", func(t *testing.T) { + a := require.New(t) + + var ( + homePhone = phonetype.Phone("+1234567890") + backgroundColor = rgbatype.RGBA{R: 255, G: 0, B: 0, A: 255} + hex = int64(100) + + u = &api.User{ + ID: 10, + Phone: "+1234567890", + ProfileColor: rgbatype.RGBA{R: 0, G: 0, B: 0, A: 255}, + } + ) + + u2, err := client.PhoneGet(ctx, u, api.PhoneGetParams{ + Phone: homePhone, + Color: api.NewOptRgba(backgroundColor), + Hex: api.NewOptHex(hex), + }) + a.NoError(err) - u2, err := client.PhoneGet(ctx, u, api.PhoneGetParams{ - Phone: homePhone, - Color: api.NewOptRgba(backgroundColor), - Hex: api.NewOptHex(hex), + a.Equal(u.ID, u2.ID) + a.Equal(u.Phone, u2.Phone) + a.Equal(u.ProfileColor, u2.ProfileColor) + a.Equal(homePhone, u2.HomePhone.Or("")) + a.Equal(backgroundColor, u2.BackgroundColor.Or(rgbatype.RGBA{})) + a.Equal(hex, u2.HexColor.Or(0)) }) - a.NoError(err) - - a.Equal(u.ID, u2.ID) - a.Equal(u.Phone, u2.Phone) - a.Equal(u.ProfileColor, u2.ProfileColor) - a.Equal(homePhone, u2.HomePhone.Or("")) - a.Equal(backgroundColor, u2.BackgroundColor.Or(rgbatype.RGBA{})) - a.Equal(hex, u2.HexColor.Or(0)) }