forked from oasis-tcs/odata-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedm.js
More file actions
368 lines (340 loc) · 11.8 KB
/
edm.js
File metadata and controls
368 lines (340 loc) · 11.8 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/**
* Entity Data Model for OData
*
* Latest version: https://github.com/oasis-tcs/odata-openapi/blob/master/lib/edm.js
*/
module.exports = { nameParts };
module.exports.EDM = class {
#elements = {}; // Map of namespace-qualified name to model element
//TODO: stronger encapsulation: private members with getters?
//TODO: use namespace-qualified names instead of document-local aliases
boundOverloads = {}; // Map of action/function names to bound overloads
derivedTypes = {}; // Map of type names to derived types
//TODO: maps per document for document-local aliases
#alias = {}; // Map of namespace or alias to alias
namespace = { Edm: "Edm" }; // Map of namespace or alias to namespace
namespaceUrl = {}; // Map of namespace to reference URL
voc = {}; // Map of vocabulary and terms to qualified name in this CSDL
/**
* Add CSDL document to model
* @param {object} csdl CSDL document
* @param {Array} messages Warnings
*/
//TODO: multi-document models
//TODO: different aliases per document
addDocument(csdl, messages) {
this.#processIncludes(csdl);
//TODO: different term maps per document, at least for Core, JSON, Validation
vocabularies(this.voc, this.#alias);
this.#processSchemas(csdl, messages);
//TODO: only for first? document
this.entityContainer = this.#elements[csdl.$EntityContainer];
}
/**
* Extract included namespaces and their aliases
* @param {object} csdl CSDL document
*/
#processIncludes(csdl) {
for (const [url, reference] of Object.entries(csdl.$Reference ?? {})) {
for (const include of reference.$Include ?? []) {
const qualifier = include.$Alias ?? include.$Namespace;
this.#alias[include.$Namespace] = qualifier;
this.namespace[qualifier] = include.$Namespace;
this.namespace[include.$Namespace] = include.$Namespace;
this.namespaceUrl[include.$Namespace] = url;
}
}
}
/**
* Extract namespaces, aliases, model elements, bound overloads, and derived types
* @param {object} csdl CSDL document
* @param {Array} messages Warnings
*/
#processSchemas(csdl, messages) {
for (const [namespace, schema] of Object.entries(csdl)) {
if (!isIdentifier(namespace)) continue;
const isDefaultNamespace = schema[this.voc.Core.DefaultNamespace];
const qualifier = schema.$Alias || namespace;
this.#alias[namespace] = qualifier;
this.namespace[qualifier] = namespace;
this.namespace[namespace] = namespace;
for (const [name, element] of Object.entries(schema)) {
if (!isIdentifier(name)) continue;
//TODO: copy element to avoid modifying input CSDL in processAnnotations
//TODO: namespace-qualify any type references inside copy of element?
//TODO: - $BaseType, property.$Type, $ReturnType.$Type, $Parameter[].$Type, resource.$Type, resource.$Action, resource.$Function
//TODO: - embedded annotations
//TODO: or inject reference to doc-local alias map?
this.#elements[`${namespace}.${name}`] = element;
if (Array.isArray(element)) {
const qualifiedName = qualifier + "." + name;
for (const overload of element) {
if (!overload.$IsBound) continue;
//TODO: this "-c" trick seems a bit hacky
const type =
overload.$Parameter[0].$Type +
(overload.$Parameter[0].$Collection ? "-c" : "");
if (!this.boundOverloads[type]) this.boundOverloads[type] = [];
this.boundOverloads[type].push({
name: isDefaultNamespace ? name : qualifiedName,
overload: overload,
});
}
}
}
}
for (const [namespace, schema] of Object.entries(csdl)) {
if (!isIdentifier(namespace)) continue;
this.#processAnnotations(schema.$Annotations ?? {}, messages);
for (const [name, element] of Object.entries(schema)) {
if (!isIdentifier(name)) continue;
if (element.$BaseType) {
const base = this.namespaceQualifiedName(element.$BaseType);
if (!this.derivedTypes[base]) this.derivedTypes[base] = [];
this.derivedTypes[base].push(namespace + "." + name);
}
}
}
}
/**
* Inject annotations with external targeting into target model elements
* @param {object} externalAnnotations Annotations with external targeting
* @param {Array} messages Warnings
*/
#processAnnotations(externalAnnotations, messages) {
for (const [target, annotations] of Object.entries(externalAnnotations)) {
const segments = target.split("/");
const open = segments[0].indexOf("(");
let element;
if (open == -1) {
element = this.element(segments[0]);
} else {
element = this.element(segments[0].substring(0, open));
let args = segments[0].substring(open + 1, segments[0].length - 1);
element = element?.find(
(overload) =>
(overload.$Kind == "Action" &&
overload.$IsBound != true &&
args == "") ||
(overload.$Kind == "Action" &&
args ==
(overload.$Parameter?.[0].$Collection
? `Collection(${overload.$Parameter[0].$Type})`
: overload.$Parameter[0].$Type || "")) ||
args ==
(overload.$Parameter || [])
.map((p) => {
const type = p.$Type || "Edm.String";
return p.$Collection ? `Collection(${type})` : type;
})
.join(","),
);
}
if (!element) {
messages.push(`Invalid annotation target '${target}'`);
} else if (Array.isArray(element)) {
messages.push(
`Ignoring annotations targeting all overloads of '${target}'`,
);
//TODO: action or function:
//- loop over all overloads
//- if there are more segments, a parameter or the return type is targeted
} else {
switch (segments.length) {
case 1:
Object.assign(element, annotations);
break;
case 2:
if (["Action", "Function"].includes(element.$Kind)) {
if (segments[1] == "$ReturnType") {
if (element.$ReturnType)
Object.assign(element.$ReturnType, annotations);
} else {
const parameter = element.$Parameter.find(
(p) => p.$Name == segments[1],
);
Object.assign(parameter, annotations);
}
} else {
if (typeof element[segments[1]] === "object") {
Object.assign(element[segments[1]], annotations);
} else if (element.$Kind !== "EnumType") {
messages.push(`Invalid annotation target '${target}'`);
}
}
break;
default:
messages.push("More than two annotation target path segments");
}
}
}
}
/**
* Find model element by qualified name
* @param {string} qualifiedName Qualified name of model element
* @return {object} Model element
*/
element(qualifiedName) {
return this.#elements[this.namespaceQualifiedName(qualifiedName)];
}
/**
* a qualified name consists of a namespace or alias, a dot, and a simple name
* @param {string} qualifiedName
* @return {string} namespace-qualified name
*/
namespaceQualifiedName(qualifiedName) {
let np = nameParts(qualifiedName);
return this.namespace[np.qualifier] + "." + np.name;
}
/**
* Key of entity type
* @param {object} entityType Entity Type object
* @return {array} Key of entity type or empty array
*/
key(entityType) {
let type = entityType;
let _key = null;
while (type) {
_key = type.$Key;
if (_key || !type.$BaseType) break;
type = this.element(type.$BaseType);
}
return _key ?? [];
}
/**
* All resources of the model's entity container
* @return {Array} Array of [name, resourceObject] arrays
*/
get resources() {
return Object.entries(this.entityContainer ?? {}).filter(([name]) =>
isIdentifier(name),
);
}
/**
* All structured types onf the model
* @return {Array} Array of [name, typeObject] arrays
*/
get structuredTypes() {
return Object.entries(this.#elements).filter(([, element]) =>
["EntityType", "ComplexType"].includes(element.$Kind),
);
}
/**
* All properties of a structured type including inherited ones
* @param {object} type Structured type
* @return {object} Map of properties
*/
propertiesMapOfStructuredType(type) {
const properties =
type && type.$BaseType
? this.propertiesMapOfStructuredType(this.element(type.$BaseType))
: {};
if (type) {
Object.keys(type)
.filter((name) => isIdentifier(name))
.forEach((name) => {
properties[name] = type[name];
});
}
return properties;
}
/**
* All properties of a structured type including inherited ones
* @param {object} type Structured type
* @return {Array} Array of [name, propertyObject] arrays
*/
propertiesOfStructuredType(type) {
return Object.entries(this.propertiesMapOfStructuredType(type));
}
/**
* Direct properties of a structured type excluding inherited ones
* @param {object} type Structured type
* @return {Array} Array of [name, propertyObject] arrays
*/
directPropertiesOfStructuredType(type) {
return Object.entries(type).filter(([name]) => isIdentifier(name));
}
/**
* Members of an enumeration type
* @param {object} type Structured type
* @return {Array} Array of [name, propertyObject] arrays
*/
enumTypeMembers(type) {
return Object.entries(type).filter(([name]) => isIdentifier(name));
}
};
/**
* an identifier does not start with $ and does not contain @
* @param {string} name
* @return {boolean} name is an identifier
*/
function isIdentifier(name) {
return !name.startsWith("$") && !name.includes("@");
}
/**
* a qualified name consists of a namespace or alias, a dot, and a simple name
* @param {string} qualifiedName
* @return {object} with components qualifier and name
*/
function nameParts(qualifiedName) {
const pos = qualifiedName.lastIndexOf(".");
return {
qualifier: qualifiedName.substring(0, pos),
name: qualifiedName.substring(pos + 1),
};
}
/**
* Construct map of qualified term names
* @param {object} voc Map of vocabularies and terms
* @param {object} alias Map of namespace or alias to alias
*/
function vocabularies(voc, alias) {
const terms = {
Aggregation: ["ApplySupported"],
Authorization: ["Authorizations", "SecuritySchemes"],
Capabilities: [
"BatchSupport",
"BatchSupported",
"ChangeTracking",
"CountRestrictions",
"DeleteRestrictions",
"DeepUpdateSupport",
"ExpandRestrictions",
"FilterRestrictions",
"IndexableByKey",
"InsertRestrictions",
"KeyAsSegmentSupported",
"NavigationRestrictions",
"OperationRestrictions",
"ReadRestrictions",
"SearchRestrictions",
"SelectSupport",
"SkipSupported",
"SortRestrictions",
"TopSupported",
"UpdateRestrictions",
],
Core: [
"AcceptableMediaTypes",
"Computed",
"ComputedDefaultValue",
"DefaultNamespace",
"Description",
"Example",
"Immutable",
"LongDescription",
"OptionalParameter",
"Permissions",
"SchemaVersion",
],
JSON: ["Schema"],
Validation: ["AllowedValues", "Exclusive", "Maximum", "Minimum", "Pattern"],
};
for (const vocab of Object.keys(terms)) {
voc[vocab] = {};
const namespace = `Org.OData.${vocab}.V1`;
for (const term of terms[vocab]) {
voc[vocab][term] = `@${alias[namespace] || namespace}.${term}`;
}
}
}