import json
import sys
from typing import Dict, Any
def deep_merge(base: Dict[str, Any], update: Dict[str, Any]) -> Dict[str, Any]:
for key, value in update.items():
if key in base:
if isinstance(base[key], dict) and isinstance(value, dict):
base[key] = deep_merge(base[key], value)
elif isinstance(base[key], list) and isinstance(value, list):
for item in value:
if item not in base[key]:
base[key].append(item)
else:
base[key] = value
else:
base[key] = value
return base
def merge_openapi_files():
openapi_files = [
'swagger/types.openapi.json', 'swagger/service.openapi.json', 'swagger/indexer.openapi.json', 'swagger/admin.openapi.json', 'swagger/signer_manager.openapi.json', 'swagger/wallet.openapi.json', ]
merged = None
for file_path in openapi_files:
try:
with open(file_path, 'r') as f:
spec = json.load(f)
if merged is None:
merged = spec.copy()
merged['info'] = {
'title': 'Ark API',
'version': '1.0.0',
'description': 'Combined Ark Service, Indexer, Admin, Signer Manager, and Wallet API'
}
else:
if 'tags' in spec:
if 'tags' not in merged:
merged['tags'] = []
for tag in spec['tags']:
if tag not in merged['tags']:
merged['tags'].append(tag)
if 'paths' in spec:
if 'paths' not in merged:
merged['paths'] = {}
merged['paths'] = deep_merge(merged['paths'], spec['paths'])
if 'components' in spec and 'schemas' in spec['components']:
if 'components' not in merged:
merged['components'] = {}
if 'schemas' not in merged['components']:
merged['components']['schemas'] = {}
merged['components']['schemas'] = deep_merge(
merged['components']['schemas'],
spec['components']['schemas']
)
if 'servers' in spec:
if 'servers' not in merged:
merged['servers'] = []
for server in spec['servers']:
if server not in merged['servers']:
merged['servers'].append(server)
except FileNotFoundError:
print(f"⚠️ Warning: {file_path} not found, skipping...")
continue
with open('swagger/merged.openapi.json', 'w') as f:
json.dump(merged, f, indent=2)
print("✅ Successfully merged OpenAPI files into swagger/merged.openapi.json")
if __name__ == '__main__':
try:
merge_openapi_files()
except Exception as e:
print(f"❌ Error merging OpenAPI files: {e}", file=sys.stderr)
sys.exit(1)