diff --git a/.github/workflows/samples-spring-jdk17.yaml b/.github/workflows/samples-spring-jdk17.yaml
index 9e48ff2f74d7..70cb0eb415eb 100644
--- a/.github/workflows/samples-spring-jdk17.yaml
+++ b/.github/workflows/samples-spring-jdk17.yaml
@@ -3,32 +3,32 @@ name: Samples Java Spring (JDK17)
on:
push:
paths:
- - samples/openapi3/client/petstore/spring-cloud-3-with-optional
- - samples/openapi3/client/petstore/spring-cloud-4-with-optional
- - samples/client/petstore/spring-http-interface-springboot-4
- - samples/openapi3/server/petstore/springboot-3
- - samples/openapi3/server/petstore/springboot-4
- - samples/server/petstore/springboot-api-response-examples
- - samples/server/petstore/springboot-lombok-data
- - samples/server/petstore/springboot-lombok-tostring
- - samples/server/petstore/springboot-file-delegate-optional
- - samples/server/petstore/springboot-petstore-with-api-response-examples
- - samples/server/petstore/spring-boot-oneof-sealed
- - samples/openapi3/server/petstore/spring-boot-oneof-interface
+ - samples/openapi3/client/petstore/spring-cloud-3-with-optional/**
+ - samples/openapi3/client/petstore/spring-cloud-4-with-optional/**
+ - samples/client/petstore/spring-http-interface-springboot-4/**
+ - samples/openapi3/server/petstore/springboot-3/**
+ - samples/openapi3/server/petstore/springboot-4/**
+ - samples/server/petstore/springboot-api-response-examples/**
+ - samples/server/petstore/springboot-lombok-data/**
+ - samples/server/petstore/springboot-lombok-tostring/**
+ - samples/server/petstore/springboot-file-delegate-optional/**
+ - samples/server/petstore/springboot-petstore-with-api-response-examples/**
+ - samples/openapi3/server/petstore/spring-boot-oneof-sealed/**
+ - samples/openapi3/server/petstore/spring-boot-oneof-interface/**
pull_request:
paths:
- - samples/openapi3/client/petstore/spring-cloud-3-with-optional
- - samples/openapi3/client/petstore/spring-cloud-4-with-optional
- - samples/client/petstore/spring-http-interface-springboot-4
- - samples/openapi3/server/petstore/springboot-3
- - samples/openapi3/server/petstore/springboot-4
- - samples/server/petstore/springboot-api-response-examples
- - samples/server/petstore/springboot-lombok-data
- - samples/server/petstore/springboot-lombok-tostring
- - samples/server/petstore/springboot-file-delegate-optional
- - samples/server/petstore/springboot-petstore-with-api-response-examples
- - samples/server/petstore/spring-boot-oneof-sealed
- - samples/openapi3/server/petstore/spring-boot-oneof-interface
+ - samples/openapi3/client/petstore/spring-cloud-3-with-optional/**
+ - samples/openapi3/client/petstore/spring-cloud-4-with-optional/**
+ - samples/client/petstore/spring-http-interface-springboot-4/**
+ - samples/openapi3/server/petstore/springboot-3/**
+ - samples/openapi3/server/petstore/springboot-4/**
+ - samples/server/petstore/springboot-api-response-examples/**
+ - samples/server/petstore/springboot-lombok-data/**
+ - samples/server/petstore/springboot-lombok-tostring/**
+ - samples/server/petstore/springboot-file-delegate-optional/**
+ - samples/server/petstore/springboot-petstore-with-api-response-examples/**
+ - samples/openapi3/server/petstore/spring-boot-oneof-sealed/**
+ - samples/openapi3/server/petstore/spring-boot-oneof-interface/**
jobs:
build:
name: Build Java Spring (JDK17)
@@ -49,7 +49,7 @@ jobs:
- samples/server/petstore/springboot-lombok-tostring
- samples/server/petstore/springboot-file-delegate-optional
- samples/server/petstore/springboot-petstore-with-api-response-examples
- - samples/server/petstore/spring-boot-oneof-sealed
+ - samples/openapi3/server/petstore/spring-boot-oneof-sealed
- samples/openapi3/server/petstore/spring-boot-oneof-interface
steps:
- uses: actions/checkout@v5
diff --git a/bin/configs/spring-boot-x-implements-skip.yaml b/bin/configs/spring-boot-x-implements-skip.yaml
index 829c029bc015..de98b7027120 100644
--- a/bin/configs/spring-boot-x-implements-skip.yaml
+++ b/bin/configs/spring-boot-x-implements-skip.yaml
@@ -3,6 +3,7 @@ outputDir: samples/server/petstore/springboot-x-implements-skip
inputSpec: modules/openapi-generator/src/test/resources/3_0/spring/petstore-with-fake-endpoints-models-for-testing-x-implements.yaml
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
additionalProperties:
+ additionalNotNullAnnotations: true
documentationProvider: springfox
artifactId: springboot
snapshotVersion: "true"
@@ -12,4 +13,4 @@ additionalProperties:
xImplementsSkip: [ com.custompackage.InterfaceToSkip ]
schemaImplements:
Foo: [ com.custompackage.WithBar, com.custompackage.WithDefaultMethod ]
- Animal: com.custompackage.WithColor
\ No newline at end of file
+ Animal: com.custompackage.WithColor
diff --git a/docs/generators/java-camel.md b/docs/generators/java-camel.md
index ecddb05f4d31..b71808af91c3 100644
--- a/docs/generators/java-camel.md
+++ b/docs/generators/java-camel.md
@@ -20,6 +20,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
| ------ | ----------- | ------ | ------- |
|additionalEnumTypeAnnotations|Additional annotations for enum type(class level annotations)| |null|
|additionalModelTypeAnnotations|Additional annotations for model type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
+|additionalNotNullAnnotations|Add @NotNull to path variables (required by default) and requestBody.| |false|
|additionalOneOfTypeAnnotations|Additional annotations for oneOf interfaces(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
|allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false|
|annotationLibrary|Select the complementary documentation annotation library.|
- **none**
- Do not annotate Model and Api with complementary annotations.
- **swagger1**
- Annotate Model and Api using the Swagger Annotations 1.x library.
- **swagger2**
- Annotate Model and Api using the Swagger Annotations 2.x library.
|swagger2|
diff --git a/docs/generators/spring.md b/docs/generators/spring.md
index aa3cf52bb706..08649ac0e6a5 100644
--- a/docs/generators/spring.md
+++ b/docs/generators/spring.md
@@ -20,6 +20,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
| ------ | ----------- | ------ | ------- |
|additionalEnumTypeAnnotations|Additional annotations for enum type(class level annotations)| |null|
|additionalModelTypeAnnotations|Additional annotations for model type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
+|additionalNotNullAnnotations|Add @NotNull to path variables (required by default) and requestBody.| |false|
|additionalOneOfTypeAnnotations|Additional annotations for oneOf interfaces(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
|allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false|
|annotationLibrary|Select the complementary documentation annotation library.|- **none**
- Do not annotate Model and Api with complementary annotations.
- **swagger1**
- Annotate Model and Api using the Swagger Annotations 1.x library.
- **swagger2**
- Annotate Model and Api using the Swagger Annotations 2.x library.
|swagger2|
diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java
index 9b94407764a9..361ab9de5521 100644
--- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java
+++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java
@@ -108,6 +108,7 @@ public class SpringCodegen extends AbstractJavaCodegen
public static final String JACKSON2_PACKAGE = "com.fasterxml.jackson";
public static final String JACKSON3_PACKAGE = "tools.jackson";
public static final String JACKSON_PACKAGE = "jacksonPackage";
+ public static final String ADDITIONAL_NOT_NULL_ANNOTATIONS = "additionalNotNullAnnotations";
@Getter
public enum RequestMappingMode {
@@ -180,6 +181,8 @@ public enum RequestMappingMode {
protected boolean useDeductionForOneOfInterfaces = false;
@Getter @Setter
protected boolean useJackson3 = false;
+ @Getter @Setter
+ protected boolean additionalNotNullAnnotations = false;
public SpringCodegen() {
super();
@@ -269,7 +272,6 @@ public SpringCodegen() {
cliOptions.add(CliOption.newString(X_IMPLEMENTS_SKIP, "Ability to choose interfaces that should NOT be implemented in the models despite their presence in vendor extension `x-implements`. Takes a list of fully qualified interface names. Example: yaml `xImplementsSkip: [com.some.pack.WithPhotoUrls]` skips implementing the interface `com.some.pack.WithPhotoUrls` in any schema", "empty list"));
cliOptions.add(CliOption.newString(SCHEMA_IMPLEMENTS, "Ability to supply interfaces per schema that should be implemented (serves similar purpose as vendor extension `x-implements`, but is fully decoupled from the api spec). Example: yaml `schemaImplements: {Pet: com.some.pack.WithId, Category: [com.some.pack.CategoryInterface], Dog: [com.some.pack.Canine, com.some.pack.OtherInterface]}` implements interfaces in schemas `Pet` (interface `com.some.pack.WithId`), `Category` (interface `com.some.pack.CategoryInterface`), `Dog`(interfaces `com.some.pack.Canine`, `com.some.pack.OtherInterface`)", "empty map"));
-
CliOption requestMappingOpt = new CliOption(REQUEST_MAPPING_OPTION,
"Where to generate the class level @RequestMapping annotation.")
.defaultValue(requestMappingMode.name());
@@ -325,6 +327,10 @@ public SpringCodegen() {
library.setEnum(supportedLibraries);
cliOptions.add(library);
+ cliOptions.add(CliOption.newBoolean(ADDITIONAL_NOT_NULL_ANNOTATIONS,
+ "Add @NotNull to path variables (required by default) and requestBody.",
+ additionalNotNullAnnotations));
+
}
@Override
@@ -532,6 +538,7 @@ public void processOpts() {
}
convertPropertyToStringAndWriteBack(RESOURCE_FOLDER, this::setResourceFolder);
+ convertPropertyToBooleanAndWriteBack(ADDITIONAL_NOT_NULL_ANNOTATIONS, this::setAdditionalNotNullAnnotations);
// override parent one
importMapping.put("JsonDeserialize", (useJackson3 ? JACKSON3_PACKAGE : JACKSON2_PACKAGE) + ".databind.annotation.JsonDeserialize");
diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/beanValidationBodyParams.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/beanValidationBodyParams.mustache
index 1f64c92fe34b..2aeccfa4b137 100644
--- a/modules/openapi-generator/src/main/resources/JavaSpring/beanValidationBodyParams.mustache
+++ b/modules/openapi-generator/src/main/resources/JavaSpring/beanValidationBodyParams.mustache
@@ -1 +1 @@
-{{^useOptional}}{{#required}}@NotNull {{/required}}{{>beanValidationCore}}{{/useOptional}}{{#useOptional}}{{#required}}{{>beanValidationCore}}{{/required}}{{/useOptional}}
\ No newline at end of file
+{{^useOptional}}{{#additionalNotNullAnnotations}}{{#required}}@NotNull {{/required}}{{/additionalNotNullAnnotations}}{{>beanValidationCore}}{{/useOptional}}{{#useOptional}}{{#required}}{{>beanValidationCore}}{{/required}}{{/useOptional}}
\ No newline at end of file
diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/beanValidationPathParams.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/beanValidationPathParams.mustache
index 84b68052401c..93b6ea9671dc 100644
--- a/modules/openapi-generator/src/main/resources/JavaSpring/beanValidationPathParams.mustache
+++ b/modules/openapi-generator/src/main/resources/JavaSpring/beanValidationPathParams.mustache
@@ -1 +1 @@
-{{! PathParam is always required }}@NotNull {{>beanValidationCore}}
\ No newline at end of file
+{{! PathParam is always required }}{{#additionalNotNullAnnotations}}@NotNull {{/additionalNotNullAnnotations}}{{>beanValidationCore}}
\ No newline at end of file
diff --git a/samples/client/petstore/spring-cloud-date-time/src/main/java/org/openapitools/api/DefaultApi.java b/samples/client/petstore/spring-cloud-date-time/src/main/java/org/openapitools/api/DefaultApi.java
index ad851c76c7ce..34b40fb977bc 100644
--- a/samples/client/petstore/spring-cloud-date-time/src/main/java/org/openapitools/api/DefaultApi.java
+++ b/samples/client/petstore/spring-cloud-date-time/src/main/java/org/openapitools/api/DefaultApi.java
@@ -50,7 +50,7 @@ public interface DefaultApi {
value = DefaultApi.PATH_GET
)
ResponseEntity get(
- @NotNull @ApiParam(value = "A date path parameter", required = true, defaultValue = "1972-01-01") @PathVariable("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date,
+ @ApiParam(value = "A date path parameter", required = true, defaultValue = "1972-01-01") @PathVariable("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date,
@NotNull @ApiParam(value = "A date-time query parameter", required = true, defaultValue = "1973-12-19T03:39:57-08:00") @Valid @RequestParam(value = "dateTime", required = true, defaultValue = "1973-12-19T03:39:57-08:00") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) OffsetDateTime dateTime,
@NotNull @ApiParam(value = "A date header parameter", required = true, defaultValue = "1974-01-01") @RequestHeader(value = "X-Order-Date", required = true, defaultValue = "1974-01-01") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate xOrderDate,
@ApiParam(value = "A date cookie parameter", defaultValue = "1975-01-01") @CookieValue(name = "loginDate", required = false, defaultValue = "1975-01-01") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate loginDate
@@ -80,7 +80,7 @@ ResponseEntity get(
consumes = "application/x-www-form-urlencoded"
)
ResponseEntity updatePetWithForm(
- @NotNull @ApiParam(value = "A date path parameter", required = true, defaultValue = "1970-01-01") @PathVariable("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date,
+ @ApiParam(value = "A date path parameter", required = true, defaultValue = "1970-01-01") @PathVariable("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date,
@ApiParam(value = "Updated last visit timestamp", defaultValue = "1971-12-19T03:39:57-08:00") @Valid @RequestParam(value = "visitDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) OffsetDateTime visitDate
);
diff --git a/samples/client/petstore/spring-cloud-deprecated/src/main/java/org/openapitools/api/PetApi.java b/samples/client/petstore/spring-cloud-deprecated/src/main/java/org/openapitools/api/PetApi.java
index 9dcaff387677..d582de53eb35 100644
--- a/samples/client/petstore/spring-cloud-deprecated/src/main/java/org/openapitools/api/PetApi.java
+++ b/samples/client/petstore/spring-cloud-deprecated/src/main/java/org/openapitools/api/PetApi.java
@@ -63,7 +63,7 @@ public interface PetApi {
consumes = "application/json"
)
ResponseEntity addPet(
- @Parameter(name = "Pet", description = "Pet object that needs to be added to the store", required = true) @NotNull @Valid @RequestBody Pet pet
+ @Parameter(name = "Pet", description = "Pet object that needs to be added to the store", required = true) @Valid @RequestBody Pet pet
);
@@ -93,7 +93,7 @@ ResponseEntity addPet(
value = PetApi.PATH_DELETE_PET
)
ResponseEntity deletePet(
- @NotNull @Parameter(name = "petId", description = "Pet id to delete", required = true, in = ParameterIn.PATH) @PathVariable("petId") Long petId,
+ @Parameter(name = "petId", description = "Pet id to delete", required = true, in = ParameterIn.PATH) @PathVariable("petId") Long petId,
@Parameter(name = "api_key", description = "", in = ParameterIn.HEADER) @RequestHeader(value = "api_key", required = false) @Nullable String apiKey
);
@@ -204,7 +204,7 @@ ResponseEntity> findPetsByTags(
produces = { "application/json", "application/xml" }
)
ResponseEntity getPetById(
- @NotNull @Parameter(name = "petId", deprecated = true, description = "ID of pet to return", required = true, in = ParameterIn.PATH) @PathVariable("petId") @Deprecated Long petId
+ @Parameter(name = "petId", deprecated = true, description = "ID of pet to return", required = true, in = ParameterIn.PATH) @PathVariable("petId") @Deprecated Long petId
);
@@ -238,7 +238,7 @@ ResponseEntity getPetById(
consumes = "application/json"
)
ResponseEntity updatePet(
- @Parameter(name = "Pet", description = "Pet object that needs to be added to the store", required = true) @NotNull @Valid @RequestBody Pet pet
+ @Parameter(name = "Pet", description = "Pet object that needs to be added to the store", required = true) @Valid @RequestBody Pet pet
);
@@ -270,7 +270,7 @@ ResponseEntity updatePet(
consumes = "application/x-www-form-urlencoded"
)
ResponseEntity updatePetWithForm(
- @NotNull @Parameter(name = "petId", description = "ID of pet that needs to be updated", required = true, in = ParameterIn.PATH) @PathVariable("petId") Long petId,
+ @Parameter(name = "petId", description = "ID of pet that needs to be updated", required = true, in = ParameterIn.PATH) @PathVariable("petId") Long petId,
@Parameter(name = "name", description = "Updated name of the pet") @Valid @RequestParam(value = "name", required = false) String name,
@Parameter(name = "status", description = "Updated status of the pet") @Valid @RequestParam(value = "status", required = false) String status
);
@@ -307,7 +307,7 @@ ResponseEntity updatePetWithForm(
consumes = "multipart/form-data"
)
ResponseEntity uploadFile(
- @NotNull @Parameter(name = "petId", description = "ID of pet to update", required = true, in = ParameterIn.PATH) @PathVariable("petId") Long petId,
+ @Parameter(name = "petId", description = "ID of pet to update", required = true, in = ParameterIn.PATH) @PathVariable("petId") Long petId,
@Parameter(name = "additionalMetadata", description = "Additional data to pass to server") @Valid @RequestParam(value = "additionalMetadata", required = false) String additionalMetadata,
@Parameter(name = "file", description = "file to upload") @RequestPart(value = "file", required = false) MultipartFile file
);
diff --git a/samples/client/petstore/spring-cloud-deprecated/src/main/java/org/openapitools/api/StoreApi.java b/samples/client/petstore/spring-cloud-deprecated/src/main/java/org/openapitools/api/StoreApi.java
index 16ececc1b4be..a6faa8443a19 100644
--- a/samples/client/petstore/spring-cloud-deprecated/src/main/java/org/openapitools/api/StoreApi.java
+++ b/samples/client/petstore/spring-cloud-deprecated/src/main/java/org/openapitools/api/StoreApi.java
@@ -60,7 +60,7 @@ public interface StoreApi {
value = StoreApi.PATH_DELETE_ORDER
)
ResponseEntity deleteOrder(
- @NotNull @Parameter(name = "orderId", description = "ID of the order that needs to be deleted", required = true, in = ParameterIn.PATH) @PathVariable("orderId") String orderId
+ @Parameter(name = "orderId", description = "ID of the order that needs to be deleted", required = true, in = ParameterIn.PATH) @PathVariable("orderId") String orderId
);
@@ -125,7 +125,7 @@ ResponseEntity