2020-04-13 22:47:40 -07:00
|
|
|
package schema
|
2020-03-07 22:22:06 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-03-18 22:29:50 -07:00
|
|
|
"net"
|
|
|
|
"net/url"
|
2020-03-07 22:22:06 -08:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2020-03-18 22:29:50 -07:00
|
|
|
"time"
|
2020-03-07 22:22:06 -08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
func Example() {
|
|
|
|
type MyObject struct {
|
|
|
|
ID string `doc:"Object ID" readOnly:"true"`
|
|
|
|
Rate float64 `doc:"Rate of change" minimum:"0"`
|
|
|
|
Coords []int `doc:"X,Y coordinates" minItems:"2" maxItems:"2"`
|
|
|
|
}
|
|
|
|
|
|
|
|
generated, err := Generate(reflect.TypeOf(MyObject{}))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fmt.Println(generated.Properties["id"].ReadOnly)
|
|
|
|
// output: true
|
|
|
|
}
|
|
|
|
|
2020-03-07 22:22:06 -08:00
|
|
|
var types = []struct {
|
2020-03-18 22:29:50 -07:00
|
|
|
in interface{}
|
|
|
|
out string
|
|
|
|
format string
|
2020-03-07 22:22:06 -08:00
|
|
|
}{
|
2020-03-18 22:29:50 -07:00
|
|
|
{false, "boolean", ""},
|
2020-04-03 22:30:56 -07:00
|
|
|
{0, "integer", "int32"},
|
2020-08-30 23:58:12 -07:00
|
|
|
{int64(0), "integer", "int64"},
|
|
|
|
{uint64(0), "integer", "int64"},
|
|
|
|
{float32(0), "number", "float"},
|
2020-04-03 22:30:56 -07:00
|
|
|
{0.0, "number", "double"},
|
2020-08-30 23:58:12 -07:00
|
|
|
{F(0.0), "number", "double"},
|
2020-03-18 22:29:50 -07:00
|
|
|
{"hello", "string", ""},
|
|
|
|
{struct{}{}, "object", ""},
|
|
|
|
{[]string{"foo"}, "array", ""},
|
|
|
|
{net.IP{}, "string", "ipv4"},
|
|
|
|
{time.Time{}, "string", "date-time"},
|
|
|
|
{url.URL{}, "string", "uri"},
|
2020-09-25 15:01:41 -07:00
|
|
|
{[]byte{}, "string", ""},
|
2020-03-07 22:22:06 -08:00
|
|
|
// TODO: map
|
|
|
|
}
|
|
|
|
|
2020-03-18 22:29:50 -07:00
|
|
|
func TestSchemaTypes(outer *testing.T) {
|
2020-03-07 22:22:06 -08:00
|
|
|
outer.Parallel()
|
|
|
|
for _, tt := range types {
|
|
|
|
local := tt
|
|
|
|
outer.Run(fmt.Sprintf("%v", tt.in), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(local.in).Type())
|
2020-03-07 22:22:06 -08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, local.out, s.Type)
|
2020-03-18 22:29:50 -07:00
|
|
|
assert.Equal(t, local.format, s.Format)
|
2020-03-07 22:22:06 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 22:29:50 -07:00
|
|
|
func TestSchemaRequiredFields(t *testing.T) {
|
2020-03-07 22:22:06 -08:00
|
|
|
type Example struct {
|
|
|
|
Optional string `json:"optional,omitempty"`
|
|
|
|
Required string `json:"required"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-07 22:22:06 -08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, s.Properties, 2)
|
|
|
|
assert.NotContains(t, s.Required, "optional")
|
|
|
|
assert.Contains(t, s.Required, "required")
|
|
|
|
}
|
|
|
|
|
2020-03-18 22:29:50 -07:00
|
|
|
func TestSchemaRenameField(t *testing.T) {
|
2020-03-07 22:22:06 -08:00
|
|
|
type Example struct {
|
|
|
|
Foo string `json:"bar"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-07 22:22:06 -08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Empty(t, s.Properties["foo"])
|
|
|
|
assert.NotEmpty(t, s.Properties["bar"])
|
|
|
|
}
|
|
|
|
|
2020-03-18 22:29:50 -07:00
|
|
|
func TestSchemaDescription(t *testing.T) {
|
2020-03-07 22:22:06 -08:00
|
|
|
type Example struct {
|
|
|
|
Foo string `json:"foo" description:"I am a test"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-07 22:22:06 -08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "I am a test", s.Properties["foo"].Description)
|
|
|
|
}
|
2020-03-18 22:29:50 -07:00
|
|
|
|
|
|
|
func TestSchemaFormat(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo string `json:"foo" format:"date-time"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-18 22:29:50 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "date-time", s.Properties["foo"].Format)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaEnum(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo string `json:"foo" enum:"one,two,three"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-18 22:29:50 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, []interface{}{"one", "two", "three"}, s.Properties["foo"].Enum)
|
|
|
|
}
|
|
|
|
|
2021-02-23 10:58:52 -08:00
|
|
|
func TestSchemaArrayEnum(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo []string `json:"foo" enum:"one,two,three"`
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Array itself should not have an enum member set.
|
|
|
|
assert.Equal(t, []interface{}{}, s.Properties["foo"].Enum)
|
|
|
|
|
|
|
|
// Items in the array should be one of the allowed enum values.
|
|
|
|
assert.Equal(t, []interface{}{"one", "two", "three"}, s.Properties["foo"].Items.Enum)
|
|
|
|
}
|
|
|
|
|
2020-03-18 22:29:50 -07:00
|
|
|
func TestSchemaDefault(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo string `json:"foo" default:"def"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-18 22:29:50 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "def", s.Properties["foo"].Default)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaExample(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo string `json:"foo" example:"ex"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-18 22:29:50 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "ex", s.Properties["foo"].Example)
|
|
|
|
}
|
2020-03-20 21:27:40 -07:00
|
|
|
|
|
|
|
func TestSchemaNullable(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo string `json:"foo" nullable:"true"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-20 21:27:40 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, true, s.Properties["foo"].Nullable)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaNullableError(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo string `json:"foo" nullable:"bad"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
_, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-20 21:27:40 -07:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaReadOnly(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo string `json:"foo" readOnly:"true"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-20 21:27:40 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, true, s.Properties["foo"].ReadOnly)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaReadOnlyError(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo string `json:"foo" readOnly:"bad"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
_, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-20 21:27:40 -07:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaWriteOnly(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo string `json:"foo" writeOnly:"true"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-20 21:27:40 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, true, s.Properties["foo"].WriteOnly)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaWriteOnlyError(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo string `json:"foo" writeOnly:"bad"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
_, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-20 21:27:40 -07:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaDeprecated(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo string `json:"foo" deprecated:"true"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-20 21:27:40 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, true, s.Properties["foo"].Deprecated)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaDeprecatedError(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo string `json:"foo" deprecated:"bad"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
_, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-20 21:27:40 -07:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
2020-03-25 20:47:25 -07:00
|
|
|
|
|
|
|
func TestSchemaMinimum(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo float64 `json:"foo" minimum:"1"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1.0, *s.Properties["foo"].Minimum)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaMinimumError(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo float64 `json:"foo" minimum:"bad"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
_, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaExclusiveMinimum(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo float64 `json:"foo" exclusiveMinimum:"1"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.NoError(t, err)
|
2021-02-16 10:44:34 -08:00
|
|
|
assert.Equal(t, 1.0, *s.Properties["foo"].Minimum)
|
|
|
|
assert.Equal(t, true, *s.Properties["foo"].ExclusiveMinimum)
|
2020-03-25 20:47:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaExclusiveMinimumError(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo float64 `json:"foo" exclusiveMinimum:"bad"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
_, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaMaximum(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo float64 `json:"foo" maximum:"0"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0.0, *s.Properties["foo"].Maximum)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaMaximumError(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo float64 `json:"foo" maximum:"bad"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
_, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaExclusiveMaximum(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo float64 `json:"foo" exclusiveMaximum:"0"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.NoError(t, err)
|
2021-02-16 10:44:34 -08:00
|
|
|
assert.Equal(t, 0.0, *s.Properties["foo"].Maximum)
|
|
|
|
assert.Equal(t, true, *s.Properties["foo"].ExclusiveMaximum)
|
2020-03-25 20:47:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaExclusiveMaximumError(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo float64 `json:"foo" exclusiveMaximum:"bad"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
_, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaMultipleOf(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo float64 `json:"foo" multipleOf:"10"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 10.0, s.Properties["foo"].MultipleOf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaMultipleOfError(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo float64 `json:"foo" multipleOf:"bad"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
_, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaMinLength(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo string `json:"foo" minLength:"10"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, uint64(10), *s.Properties["foo"].MinLength)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaMinLengthError(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo string `json:"foo" minLength:"bad"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
_, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaMaxLength(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo string `json:"foo" maxLength:"10"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, uint64(10), *s.Properties["foo"].MaxLength)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaMaxLengthError(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo string `json:"foo" maxLength:"bad"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
_, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaPattern(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo string `json:"foo" pattern:"a-z+"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "a-z+", s.Properties["foo"].Pattern)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaPatternError(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo string `json:"foo" pattern:"(.*"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
_, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaMinItems(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo []string `json:"foo" minItems:"10"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, uint64(10), *s.Properties["foo"].MinItems)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaMinItemsError(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo []string `json:"foo" minItems:"bad"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
_, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaMaxItems(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo []string `json:"foo" maxItems:"10"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, uint64(10), *s.Properties["foo"].MaxItems)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaMaxItemsError(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo []string `json:"foo" maxItems:"bad"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
_, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaUniqueItems(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo []string `json:"foo" uniqueItems:"true"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, true, s.Properties["foo"].UniqueItems)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaUniqueItemsError(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo []string `json:"foo" uniqueItems:"bad"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
_, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaMinProperties(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo []string `json:"foo" minProperties:"10"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, uint64(10), *s.Properties["foo"].MinProperties)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaMinPropertiesError(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo []string `json:"foo" minProperties:"bad"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
_, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaMaxProperties(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo []string `json:"foo" maxProperties:"10"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, uint64(10), *s.Properties["foo"].MaxProperties)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaMaxPropertiesError(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo []string `json:"foo" maxProperties:"bad"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
_, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaMap(t *testing.T) {
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.TypeOf(map[string]string{}))
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, &Schema{
|
|
|
|
Type: "object",
|
|
|
|
AdditionalProperties: &Schema{
|
|
|
|
Type: "string",
|
|
|
|
},
|
|
|
|
}, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaSlice(t *testing.T) {
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.TypeOf([]string{}))
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, &Schema{
|
|
|
|
Type: "array",
|
|
|
|
Items: &Schema{
|
|
|
|
Type: "string",
|
|
|
|
},
|
|
|
|
}, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaUnsigned(t *testing.T) {
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.TypeOf(uint(10)))
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
min := 0.0
|
|
|
|
assert.Equal(t, &Schema{
|
|
|
|
Type: "integer",
|
2020-04-03 22:30:56 -07:00
|
|
|
Format: "int32",
|
2020-03-25 20:47:25 -07:00
|
|
|
Minimum: &min,
|
|
|
|
}, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaNonStringExample(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo uint32 `json:"foo" example:"10"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, uint32(10), s.Properties["foo"].Example)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaNonStringExampleErrorUnmarshal(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo uint32 `json:"foo" example:"bad"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
_, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemaNonStringExampleErrorCast(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo bool `json:"foo" example:"1"`
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:40 -07:00
|
|
|
_, err := Generate(reflect.ValueOf(Example{}).Type())
|
2020-03-25 20:47:25 -07:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
2020-03-28 22:44:41 -07:00
|
|
|
|
2020-04-20 21:55:44 -07:00
|
|
|
func TestSchemaFieldFilteredOut(t *testing.T) {
|
|
|
|
type Example struct {
|
|
|
|
Foo bool `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := Generate(reflect.ValueOf(Example{}).Type())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, len(s.Properties))
|
|
|
|
}
|
|
|
|
|
2020-03-28 22:44:41 -07:00
|
|
|
func TestPointerHelpers(t *testing.T) {
|
|
|
|
// Just confirm this compiles.
|
|
|
|
_ = Schema{
|
|
|
|
Minimum: F(98.6),
|
|
|
|
MinLength: I(5),
|
|
|
|
}
|
|
|
|
}
|
2020-04-26 22:18:37 -07:00
|
|
|
|
|
|
|
func TestHasValidation(t *testing.T) {
|
|
|
|
s := Schema{
|
|
|
|
Type: "string",
|
|
|
|
}
|
|
|
|
assert.Equal(t, false, s.HasValidation())
|
|
|
|
|
|
|
|
s.Pattern = "^[a-z]+$"
|
|
|
|
assert.Equal(t, true, s.HasValidation())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveProperty(t *testing.T) {
|
|
|
|
s := Schema{
|
|
|
|
Type: "object",
|
|
|
|
Properties: map[string]*Schema{
|
|
|
|
"foo": {Type: "string"},
|
|
|
|
"bar": {Type: "number"},
|
|
|
|
},
|
|
|
|
Required: []string{"foo", "bar"},
|
|
|
|
}
|
|
|
|
|
|
|
|
s.RemoveProperty("foo")
|
|
|
|
|
|
|
|
assert.Nil(t, s.Properties["foo"])
|
|
|
|
assert.NotContains(t, "foo", s.Required)
|
|
|
|
}
|
2020-05-02 21:16:28 -07:00
|
|
|
|
|
|
|
func TestEmbedded(t *testing.T) {
|
|
|
|
type Foo struct {
|
|
|
|
A string `json:"a"`
|
|
|
|
B string `json:"b"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Bar struct {
|
|
|
|
*Foo
|
|
|
|
B int `json:"b"`
|
|
|
|
C string `json:"c"`
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := Generate(reflect.TypeOf(Bar{}))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, s.Properties, 3)
|
|
|
|
assert.Equal(t, "integer", s.Properties["b"].Type)
|
|
|
|
}
|
2020-11-19 16:43:16 -08:00
|
|
|
|
|
|
|
func TestStringArrayExample(t *testing.T) {
|
|
|
|
type Foo struct {
|
|
|
|
A []string `json:"a" example:"[\"a\",\"b\",\"c\"]"`
|
|
|
|
B []string `json:"b" example:"a, b, c"`
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := Generate(reflect.TypeOf(Foo{}))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, s.Properties["a"].Example, []string{"a", "b", "c"})
|
|
|
|
assert.Equal(t, s.Properties["b"].Example, []string{"a", "b", "c"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExampleBadJSON(t *testing.T) {
|
|
|
|
type Foo struct {
|
|
|
|
A []string `json:"a" example:"[\"a\", 1]"`
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := Generate(reflect.TypeOf(Foo{}))
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|