-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathschemaManipulationUtils.ts
More file actions
178 lines (157 loc) · 6.06 KB
/
schemaManipulationUtils.ts
File metadata and controls
178 lines (157 loc) · 6.06 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import {dataAt} from '@/utility/resolveDataAtPath';
import {pathToJsonPointer} from '@/utility/pathUtils';
import type {Path} from '@/utility/path';
import {findAvailableSchemaId, isSubSchemaDefinedInDefinitions} from '@/schema/schemaReadingUtils';
import type {ManagedData} from '@/data/managedData';
import {constructSchemaGraph} from '@/schema/graph-representation/schemaGraphConstructor';
import type {SchemaNodeData} from '@/schema/graph-representation/schemaGraphTypes';
import {updateReferences} from '@/utility/renameUtils';
import {stringToIdentifier} from '@/utility/stringToIdentifier';
export function extractAllInlinedSchemaElements(
schemaData: ManagedData,
extractRootElement: boolean,
extractEnums: boolean
): number {
const graph = constructSchemaGraph(schemaData.data.value, false);
// filter by nodes which are inlined and an object node
const nodedFiltered = graph.nodes.filter(
node =>
!isSubSchemaDefinedInDefinitions(node.absolutePath) &&
(extractRootElement || node.absolutePath.length > 1) &&
((extractEnums && node.getNodeType() == 'schemaenum') || node.getNodeType() == 'schemaobject')
);
// sort nodes by path depth, so that we can extract the deepest nodes first, to avoid a node being moved before its children and then not being able to find the children anymore
const nodesSorted = sortNodesByPathDepthDescending(nodedFiltered);
let nodesExtracted = 0;
nodesSorted.forEach(node => {
const newIdentifier = createIdentifierForExtractedElement(
node.name,
node.title,
node.fallbackDisplayName
);
extractInlinedSchemaElement(node.absolutePath, schemaData, newIdentifier);
nodesExtracted++;
});
return nodesExtracted;
}
function sortNodesByPathDepthDescending(nodes: SchemaNodeData[]): SchemaNodeData[] {
return nodes.sort((a, b) => b.absolutePath.length - a.absolutePath.length);
}
export function extractInlinedSchemaElement(
absoluteElementPath: Path,
schemaData: ManagedData,
elementName: string,
forgetIfDuplicateExists: boolean = false
): Path {
const dataAtPath = dataAt(absoluteElementPath, schemaData.data.value);
const updateDataFct: (path: Path, newValue: any) => void = (path, newValue) => {
schemaData.setDataAt(path, newValue);
};
if (forgetIfDuplicateExists) {
// if an existing definition exists with the same content, we can just reference that
const existingElementDefPath = ['$defs', elementName];
const existingElementDef = dataAt(existingElementDefPath, schemaData.data.value);
if (existingElementDef) {
if (JSON.stringify(existingElementDef) === JSON.stringify(dataAtPath)) {
const referenceToNewElement = '#' + pathToJsonPointer(existingElementDefPath);
schemaData.setDataAt(absoluteElementPath, {
$ref: referenceToNewElement,
});
updateReferences(
absoluteElementPath,
existingElementDefPath,
schemaData.data.value,
updateDataFct
);
return existingElementDefPath;
}
}
}
const newElementId = findAvailableSchemaId(schemaData, ['$defs'], elementName, true);
schemaData.setDataAt(newElementId, dataAtPath);
const referenceToNewElement = '#' + pathToJsonPointer(newElementId);
schemaData.setDataAt(absoluteElementPath, {
$ref: referenceToNewElement,
});
updateReferences(absoluteElementPath, newElementId, schemaData.data.value, updateDataFct);
return newElementId;
}
export function createIdentifierForExtractedElement(
name: string | undefined,
title: string | undefined,
fallbackDisplayName: string
) {
let identifier = name;
// if the name is a json schema keyword which has a json schema as a value (except via additionalProperties, where the user then can define the name for their property), then do not use it (e.g. 'items', 'not', 'if', 'then', 'else'), we instead want a more suitable name
// note that the current implementation here will not catch each of these keywords but only the most common ones
if (identifier !== undefined && ['items', 'not', 'if', 'then', 'else'].includes(identifier)) {
identifier = undefined;
}
if (identifier === undefined && title !== undefined) {
identifier = stringToIdentifier(title, false);
}
if (identifier === undefined) {
identifier = stringToIdentifier(fallbackDisplayName, false);
}
return identifier;
}
export function addSchemaObject(
schemaData: ManagedData,
connectWithRootIfRootEmpty: boolean = true,
schema: any = undefined,
identifier: string | undefined = undefined
) {
const rawData = schemaData.data.value;
// set type of root element to object if not done yet
if (rawData.type !== 'object') {
rawData.type = 'object';
}
let objectPath: Path;
if (identifier !== undefined) {
objectPath = findAvailableSchemaId(schemaData, ['$defs'], identifier, true);
} else {
objectPath = findAvailableSchemaId(schemaData, ['$defs'], 'object');
}
if (schema !== undefined) {
schemaData.setDataAt(objectPath, schema);
} else {
schemaData.setDataAt(objectPath, {
type: 'object',
properties: {
property1: {
type: 'string',
},
},
});
}
// make connection from root element to new object if root has no properties yet
if (connectWithRootIfRootEmpty && rawData.properties === undefined) {
const objectName = objectPath[objectPath.length - 1]!;
const referenceToNewObject = '#' + pathToJsonPointer(objectPath);
schemaData.setDataAt(['properties', objectName], {
$ref: referenceToNewObject,
});
}
return objectPath;
}
export function addSchemaEnum(
schemaData: ManagedData,
schema: any = undefined,
identifier: string | undefined = undefined
) {
let enumPath: Path;
if (identifier !== undefined) {
enumPath = findAvailableSchemaId(schemaData, ['$defs'], identifier, true);
} else {
enumPath = findAvailableSchemaId(schemaData, ['$defs'], 'enum');
}
if (schema !== undefined) {
schemaData.setDataAt(enumPath, schema);
} else {
schemaData.setDataAt(enumPath, {
type: 'string',
enum: ['VAL_1', 'VAL_2'],
});
}
return enumPath;
}