Skip to content

fix: validate owner-pet relationship in update endpoint#311

Open
sourav-18 wants to merge 2 commits intospring-petclinic:masterfrom
sourav-18:fix-owner-pet-validation
Open

fix: validate owner-pet relationship in update endpoint#311
sourav-18 wants to merge 2 commits intospring-petclinic:masterfrom
sourav-18:fix-owner-pet-validation

Conversation

@sourav-18
Copy link
Copy Markdown
Contributor

Fixes #310

The endpoint PUT /owners/{ownerId}/pets/{petId} allowed updating pets that do not belong to the specified owner.

This PR adds validation to ensure that the pet being updated belongs to the given owner:

if (currentOwner != null &&
    currentOwner.getPets().stream().anyMatch(pet -> Objects.equals(pet.getId(), petId))) {
    ...
}

This approach keeps the service and repository layers unchanged and introduces a minimal fix at the controller level.

An alternative approach could be to validate ownership at the repository level using a method like findPetByIdAndOwnerId, but the current solution avoids additional changes and resolves the issue effectively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PUT /owners/{ownerId}/pets/{petId} allows updating pets that do not belong to the specified owner

1 participant