Files
godoxy-yusing/scripts/fix-swagger-json.py
yusing 35a3e3fef6 refactor(api): restructured API for type safety, maintainability and docs generation
- These changes makes the API incombatible with previous versions
- Added new types for error handling, success responses, and health checks.
- Updated health check logic to utilize the new types for better clarity and structure.
- Refactored existing handlers to improve response consistency and error handling.
- Updated Makefile to include a new target for generating API types from Swagger.
- Updated "new agent" API to respond an encrypted cert pair
2025-08-16 13:04:05 +08:00

42 lines
1.3 KiB
Python

# This script aims to fix the swagger.json file by setting the x-nullable flag to False if not present for all objects and arrays.
# This prevents from generating optional (undefined) fields in the generated API client.
import json
path = "internal/api/v1/docs/swagger.json"
with open(path, "r") as f:
data = json.load(f)
def set_non_nullable(data):
if not isinstance(data, dict):
return
if "x-nullable" not in data:
data["x-nullable"] = False
if "x-omitempty" not in data and data["x-nullable"] == False:
data["x-omitempty"] = False
if "type" not in data:
return
if data["type"] == "object" and "properties" in data:
for v in data["properties"].values():
set_non_nullable(v)
if data["type"] == "array":
for v in data["items"]:
set_non_nullable(v)
def set_operation_id(data):
if isinstance(data, dict):
if "x-id" in data:
data["operationId"] = data["x-id"]
return
for v in data.values():
set_operation_id(v)
for key, value in data.items():
if key == "definitions":
for k, v in value.items():
set_non_nullable(v)
else:
set_operation_id(value)
with open(path, "w") as f:
json.dump(data, f, indent=2)