|
2 | 2 |
|
3 | 3 | import org.openapitools.model.NullableModel; |
4 | 4 |
|
5 | | - |
6 | | -import org.springframework.beans.factory.annotation.Autowired; |
7 | 5 | import org.springframework.http.HttpStatus; |
8 | | -import org.springframework.http.MediaType; |
9 | 6 | import org.springframework.http.ResponseEntity; |
10 | | -import org.springframework.stereotype.Controller; |
11 | | -import org.springframework.web.bind.annotation.PathVariable; |
12 | | -import org.springframework.web.bind.annotation.RequestBody; |
13 | | -import org.springframework.web.bind.annotation.RequestHeader; |
14 | | -import org.springframework.web.bind.annotation.RequestMapping; |
15 | | -import org.springframework.web.bind.annotation.CookieValue; |
16 | | -import org.springframework.web.bind.annotation.RequestParam; |
17 | | -import org.springframework.web.bind.annotation.RequestPart; |
18 | | -import org.springframework.web.multipart.MultipartFile; |
19 | | -import org.springframework.web.context.request.NativeWebRequest; |
20 | | - |
21 | | -import jakarta.validation.constraints.*; |
22 | | -import jakarta.validation.Valid; |
| 7 | +import org.springframework.web.bind.annotation.RestController; |
23 | 8 |
|
24 | | -import java.util.List; |
25 | | -import java.util.Map; |
26 | | -import java.util.Optional; |
27 | | -import jakarta.annotation.Generated; |
| 9 | +import jakarta.validation.Valid; |
28 | 10 |
|
29 | | -@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", comments = "Generator version: 7.23.0-SNAPSHOT") |
30 | | -@Controller |
| 11 | +/** |
| 12 | + * Sample implementation of {@link NullableApi} demonstrating that the generated |
| 13 | + * nullable annotations work correctly at runtime. |
| 14 | + * |
| 15 | + * Each method receives a deserialized {@link NullableModel} and asserts the expected |
| 16 | + * state of its fields. When Jackson correctly applies {@code @JsonSetter(nulls = Nulls.FAIL)} |
| 17 | + * and {@code JsonNullable<T>}, the assertions pass and HTTP 200 is returned. If the |
| 18 | + * deserialized state is wrong, the assertion throws {@link IllegalStateException} and |
| 19 | + * the request fails with HTTP 500, causing any calling test to fail with a clear message. |
| 20 | + */ |
| 21 | +@RestController |
31 | 22 | public class NullableApiController implements NullableApi { |
32 | 23 |
|
33 | | - private final NativeWebRequest request; |
34 | | - |
35 | | - @Autowired |
36 | | - public NullableApiController(NativeWebRequest request) { |
37 | | - this.request = request; |
| 24 | + /** |
| 25 | + * POST with only required fields — asserts optional fields are absent/undefined. |
| 26 | + * |
| 27 | + * The JSON body contains only the two required fields; both optional fields are absent. |
| 28 | + * Expected state: |
| 29 | + * <ul> |
| 30 | + * <li>{@code optionalNonNullable} → {@code null} (absent key → default null)</li> |
| 31 | + * <li>{@code optionalNullable} → {@code JsonNullable.undefined()} (absent key → undefined)</li> |
| 32 | + * </ul> |
| 33 | + */ |
| 34 | + @Override |
| 35 | + public ResponseEntity<Void> checkRequiredOnly(@Valid NullableModel nullableModel) { |
| 36 | + if (nullableModel.getOptionalNonNullable() != null) { |
| 37 | + throw new IllegalStateException( |
| 38 | + "optionalNonNullable: expected null (absent from JSON), got " |
| 39 | + + nullableModel.getOptionalNonNullable()); |
| 40 | + } |
| 41 | + if (nullableModel.getOptionalNullable().isPresent()) { |
| 42 | + throw new IllegalStateException( |
| 43 | + "optionalNullable: expected JsonNullable.undefined() (absent from JSON), got " |
| 44 | + + nullableModel.getOptionalNullable()); |
| 45 | + } |
| 46 | + return new ResponseEntity<>(HttpStatus.OK); |
38 | 47 | } |
39 | 48 |
|
| 49 | + /** |
| 50 | + * POST with optionalNullable set to null — asserts JsonNullable is present-with-null. |
| 51 | + * |
| 52 | + * The JSON body contains the required fields plus {@code "optionalNullable": null}. |
| 53 | + * Expected state: |
| 54 | + * <ul> |
| 55 | + * <li>{@code optionalNullable} → {@code JsonNullable.of(null)} |
| 56 | + * (key present with null value → isPresent = true, get() = null)</li> |
| 57 | + * </ul> |
| 58 | + */ |
40 | 59 | @Override |
41 | | - public Optional<NativeWebRequest> getRequest() { |
42 | | - return Optional.ofNullable(request); |
| 60 | + public ResponseEntity<Void> checkOptionalNullableNull(@Valid NullableModel nullableModel) { |
| 61 | + if (!nullableModel.getOptionalNullable().isPresent()) { |
| 62 | + throw new IllegalStateException( |
| 63 | + "optionalNullable: expected JsonNullable present (explicit null in JSON), got " |
| 64 | + + nullableModel.getOptionalNullable()); |
| 65 | + } |
| 66 | + if (nullableModel.getOptionalNullable().get() != null) { |
| 67 | + throw new IllegalStateException( |
| 68 | + "optionalNullable: expected null inner value, got " |
| 69 | + + nullableModel.getOptionalNullable().get()); |
| 70 | + } |
| 71 | + return new ResponseEntity<>(HttpStatus.OK); |
43 | 72 | } |
44 | 73 |
|
| 74 | + /** |
| 75 | + * POST with all 4 fields present — asserts each field has the expected value. |
| 76 | + * |
| 77 | + * The JSON body contains all four fields with non-null string values. |
| 78 | + * Expected state: |
| 79 | + * <ul> |
| 80 | + * <li>{@code optionalNonNullable} → {@code "opt-non-null"}</li> |
| 81 | + * <li>{@code optionalNullable} → {@code JsonNullable.of("opt-nullable")} (isPresent, non-null value)</li> |
| 82 | + * </ul> |
| 83 | + */ |
| 84 | + @Override |
| 85 | + public ResponseEntity<Void> checkAllPresent(@Valid NullableModel nullableModel) { |
| 86 | + if (!"opt-non-null".equals(nullableModel.getOptionalNonNullable())) { |
| 87 | + throw new IllegalStateException( |
| 88 | + "optionalNonNullable: expected 'opt-non-null', got " |
| 89 | + + nullableModel.getOptionalNonNullable()); |
| 90 | + } |
| 91 | + if (!nullableModel.getOptionalNullable().isPresent()) { |
| 92 | + throw new IllegalStateException( |
| 93 | + "optionalNullable: expected JsonNullable present, got " |
| 94 | + + nullableModel.getOptionalNullable()); |
| 95 | + } |
| 96 | + if (!"opt-nullable".equals(nullableModel.getOptionalNullable().get())) { |
| 97 | + throw new IllegalStateException( |
| 98 | + "optionalNullable: expected 'opt-nullable', got " |
| 99 | + + nullableModel.getOptionalNullable().get()); |
| 100 | + } |
| 101 | + return new ResponseEntity<>(HttpStatus.OK); |
| 102 | + } |
45 | 103 | } |
| 104 | + |
0 commit comments