Esempio n. 1
0
func verifyParsedPetStore(t testing.TB, doc *spec.Swagger) {
	assert.EqualValues(t, []string{"application/json"}, doc.Consumes)
	assert.EqualValues(t, []string{"application/json"}, doc.Produces)
	assert.EqualValues(t, []string{"http", "https"}, doc.Schemes)
	assert.Equal(t, "localhost", doc.Host)
	assert.Equal(t, "/v2", doc.BasePath)

	verifyInfo(t, doc.Info)

	if assert.NotNil(t, doc.Paths) {
		assert.Len(t, doc.Paths.Paths, 4)
	}
	assert.Len(t, doc.Definitions, 3)
	assert.Len(t, doc.Responses, 2)

	definitions := doc.Definitions
	mod, ok := definitions["tag"]
	assert.True(t, ok)
	assert.Equal(t, spec.StringOrArray([]string{"object"}), mod.Type)
	assert.Equal(t, "A Tag is an extra piece of data to provide more information about a pet.", mod.Title)
	assert.Equal(t, "It is used to describe the animals available in the store.", mod.Description)
	assert.Len(t, mod.Required, 2)

	assertProperty(t, &mod, "number", "id", "int64", "ID")
	prop, ok := mod.Properties["id"]
	assert.True(t, ok, "should have had an 'id' property")
	assert.Equal(t, "The id of the tag.", prop.Description)

	assertProperty(t, &mod, "string", "value", "", "Value")
	prop, ok = mod.Properties["value"]
	assert.Equal(t, "The value of the tag.", prop.Description)

	mod, ok = definitions["pet"]
	assert.True(t, ok)
	assert.Equal(t, spec.StringOrArray([]string{"object"}), mod.Type)
	assert.Equal(t, "A Pet is the main product in the store.", mod.Title)
	assert.Equal(t, "It is used to describe the animals available in the store.", mod.Description)
	assert.Len(t, mod.Required, 2)

	assertProperty(t, &mod, "number", "id", "int64", "ID")
	prop, ok = mod.Properties["id"]
	assert.True(t, ok, "should have had an 'id' property")
	assert.Equal(t, "The id of the pet.", prop.Description)

	assertProperty(t, &mod, "string", "name", "", "Name")
	prop, ok = mod.Properties["name"]
	assert.Equal(t, "The name of the pet.", prop.Description)
	assert.EqualValues(t, 3, *prop.MinLength)
	assert.EqualValues(t, 50, *prop.MaxLength)
	assert.Equal(t, "\\w[\\w-]+", prop.Pattern)

	assertArrayProperty(t, &mod, "string", "photoUrls", "", "PhotoURLs")
	prop, ok = mod.Properties["photoUrls"]
	assert.Equal(t, "The photo urls for the pet.\nThis only accepts jpeg or png images.", prop.Description)
	if assert.NotNil(t, prop.Items) && assert.NotNil(t, prop.Items.Schema) {
		assert.Equal(t, "\\.(jpe?g|png)$", prop.Items.Schema.Pattern)
	}

	assertProperty(t, &mod, "string", "status", "", "Status")
	prop, ok = mod.Properties["status"]
	assert.Equal(t, "The current status of the pet in the store.", prop.Description)

	assertArrayRef(t, &mod, "tags", "Tags", "#/definitions/tag")
	prop, ok = mod.Properties["tags"]
	assert.True(t, ok)
	assert.Equal(t, "Extra bits of information attached to this pet.", prop.Description)

	mod, ok = definitions["order"]
	assert.True(t, ok)
	assert.Len(t, mod.Properties, 4)
	assert.Len(t, mod.Required, 3)

	assertProperty(t, &mod, "number", "id", "int64", "ID")
	prop, ok = mod.Properties["id"]
	assert.True(t, ok, "should have had an 'id' property")
	assert.Equal(t, "the ID of the order", prop.Description)

	assertProperty(t, &mod, "number", "userId", "int64", "UserID")
	prop, ok = mod.Properties["userId"]
	assert.True(t, ok, "should have had an 'userId' property")
	assert.Equal(t, "the id of the user who placed the order.", prop.Description)

	assertProperty(t, &mod, "string", "orderedAt", "date-time", "OrderedAt")
	prop, ok = mod.Properties["orderedAt"]
	assert.Equal(t, "the time at which this order was made.", prop.Description)
	assert.True(t, ok, "should have an 'orderedAt' property")

	assertArrayProperty(t, &mod, "object", "items", "", "Items")
	prop, ok = mod.Properties["items"]
	assert.True(t, ok, "should have an 'items' slice")
	assert.NotNil(t, prop.Items, "items should have had an items property")
	assert.NotNil(t, prop.Items.Schema, "items.items should have had a schema property")

	itprop := prop.Items.Schema
	assert.Len(t, itprop.Properties, 2)
	assert.Len(t, itprop.Required, 2)

	assertProperty(t, itprop, "number", "petId", "int64", "PetID")
	iprop, ok := itprop.Properties["petId"]
	assert.True(t, ok, "should have had a 'petId' property")
	assert.Equal(t, "the id of the pet to order", iprop.Description)

	assertProperty(t, itprop, "number", "qty", "int32", "Quantity")
	iprop, ok = itprop.Properties["qty"]
	assert.True(t, ok, "should have had a 'qty' property")
	assert.Equal(t, "the quantity of this pet to order", iprop.Description)
	assert.EqualValues(t, 1, *iprop.Minimum)

	// responses
	resp, ok := doc.Responses["genericError"]
	assert.True(t, ok)
	assert.NotNil(t, resp.Schema)
	assert.Len(t, resp.Schema.Properties, 2)
	assertProperty(t, resp.Schema, "number", "code", "int32", "Code")
	assertProperty(t, resp.Schema, "string", "message", "", "Message")

	resp, ok = doc.Responses["validationError"]
	assert.True(t, ok)
	assert.NotNil(t, resp.Schema)
	assert.Len(t, resp.Schema.Properties, 3)
	assertProperty(t, resp.Schema, "number", "code", "int32", "Code")
	assertProperty(t, resp.Schema, "string", "message", "", "Message")
	assertProperty(t, resp.Schema, "string", "field", "", "Field")

	paths := doc.Paths.Paths

	// path /pets
	op, ok := paths["/pets"]
	assert.True(t, ok)
	assert.NotNil(t, op)

	// listPets
	assert.NotNil(t, op.Get)
	assert.Equal(t, "Lists the pets known to the store.", op.Get.Summary)
	assert.Equal(t, "By default it will only lists pets that are available for sale.\nThis can be changed with the status flag.", op.Get.Description)
	assert.Equal(t, "listPets", op.Get.ID)
	assert.EqualValues(t, []string{"pets"}, op.Get.Tags)
	sparam := op.Get.Parameters[0]
	assert.Equal(t, "", sparam.Description)
	assert.Equal(t, "query", sparam.In)
	assert.Equal(t, "string", sparam.Type)
	assert.Equal(t, "", sparam.Format)
	assert.False(t, sparam.Required)
	assert.Equal(t, "Status", sparam.Extensions["x-go-name"])
	assert.Equal(t, "#/responses/genericError", op.Get.Responses.Default.Ref.String())
	rs, ok := op.Get.Responses.StatusCodeResponses[200]
	assert.True(t, ok)
	assert.NotNil(t, rs.Schema)
	aprop := rs.Schema
	assert.Equal(t, "array", aprop.Type[0])
	assert.NotNil(t, aprop.Items)
	assert.NotNil(t, aprop.Items.Schema)
	assert.Equal(t, "#/definitions/pet", aprop.Items.Schema.Ref.String())

	// createPet
	assert.NotNil(t, op.Post)
	assert.Equal(t, "Creates a new pet in the store.", op.Post.Summary)
	assert.Equal(t, "", op.Post.Description)
	assert.Equal(t, "createPet", op.Post.ID)
	assert.EqualValues(t, []string{"pets"}, op.Post.Tags)
	verifyRefParam(t, op.Post.Parameters[0], "The pet to submit.", "pet")
	assert.Equal(t, "#/responses/genericError", op.Post.Responses.Default.Ref.String())
	rs, ok = op.Post.Responses.StatusCodeResponses[200]
	assert.True(t, ok)
	assert.NotNil(t, rs.Schema)
	aprop = rs.Schema
	assert.Equal(t, "#/definitions/pet", aprop.Ref.String())

	// path /pets/{id}
	op, ok = paths["/pets/{id}"]
	assert.True(t, ok)
	assert.NotNil(t, op)

	// getPetById
	assert.NotNil(t, op.Get)
	assert.Equal(t, "Gets the details for a pet.", op.Get.Summary)
	assert.Equal(t, "", op.Get.Description)
	assert.Equal(t, "getPetById", op.Get.ID)
	assert.EqualValues(t, []string{"pets"}, op.Get.Tags)
	verifyIDParam(t, op.Get.Parameters[0], "The ID of the pet")
	assert.Equal(t, "#/responses/genericError", op.Get.Responses.Default.Ref.String())
	rs, ok = op.Get.Responses.StatusCodeResponses[200]
	assert.True(t, ok)
	assert.NotNil(t, rs.Schema)
	aprop = rs.Schema
	assert.Equal(t, "#/definitions/pet", aprop.Ref.String())

	// updatePet
	assert.NotNil(t, op.Put)
	assert.Equal(t, "Updates the details for a pet.", op.Put.Summary)
	assert.Equal(t, "", op.Put.Description)
	assert.Equal(t, "updatePet", op.Put.ID)
	assert.EqualValues(t, []string{"pets"}, op.Put.Tags)
	verifyIDParam(t, op.Put.Parameters[0], "The ID of the pet")
	verifyRefParam(t, op.Put.Parameters[1], "The pet to submit.", "pet")
	assert.Equal(t, "#/responses/genericError", op.Put.Responses.Default.Ref.String())
	rs, ok = op.Put.Responses.StatusCodeResponses[200]
	assert.True(t, ok)
	assert.NotNil(t, rs.Schema)
	aprop = rs.Schema
	assert.Equal(t, "#/definitions/pet", aprop.Ref.String())

	// deletePet
	assert.NotNil(t, op.Delete)
	assert.Equal(t, "Deletes a pet from the store.", op.Delete.Summary)
	assert.Equal(t, "", op.Delete.Description)
	assert.Equal(t, "deletePet", op.Delete.ID)
	assert.EqualValues(t, []string{"pets"}, op.Delete.Tags)
	verifyIDParam(t, op.Delete.Parameters[0], "The ID of the pet")
	assert.Equal(t, "#/responses/genericError", op.Delete.Responses.Default.Ref.String())
	_, ok = op.Delete.Responses.StatusCodeResponses[204]
	assert.True(t, ok)

	// path /orders/{id}
	op, ok = paths["/orders/{id}"]
	assert.True(t, ok)
	assert.NotNil(t, op)

	// getOrderDetails
	assert.NotNil(t, op.Get)
	assert.Equal(t, "Gets the details for an order.", op.Get.Summary)
	assert.Equal(t, "", op.Get.Description)
	assert.Equal(t, "getOrderDetails", op.Get.ID)
	assert.EqualValues(t, []string{"orders"}, op.Get.Tags)
	verifyIDParam(t, op.Get.Parameters[0], "The ID of the order")
	assert.Equal(t, "#/responses/genericError", op.Get.Responses.Default.Ref.String())
	rs, ok = op.Get.Responses.StatusCodeResponses[200]
	assert.True(t, ok)
	assert.NotNil(t, rs.Schema)
	aprop = rs.Schema
	assert.Equal(t, "#/definitions/order", aprop.Ref.String())

	// cancelOrder
	assert.NotNil(t, op.Delete)
	assert.Equal(t, "Deletes an order.", op.Delete.Summary)
	assert.Equal(t, "", op.Delete.Description)
	assert.Equal(t, "cancelOrder", op.Delete.ID)
	assert.EqualValues(t, []string{"orders"}, op.Delete.Tags)
	verifyIDParam(t, op.Delete.Parameters[0], "The ID of the order")
	assert.Equal(t, "#/responses/genericError", op.Delete.Responses.Default.Ref.String())
	_, ok = op.Delete.Responses.StatusCodeResponses[204]
	assert.True(t, ok)

	// updateOrder
	assert.NotNil(t, op.Put)
	assert.Equal(t, "Updates an order.", op.Put.Summary)
	assert.Equal(t, "", op.Put.Description)
	assert.Equal(t, "updateOrder", op.Put.ID)
	assert.EqualValues(t, []string{"orders"}, op.Put.Tags)
	verifyIDParam(t, op.Put.Parameters[0], "The ID of the order")
	verifyRefParam(t, op.Put.Parameters[1], "The order to submit", "order")
	assert.Equal(t, "#/responses/genericError", op.Put.Responses.Default.Ref.String())
	rs, ok = op.Put.Responses.StatusCodeResponses[200]
	assert.True(t, ok)
	assert.NotNil(t, rs.Schema)
	aprop = rs.Schema
	assert.Equal(t, "#/definitions/order", aprop.Ref.String())

	// path /orders
	op, ok = paths["/orders"]
	assert.True(t, ok)
	assert.NotNil(t, op)

	// createOrder
	assert.NotNil(t, op.Post)
	assert.Equal(t, "Creates an order.", op.Post.Summary)
	assert.Equal(t, "", op.Post.Description)
	assert.Equal(t, "createOrder", op.Post.ID)
	assert.EqualValues(t, []string{"orders"}, op.Post.Tags)
	verifyRefParam(t, op.Post.Parameters[0], "The order to submit", "order")
	assert.Equal(t, "#/responses/genericError", op.Post.Responses.Default.Ref.String())
	rs, ok = op.Post.Responses.StatusCodeResponses[200]
	assert.True(t, ok)
	assert.NotNil(t, rs.Schema)
	aprop = rs.Schema
	assert.Equal(t, "#/definitions/order", aprop.Ref.String())
}
Esempio n. 2
0
func TestSchemaParser(t *testing.T) {
	_ = classificationProg
	schema := noModelDefs["NoModel"]

	assert.Equal(t, spec.StringOrArray([]string{"object"}), schema.Type)
	assert.Equal(t, "NoModel is a struct without an annotation.", schema.Title)
	assert.Equal(t, "NoModel exists in a package\nbut is not annotated with the swagger model annotations\nso it should now show up in a test.", schema.Description)
	assert.Len(t, schema.Required, 3)

	assertProperty(t, &schema, "number", "id", "int64", "ID")
	prop, ok := schema.Properties["id"]
	assert.Equal(t, "ID of this no model instance.\nids in this application start at 11 and are smaller than 1000", prop.Description)
	assert.True(t, ok, "should have had an 'id' property")
	assert.EqualValues(t, 1000, *prop.Maximum)
	assert.True(t, prop.ExclusiveMaximum, "'id' should have had an exclusive maximum")
	assert.NotNil(t, prop.Minimum)
	assert.EqualValues(t, 10, *prop.Minimum)
	assert.True(t, prop.ExclusiveMinimum, "'id' should have had an exclusive minimum")

	assertProperty(t, &schema, "number", "score", "int32", "Score")
	prop, ok = schema.Properties["score"]
	assert.Equal(t, "The Score of this model", prop.Description)
	assert.True(t, ok, "should have had a 'score' property")
	assert.EqualValues(t, 45, *prop.Maximum)
	assert.False(t, prop.ExclusiveMaximum, "'score' should not have had an exclusive maximum")
	assert.NotNil(t, prop.Minimum)
	assert.EqualValues(t, 3, *prop.Minimum)
	assert.False(t, prop.ExclusiveMinimum, "'score' should not have had an exclusive minimum")

	assertProperty(t, &schema, "string", "name", "", "Name")
	prop, ok = schema.Properties["name"]
	assert.Equal(t, "Name of this no model instance", prop.Description)
	assert.EqualValues(t, 4, *prop.MinLength)
	assert.EqualValues(t, 50, *prop.MaxLength)
	assert.Equal(t, "[A-Za-z0-9-.]*", prop.Pattern)

	assertProperty(t, &schema, "string", "created", "date-time", "Created")
	prop, ok = schema.Properties["created"]
	assert.Equal(t, "Created holds the time when this entry was created", prop.Description)
	assert.True(t, ok, "should have a 'created' property")
	assert.True(t, prop.ReadOnly, "'created' should be read only")

	assertArrayProperty(t, &schema, "string", "foo_slice", "", "FooSlice")
	prop, ok = schema.Properties["foo_slice"]
	assert.Equal(t, "a FooSlice has foos which are strings", prop.Description)
	assert.True(t, ok, "should have a 'foo_slice' property")
	assert.NotNil(t, prop.Items, "foo_slice should have had an items property")
	assert.NotNil(t, prop.Items.Schema, "foo_slice.items should have had a schema property")
	assert.True(t, prop.UniqueItems, "'foo_slice' should have unique items")
	assert.EqualValues(t, 3, *prop.MinItems, "'foo_slice' should have had 3 min items")
	assert.EqualValues(t, 10, *prop.MaxItems, "'foo_slice' should have had 10 max items")
	itprop := prop.Items.Schema
	assert.EqualValues(t, 3, *itprop.MinLength, "'foo_slice.items.minLength' should have been 3")
	assert.EqualValues(t, 10, *itprop.MaxLength, "'foo_slice.items.maxLength' should have been 10")
	assert.EqualValues(t, "\\w+", itprop.Pattern, "'foo_slice.items.pattern' should have \\w+")

	assertArrayProperty(t, &schema, "object", "items", "", "Items")
	prop, ok = schema.Properties["items"]
	assert.True(t, ok, "should have an 'items' slice")
	assert.NotNil(t, prop.Items, "items should have had an items property")
	assert.NotNil(t, prop.Items.Schema, "items.items should have had a schema property")
	itprop = prop.Items.Schema
	assert.Len(t, itprop.Properties, 4)
	assert.Len(t, itprop.Required, 3)
	assertProperty(t, itprop, "number", "id", "int32", "ID")
	iprop, ok := itprop.Properties["id"]
	assert.True(t, ok)
	assert.Equal(t, "ID of this no model instance.\nids in this application start at 11 and are smaller than 1000", iprop.Description)
	assert.EqualValues(t, 1000, *iprop.Maximum)
	assert.True(t, iprop.ExclusiveMaximum, "'id' should have had an exclusive maximum")
	assert.NotNil(t, iprop.Minimum)
	assert.EqualValues(t, 10, *iprop.Minimum)
	assert.True(t, iprop.ExclusiveMinimum, "'id' should have had an exclusive minimum")

	assertRef(t, itprop, "pet", "Pet", "#/definitions/pet")
	iprop, ok = itprop.Properties["pet"]
	assert.True(t, ok)
	assert.Equal(t, "The Pet to add to this NoModel items bucket.\nPets can appear more than once in the bucket", iprop.Description)

	assertProperty(t, itprop, "number", "quantity", "int16", "Quantity")
	iprop, ok = itprop.Properties["quantity"]
	assert.True(t, ok)
	assert.Equal(t, "The amount of pets to add to this bucket.", iprop.Description)
	assert.EqualValues(t, 1, *iprop.Minimum)
	assert.EqualValues(t, 10, *iprop.Maximum)

	assertProperty(t, itprop, "string", "notes", "", "Notes")
	iprop, ok = itprop.Properties["notes"]
	assert.True(t, ok)
	assert.Equal(t, "Notes to add to this item.\nThis can be used to add special instructions.", iprop.Description)

	definitions := make(map[string]spec.Schema)
	sp := newSchemaParser(classificationProg)
	pn := "github.com/casualjim/go-swagger/fixtures/goparsing/classification/models"
	pnr := "../fixtures/goparsing/classification/models"
	pkg := classificationProg.Package(pnr)
	assert.NotNil(t, pkg)

	fnd := false
	for _, fil := range pkg.Files {
		nm := filepath.Base(classificationProg.Fset.File(fil.Pos()).Name())
		if nm == "order.go" {
			fnd = true
			sp.Parse(fil, definitions)
			break
		}
	}
	assert.True(t, fnd)
	msch, ok := definitions["order"]
	assert.True(t, ok)
	assert.Equal(t, pn, msch.Extensions["x-go-package"])
	assert.Equal(t, "StoreOrder", msch.Extensions["x-go-name"])
}