@workos/oagen 0.13.0 → 0.14.1

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.
@@ -7221,6 +7221,10 @@ function diffSnapshots(baseline, candidate, policy) {
7221
7221
  changes.push(classifyAddedSymbol(candSym));
7222
7222
  }
7223
7223
  detectForkedSchemas(changes, baseline, candidate);
7224
+ const typeRenames = detectTypeRenames(changes, baseline, candidate);
7225
+ const enumRenames = detectEnumRenames(changes, baseline, candidate);
7226
+ inferTransitiveTypeRenames(typeRenames, baseline, candidate, changes);
7227
+ cascadeRenameDowngrades(changes, typeRenames, enumRenames, baseline, candidate);
7224
7228
  return {
7225
7229
  changes,
7226
7230
  summary: summarizeChanges(changes)
@@ -7295,6 +7299,279 @@ function collectTypeFieldSets(snapshot) {
7295
7299
  return result;
7296
7300
  }
7297
7301
  /**
7302
+ * Detect type renames: a baseline type symbol disappears and a candidate
7303
+ * type symbol with the same (or superset) field set takes its place.
7304
+ *
7305
+ * Common when an upstream spec promotes a single schema into multiple
7306
+ * (e.g. `ApiKey` → `OrganizationApiKey` + `UserApiKey`). The wire shape
7307
+ * returned by individual endpoints is unchanged — `OrganizationApiKey`
7308
+ * has the same fields `ApiKey` had — so consumer code accessing those
7309
+ * fields keeps working. The compat report flags `ApiKey` as removed
7310
+ * because its symbol is gone; this pass downgrades the removal to
7311
+ * soft-risk and records the rename so its child fields/methods cascade.
7312
+ *
7313
+ * Identity criteria (must all hold):
7314
+ * 1. The removed symbol owns ≥ 1 field/property in the baseline (i.e.
7315
+ * it's a type-shaped symbol — model/interface/class — not a plain
7316
+ * function or constant).
7317
+ * 2. Some candidate type that did *not* exist in the baseline has a
7318
+ * field set that is a non-strict superset of the removed type's
7319
+ * fields. (Strict-subset would mean fields were lost — a real break.)
7320
+ * 3. The candidate type is the alphabetically-first such match, so
7321
+ * pairing is deterministic when multiple candidates fit (e.g. both
7322
+ * `OrganizationApiKey` and `UserApiKey` share the original fields).
7323
+ *
7324
+ * Returns a `removedName -> newName` map so a downstream cascade pass
7325
+ * can downgrade owned-field removals and `*_type_changed` pointing at
7326
+ * the same pair.
7327
+ *
7328
+ * Mutates matching `symbol_removed` entries in `changes`: severity →
7329
+ * `soft-risk`, attaches a `remediation` describing the rename.
7330
+ */
7331
+ function detectTypeRenames(changes, baseline, candidate) {
7332
+ const renameMap = /* @__PURE__ */ new Map();
7333
+ const baselineTypeFields = collectTypeFieldSets(baseline);
7334
+ const candidateTypeFields = collectTypeFieldSets(candidate);
7335
+ const baselineTypeNames = new Set(baselineTypeFields.keys());
7336
+ const candidateTypesSorted = [...candidateTypeFields.entries()].sort((a, b) => a[0].localeCompare(b[0]));
7337
+ const parentRemovalByName = /* @__PURE__ */ new Map();
7338
+ for (const change of changes) if (change.category === "symbol_removed" && change.severity === "breaking") parentRemovalByName.set(change.old.symbol ?? "", change);
7339
+ for (const [baselineTypeName, baselineFields] of baselineTypeFields) {
7340
+ if (baselineFields.size === 0) continue;
7341
+ const sameNameFields = candidateTypeFields.get(baselineTypeName);
7342
+ if (sameNameFields && setEquals(baselineFields, sameNameFields)) continue;
7343
+ const match = candidateTypesSorted.find(([candName, candFields]) => {
7344
+ if (baselineTypeNames.has(candName)) return false;
7345
+ for (const f of baselineFields) if (!candFields.has(f)) return false;
7346
+ return true;
7347
+ });
7348
+ if (!match) continue;
7349
+ const [newName] = match;
7350
+ renameMap.set(baselineTypeName, newName);
7351
+ const parentRemoval = parentRemovalByName.get(baselineTypeName);
7352
+ if (parentRemoval) {
7353
+ parentRemoval.severity = "soft-risk";
7354
+ parentRemoval.remediation = `Type "${baselineTypeName}" appears to have been renamed to "${newName}" — the new type has every field of the old (a non-strict superset). Field accesses and method calls on values of type "${newName}" continue to work; only explicit "${baselineTypeName}" type annotations need to migrate. Consider emitting a deprecated alias \`type ${baselineTypeName} = ${newName}\` in languages that support it.`;
7355
+ }
7356
+ }
7357
+ return renameMap;
7358
+ }
7359
+ /**
7360
+ * Transitive rename inference. After `detectTypeRenames` records direct
7361
+ * structural matches (e.g. `ApiKeyWithValue → OrganizationApiKeyWithValue`),
7362
+ * walk the parent's fields and follow the `typeRef.name` of each: if the
7363
+ * baseline parent's field references an alias-type `OldOwner` and the new
7364
+ * parent's same-named field references a different alias `NewOwner`, the
7365
+ * pair is the same type-concept under a renamed name. Record the
7366
+ * secondary rename so the cascade can downgrade `OldOwner`'s
7367
+ * `symbol_removed` and any `field_type_changed` pointing at the pair.
7368
+ *
7369
+ * Why this is necessary: discriminated-union owner types (Go's
7370
+ * `APIKeyWithValueOwner`, PHP/Python's nested constructor params) are
7371
+ * frequently emitted as `kind: 'alias'` symbols with no extractable
7372
+ * field children — so `collectTypeFieldSets` returns empty for them and
7373
+ * the structural matcher can't pair them directly. The transitive
7374
+ * lookup gives us a deterministic, conservative way to propagate the
7375
+ * rename: it only fires when the parent rename has already been
7376
+ * positively identified, and only follows fields that share the same
7377
+ * wire name across baseline and candidate.
7378
+ */
7379
+ function inferTransitiveTypeRenames(typeRenames, baseline, candidate, changes) {
7380
+ if (typeRenames.size === 0) return;
7381
+ const baselineFieldType = /* @__PURE__ */ new Map();
7382
+ for (const sym of baseline.symbols) {
7383
+ if (sym.kind !== "field" && sym.kind !== "property") continue;
7384
+ if (!sym.ownerFqName || !sym.typeRef) continue;
7385
+ const localName = sym.fqName.includes(".") ? sym.fqName.slice(sym.fqName.lastIndexOf(".") + 1) : sym.fqName;
7386
+ baselineFieldType.set(`${sym.ownerFqName}.${localName.toLowerCase()}`, sym.typeRef.name);
7387
+ }
7388
+ const candidateFieldType = /* @__PURE__ */ new Map();
7389
+ for (const sym of candidate.symbols) {
7390
+ if (sym.kind !== "field" && sym.kind !== "property") continue;
7391
+ if (!sym.ownerFqName || !sym.typeRef) continue;
7392
+ const localName = sym.fqName.includes(".") ? sym.fqName.slice(sym.fqName.lastIndexOf(".") + 1) : sym.fqName;
7393
+ candidateFieldType.set(`${sym.ownerFqName}.${localName.toLowerCase()}`, sym.typeRef.name);
7394
+ }
7395
+ const parentRemovalByName = /* @__PURE__ */ new Map();
7396
+ for (const change of changes) if (change.category === "symbol_removed" && change.severity === "breaking") parentRemovalByName.set(change.old.symbol ?? "", change);
7397
+ const initialEntries = [...typeRenames.entries()];
7398
+ for (const [oldOwner, newOwner] of initialEntries) for (const [baselineKey, baselineFieldTypeRef] of baselineFieldType) {
7399
+ if (!baselineKey.startsWith(`${oldOwner}.`)) continue;
7400
+ const localName = baselineKey.slice(oldOwner.length + 1);
7401
+ const candidateKey = `${newOwner}.${localName}`;
7402
+ const candidateFieldTypeRef = candidateFieldType.get(candidateKey);
7403
+ if (!candidateFieldTypeRef) continue;
7404
+ const oldT = bareTypeName(baselineFieldTypeRef);
7405
+ const newT = bareTypeName(candidateFieldTypeRef);
7406
+ if (!oldT || !newT || oldT === newT) continue;
7407
+ if (typeRenames.has(oldT)) continue;
7408
+ typeRenames.set(oldT, newT);
7409
+ const removal = parentRemovalByName.get(oldT);
7410
+ if (removal) {
7411
+ removal.severity = "soft-risk";
7412
+ removal.remediation = `Type "${oldT}" appears to have been renamed to "${newT}" — inferred transitively because the renamed parent "${oldOwner}" → "${newOwner}" has a "${localName}" field whose type swapped from "${oldT}" to "${newT}". Consider emitting a deprecated alias \`type ${oldT} = ${newT}\` in languages that support it.`;
7413
+ }
7414
+ }
7415
+ }
7416
+ /**
7417
+ * Detect enum canonical-flips: a baseline enum disappears and a candidate
7418
+ * enum with the **same wire-value set** takes its place.
7419
+ *
7420
+ * Caused by the emitter's enum-dedup heuristic picking a different
7421
+ * canonical name when a new same-shape enum joins the spec. Languages
7422
+ * that emit type aliases (Go, Ruby, Python, PHP, Kotlin) handle this
7423
+ * transparently via `type Old = New`; languages without first-class
7424
+ * aliases (dotnet) report the old enum as removed. The wire values are
7425
+ * unchanged — every legal value still serializes to the same JSON — so
7426
+ * consumer code constructing or matching on these enum values keeps
7427
+ * working. Only references to the typed enum class need migration.
7428
+ *
7429
+ * Identity criterion: a removed enum's value set is *exactly* equal to
7430
+ * a newly-added enum's value set (not superset — narrowing the value
7431
+ * set would be a real break for consumers expecting the dropped values).
7432
+ */
7433
+ function detectEnumRenames(changes, baseline, candidate) {
7434
+ const renameMap = /* @__PURE__ */ new Map();
7435
+ const baselineEnumValues = collectEnumValueSets(baseline);
7436
+ const candidateEnumValues = collectEnumValueSets(candidate);
7437
+ const baselineEnumNames = new Set(baselineEnumValues.keys());
7438
+ const candidateEnumsSorted = [...candidateEnumValues.entries()].sort((a, b) => a[0].localeCompare(b[0]));
7439
+ const parentRemovalByName = /* @__PURE__ */ new Map();
7440
+ for (const change of changes) if (change.category === "symbol_removed" && change.severity === "breaking") parentRemovalByName.set(change.old.symbol ?? "", change);
7441
+ for (const [baselineEnumName, baselineValues] of baselineEnumValues) {
7442
+ if (baselineValues.size === 0) continue;
7443
+ const sameNameValues = candidateEnumValues.get(baselineEnumName);
7444
+ if (sameNameValues && setEquals(baselineValues, sameNameValues)) continue;
7445
+ const match = candidateEnumsSorted.find(([candName, candValues]) => !baselineEnumNames.has(candName) && setEquals(baselineValues, candValues));
7446
+ if (!match) continue;
7447
+ const [newName] = match;
7448
+ renameMap.set(baselineEnumName, newName);
7449
+ const parentRemoval = parentRemovalByName.get(baselineEnumName);
7450
+ if (parentRemoval) {
7451
+ parentRemoval.severity = "soft-risk";
7452
+ parentRemoval.remediation = `Enum "${baselineEnumName}" appears to have been renamed to "${newName}" — both enums have identical wire values, so on-the-wire serialization is unchanged. This is typically the emitter's dedup canonical-flip after a new same-shape enum joined the spec. Consider emitting a deprecated alias in languages that support it, or pinning the canonical via emitter config.`;
7453
+ }
7454
+ }
7455
+ return renameMap;
7456
+ }
7457
+ /**
7458
+ * Cascade rename downgrades to changes whose meaning depends on a renamed
7459
+ * symbol. Walks every change and:
7460
+ *
7461
+ * - Downgrades child removals (`Owner.field` removed where `Owner` was
7462
+ * renamed) — the field still exists, just under a new owner fqName.
7463
+ * Same logic for enum members under a renamed enum.
7464
+ * - Downgrades `return_type_changed` / `field_type_changed` whose
7465
+ * old → new pair matches a recorded rename — the type swap is the
7466
+ * rename itself, not a meaningful signature break.
7467
+ *
7468
+ * Each cascaded change gets a remediation pointing at the parent rename
7469
+ * so the reviewer can find the explanation in the report.
7470
+ */
7471
+ function cascadeRenameDowngrades(changes, typeRenames, enumRenames, baseline, candidate) {
7472
+ let baselineTypeFields;
7473
+ let candidateTypeFields;
7474
+ const getBaselineFields = () => {
7475
+ if (!baselineTypeFields) baselineTypeFields = collectTypeFieldSets(baseline);
7476
+ return baselineTypeFields;
7477
+ };
7478
+ const getCandidateFields = () => {
7479
+ if (!candidateTypeFields) candidateTypeFields = collectTypeFieldSets(candidate);
7480
+ return candidateTypeFields;
7481
+ };
7482
+ /**
7483
+ * Structural equivalence: candidate type's field set is a non-strict
7484
+ * superset of baseline type's field set, AND the candidate type was
7485
+ * newly added in this diff (not a swap to a pre-existing type, which
7486
+ * is a real signature break the consumer chose). When `New ⊇ Old` and
7487
+ * `New` is new, every field the consumer accessed on the old type
7488
+ * still exists on the value they receive — so the swap is non-breaking
7489
+ * at the value level even though the declared type changed. This is
7490
+ * the fork-detector's positive signal, applied as a severity
7491
+ * downgrade in addition to the existing remediation hint.
7492
+ */
7493
+ const isStructurallyEquivalent = (oldT, newT) => {
7494
+ if (!oldT || !newT || oldT === newT) return false;
7495
+ const baselineFields = getBaselineFields();
7496
+ const candidateFields = getCandidateFields();
7497
+ if (baselineFields.has(newT)) return false;
7498
+ const oldFields = baselineFields.get(oldT);
7499
+ const newFields = candidateFields.get(newT);
7500
+ if (!oldFields || !newFields) return false;
7501
+ if (oldFields.size === 0) return false;
7502
+ for (const f of oldFields) if (!newFields.has(f)) return false;
7503
+ return true;
7504
+ };
7505
+ for (const change of changes) {
7506
+ if (change.severity !== "breaking") continue;
7507
+ if (change.category === "symbol_removed") {
7508
+ const removed = change.old.symbol ?? "";
7509
+ const dotIdx = removed.indexOf(".");
7510
+ if (dotIdx <= 0) continue;
7511
+ const ownerName = removed.slice(0, dotIdx);
7512
+ const renamedTo = typeRenames.get(ownerName) ?? enumRenames.get(ownerName);
7513
+ if (!renamedTo) continue;
7514
+ change.severity = "soft-risk";
7515
+ change.remediation = `Owned by renamed symbol "${ownerName}" (now "${renamedTo}"). The same member exists on the new symbol under "${renamedTo}.${removed.slice(dotIdx + 1)}".`;
7516
+ continue;
7517
+ }
7518
+ if (change.category === "return_type_changed") {
7519
+ const oldT = bareTypeName(change.old.returnType ?? "");
7520
+ const newT = bareTypeName(change.new.returnType ?? "");
7521
+ if (typeRenames.get(oldT) === newT) {
7522
+ change.severity = "soft-risk";
7523
+ change.remediation = `Return type swap matches recorded rename "${oldT}" → "${newT}". The underlying field set is preserved (see the rename advisory on "${oldT}").`;
7524
+ } else if (isStructurallyEquivalent(oldT, newT)) {
7525
+ change.severity = "soft-risk";
7526
+ if (!change.remediation) change.remediation = `Return type swap from "${oldT}" to "${newT}" is structurally equivalent — every field on "${oldT}" still exists on "${newT}". Consumer code accessing fields on the returned value continues to work; only explicit "${oldT}" type annotations need to migrate.`;
7527
+ }
7528
+ continue;
7529
+ }
7530
+ if (change.category === "field_type_changed") {
7531
+ const oldT = bareTypeName(change.old.type ?? "");
7532
+ const newT = bareTypeName(change.new.type ?? "");
7533
+ if ((typeRenames.get(oldT) ?? enumRenames.get(oldT)) === newT) {
7534
+ change.severity = "soft-risk";
7535
+ change.remediation = `Field type swap matches recorded rename "${oldT}" → "${newT}". On-the-wire shape is unchanged (see the rename advisory on "${oldT}").`;
7536
+ } else if (isStructurallyEquivalent(oldT, newT)) {
7537
+ change.severity = "soft-risk";
7538
+ if (!change.remediation) change.remediation = `Field type swap from "${oldT}" to "${newT}" is structurally equivalent — every field on "${oldT}" still exists on "${newT}".`;
7539
+ }
7540
+ }
7541
+ }
7542
+ }
7543
+ /**
7544
+ * Build a map from enum fqName → set of wire values. Used by
7545
+ * `detectEnumRenames` to find structurally-identical enums across
7546
+ * baseline and candidate. Wire values come from `enum_member.value`
7547
+ * (the JSON-level value) — not the member names, which are
7548
+ * language-specific PascalCase forms.
7549
+ *
7550
+ * Members whose `value` is undefined are skipped — they contribute no
7551
+ * identity information.
7552
+ */
7553
+ function collectEnumValueSets(snapshot) {
7554
+ const result = /* @__PURE__ */ new Map();
7555
+ for (const sym of snapshot.symbols) {
7556
+ if (sym.kind !== "enum_member") continue;
7557
+ if (!sym.ownerFqName) continue;
7558
+ if (sym.value === void 0) continue;
7559
+ let set = result.get(sym.ownerFqName);
7560
+ if (!set) {
7561
+ set = /* @__PURE__ */ new Set();
7562
+ result.set(sym.ownerFqName, set);
7563
+ }
7564
+ set.add(String(sym.value));
7565
+ }
7566
+ return result;
7567
+ }
7568
+ /** Set equality for the small string sets used by rename detection. */
7569
+ function setEquals(a, b) {
7570
+ if (a.size !== b.size) return false;
7571
+ for (const v of a) if (!b.has(v)) return false;
7572
+ return true;
7573
+ }
7574
+ /**
7298
7575
  * Strip array/nullable suffixes so we compare bare type names. Languages
7299
7576
  * encode these differently (`Foo[]`, `Foo | null`, `Foo?`, `List<Foo>`,
7300
7577
  * `Optional[Foo]`); this is a best-effort common-case stripper, not an
@@ -7302,10 +7579,13 @@ function collectTypeFieldSets(snapshot) {
7302
7579
  */
7303
7580
  function bareTypeName(t) {
7304
7581
  let s = t.trim();
7582
+ s = s.replace(/^\*/, "");
7305
7583
  s = s.replace(/\s*\|\s*null$/, "").replace(/\?$/, "");
7306
7584
  s = s.replace(/\[\]$/, "");
7307
- const generic = s.match(/^[A-Za-z_][A-Za-z0-9_.]*<\s*([A-Za-z_][A-Za-z0-9_.]*)\s*>$/);
7308
- if (generic) s = generic[1];
7585
+ const angleGeneric = s.match(/^[A-Za-z_][A-Za-z0-9_.]*<\s*([A-Za-z_*][A-Za-z0-9_.*]*)\s*>$/);
7586
+ if (angleGeneric) return bareTypeName(angleGeneric[1]);
7587
+ const bracketGeneric = s.match(/^[A-Za-z_][A-Za-z0-9_.]*\[\s*([A-Za-z_*][A-Za-z0-9_.*]*)\s*\]$/);
7588
+ if (bracketGeneric) return bareTypeName(bracketGeneric[1]);
7309
7589
  return s;
7310
7590
  }
7311
7591
  /**