Skip to content

Commit

Permalink
Fix error response schemas
Browse files Browse the repository at this point in the history
This improves the auto-generated response
documentation by dynamically including different
response schemas for invalid request bodies, invalid
query parameters etc. to the auto-generated OpenAPI
schema based on the defined endpoints.
  • Loading branch information
blomqma committed Aug 26, 2024
1 parent b9a592c commit 612fac5
Show file tree
Hide file tree
Showing 8 changed files with 311 additions and 89 deletions.
141 changes: 106 additions & 35 deletions apps/example/public/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,19 @@
}
}
},
"400": {
"description": "Invalid request body.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/MessageWithErrors" }
}
}
},
"500": {
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand Down Expand Up @@ -69,7 +77,7 @@
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand All @@ -94,7 +102,7 @@
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand All @@ -119,7 +127,7 @@
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand Down Expand Up @@ -165,7 +173,7 @@
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand Down Expand Up @@ -197,7 +205,7 @@
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand Down Expand Up @@ -228,10 +236,21 @@
}
},
"400": {
"description": "An unknown error occurred, trying again might help.",
"description": "Error response.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": {
"oneOf": [
{
"description": "Invalid request body.",
"$ref": "#/components/schemas/MessageWithErrors"
},
{
"description": "An unknown error occurred, trying again might help.",
"$ref": "#/components/schemas/ErrorMessage"
}
]
}
}
}
}
Expand Down Expand Up @@ -265,7 +284,7 @@
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand Down Expand Up @@ -299,7 +318,7 @@
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand All @@ -324,7 +343,7 @@
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand All @@ -349,7 +368,7 @@
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand Down Expand Up @@ -390,7 +409,7 @@
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand Down Expand Up @@ -425,7 +444,7 @@
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand Down Expand Up @@ -466,7 +485,7 @@
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand Down Expand Up @@ -505,11 +524,19 @@
}
}
},
"400": {
"description": "Invalid request body.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/MessageWithErrors" }
}
}
},
"500": {
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand Down Expand Up @@ -544,7 +571,7 @@
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand All @@ -569,7 +596,7 @@
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand All @@ -594,7 +621,7 @@
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand Down Expand Up @@ -640,7 +667,7 @@
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand Down Expand Up @@ -672,7 +699,7 @@
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand Down Expand Up @@ -703,10 +730,21 @@
}
},
"400": {
"description": "An unknown error occurred, trying again might help.",
"description": "Error response.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": {
"oneOf": [
{
"description": "Invalid request body.",
"$ref": "#/components/schemas/MessageWithErrors"
},
{
"description": "An unknown error occurred, trying again might help.",
"$ref": "#/components/schemas/ErrorMessage"
}
]
}
}
}
}
Expand Down Expand Up @@ -740,7 +778,7 @@
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand Down Expand Up @@ -774,7 +812,7 @@
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand All @@ -799,7 +837,7 @@
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand All @@ -824,7 +862,7 @@
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand Down Expand Up @@ -865,7 +903,7 @@
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand Down Expand Up @@ -900,7 +938,7 @@
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand Down Expand Up @@ -941,7 +979,7 @@
"description": "An unknown error occurred, trying again might help.",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UnexpectedError" }
"schema": { "$ref": "#/components/schemas/ErrorMessage" }
}
}
}
Expand Down Expand Up @@ -1001,6 +1039,11 @@
"additionalProperties": false,
"description": "TODO deleted message."
},
"ErrorMessage": {
"type": "object",
"properties": { "message": { "type": "string" } },
"additionalProperties": false
},
"FormDataMultipartRequestBody": {
"description": "Test form description.",
"type": "object",
Expand Down Expand Up @@ -1113,6 +1156,39 @@
},
"description": "List of TODOs."
},
"MessageWithErrors": {
"type": "object",
"properties": {
"message": { "type": "string" },
"errors": {
"type": "array",
"items": {
"type": "object",
"required": ["code", "path", "message"],
"properties": {
"code": {
"type": "string",
"description": "Discriminator field for the Zod issue type."
},
"path": {
"type": "array",
"items": {
"oneOf": [{ "type": "string" }, { "type": "number" }]
},
"description": "Path to the error in the validated object, represented as an array of strings and/or numbers."
},
"message": {
"type": "string",
"description": "Human-readable message describing the validation error."
}
},
"additionalProperties": true
}
}
},
"required": ["message"],
"additionalProperties": false
},
"MultipartFormData200ResponseBody": {
"description": "File response.",
"type": "string",
Expand All @@ -1127,11 +1203,6 @@
}
},
"RouteWithExternalDep200ResponseBody": { "type": "string" },
"UnexpectedError": {
"type": "object",
"properties": { "message": { "type": "string" } },
"additionalProperties": false
},
"UrlEncodedFormData200ResponseBody": {
"type": "object",
"properties": { "text": { "type": "string" } },
Expand Down
2 changes: 1 addition & 1 deletion packages/next-rest-framework/src/app-router/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export const route = <T extends Record<string, RouteOperationDefinition>>(
if (contentTypeSchema && contentType !== contentTypeSchema) {
return NextResponse.json(
{ message: DEFAULT_ERRORS.invalidMediaType },
{ status: 415 }
{ status: 415, headers: { Allow: contentTypeSchema } }
);
}

Expand Down
Loading

0 comments on commit 612fac5

Please sign in to comment.