Skip to content

Commit

Permalink
[java] No @NotNull annotation for readOnly (required) attributes - fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-sc committed Feb 15, 2022
1 parent 380aaa5 commit f0d1dc4
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{{#required}}
{{^isReadOnly}}
@NotNull
{{/isReadOnly}}
{{/required}}
{{#isContainer}}
{{^isPrimitiveType}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{{#required}}
{{^isReadOnly}}
@NotNull
{{/isReadOnly}}
{{/required}}
{{>beanValidationCore}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{#required}}@NotNull {{/required}}{{#isContainer}}{{^isPrimitiveType}}{{^isEnum}}@Valid {{/isEnum}}{{/isPrimitiveType}}{{/isContainer}}{{^isContainer}}{{^isPrimitiveType}}@Valid {{/isPrimitiveType}}{{/isContainer}}{{>beanValidationCore}}
{{#required}}{{^isReadOnly}}@NotNull {{/isReadOnly}}{{/required}}{{#isContainer}}{{^isPrimitiveType}}{{^isEnum}}@Valid {{/isEnum}}{{/isPrimitiveType}}{{/isContainer}}{{^isContainer}}{{^isPrimitiveType}}@Valid {{/isPrimitiveType}}{{/isContainer}}{{>beanValidationCore}}
Original file line number Diff line number Diff line change
Expand Up @@ -793,11 +793,73 @@ public void testConfigFileGeneration() {
assertEquals(desFile, DESTINATIONFILE);
}
}
if(!flag){
if (!flag) {
fail("OpenAPIDocumentationConfig.java not generated");
}
}

@Test
public void shouldAddNotNullOnRequiredAttributes() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
String outputPath = output.getAbsolutePath().replace('\\', '/');

final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/spring/issue_5026-b.yaml");
final SpringCodegen codegen = new SpringCodegen();
codegen.setOpenAPI(openAPI);
codegen.setOutputDir(output.getAbsolutePath());

ClientOptInput input = new ClientOptInput();
input.openAPI(openAPI);
input.config(codegen);

DefaultGenerator generator = new DefaultGenerator();

generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");

generator.opts(input).generate();

assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/model/Dummy.java"), "status");
assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/model/Dummy.java"), "@NotNull");
Files.readAllLines(Paths.get(outputPath + "/src/main/java/org/openapitools/model/Dummy.java")).forEach(System.out::println);

}

@Test
public void shouldNotAddNotNullOnReadOnlyAttributes() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
String outputPath = output.getAbsolutePath().replace('\\', '/');

final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/spring/issue_5026.yaml");
final SpringCodegen codegen = new SpringCodegen();
codegen.setOpenAPI(openAPI);
codegen.setOutputDir(output.getAbsolutePath());

ClientOptInput input = new ClientOptInput();
input.openAPI(openAPI);
input.config(codegen);

DefaultGenerator generator = new DefaultGenerator();

generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");

generator.opts(input).generate();

assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/model/Dummy.java"), "status");
assertFileNotContains(Paths.get(outputPath + "/src/main/java/org/openapitools/model/Dummy.java"), "@NotNull");
Files.readAllLines(Paths.get(outputPath + "/src/main/java/org/openapitools/model/Dummy.java")).forEach(System.out::println);

}

@Test
public void testTypeMappings() {
final SpringCodegen codegen = new SpringCodegen();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
openapi: 3.0.0
info:
version: "1.0.0"
title: reactive-spring-boot-request-body-issue
tags:
- name: ReactiveSpringBootRequestBodyIssue
paths:
/some/dummy/endpoint:
post:
tags:
- ReactiveSpringBootRequestBodyIssue
requestBody:
description: request
content:
application/json:
schema:
$ref: '#/components/schemas/Dummy'
required: true
responses:
200:
description: Successfully created reverse listings for retail
content:
application/json:
schema:
$ref: '#/components/schemas/Dummy'
components:
schemas:
Dummy:
required:
- status
type: object
properties:
status:
type: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
openapi: 3.0.0
info:
version: "1.0.0"
title: reactive-spring-boot-request-body-issue
tags:
- name: ReactiveSpringBootRequestBodyIssue
paths:
/some/dummy/endpoint:
post:
tags:
- ReactiveSpringBootRequestBodyIssue
requestBody:
description: request
content:
application/json:
schema:
$ref: '#/components/schemas/Dummy'
required: true
responses:
200:
description: Successfully created reverse listings for retail
content:
application/json:
schema:
$ref: '#/components/schemas/Dummy'
components:
schemas:
Dummy:
required:
- status
type: object
properties:
status:
type: string
readOnly: true

0 comments on commit f0d1dc4

Please sign in to comment.