@atolis-hq/corum 0.1.38 → 0.1.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,5 @@
1
1
  import { applyComponentNameReplacements } from '../../import/config.js';
2
+ import { sanitizeIdSegment } from '../../loader/id-grammar.js';
2
3
  export function extractValue(strategy, operation, message) {
3
4
  switch (strategy.strategy) {
4
5
  case 'channel-segment': {
@@ -76,9 +77,11 @@ export function classifyEvent(classification, operation, message) {
76
77
  return value === classification.domainValue ? 'DomainEvent' : 'IntegrationEvent';
77
78
  }
78
79
  export function deriveNodeId(kind, component, name, opts) {
80
+ // Spec-supplied names (message names, schema names) may contain dots or other
81
+ // characters reserved by the node ID grammar — sanitise them to one segment.
79
82
  if (kind === 'event')
80
- return `${component}.${opts?.template ?? 'IntegrationEvent'}.${name}`;
81
- return `${opts.parentId}.${opts.section}.${name}`;
83
+ return `${sanitizeIdSegment(component)}.${opts?.template ?? 'IntegrationEvent'}.${sanitizeIdSegment(name)}`;
84
+ return `${opts.parentId}.${opts.section}.${sanitizeIdSegment(name)}`;
82
85
  }
83
86
  // Extract the original component schema name from a resolved schema object.
84
87
  // @asyncapi/parser resolves $refs inline but tags each resolved object with
@@ -1,7 +1,8 @@
1
1
  const VALID_EDGE_TYPES = new Set([
2
- 'triggers', 'produces', 'reads', 'calls', 'implements',
2
+ 'triggers', 'produces', 'reads', 'uses-type', 'calls', 'implements',
3
3
  'maps-to', 'derived-from', 'renamed-from', 'has-field', 'has-value',
4
4
  ]);
5
+ const UNRESOLVED_COMPONENT = '_unresolved';
5
6
  export function mapDocument(document, specPath) {
6
7
  const nodes = [];
7
8
  const edges = [];
@@ -9,6 +10,10 @@ export function mapDocument(document, specPath) {
9
10
  const allSchemas = (document.components?.schemas ?? {});
10
11
  // Maps #/components/schemas/X and #/components/schemas/X/properties/Y to materialized node IDs
11
12
  const refToNodeId = new Map();
13
+ const rootNodeIds = getRootNodeIds(document.nodes);
14
+ // Pre-pass: determine canonical component for each schema (shared if referenced by 2+ components)
15
+ const schemaComponents = computeSchemaComponents(document, allSchemas);
16
+ const schemaUsage = computeSchemaUsage(document, allSchemas, rootNodeIds);
12
17
  for (const gap of document.gaps ?? []) {
13
18
  const msg = gap.nodeId
14
19
  ? `[${gap.kind}] ${gap.nodeId}: ${gap.reason ?? ''}`
@@ -20,7 +25,8 @@ export function mapDocument(document, specPath) {
20
25
  diagnostics.push({ severity: 'warning', file: specPath, message: 'Node missing id or type — skipping' });
21
26
  continue;
22
27
  }
23
- const component = nodeId.split('.')[0];
28
+ const normalizedNodeId = normalizeNodeId(nodeId);
29
+ const component = componentFromNodeId(normalizedNodeId);
24
30
  const properties = {};
25
31
  let schemaName;
26
32
  if (entry.schema?.$ref) {
@@ -34,50 +40,40 @@ export function mapDocument(document, specPath) {
34
40
  if (entry['x-aka']?.length) {
35
41
  properties['x-aka'] = entry['x-aka'];
36
42
  }
37
- const node = makeNode(entry.type, component, specPath, nodeId, entry.provenance, properties);
43
+ const node = makeNode(entry.type, component, specPath, normalizedNodeId, entry.provenance, properties);
38
44
  nodes.push(node);
39
45
  if (schemaName) {
40
46
  const expanded = new Map();
41
- expandSchema(schemaName, nodeId, allSchemas, nodes, edges, specPath, component, expanded, refToNodeId);
47
+ expandSchema(schemaName, normalizedNodeId, allSchemas, nodes, edges, specPath, component, expanded, refToNodeId, schemaComponents);
42
48
  }
43
49
  }
44
- // Expand schemas referenced in edges that weren't reachable via any node's schema ref.
45
- // Uses schema name as its own synthetic root so IDs are self-contained and don't collide.
46
- // Enum schemas register their enum values as field nodes so edge refs to them can resolve.
47
- const orphanExpanded = new Set();
48
- for (const raw of document.edges ?? []) {
49
- for (const endpoint of [raw.from, raw.to]) {
50
- const schemaName = extractSchemaName(endpoint);
51
- if (!schemaName)
52
- continue;
53
- const schemaRef = `#/components/schemas/${schemaName}`;
54
- if (refToNodeId.has(schemaRef) || orphanExpanded.has(schemaName))
55
- continue;
56
- orphanExpanded.add(schemaName);
57
- const schemaLocalName = schemaName.includes('.')
58
- ? schemaName.slice(schemaName.lastIndexOf('.') + 1)
59
- : schemaName;
60
- const schemaId = `${schemaLocalName}.schemas.${schemaLocalName}`;
61
- refToNodeId.set(schemaRef, schemaId);
62
- nodes.push(makeNode('Schema', '_', specPath, schemaId, undefined, {}));
63
- const schemaDef = allSchemas[schemaName];
64
- for (const fieldName of Object.keys(schemaDef?.properties ?? {})) {
65
- const fieldId = `${schemaId}.fields.${fieldName}`;
66
- refToNodeId.set(`${schemaRef}/properties/${fieldName}`, fieldId);
67
- nodes.push(makeNode('Field', '_', specPath, fieldId, undefined, {}));
68
- edges.push(makeHasFieldEdge(schemaId, fieldId));
69
- }
70
- if (Array.isArray(schemaDef?.enum)) {
71
- for (const value of schemaDef.enum) {
72
- if (typeof value !== 'string')
73
- continue;
74
- const fieldId = `${schemaId}.values.${value}`;
75
- refToNodeId.set(`${schemaRef}/properties/${value}`, fieldId);
76
- nodes.push(makeNode('EnumValue', '_', specPath, fieldId, undefined, { value }));
77
- edges.push(makeHasFieldEdge(schemaId, fieldId));
78
- }
79
- }
50
+ for (const schemaName of Object.keys(allSchemas)) {
51
+ const schemaRef = `#/components/schemas/${schemaName}`;
52
+ if (refToNodeId.has(schemaRef))
53
+ continue;
54
+ const ownership = determineSchemaOwnership(schemaName, schemaUsage);
55
+ if (ownership.kind === 'unused') {
56
+ diagnostics.push({
57
+ severity: 'warning',
58
+ file: specPath,
59
+ message: `Skipping unused schema '${schemaName}' because no node or edge refers to it`,
60
+ });
61
+ continue;
80
62
  }
63
+ if (ownership.kind === 'unresolved') {
64
+ diagnostics.push({
65
+ severity: 'warning',
66
+ file: specPath,
67
+ message: `Schema '${schemaName}' is only referenced by unresolved schema edges; keeping it under ${UNRESOLVED_COMPONENT}`,
68
+ });
69
+ materializeStandaloneSchema(schemaName, UNRESOLVED_COMPONENT, allSchemas, nodes, edges, specPath, refToNodeId, schemaComponents);
70
+ continue;
71
+ }
72
+ if (ownership.kind === 'inline') {
73
+ expandSchema(schemaName, ownership.rootNodeId, allSchemas, nodes, edges, specPath, ownership.component, new Map(), refToNodeId, schemaComponents);
74
+ continue;
75
+ }
76
+ materializeStandaloneSchema(schemaName, ownership.component, allSchemas, nodes, edges, specPath, refToNodeId, schemaComponents);
81
77
  }
82
78
  // Build case-insensitive fallback index to handle PascalCase/camelCase mismatches
83
79
  // between edge field refs and actual schema property names
@@ -91,8 +87,8 @@ export function mapDocument(document, specPath) {
91
87
  });
92
88
  continue;
93
89
  }
94
- const from = refToNodeId.get(raw.from) ?? refToNodeIdLower.get(raw.from.toLowerCase()) ?? raw.from;
95
- const to = refToNodeId.get(raw.to) ?? refToNodeIdLower.get(raw.to.toLowerCase()) ?? raw.to;
90
+ const from = refToNodeId.get(raw.from) ?? refToNodeIdLower.get(raw.from.toLowerCase()) ?? normalizeNodeId(raw.from);
91
+ const to = refToNodeId.get(raw.to) ?? refToNodeIdLower.get(raw.to.toLowerCase()) ?? normalizeNodeId(raw.to);
96
92
  if (from.startsWith('#/') || to.startsWith('#/')) {
97
93
  diagnostics.push({
98
94
  severity: 'warning',
@@ -105,6 +101,153 @@ export function mapDocument(document, specPath) {
105
101
  }
106
102
  return { nodes, edges, diagnostics };
107
103
  }
104
+ // Pre-pass: walk every node's schema tree to determine which component each schema belongs to.
105
+ // If a schema is referenced by nodes in 2+ different components it is assigned 'shared'.
106
+ function computeSchemaComponents(document, allSchemas) {
107
+ const result = new Map();
108
+ for (const [nodeId, entry] of Object.entries(document.nodes)) {
109
+ const rootName = entry.schema?.$ref ? schemaRefName(entry.schema.$ref) : undefined;
110
+ if (!rootName)
111
+ continue;
112
+ const component = componentFromNodeId(normalizeNodeId(nodeId));
113
+ const visited = new Set();
114
+ collectSchemaRefNames(rootName, allSchemas, visited);
115
+ for (const name of visited) {
116
+ const existing = result.get(name);
117
+ if (!existing)
118
+ result.set(name, component);
119
+ else if (existing !== component)
120
+ result.set(name, 'shared');
121
+ }
122
+ }
123
+ return result;
124
+ }
125
+ function getRootNodeIds(nodes) {
126
+ const nodeIds = Object.keys(nodes).map(normalizeNodeId);
127
+ return nodeIds
128
+ .filter(nodeId => !nodeIds.some(other => other !== nodeId && nodeId.startsWith(`${other}.`)))
129
+ .sort((a, b) => b.length - a.length);
130
+ }
131
+ function computeSchemaUsage(document, allSchemas, rootNodeIds) {
132
+ const usage = {
133
+ rootsBySchema: new Map(),
134
+ directEdgeSchemas: new Set(),
135
+ };
136
+ const schemaLinks = new Map();
137
+ for (const [nodeId, entry] of Object.entries(document.nodes)) {
138
+ const rootName = entry.schema?.$ref ? schemaRefName(entry.schema.$ref) : undefined;
139
+ if (!rootName)
140
+ continue;
141
+ const normalizedNodeId = normalizeNodeId(nodeId);
142
+ const visited = new Set();
143
+ collectSchemaRefNames(rootName, allSchemas, visited);
144
+ for (const name of visited)
145
+ addSchemaUsage(usage, name, normalizedNodeId);
146
+ }
147
+ for (const [schemaName, rawSchema] of Object.entries(allSchemas)) {
148
+ for (const refName of collectDirectSchemaRefs(rawSchema)) {
149
+ addSchemaLink(schemaLinks, schemaName, refName);
150
+ }
151
+ }
152
+ for (const edge of document.edges ?? []) {
153
+ const fromSchema = extractSchemaName(edge.from);
154
+ const toSchema = extractSchemaName(edge.to);
155
+ if (fromSchema && toSchema) {
156
+ usage.directEdgeSchemas.add(fromSchema);
157
+ usage.directEdgeSchemas.add(toSchema);
158
+ addSchemaLink(schemaLinks, fromSchema, toSchema);
159
+ continue;
160
+ }
161
+ if (fromSchema) {
162
+ usage.directEdgeSchemas.add(fromSchema);
163
+ const owner = resolveOwningRootNodeId(normalizeNodeId(edge.to), rootNodeIds);
164
+ if (owner)
165
+ addSchemaUsage(usage, fromSchema, owner);
166
+ }
167
+ if (toSchema) {
168
+ usage.directEdgeSchemas.add(toSchema);
169
+ const owner = resolveOwningRootNodeId(normalizeNodeId(edge.from), rootNodeIds);
170
+ if (owner)
171
+ addSchemaUsage(usage, toSchema, owner);
172
+ }
173
+ }
174
+ let changed = true;
175
+ while (changed) {
176
+ changed = false;
177
+ for (const [schemaName, linkedNames] of schemaLinks) {
178
+ const ownUsage = usage.rootsBySchema.get(schemaName);
179
+ if (!ownUsage)
180
+ continue;
181
+ for (const linkedName of linkedNames) {
182
+ const beforeSize = usage.rootsBySchema.get(linkedName)?.size ?? 0;
183
+ for (const rootNodeId of ownUsage)
184
+ addSchemaUsage(usage, linkedName, rootNodeId);
185
+ if ((usage.rootsBySchema.get(linkedName)?.size ?? 0) !== beforeSize)
186
+ changed = true;
187
+ }
188
+ }
189
+ }
190
+ return usage;
191
+ }
192
+ function collectSchemaRefNames(schemaName, allSchemas, visited) {
193
+ if (visited.has(schemaName))
194
+ return;
195
+ visited.add(schemaName);
196
+ const def = allSchemas[schemaName];
197
+ for (const prop of Object.values(def?.properties ?? {})) {
198
+ const p = prop;
199
+ const ref = typeof p.$ref === 'string' ? schemaRefName(p.$ref) : undefined;
200
+ if (ref)
201
+ collectSchemaRefNames(ref, allSchemas, visited);
202
+ if (p.type === 'array') {
203
+ const items = p.items;
204
+ const itemRef = items && typeof items.$ref === 'string' ? schemaRefName(items.$ref) : undefined;
205
+ if (itemRef)
206
+ collectSchemaRefNames(itemRef, allSchemas, visited);
207
+ }
208
+ }
209
+ }
210
+ function collectDirectSchemaRefs(value) {
211
+ const refs = new Set();
212
+ collectDirectSchemaRefsInto(value, refs);
213
+ return refs;
214
+ }
215
+ function collectDirectSchemaRefsInto(value, refs) {
216
+ if (!value || typeof value !== 'object')
217
+ return;
218
+ if (Array.isArray(value)) {
219
+ for (const item of value)
220
+ collectDirectSchemaRefsInto(item, refs);
221
+ return;
222
+ }
223
+ const record = value;
224
+ const refName = typeof record.$ref === 'string' ? schemaRefName(record.$ref) : undefined;
225
+ if (refName)
226
+ refs.add(refName);
227
+ collectDirectSchemaRefsInto(record.properties, refs);
228
+ collectDirectSchemaRefsInto(record.items, refs);
229
+ collectDirectSchemaRefsInto(record.additionalProperties, refs);
230
+ collectDirectSchemaRefsInto(record.allOf, refs);
231
+ collectDirectSchemaRefsInto(record.anyOf, refs);
232
+ collectDirectSchemaRefsInto(record.oneOf, refs);
233
+ }
234
+ function addSchemaUsage(usage, schemaName, rootNodeId) {
235
+ const roots = usage.rootsBySchema.get(schemaName) ?? new Set();
236
+ roots.add(rootNodeId);
237
+ usage.rootsBySchema.set(schemaName, roots);
238
+ }
239
+ function addSchemaLink(schemaLinks, from, to) {
240
+ if (from === to)
241
+ return;
242
+ const fromSet = schemaLinks.get(from) ?? new Set();
243
+ fromSet.add(to);
244
+ schemaLinks.set(from, fromSet);
245
+ }
246
+ function resolveOwningRootNodeId(nodeId, rootNodeIds) {
247
+ if (nodeId.startsWith('#/'))
248
+ return undefined;
249
+ return rootNodeIds.find(rootNodeId => nodeId === rootNodeId || nodeId.startsWith(`${rootNodeId}.`));
250
+ }
108
251
  function schemaRefName(ref) {
109
252
  const prefix = '#/components/schemas/';
110
253
  if (!ref.startsWith(prefix))
@@ -117,7 +260,82 @@ function extractSchemaName(endpoint) {
117
260
  return undefined;
118
261
  return endpoint.slice(prefix.length).split('/')[0];
119
262
  }
120
- function expandSchema(schemaName, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, refToNodeId) {
263
+ function normalizeNodeId(nodeId) {
264
+ if (nodeId.startsWith('#/'))
265
+ return nodeId;
266
+ const parts = nodeId.split('.');
267
+ if (parts.length === 0)
268
+ return nodeId;
269
+ parts[0] = normalizeComponent(parts[0]);
270
+ return parts.join('.');
271
+ }
272
+ function componentFromNodeId(nodeId) {
273
+ return normalizeComponent(nodeId.split('.')[0] ?? '');
274
+ }
275
+ function normalizeComponent(component) {
276
+ return component === '_' || component.trim() === '' ? UNRESOLVED_COMPONENT : component;
277
+ }
278
+ function determineSchemaOwnership(schemaName, usage) {
279
+ const rootNodeIds = [...(usage.rootsBySchema.get(schemaName) ?? [])];
280
+ if (rootNodeIds.length === 0) {
281
+ return usage.directEdgeSchemas.has(schemaName) ? { kind: 'unresolved' } : { kind: 'unused' };
282
+ }
283
+ if (rootNodeIds.length === 1) {
284
+ return {
285
+ kind: 'inline',
286
+ rootNodeId: rootNodeIds[0],
287
+ component: componentFromNodeId(rootNodeIds[0]),
288
+ };
289
+ }
290
+ const components = new Set(rootNodeIds.map(componentFromNodeId));
291
+ return {
292
+ kind: 'shared',
293
+ component: components.size === 1 ? [...components][0] : 'shared',
294
+ };
295
+ }
296
+ function localSchemaName(schemaName) {
297
+ return schemaName.includes('.')
298
+ ? schemaName.slice(schemaName.lastIndexOf('.') + 1)
299
+ : schemaName;
300
+ }
301
+ function materializeStandaloneSchema(schemaName, component, allSchemas, nodes, edges, specPath, refToNodeId, schemaComponents) {
302
+ const schemaLocalName = localSchemaName(schemaName);
303
+ const schemaDef = allSchemas[schemaName];
304
+ const isEnumSchema = Array.isArray(schemaDef?.enum) && !schemaDef?.properties;
305
+ const template = isEnumSchema ? 'EnumDefinition' : 'Schema';
306
+ const schemaId = `${component}.${template}.${schemaLocalName}`;
307
+ const schemaRef = `#/components/schemas/${schemaName}`;
308
+ if (refToNodeId.has(schemaRef))
309
+ return refToNodeId.get(schemaRef);
310
+ refToNodeId.set(schemaRef, schemaId);
311
+ nodes.push(makeNode(template, component, specPath, schemaId, undefined, {}));
312
+ if (isEnumSchema) {
313
+ for (const value of schemaDef.enum) {
314
+ if (typeof value !== 'string')
315
+ continue;
316
+ const valueId = `${schemaId}.values.${value}`;
317
+ refToNodeId.set(`${schemaRef}/properties/${value}`, valueId);
318
+ nodes.push(makeNode('EnumValue', component, specPath, valueId, undefined, { name: value }));
319
+ edges.push(makeHasValueEdge(schemaId, valueId));
320
+ }
321
+ return schemaId;
322
+ }
323
+ const expanded = new Map([[schemaName, schemaId]]);
324
+ const localMappings = new Map();
325
+ for (const [fieldName, rawProp] of Object.entries(schemaDef?.properties ?? {})) {
326
+ const fieldId = `${schemaId}.fields.${fieldName}`;
327
+ const fieldRef = `${schemaRef}/properties/${fieldName}`;
328
+ if (!refToNodeId.has(fieldRef))
329
+ refToNodeId.set(fieldRef, fieldId);
330
+ const fieldNode = makeNode('Field', component, specPath, fieldId, undefined, {});
331
+ const required = Array.isArray(schemaDef?.required) && schemaDef.required.includes(fieldName);
332
+ fieldNode.properties = resolveFieldProperties(fieldName, rawProp, required, schemaId, schemaId, allSchemas, nodes, edges, specPath, component, expanded, localMappings, refToNodeId, schemaComponents);
333
+ nodes.push(fieldNode);
334
+ edges.push(makeHasFieldEdge(schemaId, fieldId));
335
+ }
336
+ return schemaId;
337
+ }
338
+ function expandSchema(schemaName, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, refToNodeId, schemaComponents = new Map()) {
121
339
  const existing = expanded.get(schemaName);
122
340
  if (existing)
123
341
  return existing;
@@ -125,17 +343,36 @@ function expandSchema(schemaName, rootNodeId, allSchemas, nodes, edges, specPath
125
343
  // (e.g. "workers.ReactToPersonCreatedCommand"). Strip the prefix so the local segment
126
344
  // in the node ID is a simple identifier — graph serialization rejects local names
127
345
  // that contain dots because it uses dots as a hierarchy separator.
128
- const schemaLocalName = schemaName.includes('.')
129
- ? schemaName.slice(schemaName.lastIndexOf('.') + 1)
130
- : schemaName;
346
+ const schemaLocalName = localSchemaName(schemaName);
347
+ const schemaDef = allSchemas[schemaName];
348
+ const isEnumSchema = Array.isArray(schemaDef?.enum) && !schemaDef?.properties;
349
+ const schemaRef = `#/components/schemas/${schemaName}`;
350
+ if (isEnumSchema) {
351
+ // Enum schemas become standalone EnumDefinition cluster roots, matching the OpenAPI/AsyncAPI
352
+ // adapters. Using refToNodeId as shared dedup so only one node is created per mapDocument call
353
+ // even though `expanded` is scoped per parent node.
354
+ const enumComponent = schemaComponents.get(schemaName) ?? component;
355
+ const enumId = `${enumComponent}.EnumDefinition.${schemaLocalName}`;
356
+ expanded.set(schemaName, enumId);
357
+ if (!refToNodeId.has(schemaRef)) {
358
+ refToNodeId.set(schemaRef, enumId);
359
+ nodes.push(makeNode('EnumDefinition', enumComponent, specPath, enumId, undefined, {}));
360
+ for (const value of schemaDef.enum) {
361
+ if (typeof value !== 'string')
362
+ continue;
363
+ const valueId = `${enumId}.values.${value}`;
364
+ refToNodeId.set(`${schemaRef}/properties/${value}`, valueId);
365
+ nodes.push(makeNode('EnumValue', enumComponent, specPath, valueId, undefined, { name: value }));
366
+ edges.push(makeHasValueEdge(enumId, valueId));
367
+ }
368
+ }
369
+ return refToNodeId.get(schemaRef);
370
+ }
131
371
  const schemaId = `${rootNodeId}.schemas.${schemaLocalName}`;
132
372
  expanded.set(schemaName, schemaId);
133
- const schemaRef = `#/components/schemas/${schemaName}`;
134
373
  if (!refToNodeId.has(schemaRef))
135
374
  refToNodeId.set(schemaRef, schemaId);
136
- const schemaDef = allSchemas[schemaName];
137
- const schemaNode = makeNode('Schema', component, specPath, schemaId, undefined, {});
138
- nodes.push(schemaNode);
375
+ nodes.push(makeNode('Schema', component, specPath, schemaId, undefined, {}));
139
376
  edges.push(makeHasFieldEdge(rootNodeId, schemaId));
140
377
  const localMappings = new Map();
141
378
  for (const [fieldName, rawProp] of Object.entries(schemaDef?.properties ?? {})) {
@@ -145,32 +382,18 @@ function expandSchema(schemaName, rootNodeId, allSchemas, nodes, edges, specPath
145
382
  refToNodeId.set(fieldRef, fieldId);
146
383
  const fieldNode = makeNode('Field', component, specPath, fieldId, undefined, {});
147
384
  const required = Array.isArray(schemaDef?.required) && schemaDef.required.includes(fieldName);
148
- fieldNode.properties = resolveFieldProperties(fieldName, rawProp, required, schemaId, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, localMappings, refToNodeId);
385
+ fieldNode.properties = resolveFieldProperties(fieldName, rawProp, required, schemaId, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, localMappings, refToNodeId, schemaComponents);
149
386
  nodes.push(fieldNode);
150
387
  edges.push(makeHasFieldEdge(schemaId, fieldId));
151
388
  }
152
- // Register enum values as field refs so edges referencing them (as /properties/{value}) can resolve
153
- if (Array.isArray(schemaDef?.enum)) {
154
- for (const value of schemaDef.enum) {
155
- if (typeof value !== 'string')
156
- continue;
157
- const fieldRef = `${schemaRef}/properties/${value}`;
158
- if (!refToNodeId.has(fieldRef)) {
159
- const fieldId = `${schemaId}.values.${value}`;
160
- refToNodeId.set(fieldRef, fieldId);
161
- nodes.push(makeNode('EnumValue', component, specPath, fieldId, undefined, { value }));
162
- edges.push(makeHasFieldEdge(schemaId, fieldId));
163
- }
164
- }
165
- }
166
389
  return schemaId;
167
390
  }
168
- function resolveFieldProperties(fieldName, prop, required, schemaId, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, localMappings, refToNodeId) {
391
+ function resolveFieldProperties(fieldName, prop, required, schemaId, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, localMappings, refToNodeId, schemaComponents) {
169
392
  const nullable = !required;
170
393
  if (typeof prop.$ref === 'string') {
171
394
  const refName = schemaRefName(prop.$ref);
172
395
  if (refName) {
173
- const refId = expandSchema(refName, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, refToNodeId);
396
+ const refId = expandSchema(refName, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, refToNodeId, schemaComponents);
174
397
  return { $ref: refId, nullable, collection: 'one' };
175
398
  }
176
399
  return { type: 'string', nullable, collection: 'one' };
@@ -181,7 +404,7 @@ function resolveFieldProperties(fieldName, prop, required, schemaId, rootNodeId,
181
404
  if (items && typeof items.$ref === 'string') {
182
405
  const refName = schemaRefName(items.$ref);
183
406
  if (refName) {
184
- const refId = expandSchema(refName, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, refToNodeId);
407
+ const refId = expandSchema(refName, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, refToNodeId, schemaComponents);
185
408
  return { $ref: refId, nullable, collection: 'array' };
186
409
  }
187
410
  }
@@ -189,13 +412,13 @@ function resolveFieldProperties(fieldName, prop, required, schemaId, rootNodeId,
189
412
  return { type: itemType, nullable, collection: 'array' };
190
413
  }
191
414
  if (type === 'object' && prop.additionalProperties !== undefined) {
192
- createMapping(fieldName, prop.additionalProperties, schemaId, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, localMappings, refToNodeId);
415
+ createMapping(fieldName, prop.additionalProperties, schemaId, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, localMappings, refToNodeId, schemaComponents);
193
416
  return { $ref: `#/mappings/${fieldName}`, nullable, collection: 'one' };
194
417
  }
195
418
  const scalarType = mapScalar(type ?? 'string', prop.format);
196
419
  return { type: scalarType, nullable, collection: 'one' };
197
420
  }
198
- function createMapping(mappingName, addlDef, schemaId, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, localMappings, refToNodeId) {
421
+ function createMapping(mappingName, addlDef, schemaId, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, localMappings, refToNodeId, schemaComponents) {
199
422
  const existing = localMappings.get(mappingName);
200
423
  if (existing)
201
424
  return existing;
@@ -211,7 +434,7 @@ function createMapping(mappingName, addlDef, schemaId, rootNodeId, allSchemas, n
211
434
  if (typeof addl.$ref === 'string') {
212
435
  const refName = schemaRefName(addl.$ref);
213
436
  if (refName) {
214
- props.$ref = expandSchema(refName, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, refToNodeId);
437
+ props.$ref = expandSchema(refName, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, refToNodeId, schemaComponents);
215
438
  }
216
439
  else {
217
440
  props.type = 'string';
@@ -223,7 +446,7 @@ function createMapping(mappingName, addlDef, schemaId, rootNodeId, allSchemas, n
223
446
  if (items && typeof items.$ref === 'string') {
224
447
  const refName = schemaRefName(items.$ref);
225
448
  if (refName) {
226
- props.$ref = expandSchema(refName, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, refToNodeId);
449
+ props.$ref = expandSchema(refName, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, refToNodeId, schemaComponents);
227
450
  }
228
451
  else {
229
452
  props.type = 'string';
@@ -235,7 +458,7 @@ function createMapping(mappingName, addlDef, schemaId, rootNodeId, allSchemas, n
235
458
  }
236
459
  else if (addl.type === 'object' && addl.additionalProperties !== undefined) {
237
460
  const innerName = `${mappingName}-values`;
238
- props.$ref = createMapping(innerName, addl.additionalProperties, schemaId, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, localMappings, refToNodeId);
461
+ props.$ref = createMapping(innerName, addl.additionalProperties, schemaId, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, localMappings, refToNodeId, schemaComponents);
239
462
  }
240
463
  else {
241
464
  props.type = mapScalar(addl.type ?? 'string', addl.format);
@@ -290,6 +513,18 @@ function makeHasFieldEdge(from, to) {
290
513
  derivedBy: 'adapter:corum',
291
514
  };
292
515
  }
516
+ function makeHasValueEdge(from, to) {
517
+ return {
518
+ id: `${from}__has-value__${to}`,
519
+ from,
520
+ to,
521
+ type: 'has-value',
522
+ state: 'implemented',
523
+ stability: 'unstable',
524
+ derivation: 'determined',
525
+ derivedBy: 'adapter:corum',
526
+ };
527
+ }
293
528
  function mapEdge(raw) {
294
529
  return {
295
530
  id: `${raw.from}__${raw.type}__${raw.to}`,
@@ -1,4 +1,5 @@
1
1
  import { applyComponentNameReplacements } from '../../import/config.js';
2
+ import { sanitizeIdSegment } from '../../loader/id-grammar.js';
2
3
  export function deriveComponent(path, mapping) {
3
4
  if (mapping.strategy === 'hardcoded')
4
5
  return mapping.component;
@@ -30,9 +31,11 @@ function resolveAllOfRef(schema) {
30
31
  return schema;
31
32
  }
32
33
  export function deriveNodeId(kind, component, name, parentId, section) {
34
+ // Spec-supplied names (operationIds, schema names) may contain dots or other
35
+ // characters reserved by the node ID grammar — sanitise them to one segment.
33
36
  if (kind === 'operation')
34
- return `${component}.APIEndpoint.${name}`;
35
- return `${parentId}.${section}.${name}`;
37
+ return `${sanitizeIdSegment(component ?? '')}.APIEndpoint.${sanitizeIdSegment(name)}`;
38
+ return `${parentId}.${section}.${sanitizeIdSegment(name)}`;
36
39
  }
37
40
  export function refName(ref) {
38
41
  return ref.split('/').pop() ?? ref;
@@ -59,6 +59,23 @@ program
59
59
  port: opts.port,
60
60
  });
61
61
  });
62
+ program
63
+ .command('lint')
64
+ .description('Lint the graph and report diagnostics')
65
+ .option('--graph <path>', 'Override graph path')
66
+ .action(async (opts) => {
67
+ const config = buildRuntimeConfig(opts.graph);
68
+ try {
69
+ const graph = await loadGraph({ source: config.source, strict: false });
70
+ reportDiagnostics(graph.diagnostics, 'Lint complete');
71
+ if (graph.diagnostics.some(d => d.severity === 'error'))
72
+ process.exit(1);
73
+ }
74
+ catch (err) {
75
+ process.stderr.write(`[ERROR] ${err instanceof Error ? err.message : String(err)}\n`);
76
+ process.exit(2);
77
+ }
78
+ });
62
79
  const CONFIG_TEMPLATE = `# Corum project configuration
63
80
  # Uncomment and set the options relevant to your setup.
64
81
  # All values can be overridden by environment variables (CORUM_*) or CLI flags.
@@ -147,6 +164,7 @@ const importCmd = program.command('import')
147
164
  .description('Import specifications into the graph')
148
165
  .option('--config <path>', 'Path to import config YAML')
149
166
  .option('--graph <path>', 'Override CORUM_GRAPH_PATH')
167
+ .option('-b, --branch <name>', 'Target branch to commit the import to (required for git sources)')
150
168
  .addHelpText('after', '\nFor import config file format and options, see: https://github.com/atolis-hq/corum#readme')
151
169
  .action(async (opts) => {
152
170
  if (!opts.config) {
@@ -156,7 +174,7 @@ const importCmd = program.command('import')
156
174
  try {
157
175
  const runtimeConfig = buildRuntimeConfig(opts.graph);
158
176
  const config = loadImportConfig(path.resolve(opts.config));
159
- const result = await runImport(config, runtimeConfig);
177
+ const result = await runImport(config, runtimeConfig, { branch: opts.branch });
160
178
  reportDiagnostics(result.diagnostics);
161
179
  if (result.diagnostics.some(d => d.severity === 'error'))
162
180
  process.exit(1);
@@ -174,11 +192,12 @@ importCmd
174
192
  .option('--pattern <regex>', 'Regex pattern (uri-segment strategy)')
175
193
  .option('--component <name>', 'Component name (hardcoded strategy)')
176
194
  .option('--graph <path>', 'Override CORUM_GRAPH_PATH')
195
+ .option('-b, --branch <name>', 'Target branch to commit the import to (required for git sources)')
177
196
  .action(async (spec, opts) => {
178
197
  try {
179
198
  const runtimeConfig = buildRuntimeConfig(opts.graph);
180
199
  const entry = buildOpenAPIConfig(spec, opts.componentStrategy, opts.segment, opts.pattern, opts.component);
181
- const result = await runImport({ imports: [entry] }, runtimeConfig);
200
+ const result = await runImport({ imports: [entry] }, runtimeConfig, { branch: opts.branch });
182
201
  reportDiagnostics(result.diagnostics);
183
202
  if (result.diagnostics.some(d => d.severity === 'error'))
184
203
  process.exit(1);
@@ -199,6 +218,7 @@ importCmd
199
218
  .option('--event-classification <mode>', 'Event classification: always-integration, always-domain', 'always-integration')
200
219
  .option('--include-consumed', 'Also import receive (consumed) operations', false)
201
220
  .option('--graph <path>', 'Override CORUM_GRAPH_PATH')
221
+ .option('-b, --branch <name>', 'Target branch to commit the import to (required for git sources)')
202
222
  .action(async (spec, opts) => {
203
223
  try {
204
224
  const runtimeConfig = buildRuntimeConfig(opts.graph);
@@ -214,7 +234,7 @@ importCmd
214
234
  if (opts.includeConsumed) {
215
235
  entry.includeConsumed = true;
216
236
  }
217
- const result = await runImport({ imports: [entry] }, runtimeConfig);
237
+ const result = await runImport({ imports: [entry] }, runtimeConfig, { branch: opts.branch });
218
238
  reportDiagnostics(result.diagnostics);
219
239
  if (result.diagnostics.some(d => d.severity === 'error'))
220
240
  process.exit(1);
@@ -228,11 +248,12 @@ importCmd
228
248
  .command('corum <spec>')
229
249
  .description('Import a corum interchange file into the graph')
230
250
  .option('--graph <path>', 'Override CORUM_GRAPH_PATH')
251
+ .option('-b, --branch <name>', 'Target branch to commit the import to (required for git sources)')
231
252
  .action(async (spec, opts) => {
232
253
  try {
233
254
  const runtimeConfig = buildRuntimeConfig(opts.graph);
234
255
  const entry = { adapter: 'corum', spec: path.resolve(spec) };
235
- const result = await runImport({ imports: [entry] }, runtimeConfig);
256
+ const result = await runImport({ imports: [entry] }, runtimeConfig, { branch: opts.branch });
236
257
  reportDiagnostics(result.diagnostics);
237
258
  if (result.diagnostics.some(d => d.severity === 'error'))
238
259
  process.exit(1);
@@ -279,14 +300,14 @@ function buildRuntimeConfig(graphOverride) {
279
300
  process.env.CORUM_GRAPH_PATH = path.resolve(graphOverride);
280
301
  return createGraphRuntimeConfig();
281
302
  }
282
- function reportDiagnostics(diagnostics) {
303
+ function reportDiagnostics(diagnostics, label = 'Import complete') {
283
304
  for (const d of diagnostics) {
284
305
  const prefix = d.severity === 'error' ? 'ERROR' : 'WARN';
285
306
  process.stderr.write(`[${prefix}] ${d.file}: ${d.message}\n`);
286
307
  }
287
308
  const errors = diagnostics.filter(d => d.severity === 'error').length;
288
309
  const warnings = diagnostics.filter(d => d.severity === 'warning').length;
289
- process.stdout.write(`Import complete. ${errors} error(s), ${warnings} warning(s).\n`);
310
+ process.stdout.write(`${label}. ${errors} error(s), ${warnings} warning(s).\n`);
290
311
  }
291
312
  function reportGraphDiagnostics(diagnostics) {
292
313
  const warnings = diagnostics.filter(d => d.severity === 'warning');