-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix-recursive-refs.js
More file actions
75 lines (68 loc) · 2.84 KB
/
Copy pathfix-recursive-refs.js
File metadata and controls
75 lines (68 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const fs = require('fs');
const spec = JSON.parse(fs.readFileSync('D:/Labs/ZURICH/MCP_HACK_26/tia-openapi3-slim.json', 'utf8'));
// All schemas that form cycles (direct or indirect)
// Direct self-refs:
const DIRECT_RECURSIVE = new Set([
'TiaPortalApi.Core.Controllers.RequestModels.SclStatement',
'TiaPortalApi.Core.DTOs.BlockTreeNodeDto',
'TiaPortalApi.Core.DTOs.DeviceDto',
'TiaPortalApi.Core.DTOs.HmiScreenTreeDto',
'TiaPortalApi.Core.DTOs.HmiTagTreeDto',
'TiaPortalApi.Core.DTOs.HmiScriptTreeDto',
'TiaPortalApi.Core.DTOs.TiaProductDto',
'TiaPortalApi.Core.DTOs.LibraryFolderDto',
'TiaPortalApi.Core.DTOs.TagTableTreeNodeDto',
'TiaPortalApi.Core.DTOs.UdtMemberDto',
'TiaPortalApi.Core.Controllers.RequestModels.UdtMemberRequest',
'TiaPortalApi.Core.DTOs.UdtTreeNodeDto',
]);
// Indirect cycle: SclStatement <-> CaseBranch
// Break CaseBranch -> SclStatement reference
const INDIRECT_BREAKS = new Map([
['TiaPortalApi.Core.Controllers.RequestModels.CaseBranch', 'TiaPortalApi.Core.Controllers.RequestModels.SclStatement'],
]);
let fixed = 0;
function fixRefs(obj, parentSchemaName) {
if (!obj || typeof obj !== 'object') return obj;
if (Array.isArray(obj)) return obj.map(v => fixRefs(v, parentSchemaName));
const result = {};
for (const k of Object.keys(obj)) {
if (k === '$ref') {
const refName = obj[k].startsWith('#/components/schemas/')
? obj[k].split('/').pop()
: null;
// Break direct self-reference
if (refName && parentSchemaName && refName === parentSchemaName) {
fixed++;
result['description'] = 'Recursive reference to ' + refName + ' (omitted for MCP compatibility)';
result['type'] = 'object';
continue;
}
// Break indirect cycle reference
if (refName && parentSchemaName && INDIRECT_BREAKS.get(parentSchemaName) === refName) {
fixed++;
result['description'] = 'Indirect recursive reference to ' + refName + ' (omitted for MCP compatibility)';
result['type'] = 'object';
continue;
}
result[k] = obj[k];
} else {
result[k] = fixRefs(obj[k], parentSchemaName);
}
}
return result;
}
// Process schemas
if (spec.components && spec.components.schemas) {
const toFix = new Set([...DIRECT_RECURSIVE, ...INDIRECT_BREAKS.keys()]);
for (const schemaName of Object.keys(spec.components.schemas)) {
if (toFix.has(schemaName)) {
spec.components.schemas[schemaName] = fixRefs(spec.components.schemas[schemaName], schemaName);
}
}
}
const output = JSON.stringify(spec);
fs.writeFileSync('D:/Labs/ZURICH/MCP_HACK_26/tia-openapi3-norec.json', output);
console.log('Fixed', fixed, 'recursive refs');
console.log('Output size:', Math.round(output.length / 1024), 'KB');
console.log('Saved to tia-openapi3-norec.json');