Skip to content

Commit 7ad0fe0

Browse files
authored
[Java][Spring] useRecords: no @NotNull on records without bean validation (#25265)
1 parent 8e382a3 commit 7ad0fe0

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

‎modules/openapi-generator/src/main/resources/JavaSpring/record.mustache‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ public record {{classname}}(
6868
{{#useBeanValidation}}
6969
{{>beanValidation}}{{! prevent indent}}
7070
{{/useBeanValidation}}
71-
{{^useBeanValidation}}
72-
{{#lambda.trim}}{{>notNull}}{{/lambda.trim}}
73-
{{/useBeanValidation}}
7471
{{#swagger2AnnotationLibrary}}
7572
@Schema(name = "{{{baseName}}}"{{#isReadOnly}}, accessMode = Schema.AccessMode.READ_ONLY{{/isReadOnly}}{{#example}}, example = "{{{.}}}"{{/example}}{{#description}}, description = "{{{.}}}"{{/description}}{{#deprecated}}, deprecated = true{{/deprecated}}, requiredMode = {{#required}}Schema.RequiredMode.REQUIRED{{/required}}{{^required}}Schema.RequiredMode.NOT_REQUIRED{{/required}}{{#isNullable}}, nullable = true{{/isNullable}})
7673
{{/swagger2AnnotationLibrary}}

‎modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10442,6 +10442,34 @@ public void useRecordsWithoutOpenApiNullable() throws IOException {
1044210442
.doesNotContain("JsonNullable", "if (nickname == null)");
1044310443
}
1044410444

10445+
@Test
10446+
public void useRecordsWithoutBeanValidationHasNoNotNullAnnotationNorImport_issue23751() throws IOException {
10447+
Map<String, File> files = generateRecords(RECORDS_SPEC, Map.of(SpringCodegen.USE_BEANVALIDATION, "false"));
10448+
10449+
assertSource(files, "Pet.java")
10450+
.contains("public record Pet(", "String name,")
10451+
.doesNotContain("@NotNull", "@Size", "validation.constraints");
10452+
assertSource(files, "Owner.java")
10453+
.doesNotContain("@NotNull", "validation.constraints");
10454+
}
10455+
10456+
@Test
10457+
public void useRecordsWithoutBeanValidationHasNoRequiredNullableMarker_issue23751() throws IOException {
10458+
Map<String, File> files = generateRecords(RECORDS_FIELDS_SPEC, Map.of(SpringCodegen.USE_BEANVALIDATION, "false"));
10459+
10460+
assertSource(files, "Defaults.java")
10461+
.contains("public record Defaults(", "JsonNullable<String> reqNullable,")
10462+
.doesNotContain("@NotNull", "@Present", "validation.constraints");
10463+
}
10464+
10465+
@Test
10466+
public void useRecordsWithBeanValidationKeepsNotNullAndItsImport() throws IOException {
10467+
Map<String, File> files = generateRecords(RECORDS_SPEC, Map.of(SpringCodegen.USE_BEANVALIDATION, "true"));
10468+
10469+
assertSource(files, "Pet.java")
10470+
.contains("@NotNull @Size(min = 2)", "import jakarta.validation.constraints.*;");
10471+
}
10472+
1044510473
@Test
1044610474
public void useRecordsHasOnlyTheCanonicalConstructor() throws IOException {
1044710475
for (Map<String, Object> options : List.<Map<String, Object>>of(

0 commit comments

Comments
 (0)