|
1 | 1 | package org.openapitools.api; |
2 | 2 |
|
3 | | -import org.springframework.lang.Nullable; |
4 | | -import org.springframework.data.domain.Pageable; |
5 | | -import org.springframework.data.web.PageableDefault; |
6 | 3 | import org.openapitools.model.Pet; |
7 | | -import org.openapitools.model.PetSort; |
8 | | -import org.openapitools.model.PetSortEnum; |
| 4 | +import org.springframework.data.domain.Pageable; |
9 | 5 | import org.springframework.data.domain.Sort; |
10 | | -import org.springframework.data.web.SortDefault; |
11 | | -import org.openapitools.configuration.ValidPageable; |
12 | | -import org.openapitools.configuration.ValidSort; |
13 | | - |
14 | | - |
15 | | -import org.springframework.beans.factory.annotation.Autowired; |
16 | | -import org.springframework.http.HttpStatus; |
17 | | -import org.springframework.http.MediaType; |
18 | 6 | import org.springframework.http.ResponseEntity; |
19 | | -import org.springframework.stereotype.Controller; |
20 | | -import org.springframework.web.bind.annotation.PathVariable; |
21 | | -import org.springframework.web.bind.annotation.RequestBody; |
22 | | -import org.springframework.web.bind.annotation.RequestHeader; |
23 | | -import org.springframework.web.bind.annotation.RequestMapping; |
24 | | -import org.springframework.web.bind.annotation.CookieValue; |
25 | | -import org.springframework.web.bind.annotation.RequestParam; |
26 | | -import org.springframework.web.bind.annotation.RequestPart; |
27 | | -import org.springframework.web.multipart.MultipartFile; |
28 | | -import org.springframework.web.context.request.NativeWebRequest; |
29 | | - |
30 | | -import jakarta.validation.constraints.*; |
31 | | -import jakarta.validation.Valid; |
| 7 | +import org.springframework.web.bind.annotation.RestController; |
32 | 8 |
|
| 9 | +import java.util.Collections; |
33 | 10 | import java.util.List; |
34 | | -import java.util.Map; |
35 | | -import java.util.Optional; |
36 | | -import jakarta.annotation.Generated; |
37 | 11 |
|
38 | | -@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2026-04-15T20:51:04.118456741Z[UTC]", comments = "Generator version: 7.22.0-SNAPSHOT") |
39 | | -@Controller |
| 12 | +/** |
| 13 | + * Sample implementation of {@link PetApi} demonstrating that the generated |
| 14 | + * annotations ({@code @ValidSort}, {@code @ValidPageable}, {@code @PageableDefault}, |
| 15 | + * {@code @SortDefault}) behave correctly at runtime. |
| 16 | + * |
| 17 | + * Methods whose endpoint carries pagination/sort defaults assert the exact expected values |
| 18 | + * inside the method body. When the Spring argument resolvers apply the annotated defaults |
| 19 | + * correctly, the assertions pass and HTTP 200 is returned. If a default is missing or wrong, |
| 20 | + * the assertion throws {@link IllegalStateException} and the request fails with HTTP 500, |
| 21 | + * causing any calling test to fail with a clear message. |
| 22 | + * |
| 23 | + * Methods that only carry {@code @ValidSort} / {@code @ValidPageable} constraints need no body |
| 24 | + * logic — the constraint annotations reject invalid input before this code is ever reached, |
| 25 | + * and the {@code DefaultExceptionHandler} maps the resulting |
| 26 | + * {@link jakarta.validation.ConstraintViolationException} to HTTP 400. |
| 27 | + */ |
| 28 | +@RestController |
40 | 29 | public class PetApiController implements PetApi { |
41 | 30 |
|
42 | | - @Nullable |
43 | | - private final NativeWebRequest request; |
| 31 | + // ── no pageable / no special defaults ──────────────────────────────────── |
| 32 | + |
| 33 | + @Override |
| 34 | + public ResponseEntity<List<Pet>> findPetsAutoDetectedWithSort( |
| 35 | + String status, Integer page, Integer size, String sort) { |
| 36 | + return ResponseEntity.ok(Collections.emptyList()); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public ResponseEntity<List<Pet>> findPetsNonPaginatedWithSortEnum(String sort) { |
| 41 | + return ResponseEntity.ok(Collections.emptyList()); |
| 42 | + } |
| 43 | + |
| 44 | + // ── @ValidSort only (+ @PageableDefault) ───────────────────────────────── |
| 45 | + |
| 46 | + @Override |
| 47 | + public ResponseEntity<List<Pet>> findPetsWithArraySortEnum(Pageable pageable) { |
| 48 | + return ResponseEntity.ok(Collections.emptyList()); |
| 49 | + } |
44 | 50 |
|
45 | | - @Autowired |
46 | | - public PetApiController(@Nullable NativeWebRequest request) { |
47 | | - this.request = request; |
| 51 | + @Override |
| 52 | + public ResponseEntity<List<Pet>> findPetsWithArraySortRefEnum(Pageable pageable) { |
| 53 | + return ResponseEntity.ok(Collections.emptyList()); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public ResponseEntity<List<Pet>> findPetsWithExternalParamRefArraySort(Pageable pageable) { |
| 58 | + return ResponseEntity.ok(Collections.emptyList()); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public ResponseEntity<List<Pet>> findPetsWithNonExplodedExternalParamRefArraySort(Pageable pageable) { |
| 63 | + return ResponseEntity.ok(Collections.emptyList()); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public ResponseEntity<List<Pet>> findPetsWithRefSort(Pageable pageable) { |
| 68 | + return ResponseEntity.ok(Collections.emptyList()); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public ResponseEntity<List<Pet>> findPetsWithSortEnum(String status, Pageable pageable) { |
| 73 | + return ResponseEntity.ok(Collections.emptyList()); |
48 | 74 | } |
49 | 75 |
|
50 | 76 | @Override |
51 | | - public Optional<NativeWebRequest> getRequest() { |
52 | | - return Optional.ofNullable(request); |
| 77 | + public ResponseEntity<List<Pet>> findPetsWithoutSortEnum(Pageable pageable) { |
| 78 | + return ResponseEntity.ok(Collections.emptyList()); |
53 | 79 | } |
54 | 80 |
|
| 81 | + // ── @ValidPageable only ─────────────────────────────────────────────────── |
| 82 | + |
| 83 | + @Override |
| 84 | + public ResponseEntity<List<Pet>> findPetsWithSizeConstraint(Pageable pageable) { |
| 85 | + return ResponseEntity.ok(Collections.emptyList()); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public ResponseEntity<List<Pet>> findPetsWithPageAndSizeConstraint(Pageable pageable) { |
| 90 | + return ResponseEntity.ok(Collections.emptyList()); |
| 91 | + } |
| 92 | + |
| 93 | + // ── @PageableDefault ───────────────────────────────────────────────────── |
| 94 | + // @PageableDefault(page = 0, size = 25) |
| 95 | + |
| 96 | + @Override |
| 97 | + public ResponseEntity<List<Pet>> findPetsWithPageSizeDefaultsOnly(Pageable pageable) { |
| 98 | + if (pageable.getPageNumber() != 0) { |
| 99 | + throw new IllegalStateException( |
| 100 | + "@PageableDefault page: expected 0, got " + pageable.getPageNumber()); |
| 101 | + } |
| 102 | + if (pageable.getPageSize() != 25) { |
| 103 | + throw new IllegalStateException( |
| 104 | + "@PageableDefault size: expected 25, got " + pageable.getPageSize()); |
| 105 | + } |
| 106 | + return ResponseEntity.ok(Collections.emptyList()); |
| 107 | + } |
| 108 | + |
| 109 | + // ── @SortDefault ───────────────────────────────────────────────────────── |
| 110 | + // @SortDefault(sort = {"name"}, direction = DESC) |
| 111 | + |
| 112 | + @Override |
| 113 | + public ResponseEntity<List<Pet>> findPetsWithSortDefaultOnly(Pageable pageable) { |
| 114 | + Sort.Order nameOrder = pageable.getSort().getOrderFor("name"); |
| 115 | + if (nameOrder == null || nameOrder.getDirection() != Sort.Direction.DESC) { |
| 116 | + throw new IllegalStateException( |
| 117 | + "@SortDefault sort: expected name DESC, got " + pageable.getSort()); |
| 118 | + } |
| 119 | + return ResponseEntity.ok(Collections.emptyList()); |
| 120 | + } |
| 121 | + |
| 122 | + // @SortDefault(sort = {"id"}, direction = ASC) |
| 123 | + |
| 124 | + @Override |
| 125 | + public ResponseEntity<List<Pet>> findPetsWithSortDefaultAsc(Pageable pageable) { |
| 126 | + Sort.Order idOrder = pageable.getSort().getOrderFor("id"); |
| 127 | + if (idOrder == null || idOrder.getDirection() != Sort.Direction.ASC) { |
| 128 | + throw new IllegalStateException( |
| 129 | + "@SortDefault sort: expected id ASC, got " + pageable.getSort()); |
| 130 | + } |
| 131 | + return ResponseEntity.ok(Collections.emptyList()); |
| 132 | + } |
| 133 | + |
| 134 | + // ── @SortDefault.SortDefaults ───────────────────────────────────────────── |
| 135 | + // @SortDefaults(SortDefault(sort = {"name"}, direction = DESC), SortDefault(sort = {"id"}, direction = ASC)) |
| 136 | + |
| 137 | + @Override |
| 138 | + public ResponseEntity<List<Pet>> findPetsWithMixedSortDefaults(Pageable pageable) { |
| 139 | + Sort.Order nameOrder = pageable.getSort().getOrderFor("name"); |
| 140 | + if (nameOrder == null || nameOrder.getDirection() != Sort.Direction.DESC) { |
| 141 | + throw new IllegalStateException( |
| 142 | + "@SortDefaults sort: expected name DESC, got " + pageable.getSort()); |
| 143 | + } |
| 144 | + Sort.Order idOrder = pageable.getSort().getOrderFor("id"); |
| 145 | + if (idOrder == null || idOrder.getDirection() != Sort.Direction.ASC) { |
| 146 | + throw new IllegalStateException( |
| 147 | + "@SortDefaults sort: expected id ASC, got " + pageable.getSort()); |
| 148 | + } |
| 149 | + return ResponseEntity.ok(Collections.emptyList()); |
| 150 | + } |
| 151 | + |
| 152 | + // ── @PageableDefault + @SortDefault.SortDefaults combined ───────────────── |
| 153 | + // @PageableDefault(page = 0, size = 10) |
| 154 | + // @SortDefaults(SortDefault(sort = {"name"}, direction = DESC), SortDefault(sort = {"id"}, direction = ASC)) |
| 155 | + |
| 156 | + @Override |
| 157 | + public ResponseEntity<List<Pet>> findPetsWithAllDefaults(Pageable pageable) { |
| 158 | + if (pageable.getPageNumber() != 0) { |
| 159 | + throw new IllegalStateException( |
| 160 | + "@PageableDefault page: expected 0, got " + pageable.getPageNumber()); |
| 161 | + } |
| 162 | + if (pageable.getPageSize() != 10) { |
| 163 | + throw new IllegalStateException( |
| 164 | + "@PageableDefault size: expected 10, got " + pageable.getPageSize()); |
| 165 | + } |
| 166 | + Sort.Order nameOrder = pageable.getSort().getOrderFor("name"); |
| 167 | + if (nameOrder == null || nameOrder.getDirection() != Sort.Direction.DESC) { |
| 168 | + throw new IllegalStateException( |
| 169 | + "@SortDefaults sort: expected name DESC, got " + pageable.getSort()); |
| 170 | + } |
| 171 | + Sort.Order idOrder = pageable.getSort().getOrderFor("id"); |
| 172 | + if (idOrder == null || idOrder.getDirection() != Sort.Direction.ASC) { |
| 173 | + throw new IllegalStateException( |
| 174 | + "@SortDefaults sort: expected id ASC, got " + pageable.getSort()); |
| 175 | + } |
| 176 | + return ResponseEntity.ok(Collections.emptyList()); |
| 177 | + } |
55 | 178 | } |
0 commit comments