Skip to content

Commit ee41035

Browse files
committed
[JavaSpring][21200] improve Kotlin interopability with optional values
nullable annotations are now added to getters, setters, parameters, for optional attributes without default value
1 parent f5b8fd6 commit ee41035

File tree

1,248 files changed

+8669
-8401
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,248 files changed

+8669
-8401
lines changed

modules/openapi-generator/src/main/resources/JavaSpring/api.mustache

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import io.virtualan.annotation.VirtualService;
3232
import org.springframework.http.HttpStatus;
3333
import org.springframework.http.MediaType;
3434
{{/jdk8-no-delegate}}
35+
import org.springframework.lang.Nullable;
3536
{{^useResponseEntity}}
3637
import org.springframework.http.HttpStatus;
3738
{{/useResponseEntity}}

modules/openapi-generator/src/main/resources/JavaSpring/apiController.mustache

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import io.swagger.annotations.*;
2121
import org.springframework.beans.factory.annotation.Autowired;
2222
import org.springframework.http.HttpStatus;
2323
import org.springframework.http.MediaType;
24+
import org.springframework.lang.Nullable;
2425
{{#useResponseEntity}}
2526
import org.springframework.http.ResponseEntity;
2627
import org.springframework.stereotype.Controller;

modules/openapi-generator/src/main/resources/JavaSpring/apiDelegate.mustache

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.springframework.http.MediaType;
77
{{#useResponseEntity}}
88
import org.springframework.http.ResponseEntity;
99
{{/useResponseEntity}}
10+
import org.springframework.lang.Nullable;
1011
import org.springframework.web.context.request.NativeWebRequest;
1112
import org.springframework.web.multipart.MultipartFile;
1213
{{#reactive}}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{#isBodyParam}}{{>paramDoc}}{{#useBeanValidation}} @Valid{{>beanValidationBodyParams}}{{/useBeanValidation}} @RequestBody{{^required}}(required = false){{/required}} {{^reactive}}{{>optionalDataType}}{{/reactive}}{{#reactive}}{{^isArray}}Mono<{{{dataType}}}>{{/isArray}}{{#isArray}}Flux<{{{baseType}}}>{{/isArray}}{{/reactive}} {{paramName}}{{/isBodyParam}}
1+
{{#isBodyParam}}{{>paramDoc}}{{#useBeanValidation}} @Valid{{>beanValidationBodyParams}}{{/useBeanValidation}} @RequestBody{{^required}}(required = false){{/required}} {{^reactive}}{{>nullableAnnotation}}{{>optionalDataType}}{{/reactive}}{{#reactive}}{{^isArray}}Mono<{{{dataType}}}>{{/isArray}}{{#isArray}}Flux<{{{baseType}}}>{{/isArray}}{{/reactive}} {{paramName}}{{/isBodyParam}}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{#isCookieParam}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}{{>paramDoc}} @CookieValue(name = "{{baseName}}"{{^required}}, required = false{{/required}}{{#defaultValue}}, defaultValue = "{{{.}}}"{{/defaultValue}}){{>dateTimeParam}} {{>optionalDataType}} {{paramName}}{{/isCookieParam}}
1+
{{#isCookieParam}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}{{>paramDoc}} @CookieValue(name = "{{baseName}}"{{^required}}, required = false{{/required}}{{#defaultValue}}, defaultValue = "{{{.}}}"{{/defaultValue}}){{>dateTimeParam}} {{>nullableAnnotation}}{{>optionalDataType}} {{paramName}}{{/isCookieParam}}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{#isHeaderParam}}{{#vendorExtensions.x-field-extra-annotation}}{{{vendorExtensions.x-field-extra-annotation}}} {{/vendorExtensions.x-field-extra-annotation}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}{{>paramDoc}} @RequestHeader(value = "{{baseName}}", required = {{#required}}true{{/required}}{{^required}}false{{/required}}{{#defaultValue}}, defaultValue = "{{{.}}}"{{/defaultValue}}){{>dateTimeParam}} {{>optionalDataType}} {{paramName}}{{/isHeaderParam}}
1+
{{#isHeaderParam}}{{#vendorExtensions.x-field-extra-annotation}}{{{vendorExtensions.x-field-extra-annotation}}} {{/vendorExtensions.x-field-extra-annotation}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}{{>paramDoc}} @RequestHeader(value = "{{baseName}}", required = {{#required}}true{{/required}}{{^required}}false{{/required}}{{#defaultValue}}, defaultValue = "{{{.}}}"{{/defaultValue}}){{>dateTimeParam}} {{>nullableAnnotation}}{{>optionalDataType}} {{paramName}}{{/isHeaderParam}}

modules/openapi-generator/src/main/resources/JavaSpring/libraries/spring-http-interface/api.mustache

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import org.springframework.http.HttpStatus;
1313
{{#useResponseEntity}}
1414
import org.springframework.http.ResponseEntity;
1515
{{/useResponseEntity}}
16+
import org.springframework.lang.Nullable;
1617
import org.springframework.web.bind.annotation.*;
1718
import org.springframework.web.service.annotation.*;
1819
import org.springframework.web.multipart.MultipartFile;

modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache

+3-3
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public {{>sealed}}class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}
154154
{{^lombok.Data}}
155155

156156
{{! begin feature: fluent setter methods }}
157-
public {{classname}} {{name}}({{{datatypeWithEnum}}} {{name}}) {
157+
public {{classname}} {{name}}({{>nullableAnnotation}}{{{datatypeWithEnum}}} {{name}}) {
158158
{{#openApiNullable}}
159159
this.{{name}} = {{#isNullable}}JsonNullable.of({{/isNullable}}{{#useOptional}}{{^required}}{{^isNullable}}{{^isContainer}}Optional.of{{#optionalAcceptNullable}}Nullable{{/optionalAcceptNullable}}({{/isContainer}}{{/isNullable}}{{/required}}{{/useOptional}}{{name}}{{#isNullable}}){{/isNullable}}{{#useOptional}}{{^required}}{{^isNullable}}{{^isContainer}}){{/isContainer}}{{/isNullable}}{{/required}}{{/useOptional}};
160160
{{/openApiNullable}}
@@ -254,7 +254,7 @@ public {{>sealed}}class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}
254254
{{#deprecated}}
255255
@Deprecated
256256
{{/deprecated}}
257-
public {{>nullableDataTypeBeanValidation}} {{getter}}() {
257+
public {{>nullableAnnotation}}{{>nullableDataTypeBeanValidation}} {{getter}}() {
258258
return {{name}};
259259
}
260260
{{/lombok.Getter}}
@@ -271,7 +271,7 @@ public {{>sealed}}class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}
271271
{{#deprecated}}
272272
@Deprecated
273273
{{/deprecated}}
274-
public void {{setter}}({{>nullableDataType}} {{name}}) {
274+
public void {{setter}}({{>nullableAnnotation}}{{>nullableDataType}} {{name}}) {
275275
this.{{name}} = {{name}};
276276
}
277277
{{/lombok.Setter}}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{#isQueryParam}}{{#vendorExtensions.x-field-extra-annotation}}{{{vendorExtensions.x-field-extra-annotation}}} {{/vendorExtensions.x-field-extra-annotation}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}{{>paramDoc}}{{#useBeanValidation}} @Valid{{/useBeanValidation}}{{^isModel}} @RequestParam(value = {{#isMap}}""{{/isMap}}{{^isMap}}"{{baseName}}"{{/isMap}}{{#required}}, required = true{{/required}}{{^required}}, required = false{{/required}}{{#defaultValue}}, defaultValue = "{{{.}}}"{{/defaultValue}}){{/isModel}}{{>dateTimeParam}}{{#isDeprecated}} @Deprecated{{/isDeprecated}} {{>optionalDataType}} {{paramName}}{{/isQueryParam}}
1+
{{#isQueryParam}}{{#vendorExtensions.x-field-extra-annotation}}{{{vendorExtensions.x-field-extra-annotation}}} {{/vendorExtensions.x-field-extra-annotation}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}{{>paramDoc}}{{#useBeanValidation}} @Valid{{/useBeanValidation}}{{^isModel}} @RequestParam(value = {{#isMap}}""{{/isMap}}{{^isMap}}"{{baseName}}"{{/isMap}}{{#required}}, required = true{{/required}}{{^required}}, required = false{{/required}}{{#defaultValue}}, defaultValue = "{{{.}}}"{{/defaultValue}}){{/isModel}}{{>dateTimeParam}}{{#isDeprecated}} @Deprecated{{/isDeprecated}} {{>nullableAnnotation}}{{>optionalDataType}} {{paramName}}{{/isQueryParam}}

samples/client/petstore/spring-cloud-auth/src/main/java/org/openapitools/api/SomeApi.java

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import org.springframework.http.HttpStatus;
99
import org.springframework.http.MediaType;
10+
import org.springframework.lang.Nullable;
1011
import org.springframework.http.ResponseEntity;
1112
import org.springframework.validation.annotation.Validated;
1213
import org.springframework.web.bind.annotation.*;

samples/client/petstore/spring-cloud-date-time/src/main/java/org/openapitools/api/DefaultApi.java

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import io.swagger.annotations.*;
1212
import org.springframework.http.HttpStatus;
1313
import org.springframework.http.MediaType;
14+
import org.springframework.lang.Nullable;
1415
import org.springframework.http.ResponseEntity;
1516
import org.springframework.validation.annotation.Validated;
1617
import org.springframework.web.bind.annotation.*;

samples/client/petstore/spring-cloud-deprecated/src/main/java/org/openapitools/api/PetApi.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.swagger.v3.oas.annotations.media.ExampleObject;
2222
import org.springframework.http.HttpStatus;
2323
import org.springframework.http.MediaType;
24+
import org.springframework.lang.Nullable;
2425
import org.springframework.http.ResponseEntity;
2526
import org.springframework.validation.annotation.Validated;
2627
import org.springframework.web.bind.annotation.*;
@@ -96,7 +97,7 @@ ResponseEntity<Void> addPet(
9697

9798
ResponseEntity<Void> deletePet(
9899
@Parameter(name = "petId", description = "Pet id to delete", required = true, in = ParameterIn.PATH) @PathVariable("petId") Long petId,
99-
@Parameter(name = "api_key", description = "", in = ParameterIn.HEADER) @RequestHeader(value = "api_key", required = false) String apiKey
100+
@Parameter(name = "api_key", description = "", in = ParameterIn.HEADER) @RequestHeader(value = "api_key", required = false) @Nullable String apiKey
100101
);
101102

102103

samples/client/petstore/spring-cloud-deprecated/src/main/java/org/openapitools/api/StoreApi.java

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.swagger.v3.oas.annotations.media.ExampleObject;
2222
import org.springframework.http.HttpStatus;
2323
import org.springframework.http.MediaType;
24+
import org.springframework.lang.Nullable;
2425
import org.springframework.http.ResponseEntity;
2526
import org.springframework.validation.annotation.Validated;
2627
import org.springframework.web.bind.annotation.*;

samples/client/petstore/spring-cloud-deprecated/src/main/java/org/openapitools/api/UserApi.java

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.swagger.v3.oas.annotations.media.ExampleObject;
2222
import org.springframework.http.HttpStatus;
2323
import org.springframework.http.MediaType;
24+
import org.springframework.lang.Nullable;
2425
import org.springframework.http.ResponseEntity;
2526
import org.springframework.validation.annotation.Validated;
2627
import org.springframework.web.bind.annotation.*;

samples/client/petstore/spring-cloud-deprecated/src/main/java/org/openapitools/model/Category.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class Category {
2727

2828
private @Nullable String name;
2929

30-
public Category id(Long id) {
30+
public Category id(@Nullable Long id) {
3131
this.id = id;
3232
return this;
3333
}
@@ -39,15 +39,15 @@ public Category id(Long id) {
3939

4040
@Schema(name = "id", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
4141
@JsonProperty("id")
42-
public Long getId() {
42+
public @Nullable Long getId() {
4343
return id;
4444
}
4545

46-
public void setId(Long id) {
46+
public void setId(@Nullable Long id) {
4747
this.id = id;
4848
}
4949

50-
public Category name(String name) {
50+
public Category name(@Nullable String name) {
5151
this.name = name;
5252
return this;
5353
}
@@ -59,11 +59,11 @@ public Category name(String name) {
5959
@Pattern(regexp = "^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$")
6060
@Schema(name = "name", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
6161
@JsonProperty("name")
62-
public String getName() {
62+
public @Nullable String getName() {
6363
return name;
6464
}
6565

66-
public void setName(String name) {
66+
public void setName(@Nullable String name) {
6767
this.name = name;
6868
}
6969

samples/client/petstore/spring-cloud-deprecated/src/main/java/org/openapitools/model/ModelApiResponse.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class ModelApiResponse {
3131

3232
private @Nullable String message;
3333

34-
public ModelApiResponse code(Integer code) {
34+
public ModelApiResponse code(@Nullable Integer code) {
3535
this.code = code;
3636
return this;
3737
}
@@ -43,15 +43,15 @@ public ModelApiResponse code(Integer code) {
4343

4444
@Schema(name = "code", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
4545
@JsonProperty("code")
46-
public Integer getCode() {
46+
public @Nullable Integer getCode() {
4747
return code;
4848
}
4949

50-
public void setCode(Integer code) {
50+
public void setCode(@Nullable Integer code) {
5151
this.code = code;
5252
}
5353

54-
public ModelApiResponse type(String type) {
54+
public ModelApiResponse type(@Nullable String type) {
5555
this.type = type;
5656
return this;
5757
}
@@ -63,15 +63,15 @@ public ModelApiResponse type(String type) {
6363

6464
@Schema(name = "type", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
6565
@JsonProperty("type")
66-
public String getType() {
66+
public @Nullable String getType() {
6767
return type;
6868
}
6969

70-
public void setType(String type) {
70+
public void setType(@Nullable String type) {
7171
this.type = type;
7272
}
7373

74-
public ModelApiResponse message(String message) {
74+
public ModelApiResponse message(@Nullable String message) {
7575
this.message = message;
7676
return this;
7777
}
@@ -83,11 +83,11 @@ public ModelApiResponse message(String message) {
8383

8484
@Schema(name = "message", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
8585
@JsonProperty("message")
86-
public String getMessage() {
86+
public @Nullable String getMessage() {
8787
return message;
8888
}
8989

90-
public void setMessage(String message) {
90+
public void setMessage(@Nullable String message) {
9191
this.message = message;
9292
}
9393

samples/client/petstore/spring-cloud-deprecated/src/main/java/org/openapitools/model/Order.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static StatusEnum fromValue(String value) {
7878

7979
private Boolean complete = false;
8080

81-
public Order id(Long id) {
81+
public Order id(@Nullable Long id) {
8282
this.id = id;
8383
return this;
8484
}
@@ -90,15 +90,15 @@ public Order id(Long id) {
9090

9191
@Schema(name = "id", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
9292
@JsonProperty("id")
93-
public Long getId() {
93+
public @Nullable Long getId() {
9494
return id;
9595
}
9696

97-
public void setId(Long id) {
97+
public void setId(@Nullable Long id) {
9898
this.id = id;
9999
}
100100

101-
public Order petId(Long petId) {
101+
public Order petId(@Nullable Long petId) {
102102
this.petId = petId;
103103
return this;
104104
}
@@ -110,15 +110,15 @@ public Order petId(Long petId) {
110110

111111
@Schema(name = "petId", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
112112
@JsonProperty("petId")
113-
public Long getPetId() {
113+
public @Nullable Long getPetId() {
114114
return petId;
115115
}
116116

117-
public void setPetId(Long petId) {
117+
public void setPetId(@Nullable Long petId) {
118118
this.petId = petId;
119119
}
120120

121-
public Order quantity(Integer quantity) {
121+
public Order quantity(@Nullable Integer quantity) {
122122
this.quantity = quantity;
123123
return this;
124124
}
@@ -130,15 +130,15 @@ public Order quantity(Integer quantity) {
130130

131131
@Schema(name = "quantity", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
132132
@JsonProperty("quantity")
133-
public Integer getQuantity() {
133+
public @Nullable Integer getQuantity() {
134134
return quantity;
135135
}
136136

137-
public void setQuantity(Integer quantity) {
137+
public void setQuantity(@Nullable Integer quantity) {
138138
this.quantity = quantity;
139139
}
140140

141-
public Order shipDate(OffsetDateTime shipDate) {
141+
public Order shipDate(@Nullable OffsetDateTime shipDate) {
142142
this.shipDate = shipDate;
143143
return this;
144144
}
@@ -150,15 +150,15 @@ public Order shipDate(OffsetDateTime shipDate) {
150150
@Valid
151151
@Schema(name = "shipDate", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
152152
@JsonProperty("shipDate")
153-
public OffsetDateTime getShipDate() {
153+
public @Nullable OffsetDateTime getShipDate() {
154154
return shipDate;
155155
}
156156

157-
public void setShipDate(OffsetDateTime shipDate) {
157+
public void setShipDate(@Nullable OffsetDateTime shipDate) {
158158
this.shipDate = shipDate;
159159
}
160160

161-
public Order status(StatusEnum status) {
161+
public Order status(@Nullable StatusEnum status) {
162162
this.status = status;
163163
return this;
164164
}
@@ -170,11 +170,11 @@ public Order status(StatusEnum status) {
170170

171171
@Schema(name = "status", description = "Order Status", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
172172
@JsonProperty("status")
173-
public StatusEnum getStatus() {
173+
public @Nullable StatusEnum getStatus() {
174174
return status;
175175
}
176176

177-
public void setStatus(StatusEnum status) {
177+
public void setStatus(@Nullable StatusEnum status) {
178178
this.status = status;
179179
}
180180

samples/client/petstore/spring-cloud-deprecated/src/main/java/org/openapitools/model/Pet.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public Pet(String name, List<String> photoUrls) {
9393
this.photoUrls = photoUrls;
9494
}
9595

96-
public Pet id(Long id) {
96+
public Pet id(@Nullable Long id) {
9797
this.id = id;
9898
return this;
9999
}
@@ -105,15 +105,15 @@ public Pet id(Long id) {
105105

106106
@Schema(name = "id", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
107107
@JsonProperty("id")
108-
public Long getId() {
108+
public @Nullable Long getId() {
109109
return id;
110110
}
111111

112-
public void setId(Long id) {
112+
public void setId(@Nullable Long id) {
113113
this.id = id;
114114
}
115115

116-
public Pet category(Category category) {
116+
public Pet category(@Nullable Category category) {
117117
this.category = category;
118118
return this;
119119
}
@@ -125,11 +125,11 @@ public Pet category(Category category) {
125125
@Valid
126126
@Schema(name = "category", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
127127
@JsonProperty("category")
128-
public Category getCategory() {
128+
public @Nullable Category getCategory() {
129129
return category;
130130
}
131131

132-
public void setCategory(Category category) {
132+
public void setCategory(@Nullable Category category) {
133133
this.category = category;
134134
}
135135

@@ -215,7 +215,7 @@ public void setTags(List<@Valid Tag> tags) {
215215
this.tags = tags;
216216
}
217217

218-
public Pet status(StatusEnum status) {
218+
public Pet status(@Nullable StatusEnum status) {
219219
this.status = status;
220220
return this;
221221
}
@@ -227,11 +227,11 @@ public Pet status(StatusEnum status) {
227227

228228
@Schema(name = "status", description = "pet status in the store", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
229229
@JsonProperty("status")
230-
public StatusEnum getStatus() {
230+
public @Nullable StatusEnum getStatus() {
231231
return status;
232232
}
233233

234-
public void setStatus(StatusEnum status) {
234+
public void setStatus(@Nullable StatusEnum status) {
235235
this.status = status;
236236
}
237237

0 commit comments

Comments
 (0)