Skip to content

Commit c13a1fd

Browse files
authored
fix(api-handler): return http 422 when the request is rejected due to validation errors (#1170)
1 parent 213fdf3 commit c13a1fd

15 files changed

+1636
-833
lines changed

packages/plugins/openapi/src/rest-generator.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ export class RESTfulOpenAPIGenerator extends OpenAPIGeneratorBase {
217217
responses: {
218218
'201': this.success(`${model.name}Response`),
219219
'403': this.forbidden(),
220+
'422': this.validationError(),
220221
},
221222
security: resourceMeta?.security ?? policies.create === true ? [] : undefined,
222223
};
@@ -292,6 +293,7 @@ export class RESTfulOpenAPIGenerator extends OpenAPIGeneratorBase {
292293
'200': this.success(`${model.name}Response`),
293294
'403': this.forbidden(),
294295
'404': this.notFound(),
296+
'422': this.validationError(),
295297
},
296298
security: resourceMeta?.security ?? policies.update === true ? [] : undefined,
297299
};
@@ -956,6 +958,17 @@ export class RESTfulOpenAPIGenerator extends OpenAPIGeneratorBase {
956958
};
957959
}
958960

961+
private validationError() {
962+
return {
963+
description: 'Request is unprocessable due to validation errors',
964+
content: {
965+
'application/vnd.api+json': {
966+
schema: this.ref('_errorResponse'),
967+
},
968+
},
969+
};
970+
}
971+
959972
private notFound() {
960973
return {
961974
description: 'Resource is not found',

packages/plugins/openapi/src/rpc-generator.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,14 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
516516
},
517517
description: 'Request is forbidden',
518518
},
519+
'422': {
520+
content: {
521+
'application/json': {
522+
schema: this.ref('_Error'),
523+
},
524+
},
525+
description: 'Request is unprocessable due to validation errors',
526+
},
519527
},
520528
};
521529

