Esempio n. 1
0
func TestSchemaAnalysis_SimpleSchema(t *testing.T) {
	for i, v := range append(knownSchemas, spec.ArrayProperty(nil), spec.MapProperty(nil)) {
		sch, err := Schema(SchemaOpts{Schema: v})
		if assert.NoError(t, err, "failed to analyze schema at %d: %v", i, err) {
			assert.True(t, sch.IsSimpleSchema, "item at %d should be a simple schema", i)
		}

		asch, err := Schema(SchemaOpts{Schema: spec.ArrayProperty(v)})
		if assert.NoError(t, err, "failed to analyze array schema at %d: %v", i, err) {
			assert.True(t, asch.IsSimpleSchema, "array item at %d should be a simple schema", i)
		}

		msch, err := Schema(SchemaOpts{Schema: spec.MapProperty(v)})
		if assert.NoError(t, err, "failed to analyze map schema at %d: %v", i, err) {
			assert.True(t, msch.IsSimpleSchema, "map item at %d should be a simple schema", i)
		}
	}

	for i, v := range complexSchemas {
		sch, err := Schema(SchemaOpts{Schema: v})
		if assert.NoError(t, err, "failed to analyze schema at %d: %v", i, err) {
			assert.False(t, sch.IsSimpleSchema, "item at %d should not be a simple schema", i)
		}
	}

}
Esempio n. 2
0
func TestSchemaAnalysis_Map(t *testing.T) {
	for i, v := range append(knownSchemas, spec.MapProperty(nil)) {
		sch, err := Schema(SchemaOpts{Schema: spec.MapProperty(v)})
		if assert.NoError(t, err, "failed to analyze schema at %d: %v", i, err) {
			assert.True(t, sch.IsMap, "item at %d should be a map type", i)
			assert.True(t, sch.IsSimpleMap, "item at %d should be a simple map type", i)
		}
	}

	for i, v := range complexSchemas {
		sch, err := Schema(SchemaOpts{Schema: spec.MapProperty(v)})
		if assert.NoError(t, err, "failed to analyze schema at %d: %v", i, err) {
			assert.True(t, sch.IsMap, "item at %d should be a map type", i)
			assert.False(t, sch.IsSimpleMap, "item at %d should not be a simple map type", i)
		}
	}
}
Esempio n. 3
0
func TestSchemaAnalysis_ExtendedObject(t *testing.T) {
	for i, v := range knownSchemas {
		wex := spec.MapProperty(v).SetProperty("name", *spec.StringProperty())
		sch, err := Schema(SchemaOpts{Schema: wex})
		if assert.NoError(t, err, "failed to analyze schema at %d: %v", i, err) {
			assert.True(t, sch.IsExtendedObject, "item at %d should be an extended map object type", i)
			assert.False(t, sch.IsMap, "item at %d should not be a map type", i)
			assert.False(t, sch.IsSimpleMap, "item at %d should not be a simple map type", i)
		}
	}
}
Esempio n. 4
0
	(&spec.Schema{}),                     // 10
	(&spec.Schema{}).Typed("object", ""), // 11
	(&spec.Schema{}).Typed("", ""),       // 12
	(&spec.Schema{}).Typed("", "uuid"),   // 13
}

func newCObj() *spec.Schema {
	return (&spec.Schema{}).Typed("object", "").SetProperty("id", *spec.Int64Property())
}

var complexObject = newCObj()

var complexSchemas = []*spec.Schema{
	complexObject,
	spec.ArrayProperty(complexObject),
	spec.MapProperty(complexObject),
}

func knownRefs(base string) []spec.Ref {
	urls := []string{"bool", "string", "integer", "float", "date", "object", "format"}

	var result []spec.Ref
	for _, u := range urls {
		result = append(result, spec.MustCreateRef(fmt.Sprintf("%s/%s", base, path.Join("known", u))))
	}
	return result
}

func complexRefs(base string) []spec.Ref {
	urls := []string{"object", "array", "map"}
Esempio n. 5
0
func assertBuiltinAdditionalPropertiesElem(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 := "map[string]" + val.Expected
	if val.Nullable {
		sliceType = "map[string]*" + val.Expected
	}
	val.Expected = sliceType

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

	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.MapProperty(items)

	rt, err := resolver.ResolveSchema(sch, !aliased, val.Required)
	if assert.NoError(t, err) {
		// pretty.Println(rt)
		if val.Nullable {
			if !assert.True(t, rt.ElemType.IsNullable, "expected nullable for item at: %d", i) {
				// fmt.Println("isRequired:", val.Required)
				// pretty.Println(sch)
				return false
			}
		} else {
			if !assert.False(t, rt.ElemType != nil && rt.ElemType.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 at %d", val.Type, val.Format, i) {
			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 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
}