spring-petclinic-rest
spring-petclinic-rest copied to clipboard
Bad Request Error while trying to get pet for a specific owner
The GET/api/owners/{id}/pets/{petId}
endpoint seems to be returning an incorrect response or handling errors improperly. This issue was encountered while attempting to retrieve specific pet for a given owner.
cURL:
curl -X 'GET' \
'http://localhost:9966/petclinic/api/owners/1/pets/1' \
-H 'accept: application/json'
Response on Swagger-ui: Error: response status is 400
I tried to debug code and found out that it comes from OwnerRestController
:
@RestController
@RequestMapping("/api")
public class OwnerRestController implements OwnersApi {
// ...
@PreAuthorize("hasRole(@roles.OWNER_ADMIN)")
@Override
public ResponseEntity<PetDto> getOwnersPet(Integer ownerId, Integer petId) {
Owner owner = this.clinicService.findOwnerById(ownerId);
Pet pet = this.clinicService.findPetById(petId);
if (owner == null || pet == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
} else {
if (!pet.getOwner().equals(owner)) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
} else {
return new ResponseEntity<>(petMapper.toPetDto(pet), HttpStatus.OK);
}
}
}
}
The problem is here : if (!pet.getOwner().equals(owner)) {}
: the equals()
check returns false
. Ive checked the entity Owner class and found out that it doesn't have equals
and hashcode()
(same for Pet entity class), which is not recommended !