Skip to content

Commit

Permalink
https://github.com/spring-projects/spring-framework/issues/15682
Browse files Browse the repository at this point in the history
  • Loading branch information
YusukeHasegawa committed Apr 5, 2019
1 parent e4f427b commit 0de94d5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/docs/openapi/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ paths:
schema:
type: integer
format: int32
maximum: 100
responses:
'200':
description: A paged array of pets
Expand Down
27 changes: 14 additions & 13 deletions src/main/java/com/example/openapi/web/api/PetsApiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import org.zalando.problem.Problem;
import org.zalando.problem.Status;
import org.zalando.problem.StatusType;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

Expand All @@ -33,6 +34,8 @@ public class PetsApiController implements PetsApi {

AtomicInteger i = new AtomicInteger();

//https://github.com/spring-projects/spring-framework/issues/15682
//5.1でsuper classのアノテーション見てくれるようになった模様 @RequestBody はなくてもよい
@Override
public ResponseEntity<Void> createPets2(@Valid @RequestBody final NewPet newPet) {
final Pets pet = petsRepository.save(petMapper.newPetToPets(newPet));
Expand All @@ -42,6 +45,11 @@ public ResponseEntity<Void> createPets2(@Valid @RequestBody final NewPet newPet)
.build();
}

@Override
public ResponseEntity<List<Pet>> listPets(@Max(100) @Valid final Integer limit) {
return ResponseEntity.ok(petsRepository.findAll().stream().map(petMapper::petsToPet).collect(Collectors.toList()));
}

@Override
public ResponseEntity<Void> createPets() {
final Pets entity = new Pets();
Expand All @@ -53,21 +61,14 @@ public ResponseEntity<Void> createPets() {
.build();
}

@Override
public ResponseEntity<List<Pet>> listPets(
@Valid @RequestParam(value = "limit", required = false) final Integer limit) {
return ResponseEntity.ok(petsRepository.findAll().stream().map(petMapper::petsToPet).collect(Collectors.toList()));

}

@Override
public ResponseEntity<List<Pet>> showPetById(
@PathVariable("petId") final String petId) {
public ResponseEntity<List<Pet>> showPetById(final String petId) {
final List<Pets> pets = petsRepository.findByName(petId);

if(!pets.isEmpty()){
if (!pets.isEmpty()) {
return ResponseEntity.ok(pets.stream().map(petMapper::petsToPet).collect(Collectors.toList()));
}else{
} else {
throw Problem.valueOf(Status.NOT_FOUND);
}
}
Expand Down

0 comments on commit 0de94d5

Please sign in to comment.