Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad Request Error while trying to get pet for a specific owner #145

Closed
firasrg opened this issue Aug 25, 2024 · 4 comments
Closed

Bad Request Error while trying to get pet for a specific owner #145

firasrg opened this issue Aug 25, 2024 · 4 comments
Labels

Comments

@firasrg
Copy link

firasrg commented Aug 25, 2024

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 !

@arey arey added the bug label Aug 26, 2024
@arey
Copy link
Member

arey commented Aug 26, 2024

The equals method is not implemented in the Owner entity. Instead of I propose to use the getId() for comparing both instances :

if (!pet.getOwner().getId().equals(owner.getId()))

@firasrg
Copy link
Author

firasrg commented Aug 26, 2024

I think that it would be more readable if we have something like this :

Pet pet = this.clinicService.findPetByOwner(ownerId, petId);

And the body of findPetByOwner() would be like this:

Owner owner = this.clinicService.findOwnerById(ownerId);
Pet pet = owner.getPets().stream().filter(pet -> pet.getId().equals(petId)).findFirst().orElseThrow();

@arey
Copy link
Member

arey commented Aug 27, 2024

To fix this issue, we can start by adding a failed test case to the OwnerRestControllerTests.

Then, you're right: I'm not sure we need to call the findPetById method. The findOwnerById calls is enough.

For error handling, we have to take care to return a 404 or a 400 http error. I'm not sure the orElseThrow() is the best way to ensure it.

@arey
Copy link
Member

arey commented Sep 14, 2024

@firasrg I let you review my fix in the PR bad request on GET /api/owners/{id}/pets/{pe…](#150)

arey added a commit to arey/spring-petclinic-rest that referenced this issue Sep 15, 2024
arey added a commit to arey/spring-petclinic-rest that referenced this issue Sep 15, 2024
arey added a commit to arey/spring-petclinic-rest that referenced this issue Sep 16, 2024
…ng a Owner::getPet(Integer petId) method
@arey arey closed this as completed in 072f200 Sep 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants