Skip to content

Commit

Permalink
fix(api-handler): return http 422 when the request is rejected due to…
Browse files Browse the repository at this point in the history
… validation errors (#1170)
  • Loading branch information
ymc9 authored Mar 24, 2024
1 parent 213fdf3 commit c13a1fd
Show file tree
Hide file tree
Showing 15 changed files with 1,636 additions and 833 deletions.
13 changes: 13 additions & 0 deletions packages/plugins/openapi/src/rest-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export class RESTfulOpenAPIGenerator extends OpenAPIGeneratorBase {
responses: {
'201': this.success(`${model.name}Response`),
'403': this.forbidden(),
'422': this.validationError(),
},
security: resourceMeta?.security ?? policies.create === true ? [] : undefined,
};
Expand Down Expand Up @@ -292,6 +293,7 @@ export class RESTfulOpenAPIGenerator extends OpenAPIGeneratorBase {
'200': this.success(`${model.name}Response`),
'403': this.forbidden(),
'404': this.notFound(),
'422': this.validationError(),
},
security: resourceMeta?.security ?? policies.update === true ? [] : undefined,
};
Expand Down Expand Up @@ -956,6 +958,17 @@ export class RESTfulOpenAPIGenerator extends OpenAPIGeneratorBase {
};
}

private validationError() {
return {
description: 'Request is unprocessable due to validation errors',
content: {
'application/vnd.api+json': {
schema: this.ref('_errorResponse'),
},
},
};
}

private notFound() {
return {
description: 'Resource is not found',
Expand Down
8 changes: 8 additions & 0 deletions packages/plugins/openapi/src/rpc-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,14 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
},
description: 'Request is forbidden',
},
'422': {
content: {
'application/json': {
schema: this.ref('_Error'),
},
},
description: 'Request is unprocessable due to validation errors',
},
},
};

Expand Down
76 changes: 65 additions & 11 deletions packages/plugins/openapi/tests/baseline/rest-3.0.0.baseline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,13 @@ paths:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
'/user/{id}':
'422':
description: Request is unprocessable due to validation errors
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
/user/{id}:
get:
operationId: fetch-User
description: Fetch a "User" resource
Expand Down Expand Up @@ -288,6 +294,12 @@ paths:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
'422':
description: Request is unprocessable due to validation errors
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
patch:
operationId: update-User-patch
description: Update a "User" resource
Expand Down Expand Up @@ -319,6 +331,12 @@ paths:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
'422':
description: Request is unprocessable due to validation errors
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
delete:
operationId: delete-User
description: Delete a "User" resource
Expand All @@ -341,7 +359,7 @@ paths:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
'/user/{id}/posts':
/user/{id}/posts:
get:
operationId: fetch-User-related-posts
description: Fetch the related "posts" resource for "User"
Expand Down Expand Up @@ -544,7 +562,7 @@ paths:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
'/user/{id}/relationships/posts':
/user/{id}/relationships/posts:
get:
operationId: fetch-User-relationship-posts
description: Fetch the "posts" relationships for a "User"
Expand Down Expand Up @@ -839,7 +857,7 @@ paths:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
'/user/{id}/profile':
/user/{id}/profile:
get:
operationId: fetch-User-related-profile
description: Fetch the related "profile" resource for "User"
Expand Down Expand Up @@ -867,7 +885,7 @@ paths:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
'/user/{id}/relationships/profile':
/user/{id}/relationships/profile:
get:
operationId: fetch-User-relationship-profile
description: Fetch the "profile" relationships for a "User"
Expand Down Expand Up @@ -1067,7 +1085,13 @@ paths:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
'/profile/{id}':
'422':
description: Request is unprocessable due to validation errors
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
/profile/{id}:
get:
operationId: fetch-Profile
description: Fetch a "Profile" resource
Expand Down Expand Up @@ -1126,6 +1150,12 @@ paths:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
'422':
description: Request is unprocessable due to validation errors
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
patch:
operationId: update-Profile-patch
description: Update a "Profile" resource
Expand Down Expand Up @@ -1157,6 +1187,12 @@ paths:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
'422':
description: Request is unprocessable due to validation errors
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
delete:
operationId: delete-Profile
description: Delete a "Profile" resource
Expand All @@ -1179,7 +1215,7 @@ paths:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
'/profile/{id}/user':
/profile/{id}/user:
get:
operationId: fetch-Profile-related-user
description: Fetch the related "user" resource for "Profile"
Expand Down Expand Up @@ -1207,7 +1243,7 @@ paths:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
'/profile/{id}/relationships/user':
/profile/{id}/relationships/user:
get:
operationId: fetch-Profile-relationship-user
description: Fetch the "user" relationships for a "Profile"
Expand Down Expand Up @@ -1593,7 +1629,13 @@ paths:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
'/post_Item/{id}':
'422':
description: Request is unprocessable due to validation errors
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
/post_Item/{id}:
get:
operationId: fetch-post_Item
description: Fetch a "post_Item" resource
Expand Down Expand Up @@ -1652,6 +1694,12 @@ paths:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
'422':
description: Request is unprocessable due to validation errors
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
patch:
operationId: update-post_Item-patch
description: Update a "post_Item" resource
Expand Down Expand Up @@ -1683,6 +1731,12 @@ paths:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
'422':
description: Request is unprocessable due to validation errors
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
delete:
operationId: delete-post_Item
description: Delete a "post_Item" resource
Expand All @@ -1705,7 +1759,7 @@ paths:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
'/post_Item/{id}/author':
/post_Item/{id}/author:
get:
operationId: fetch-post_Item-related-author
description: Fetch the related "author" resource for "post_Item"
Expand Down Expand Up @@ -1733,7 +1787,7 @@ paths:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/_errorResponse'
'/post_Item/{id}/relationships/author':
/post_Item/{id}/relationships/author:
get:
operationId: fetch-post_Item-relationship-author
description: Fetch the "author" relationships for a "post_Item"
Expand Down
Loading

0 comments on commit c13a1fd

Please sign in to comment.