Skip to content

Commit c497e4f

Browse files
committed
Add per-status error examples so each error response shows correct code and message
1 parent b95f1c7 commit c497e4f

2 files changed

Lines changed: 67 additions & 11 deletions

File tree

openapi-public.yml

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,24 +1800,36 @@ paths:
18001800
application/json:
18011801
schema:
18021802
$ref: '#/components/schemas/Error'
1803+
example: &id009
1804+
code: 401
1805+
message: 'Authentication error: missing or invalid API key'
18031806
'400':
18041807
description: Invalid path
18051808
content:
18061809
application/json:
18071810
schema:
18081811
$ref: '#/components/schemas/Error'
1812+
example: &id008
1813+
code: 400
1814+
message: 'Bad request: invalid or missing request parameters'
18091815
'404':
18101816
description: File not found
18111817
content:
18121818
application/json:
18131819
schema:
18141820
$ref: '#/components/schemas/Error'
1821+
example: &id011
1822+
code: 404
1823+
message: 'Not found: the requested resource does not exist'
18151824
'500':
18161825
description: Internal server error
18171826
content:
18181827
application/json:
18191828
schema:
18201829
$ref: '#/components/schemas/Error'
1830+
example: &id010
1831+
code: 500
1832+
message: 'Server error: an unexpected error occurred'
18211833
'502': *id003
18221834
operationId: downloadFile
18231835
post:
@@ -1849,24 +1861,30 @@ paths:
18491861
application/json:
18501862
schema:
18511863
$ref: '#/components/schemas/Error'
1864+
example: *id008
18521865
'401':
18531866
description: Invalid user
18541867
content:
18551868
application/json:
18561869
schema:
18571870
$ref: '#/components/schemas/Error'
1871+
example: *id009
18581872
'500':
18591873
description: Internal server error
18601874
content:
18611875
application/json:
18621876
schema:
18631877
$ref: '#/components/schemas/Error'
1878+
example: *id010
18641879
'507':
18651880
description: Not enough disk space
18661881
content:
18671882
application/json:
18681883
schema:
18691884
$ref: '#/components/schemas/Error'
1885+
example:
1886+
code: 507
1887+
message: 'Insufficient storage: not enough disk space'
18701888
'502': *id003
18711889
operationId: uploadFile
18721890
servers:
@@ -2064,36 +2082,46 @@ components:
20642082
application/json:
20652083
schema:
20662084
$ref: '#/components/schemas/Error'
2085+
example: *id008
20672086
'401':
20682087
description: Authentication error
20692088
content:
20702089
application/json:
20712090
schema:
20722091
$ref: '#/components/schemas/Error'
2092+
example: *id009
20732093
'403':
20742094
description: Forbidden
20752095
content:
20762096
application/json:
20772097
schema:
20782098
$ref: '#/components/schemas/Error'
2099+
example:
2100+
code: 403
2101+
message: 'Forbidden: insufficient permissions'
20792102
'404':
20802103
description: Not found
20812104
content:
20822105
application/json:
20832106
schema:
20842107
$ref: '#/components/schemas/Error'
2108+
example: *id011
20852109
'409':
20862110
description: Conflict
20872111
content:
20882112
application/json:
20892113
schema:
20902114
$ref: '#/components/schemas/Error'
2115+
example:
2116+
code: 409
2117+
message: 'Conflict: the resource is in a conflicting state'
20912118
'500':
20922119
description: Server error
20932120
content:
20942121
application/json:
20952122
schema:
20962123
$ref: '#/components/schemas/Error'
2124+
example: *id010
20972125
schemas:
20982126
Error:
20992127
required:
@@ -2108,9 +2136,6 @@ components:
21082136
type: string
21092137
description: Error
21102138
type: object
2111-
example:
2112-
code: 400
2113-
message: 'Bad request: invalid or missing request parameters'
21142139
EntryInfo:
21152140
required:
21162141
- path

scripts/generate_openapi_reference.py

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,14 +1022,45 @@ def _singularize(word: str) -> str:
10221022
responses["500"] = {"$ref": "#/components/responses/500"}
10231023
fixes.append("/sandboxes/{sandboxID}/refreshes: added 500 response")
10241024

1025-
# 25. Add meaningful example to the Error schema (applies everywhere it's referenced)
1026-
error_schema = schemas.get("Error")
1027-
if error_schema and "example" not in error_schema:
1028-
error_schema["example"] = {
1029-
"code": 400,
1030-
"message": "Bad request: invalid or missing request parameters",
1031-
}
1032-
fixes.append("Error schema: added example values")
1025+
# 25. Add per-status error examples to every error response in every operation
1026+
status_examples = {
1027+
"400": {"code": 400, "message": "Bad request: invalid or missing request parameters"},
1028+
"401": {"code": 401, "message": "Authentication error: missing or invalid API key"},
1029+
"403": {"code": 403, "message": "Forbidden: insufficient permissions"},
1030+
"404": {"code": 404, "message": "Not found: the requested resource does not exist"},
1031+
"409": {"code": 409, "message": "Conflict: the resource is in a conflicting state"},
1032+
"500": {"code": 500, "message": "Server error: an unexpected error occurred"},
1033+
"507": {"code": 507, "message": "Insufficient storage: not enough disk space"},
1034+
}
1035+
for path_item in spec.get("paths", {}).values():
1036+
for method in ("get", "post", "put", "patch", "delete", "head", "options"):
1037+
op = path_item.get(method)
1038+
if not op:
1039+
continue
1040+
for status_code, resp in op.get("responses", {}).items():
1041+
if not isinstance(resp, dict) or "$ref" in resp:
1042+
continue
1043+
example = status_examples.get(str(status_code))
1044+
if not example:
1045+
continue
1046+
json_media = resp.get("content", {}).get("application/json")
1047+
if not json_media:
1048+
continue
1049+
schema = json_media.get("schema", {})
1050+
# Only add example if schema references Error
1051+
ref = schema.get("$ref", "")
1052+
if ref.endswith("/Error") and "example" not in json_media:
1053+
json_media["example"] = example
1054+
# Also set examples on component-level responses
1055+
comp_responses = spec.get("components", {}).get("responses", {})
1056+
for status_code, example in status_examples.items():
1057+
resp = comp_responses.get(status_code)
1058+
if not resp or "content" not in resp:
1059+
continue
1060+
json_media = resp["content"].get("application/json")
1061+
if json_media and "example" not in json_media:
1062+
json_media["example"] = example
1063+
fixes.append("Error responses: added per-status example values")
10331064

10341065
if fixes:
10351066
print(f"==> Fixed {len(fixes)} spec issues:")

0 commit comments

Comments
 (0)