packages/plugins/openapi/tests/baseline/rest-3.0.0.baseline.yaml

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,13 @@ paths:
229229
application/vnd.api+json:
230230
schema:
231231
$ref: '#/components/schemas/_errorResponse'
232-
'/user/{id}':
232+
'422':
233+
description: Request is unprocessable due to validation errors
234+
content:
235+
application/vnd.api+json:
236+
schema:
237+
$ref: '#/components/schemas/_errorResponse'
238+
/user/{id}:
233239
get:
234240
operationId: fetch-User
235241
description: Fetch a "User" resource
@@ -288,6 +294,12 @@ paths:
288294
application/vnd.api+json:
289295
schema:
290296
$ref: '#/components/schemas/_errorResponse'
297+
'422':
298+
description: Request is unprocessable due to validation errors
299+
content:
300+
application/vnd.api+json:
301+
schema:
302+
$ref: '#/components/schemas/_errorResponse'
291303
patch:
292304
operationId: update-User-patch
293305
description: Update a "User" resource
@@ -319,6 +331,12 @@ paths:
319331
application/vnd.api+json:
320332
schema:
321333
$ref: '#/components/schemas/_errorResponse'
334+
'422':
335+
description: Request is unprocessable due to validation errors
336+
content:
337+
application/vnd.api+json:
338+
schema:
339+
$ref: '#/components/schemas/_errorResponse'
322340
delete:
323341
operationId: delete-User
324342
description: Delete a "User" resource
@@ -341,7 +359,7 @@ paths:
341359
application/vnd.api+json:
342360
schema:
343361
$ref: '#/components/schemas/_errorResponse'
344-
'/user/{id}/posts':
362+
/user/{id}/posts:
345363
get:
346364
operationId: fetch-User-related-posts
347365
description: Fetch the related "posts" resource for "User"
@@ -544,7 +562,7 @@ paths:
544562
application/vnd.api+json:
545563
schema:
546564
$ref: '#/components/schemas/_errorResponse'
547-
'/user/{id}/relationships/posts':
565+
/user/{id}/relationships/posts:
548566
get:
549567
operationId: fetch-User-relationship-posts
550568
description: Fetch the "posts" relationships for a "User"
@@ -839,7 +857,7 @@ paths:
839857
application/vnd.api+json:
840858
schema:
841859
$ref: '#/components/schemas/_errorResponse'
842-
'/user/{id}/profile':
860+
/user/{id}/profile:
843861
get:
844862
operationId: fetch-User-related-profile
845863
description: Fetch the related "profile" resource for "User"
@@ -867,7 +885,7 @@ paths:
867885
application/vnd.api+json:
868886
schema:
869887
$ref: '#/components/schemas/_errorResponse'
870-
'/user/{id}/relationships/profile':
888+
/user/{id}/relationships/profile:
871889
get:
872890
operationId: fetch-User-relationship-profile
873891
description: Fetch the "profile" relationships for a "User"
@@ -1067,7 +1085,13 @@ paths:
10671085
application/vnd.api+json:
10681086
schema:
10691087
$ref: '#/components/schemas/_errorResponse'
1070-
'/profile/{id}':
1088+
'422':
1089+
description: Request is unprocessable due to validation errors
1090+
content:
1091+
application/vnd.api+json:
1092+
schema:
1093+
$ref: '#/components/schemas/_errorResponse'
1094+
/profile/{id}:
10711095
get:
10721096
operationId: fetch-Profile
10731097
description: Fetch a "Profile" resource
@@ -1126,6 +1150,12 @@ paths:
11261150
application/vnd.api+json:
11271151
schema:
11281152
$ref: '#/components/schemas/_errorResponse'
1153+
'422':
1154+
description: Request is unprocessable due to validation errors
1155+
content:
1156+
application/vnd.api+json:
1157+
schema:
1158+
$ref: '#/components/schemas/_errorResponse'
11291159
patch:
11301160
operationId: update-Profile-patch
11311161
description: Update a "Profile" resource
@@ -1157,6 +1187,12 @@ paths:
11571187
application/vnd.api+json:
11581188
schema:
11591189
$ref: '#/components/schemas/_errorResponse'
1190+
'422':
1191+
description: Request is unprocessable due to validation errors
1192+
content:
1193+
application/vnd.api+json:
1194+
schema:
1195+
$ref: '#/components/schemas/_errorResponse'
11601196
delete:
11611197
operationId: delete-Profile
11621198
description: Delete a "Profile" resource
@@ -1179,7 +1215,7 @@ paths:
11791215
application/vnd.api+json:
11801216
schema:
11811217
$ref: '#/components/schemas/_errorResponse'
1182-
'/profile/{id}/user':
1218+
/profile/{id}/user:
11831219
get:
11841220
operationId: fetch-Profile-related-user
11851221
description: Fetch the related "user" resource for "Profile"
@@ -1207,7 +1243,7 @@ paths:
12071243
application/vnd.api+json:
12081244
schema:
12091245
$ref: '#/components/schemas/_errorResponse'
1210-
'/profile/{id}/relationships/user':
1246+
/profile/{id}/relationships/user:
12111247
get:
12121248
operationId: fetch-Profile-relationship-user
12131249
description: Fetch the "user" relationships for a "Profile"
@@ -1593,7 +1629,13 @@ paths:
15931629
application/vnd.api+json:
15941630
schema:
15951631
$ref: '#/components/schemas/_errorResponse'
1596-
'/post_Item/{id}':
1632+
'422':
1633+
description: Request is unprocessable due to validation errors
1634+
content:
1635+
application/vnd.api+json:
1636+
schema:
1637+
$ref: '#/components/schemas/_errorResponse'
1638+
/post_Item/{id}:
15971639
get:
15981640
operationId: fetch-post_Item
15991641
description: Fetch a "post_Item" resource
@@ -1652,6 +1694,12 @@ paths:
16521694
application/vnd.api+json:
16531695
schema:
16541696
$ref: '#/components/schemas/_errorResponse'
1697+
'422':
1698+
description: Request is unprocessable due to validation errors
1699+
content:
1700+
application/vnd.api+json:
1701+
schema:
1702+
$ref: '#/components/schemas/_errorResponse'
16551703
patch:
16561704
operationId: update-post_Item-patch
16571705
description: Update a "post_Item" resource
@@ -1683,6 +1731,12 @@ paths:
16831731
application/vnd.api+json:
16841732
schema:
16851733
$ref: '#/components/schemas/_errorResponse'
1734+
'422':
1735+
description: Request is unprocessable due to validation errors
1736+
content:
1737+
application/vnd.api+json:
1738+
schema:
1739+
$ref: '#/components/schemas/_errorResponse'
16861740
delete:
16871741
operationId: delete-post_Item
16881742
description: Delete a "post_Item" resource
@@ -1705,7 +1759,7 @@ paths:
17051759
application/vnd.api+json:
17061760
schema:
17071761
$ref: '#/components/schemas/_errorResponse'
1708-
'/post_Item/{id}/author':
1762+
/post_Item/{id}/author:
17091763
get:
17101764
operationId: fetch-post_Item-related-author
17111765
description: Fetch the related "author" resource for "post_Item"
@@ -1733,7 +1787,7 @@ paths:
17331787
application/vnd.api+json:
17341788
schema:
17351789
$ref: '#/components/schemas/_errorResponse'
1736-
'/post_Item/{id}/relationships/author':
1790+
/post_Item/{id}/relationships/author:
17371791
get:
17381792
operationId: fetch-post_Item-relationship-author
17391793
description: Fetch the "author" relationships for a "post_Item"

0 commit comments

Comments
 (0)