Skip to content

Commit 9f9463b

Browse files
committed
fix jackson-annotations import package. regenerate samples
1 parent d7badae commit 9f9463b

9 files changed

Lines changed: 108 additions & 48 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinSpringServerCodegen.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,8 @@ public void processOpts() {
553553
if (isUseJackson3()) {
554554
// Override databind imports for Jackson 3
555555
importMapping.put("JsonDeserialize", "tools.jackson.databind.annotation.JsonDeserialize");
556-
importMapping.put("JsonSetter", "tools.jackson.annotation.JsonSetter");
557-
importMapping.put("Nulls", "tools.jackson.annotation.Nulls");
556+
importMapping.put("JsonSetter", "com.fasterxml.jackson.annotation.JsonSetter");
557+
importMapping.put("Nulls", "com.fasterxml.jackson.annotation.Nulls");
558558
// jackson-databind-nullable >= 0.2.10 supports both Jackson 2 and 3;
559559
// the JsonNullable class lives in the same package regardless of Jackson version.
560560
importMapping.put("JsonNullable", "org.openapitools.jackson.nullable.JsonNullable");

modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/spring/KotlinSpringServerCodegenTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6217,9 +6217,10 @@ public void requiredNullable_scenario4_optionalNullable_withOpenApiNullable() th
62176217

62186218
/**
62196219
* Scenario 3 with Jackson 3 (Spring Boot 4): optional + non-nullable.
6220-
* @JsonSetter / Nulls imports should come from tools.jackson.annotation.
6220+
* @JsonSetter / Nulls imports should come from com.fasterxml.jackson.annotation
6221+
* (Jackson 3.x intentionally kept jackson-annotations at 2.x, same package).
62216222
*/
6222-
@Test(description = "Scenario 3 with Jackson 3: tools.jackson.annotation.JsonSetter + Nulls imports")
6223+
@Test(description = "Scenario 3 with Jackson 3: com.fasterxml.jackson.annotation.JsonSetter + Nulls imports")
62236224
public void requiredNullable_scenario3_optionalNonNullable_withJackson3() throws IOException {
62246225
Map<String, Object> props = new HashMap<>();
62256226
props.put(KotlinSpringServerCodegen.USE_SPRING_BOOT4, "true");
@@ -6230,10 +6231,10 @@ public void requiredNullable_scenario3_optionalNonNullable_withJackson3() throws
62306231
Path modelFile = files.get("TestModel.kt").toPath();
62316232
// Annotation must still be rendered
62326233
assertFileContains(modelFile, "@field:JsonSetter(nulls = Nulls.FAIL)");
6233-
// Imports must come from tools.jackson (Jackson 3 package)
6234+
// Imports must come from com.fasterxml.jackson.annotation (Jackson 3.x keeps annotations at 2.x)
62346235
assertFileContains(modelFile,
6235-
"import tools.jackson.annotation.JsonSetter",
6236-
"import tools.jackson.annotation.Nulls");
6236+
"import com.fasterxml.jackson.annotation.JsonSetter",
6237+
"import com.fasterxml.jackson.annotation.Nulls");
62376238
// Must be nullable type with null default
62386239
assertFileContains(modelFile, "val optionalNonNullable: kotlin.String? = null");
62396240
}

samples/server/petstore/kotlin-springboot-4/src/main/kotlin/org/openapitools/model/Category.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package org.openapitools.model
22

33
import java.util.Objects
44
import com.fasterxml.jackson.annotation.JsonProperty
5-
import tools.jackson.annotation.JsonSetter
6-
import tools.jackson.annotation.Nulls
5+
import com.fasterxml.jackson.annotation.JsonSetter
6+
import com.fasterxml.jackson.annotation.Nulls
77
import jakarta.validation.constraints.DecimalMax
88
import jakarta.validation.constraints.DecimalMin
99
import jakarta.validation.constraints.Email

samples/server/petstore/kotlin-springboot-4/src/main/kotlin/org/openapitools/model/ModelApiResponse.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package org.openapitools.model
22

33
import java.util.Objects
44
import com.fasterxml.jackson.annotation.JsonProperty
5-
import tools.jackson.annotation.JsonSetter
6-
import tools.jackson.annotation.Nulls
5+
import com.fasterxml.jackson.annotation.JsonSetter
6+
import com.fasterxml.jackson.annotation.Nulls
77
import jakarta.validation.constraints.DecimalMax
88
import jakarta.validation.constraints.DecimalMin
99
import jakarta.validation.constraints.Email

samples/server/petstore/kotlin-springboot-4/src/main/kotlin/org/openapitools/model/Order.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package org.openapitools.model
33
import java.util.Objects
44
import com.fasterxml.jackson.annotation.JsonCreator
55
import com.fasterxml.jackson.annotation.JsonProperty
6+
import com.fasterxml.jackson.annotation.JsonSetter
67
import com.fasterxml.jackson.annotation.JsonValue
7-
import tools.jackson.annotation.JsonSetter
8-
import tools.jackson.annotation.Nulls
8+
import com.fasterxml.jackson.annotation.Nulls
99
import jakarta.validation.constraints.DecimalMax
1010
import jakarta.validation.constraints.DecimalMin
1111
import jakarta.validation.constraints.Email

samples/server/petstore/kotlin-springboot-4/src/main/kotlin/org/openapitools/model/Pet.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package org.openapitools.model
33
import java.util.Objects
44
import com.fasterxml.jackson.annotation.JsonCreator
55
import com.fasterxml.jackson.annotation.JsonProperty
6+
import com.fasterxml.jackson.annotation.JsonSetter
67
import com.fasterxml.jackson.annotation.JsonValue
8+
import com.fasterxml.jackson.annotation.Nulls
79
import org.openapitools.model.Category
810
import org.openapitools.model.Tag
9-
import tools.jackson.annotation.JsonSetter
10-
import tools.jackson.annotation.Nulls
1111
import jakarta.validation.constraints.DecimalMax
1212
import jakarta.validation.constraints.DecimalMin
1313
import jakarta.validation.constraints.Email

samples/server/petstore/kotlin-springboot-4/src/main/kotlin/org/openapitools/model/Tag.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package org.openapitools.model
22

33
import java.util.Objects
44
import com.fasterxml.jackson.annotation.JsonProperty
5-
import tools.jackson.annotation.JsonSetter
6-
import tools.jackson.annotation.Nulls
5+
import com.fasterxml.jackson.annotation.JsonSetter
6+
import com.fasterxml.jackson.annotation.Nulls
77
import jakarta.validation.constraints.DecimalMax
88
import jakarta.validation.constraints.DecimalMin
99
import jakarta.validation.constraints.Email

samples/server/petstore/kotlin-springboot-4/src/main/kotlin/org/openapitools/model/User.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package org.openapitools.model
22

33
import java.util.Objects
44
import com.fasterxml.jackson.annotation.JsonProperty
5-
import tools.jackson.annotation.JsonSetter
6-
import tools.jackson.annotation.Nulls
5+
import com.fasterxml.jackson.annotation.JsonSetter
6+
import com.fasterxml.jackson.annotation.Nulls
77
import jakarta.validation.constraints.DecimalMax
88
import jakarta.validation.constraints.DecimalMin
99
import jakarta.validation.constraints.Email

samples/server/petstore/springboot-sort-validation/src/main/java/org/openapitools/api/NullableApiController.java

Lines changed: 88 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,103 @@
22

33
import org.openapitools.model.NullableModel;
44

5-
6-
import org.springframework.beans.factory.annotation.Autowired;
75
import org.springframework.http.HttpStatus;
8-
import org.springframework.http.MediaType;
96
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;
238

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;
2810

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
3122
public class NullableApiController implements NullableApi {
3223

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);
3847
}
3948

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+
*/
4059
@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);
4372
}
4473

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+
}
45103
}
104+

0 commit comments

Comments
 (0)