Skip to content

fix: owner not exists validation addPetToOwner#292

Open
LeonardoSantosBR wants to merge 5 commits intospring-petclinic:masterfrom
LeonardoSantosBR:fix/api-error-responses-expose-technical-exception-messages
Open

fix: owner not exists validation addPetToOwner#292
LeonardoSantosBR wants to merge 5 commits intospring-petclinic:masterfrom
LeonardoSantosBR:fix/api-error-responses-expose-technical-exception-messages

Conversation

@LeonardoSantosBR
Copy link
Copy Markdown

This PR fixes issue #287.

The previous error handling exposed SQL and constraint details in API responses by using ex.getLocalizedMessage().

Changes:

  • Replaced localized exception messages with stable API-safe messages
  • Logged technical details server-side only
  • Added owner existence validation before saving pets

This prevents leakage of internal persistence details while maintaining useful logging.

if (owner == null)
return new ResponseEntity<>(HttpStatus.NOT_FOUND);

owner.setId(ownerId);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this assignement doesn't make sense now

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why? it is a validation if owner doesnt exist ...

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already fetched owner, so no reason to set owner Id again in
owner.setId(ownerId);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahhh got you, this line was already there, i will remove

Owner owner = new Owner();
Owner owner = clinicService.findOwnerById(ownerId);

if (owner == null)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls add/update tests for this case

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how can i do this ? you mean add this validation in update endpoint?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check/fix tests in OwnerRestControllerTests.java

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i found the archive but i dont now how this works, i am learning spring boot

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i added the tests , check it pls

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image it is the result of my test

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that correct? Or do you need something else?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I said “tests,” not “a test.” The CI/CD is failing because one of the tests started failing after your changes. It would be good to check why this happened and fix it.
P.S.
It’s good practice to run mvn test locally before creating PRs - may save you some time.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, There's an error in the test from four years ago, what i need to do?
image

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep. this test was fine before you changed controller logic. so think about it - how this test was verify previous logic, what is changed, how you can fix it etc.

PetDto petDto = petMapper.toPetDto(pet);
headers.setLocation(UriComponentsBuilder.newInstance().path("/api/pets/{id}")
.buildAndExpand(pet.getId()).toUri());
.buildAndExpand(pet.getId()).toUri());
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d suggest keeping changes to a minimum and preserving the existing formatting (here and in other places)

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.

2 participants