@@ -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