Beispiel #1
0
func assertBuiltinSliceElem(t testing.TB, resolver *typeResolver, aliased bool, i int, val builtinVal) bool {
	val.Nullable = false
	if nullableExtension(val.Extensions) != nil {
		val.Nullable = *nullableExtension(val.Extensions)
	}
	sliceType := "[]" + val.Expected
	if val.Nullable {
		sliceType = "[]*" + val.Expected
	}
	val.Expected = sliceType

	val.Aliased = aliased
	if aliased {
		val.AliasedType = val.Expected
		val.Expected = "models.MyAliasedThing"
	}

	// fmt.Println("nullable:", val.Nullable)
	items := new(spec.Schema)
	items.Typed(val.Type, val.Format)
	items.Default = val.Default
	items.ReadOnly = val.ReadOnly
	items.Extensions = val.Extensions
	items.Minimum = val.Minimum
	items.Maximum = val.Maximum
	items.MultipleOf = val.MultipleOf
	items.MinLength = val.MinLength
	items.MaxLength = val.MaxLength

	sch := spec.ArrayProperty(items)

	rt, err := resolver.ResolveSchema(sch, !aliased, val.Required)
	if assert.NoError(t, err) {

		if val.Nullable {
			if !assert.True(t, rt.ElemType.IsNullable, "expected nullable for item at: %d", i) {
				return false
			}
		} else {
			if !assert.False(t, rt.ElemType != nil && rt.ElemType.IsNullable, "expected not nullable for item at: %d", i) {
				return false
			}
		}

		if val.Aliased {
			if !assert.Equal(t, val.Aliased, rt.IsAliased, "expected (%q, %q) to be an aliased type at: %d", val.Type, val.Format, i) {
				return false
			}
			if !assert.Equal(t, val.AliasedType, rt.AliasedType, "expected %q (%q, %q) to be aliased as %q, but got %q at %d", val.Expected, val.Type, val.Format, val.AliasedType, rt.AliasedType, i) {
				return false
			}
		}

		if !assertBuiltinSliceElemnResolve(t, val.Type, val.Format, val.Expected, rt, i) {
			return false
		}
	}
	return true
}
Beispiel #2
0
func assertBuiltinVal(t testing.TB, resolver *typeResolver, aliased bool, i int, val builtinVal) bool {
	val.Aliased = aliased
	if aliased {
		val.AliasedType = val.Expected
		val.Expected = "models.MyAliasedThing"
	}

	sch := new(spec.Schema)
	sch.Typed(val.Type, val.Format)
	sch.Default = val.Default
	sch.ReadOnly = val.ReadOnly
	sch.Extensions = val.Extensions
	sch.Minimum = val.Minimum
	sch.Maximum = val.Maximum
	sch.MultipleOf = val.MultipleOf
	sch.MinLength = val.MinLength
	sch.MaxLength = val.MaxLength

	rt, err := resolver.ResolveSchema(sch, !aliased, val.Required)
	if assert.NoError(t, err) {
		if val.Nullable {
			if !assert.True(t, rt.IsNullable, "expected nullable for item at: %d", i) {
				// fmt.Println("isRequired:", val.Required)
				// pretty.Println(sch)
				return false
			}
		} else {
			if !assert.False(t, rt.IsNullable, "expected not nullable for item at: %d", i) {
				// fmt.Println("isRequired:", val.Required)
				// pretty.Println(sch)
				return false
			}
		}
		if !assert.Equal(t, val.Aliased, rt.IsAliased, "expected (%q, %q) to be an aliased type", val.Type, val.Format) {
			return false
		}
		if val.Aliased {
			if !assert.Equal(t, val.AliasedType, rt.AliasedType, "expected %q (%q, %q) to be aliased as %q, but got %q", val.Expected, val.Type, val.Format, val.AliasedType, rt.AliasedType) {
				return false
			}
		}
		if !assertBuiltinResolve(t, val.Type, val.Format, val.Expected, rt, i) {
			return false
		}
	}
	return true
}