Example #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)
		}
	}

}
Example #2
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
}
Example #3
0
func TestSchemaAnalysis_Tuple(t *testing.T) {
	at := spec.ArrayProperty(nil)
	at.Items = &spec.SchemaOrArray{}
	at.Items.Schemas = append(at.Items.Schemas, *spec.StringProperty(), *spec.Int64Property())

	sch, err := Schema(SchemaOpts{Schema: at})
	if assert.NoError(t, err) {
		assert.True(t, sch.IsTuple)
		assert.False(t, sch.IsTupleWithExtra)
		assert.False(t, sch.IsKnownType)
		assert.False(t, sch.IsSimpleSchema)
	}
}
Example #4
0
func TestSchemaAnalysis_Array(t *testing.T) {
	for i, v := range append(knownSchemas, (&spec.Schema{}).Typed("array", "")) {
		sch, err := Schema(SchemaOpts{Schema: spec.ArrayProperty(v)})
		if assert.NoError(t, err, "failed to analyze schema at %d: %v", i, err) {
			assert.True(t, sch.IsArray, "item at %d should be an array type", i)
			assert.True(t, sch.IsSimpleArray, "item at %d should be a simple array type", i)
		}
	}

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

	serv := refServer()
	defer serv.Close()

	for i, ref := range knownRefs(serv.URL) {
		sch, err := Schema(SchemaOpts{Schema: spec.ArrayProperty(refSchema(ref))})
		if assert.NoError(t, err, "failed to analyze schema at %d: %v", i, err) {
			assert.True(t, sch.IsArray, "item at %d should be an array type", i)
			assert.True(t, sch.IsSimpleArray, "item at %d should be a simple array type", i)
		}
	}
	for i, ref := range complexRefs(serv.URL) {
		sch, err := Schema(SchemaOpts{Schema: spec.ArrayProperty(refSchema(ref))})
		if assert.NoError(t, err, "failed to analyze schema at %d: %v", i, err) {
			assert.False(t, sch.IsKnownType, "item at %d should not be a known type", i)
			assert.True(t, sch.IsArray, "item at %d should be an array type", i)
			assert.False(t, sch.IsSimpleArray, "item at %d should not be a simple array type", i)
		}
	}

}
Example #5
0
	spec.DateTimeProperty(),              // 9
	(&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"}
Example #6
0
func TestValidateItems(t *testing.T) {
	doc, _ := loads.Analyzed(PetStoreJSONMessage, "")
	validator := NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default)
	validator.spec = doc
	validator.analyzer = analysis.New(doc.Spec())
	res := validator.validateItems()
	assert.Empty(t, res.Errors)

	// in operation parameters
	sw := doc.Spec()
	sw.Paths.Paths["/pets"].Get.Parameters[0].Type = "array"
	res = validator.validateItems()
	assert.NotEmpty(t, res.Errors)

	sw.Paths.Paths["/pets"].Get.Parameters[0].Items = spec.NewItems().Typed("string", "")
	res = validator.validateItems()
	assert.Empty(t, res.Errors)

	sw.Paths.Paths["/pets"].Get.Parameters[0].Items = spec.NewItems().Typed("array", "")
	res = validator.validateItems()
	assert.NotEmpty(t, res.Errors)

	sw.Paths.Paths["/pets"].Get.Parameters[0].Items.Items = spec.NewItems().Typed("string", "")
	res = validator.validateItems()
	assert.Empty(t, res.Errors)

	// in global parameters
	sw.Parameters = make(map[string]spec.Parameter)
	sw.Parameters["other"] = *spec.SimpleArrayParam("other", "array", "csv")
	res = validator.validateItems()
	assert.Empty(t, res.Errors)

	//pp := spec.SimpleArrayParam("other", "array", "")
	//pp.Items = nil
	//sw.Parameters["other"] = *pp
	//res = validator.validateItems()
	//assert.NotEmpty(t, res.Errors)

	// in shared path object parameters
	doc, _ = loads.Analyzed(PetStoreJSONMessage, "")
	validator = NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default)
	validator.spec = doc
	validator.analyzer = analysis.New(doc.Spec())
	sw = doc.Spec()

	pa := sw.Paths.Paths["/pets"]
	pa.Parameters = []spec.Parameter{*spec.SimpleArrayParam("another", "array", "csv")}
	sw.Paths.Paths["/pets"] = pa
	res = validator.validateItems()
	assert.Empty(t, res.Errors)

	pa = sw.Paths.Paths["/pets"]
	pp := spec.SimpleArrayParam("other", "array", "")
	pp.Items = nil
	pa.Parameters = []spec.Parameter{*pp}
	sw.Paths.Paths["/pets"] = pa
	res = validator.validateItems()
	assert.NotEmpty(t, res.Errors)

	// in body param schema
	doc, _ = loads.Analyzed(PetStoreJSONMessage, "")
	validator = NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default)
	validator.spec = doc
	validator.analyzer = analysis.New(doc.Spec())
	sw = doc.Spec()
	pa = sw.Paths.Paths["/pets"]
	pa.Post.Parameters[0].Schema = spec.ArrayProperty(nil)
	res = validator.validateItems()
	assert.NotEmpty(t, res.Errors)

	// in response headers
	doc, _ = loads.Analyzed(PetStoreJSONMessage, "")
	validator = NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default)
	validator.spec = doc
	validator.analyzer = analysis.New(doc.Spec())
	sw = doc.Spec()
	pa = sw.Paths.Paths["/pets"]
	rp := pa.Post.Responses.StatusCodeResponses[200]
	var hdr spec.Header
	hdr.Type = "array"
	rp.Headers = make(map[string]spec.Header)
	rp.Headers["X-YADA"] = hdr
	pa.Post.Responses.StatusCodeResponses[200] = rp
	res = validator.validateItems()
	assert.NotEmpty(t, res.Errors)

	// in response schema
	doc, _ = loads.Analyzed(PetStoreJSONMessage, "")
	validator = NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default)
	validator.spec = doc
	validator.analyzer = analysis.New(doc.Spec())
	sw = doc.Spec()
	pa = sw.Paths.Paths["/pets"]
	rp = pa.Post.Responses.StatusCodeResponses[200]
	rp.Schema = spec.ArrayProperty(nil)
	pa.Post.Responses.StatusCodeResponses[200] = rp
	res = validator.validateItems()
	assert.NotEmpty(t, res.Errors)
}