Fix /pets endpoint in examples returning null when there's no pet
When no pet is found the examples pet store implementations are returning null as the http response body as opposed to empty array ([]).
Since the zero value of a slice is nil the pets slice should be created using make so that if no pet matches the query we can return an initialized slice. Otherwise the json package will encode the nil slice as json null which breaks the api contract.
func (p *PetStore) FindPets(...){
- var result []Pet
+ var result []Pet = make([]Pet, 0)
// Code for populating the result
// Return or write the result to the response
}
The go json encoder and decoder doesn't complain about the nil value, so when consuming the api using the go standard library (as the tests do) everything is fine but if we would use clients written with languages/tools (javascript for example) they would fail to parse the response body. That's why I added additional checks to the examples api's tests.