From 2db3bcb169067869246bdf9f754baac6c255849a Mon Sep 17 00:00:00 2001 From: Antoine Rey Date: Wed, 29 Dec 2021 11:29:47 +0100 Subject: [PATCH] Fix Vet creation from the POST /vets API --- .../samples/petclinic/mapper/VetMapper.java | 3 ++ .../rest/controller/VetRestController.java | 11 ++++--- src/main/resources/openapi.yml | 33 ++++++++++++------- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/mapper/VetMapper.java b/src/main/java/org/springframework/samples/petclinic/mapper/VetMapper.java index 98d089457..3c25e5142 100644 --- a/src/main/java/org/springframework/samples/petclinic/mapper/VetMapper.java +++ b/src/main/java/org/springframework/samples/petclinic/mapper/VetMapper.java @@ -3,6 +3,7 @@ import org.mapstruct.Mapper; import org.springframework.samples.petclinic.rest.dto.VetDto; import org.springframework.samples.petclinic.model.Vet; +import org.springframework.samples.petclinic.rest.dto.VetFieldsDto; import java.util.Collection; @@ -13,6 +14,8 @@ public interface VetMapper { Vet toVet(VetDto vetDto); + Vet toVet(VetFieldsDto vetFieldsDto); + VetDto toVetDto(Vet vet); Collection toVetDtos(Collection vets); diff --git a/src/main/java/org/springframework/samples/petclinic/rest/controller/VetRestController.java b/src/main/java/org/springframework/samples/petclinic/rest/controller/VetRestController.java index 7c6144320..1e3f0f81d 100644 --- a/src/main/java/org/springframework/samples/petclinic/rest/controller/VetRestController.java +++ b/src/main/java/org/springframework/samples/petclinic/rest/controller/VetRestController.java @@ -24,6 +24,7 @@ import org.springframework.samples.petclinic.mapper.VetMapper; import org.springframework.samples.petclinic.model.Specialty; import org.springframework.samples.petclinic.model.Vet; +import org.springframework.samples.petclinic.rest.dto.VetFieldsDto; import org.springframework.samples.petclinic.service.ClinicService; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.BindingResult; @@ -77,18 +78,18 @@ public ResponseEntity getVet(@PathVariable("vetId") int vetId) { @PreAuthorize("hasRole(@roles.VET_ADMIN)") @RequestMapping(value = "/vets", method = RequestMethod.POST, produces = "application/json") - public ResponseEntity addVet(@RequestBody @Valid VetDto vetDto, BindingResult bindingResult, UriComponentsBuilder ucBuilder) { + public ResponseEntity addVet(@RequestBody @Valid VetFieldsDto vetFieldsDto, BindingResult bindingResult, UriComponentsBuilder ucBuilder) { BindingErrorsResponse errors = new BindingErrorsResponse(); HttpHeaders headers = new HttpHeaders(); - if (bindingResult.hasErrors() || (vetDto == null)) { + if (bindingResult.hasErrors() || (vetFieldsDto == null)) { errors.addAllErrors(bindingResult); headers.add("errors", errors.toJSON()); - return new ResponseEntity(headers, HttpStatus.BAD_REQUEST); + return new ResponseEntity<>(headers, HttpStatus.BAD_REQUEST); } - Vet vet = vetMapper.toVet(vetDto); + Vet vet = vetMapper.toVet(vetFieldsDto); this.clinicService.saveVet(vet); headers.setLocation(ucBuilder.path("/api/vets/{id}").buildAndExpand(vet.getId()).toUri()); - return new ResponseEntity(vetMapper.toVetDto(vet), headers, HttpStatus.CREATED); + return new ResponseEntity<>(vetMapper.toVetDto(vet), headers, HttpStatus.CREATED); } @PreAuthorize("hasRole(@roles.VET_ADMIN)") diff --git a/src/main/resources/openapi.yml b/src/main/resources/openapi.yml index b03cc296b..7289509d5 100755 --- a/src/main/resources/openapi.yml +++ b/src/main/resources/openapi.yml @@ -733,19 +733,11 @@ components: - id - type - visits - Vet: - title: Vet - description: A veterinarian. + VetFields: + title: VetFields + description: Editable fields of a veterinarian. type: object properties: - id: - title: ID - description: The ID of the vet. - type: integer - format: int32 - minimum: 0 - example: 1 - readOnly: true firstName: title: First name description: The first name of the vet. @@ -768,6 +760,25 @@ components: type: array items: $ref: '#/components/schemas/Specialty' + required: + - firstName + - lastName + - specialties + Vet: + title: Vet + description: A veterinarian. + allOf: + - $ref: '#/components/schemas/VetFields' + - type: object + properties: + id: + title: ID + description: The ID of the vet. + type: integer + format: int32 + minimum: 0 + example: 1 + readOnly: true required: - id - firstName