From a6622e87b7d4e4c58117636ec36c9837784ac563 Mon Sep 17 00:00:00 2001 From: tdakkota Date: Tue, 13 Sep 2022 19:05:44 +0300 Subject: [PATCH] test: add some tests to ensure extensions parsing --- jsonschema/parser_test.go | 43 +++++++++++++++++++++++++++++++++++++++ spec_test.go | 43 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 spec_test.go diff --git a/jsonschema/parser_test.go b/jsonschema/parser_test.go index 505db4cc9..5e001c85b 100644 --- a/jsonschema/parser_test.go +++ b/jsonschema/parser_test.go @@ -1,14 +1,17 @@ package jsonschema import ( + "fmt" "strconv" "strings" "testing" "github.com/go-faster/errors" + yaml "github.com/go-faster/yamlx" "github.com/stretchr/testify/require" "github.com/ogen-go/ogen/internal/jsonpointer" + "github.com/ogen-go/ogen/internal/location" ) type components map[string]*RawSchema @@ -345,6 +348,46 @@ func TestSchemaReferencedArray(t *testing.T) { require.Equal(t, expect, out) } +func TestSchemaExtensions(t *testing.T) { + tests := []struct { + raw string + expect *Schema + expectErr bool + }{ + { + `{"type": "string", "x-ogen-name": "foo"}`, + &Schema{ + Type: String, + XOgenName: "foo", + }, + false, + }, + {`{"type": "string", "x-ogen-name": {}}`, nil, true}, + } + + for i, tt := range tests { + tt := tt + t.Run(fmt.Sprintf("Test%d", i+1), func(t *testing.T) { + a := require.New(t) + + var raw RawSchema + a.NoError(yaml.Unmarshal([]byte(tt.raw), &raw)) + + out, err := NewParser(Settings{ + Filename: "test.yaml", + }).Parse(&raw) + if tt.expectErr { + a.Error(err) + return + } + a.NoError(err) + // Zero locator to simplify comparison. + out.Locator = location.Locator{} + a.Equal(tt.expect, out) + }) + } +} + func TestInvalidMultipleOf(t *testing.T) { values := []int{0, -1, -10} parser := NewParser(Settings{ diff --git a/spec_test.go b/spec_test.go new file mode 100644 index 000000000..bd15bb713 --- /dev/null +++ b/spec_test.go @@ -0,0 +1,43 @@ +package ogen_test + +import ( + "testing" + + yaml "github.com/go-faster/yamlx" + "github.com/stretchr/testify/require" + + "github.com/ogen-go/ogen" +) + +func encodeDecode[T any](a *require.Assertions, input T) (result T) { + data, err := yaml.Marshal(input) + a.NoError(err) + + a.NoError(yaml.Unmarshal(data, &result)) + return result +} + +func TestExtensionParsing(t *testing.T) { + a := require.New(t) + + { + var ( + input = `{"url": "/api/v1", "x-ogen-name": "foo"}` + s ogen.Server + ) + a.NoError(yaml.Unmarshal([]byte(input), &s)) + a.Equal("foo", s.Extensions["x-ogen-name"].Value) + s2 := encodeDecode(a, s) + a.Equal("foo", s2.Extensions["x-ogen-name"].Value) + } + + { + var ( + input = `{"description": "foo", "x-ogen-extension": "bar"}` + s ogen.Response + ) + a.NoError(yaml.Unmarshal([]byte(input), &s)) + a.Equal("bar", s.Common.Extensions["x-ogen-extension"].Value) + // FIXME(tdakkota): encodeDecode doesn't work for this type + } +}