Skip to content

Commit ab41c1e

Browse files
authored
Merge pull request #729 from shourien/codegen-issue-728
728 Fix parameter bean validation issues for micronaut code generator
2 parents 74be7ac + bb50efa commit ab41c1e

File tree

6 files changed

+150
-8
lines changed

6 files changed

+150
-8
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{#required}}@NotNull {{/required}}{{^required}}@Nullable {{/required}}{{>beanValidationCore}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{#required}}@NotNull {{/required}}{{>beanValidationCore}}
1+
{{#required}}@NotNull {{/required}}{{^required}}@Nullable {{/required}}{{>beanValidationCore}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{#isHeaderParam}}@Parameter(description = "{{{description}}}" {{#defaultValue}}, defaultValue = "{{{defaultValue}}}"{{/defaultValue}}) @Header(value = "{{baseName}}") {{>optionalDataType}} {{paramName}}{{/isHeaderParam}}
1+
{{#isHeaderParam}}{{#useBeanValidation}}{{>beanValidationHeaderParams}}{{/useBeanValidation}}@Parameter(description = "{{{description}}}" {{#defaultValue}}, defaultValue = "{{{defaultValue}}}"{{/defaultValue}}) {{#useBeanValidation}}@Valid {{/useBeanValidation}}@Header(value = "{{baseName}}") {{>optionalDataType}} {{paramName}}{{/isHeaderParam}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{#isQueryParam}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}@Parameter(description = "{{{description}}}"{{#defaultValue}}, defaultValue = "{{{defaultValue}}}"{{/defaultValue}}) {{#useBeanValidation}}@Valid {{#required}}@NotNull {{/required}}{{/useBeanValidation}}@QueryValue(value = "{{baseName}}"{{#defaultValue}}, defaultValue = "{{{defaultValue}}}"{{/defaultValue}}) {{>optionalDataType}} {{paramName}}{{/isQueryParam}}
1+
{{#isQueryParam}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}@Parameter(description = "{{{description}}}"{{#defaultValue}}, defaultValue = "{{{defaultValue}}}"{{/defaultValue}}) {{#useBeanValidation}}@Valid {{/useBeanValidation}}@QueryValue(value = "{{baseName}}"{{#defaultValue}}, defaultValue = "{{{defaultValue}}}"{{/defaultValue}}) {{>optionalDataType}} {{paramName}}{{/isQueryParam}}

src/test/java/io/swagger/codegen/v3/generators/java/MicronautGeneratorCodegenTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void generateCode() throws IOException {
2626

2727
final CodegenConfigurator configurator = new CodegenConfigurator()
2828
.setLang("micronaut")
29-
.setInputSpecURL("src/test/resources/3_0_0/parameterOrder.yaml")
29+
.setInputSpecURL("src/test/resources/3_0_0/parameterValidation.yaml")
3030
.setOutputDir(output.getAbsolutePath());
3131

3232
final ClientOptInput clientOptInput = configurator.toClientOptInput();
@@ -57,11 +57,12 @@ public void testApiInterface() throws IOException {
5757
Assert.assertTrue(content.contains(expectedContent));
5858
}
5959

60-
@Test(description = "verify that parameters are listed as follows: header, query, path, cookie, body")
60+
@Test(description = "verify that parameters are listed as follows: header, path, query, cookie, body")
6161
public void testApiParameters() throws IOException {
62-
final String expectedContent = "default Single<HttpResponse<LocalizedText>> updateTest(@Parameter(description = \"Localized Text object containing updated data.\" ) @Valid @Body LocalizedText body" + System.lineSeparator()
63-
+ ",@Parameter(description = \"description\") @PathVariable(\"id\") Long id" + System.lineSeparator()
64-
+ ")";
62+
final String expectedContent = "default Single<HttpResponse<LocalizedText>> updateTest(@Parameter(description = \"Localized Text object.\" ) @Valid @Body LocalizedText body" + System.lineSeparator()
63+
+ ",@NotNull @Pattern(regexp=\"[0-9]+\") @Parameter(description = \"header description\" ) @Valid @Header(value = \"x-header\") String xHeader" + System.lineSeparator()
64+
+ ",@Parameter(description = \"path description\") @PathVariable(\"id\") Long id" + System.lineSeparator()
65+
+ ",@Nullable @Parameter(description = \"query description\") @Valid @QueryValue(value = \"name\") String name";
6566
final File controllerFile = new File(output, "/src/main/java/io/swagger/api/AdminApi.java");
6667
final String content = FileUtils.readFileToString(controllerFile);
6768
Assert.assertTrue(content.contains(expectedContent));
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
openapi: 3.0.3
2+
info:
3+
title: Title
4+
version: "2.0.0"
5+
description: test
6+
contact:
7+
name: test contact
8+
url: 'http://www.contact.com'
9+
security:
10+
- OAuth2: []
11+
paths:
12+
'/admin/texts/{id}':
13+
parameters:
14+
- name: x-header
15+
in: header
16+
description: header description
17+
required: true
18+
schema:
19+
type: string
20+
pattern: "[0-9]+"
21+
- name: name
22+
in: query
23+
description: query description
24+
schema:
25+
type: string
26+
- name: id
27+
in: path
28+
description: path description
29+
required: true
30+
schema:
31+
type: integer
32+
format: int64
33+
put:
34+
summary: Update
35+
description: description
36+
operationId: updateTest
37+
responses:
38+
200:
39+
description: Successfully updated
40+
content:
41+
application/json:
42+
schema:
43+
$ref: '#/components/schemas/LocalizedText'
44+
requestBody:
45+
content:
46+
application/json:
47+
schema:
48+
$ref: '#/components/schemas/LocalizedText'
49+
description: Localized Text object.
50+
required: true
51+
x-ecc-validate:
52+
validated: [Update]
53+
servers:
54+
- url: 'http://localhost:8082/v1'
55+
- url: 'https://localhost:8082/v1'
56+
components:
57+
schemas:
58+
BaseAudit:
59+
type: object
60+
description: Base audit fields
61+
properties:
62+
created:
63+
type: string
64+
format: date-time
65+
description: Indicates time of creation.
66+
modified:
67+
type: string
68+
format: date-time
69+
description: Indicates time of last modification.
70+
createdBy:
71+
type: string
72+
description: Identification of creator.
73+
modifiedBy:
74+
type: string
75+
description: Identification of modifier.
76+
BaseRecordVersion:
77+
type: object
78+
description: BaseRecordVersion
79+
properties:
80+
recordVersion:
81+
type: integer
82+
x-ecc-validate:
83+
check-null: [Create]
84+
check-not-null: [Update]
85+
check-required: [Patch, BulkPatch]
86+
description: 'Timestamp from last update, not to be updated directly'
87+
format: int64
88+
LocalizedText:
89+
type: object
90+
required:
91+
- module
92+
- locale
93+
- key
94+
- defaultValue
95+
allOf:
96+
- $ref: '#/components/schemas/BaseRecordVersion'
97+
- $ref: '#/components/schemas/BaseAudit'
98+
- properties:
99+
id:
100+
type: integer
101+
format: int64
102+
x-ecc-validate:
103+
check-null: [Create, Update]
104+
check-not-valid: [Patch]
105+
description: ID of the Localized text
106+
example: 1
107+
module:
108+
type: string
109+
x-ecc-validate:
110+
check-not-empty: [Create, Update]
111+
check-not-valid: [Patch]
112+
description: Module name to be used for key value to be loaded
113+
example: COMMONS
114+
locale:
115+
type: string
116+
x-ecc-validate:
117+
check-not-empty: [Create, Update]
118+
check-not-valid: [Patch]
119+
description: Localization to be retrieved
120+
example: en
121+
key:
122+
type: string
123+
x-ecc-validate:
124+
check-not-empty: [Create, Update]
125+
check-not-valid: [Patch]
126+
description: Localized text key value normally used for retrieval
127+
example: LocalizedTextToBeRetrieved
128+
defaultValue:
129+
type: string
130+
x-ecc-validate:
131+
check-not-empty: [Create, Update]
132+
check-valid: [Patch]
133+
description: Default Text to be used
134+
example: Default Localized Text
135+
customValue:
136+
type: string
137+
x-ecc-validate:
138+
check-valid: [Create, Update, Patch]
139+
description: Alternative Text to be used
140+
example: Alternative Localized Text

0 commit comments

Comments
 (0)