@workos/oagen 0.14.1 → 0.16.0

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.
package/dist/smoke.mjs CHANGED
@@ -577,6 +577,7 @@ function findNestedFieldSchema(fieldName, parentSchema, schemas) {
577
577
  function extractNestedSchema(name, schema) {
578
578
  if (schema.oneOf) {
579
579
  const models = [];
580
+ const constNamingDiscriminator = deriveConstNamingDiscriminator(schema.oneOf.filter((v) => !v.$ref && v.properties && (v.type === "object" || !v.type)));
580
581
  for (const variant of schema.oneOf) {
581
582
  if (variant.$ref) continue;
582
583
  if (variant.properties && (variant.type === "object" || !variant.type)) {
@@ -586,7 +587,7 @@ function extractNestedSchema(name, schema) {
586
587
  if (!fs) continue;
587
588
  fields.push(buildFieldFromSchema(fn, fs, name, requiredSet));
588
589
  }
589
- const variantName = models.length === 0 ? name : `${name}${models.length + 1}`;
590
+ const variantName = nameVariantModel(variant, name, models, constNamingDiscriminator);
590
591
  models.push({
591
592
  name: variantName,
592
593
  description: variant.description,
@@ -624,6 +625,69 @@ function extractNestedSchema(name, schema) {
624
625
  }
625
626
  return [];
626
627
  }
628
+ /**
629
+ * Derive a meaningful name for a oneOf object variant past the first,
630
+ * falling back to the numeric-suffix scheme (`Foo2`, `Foo3`) when no
631
+ * better signal is available. The "better signal" is a const-valued
632
+ * discriminator property that distinguishes one variant from the others.
633
+ *
634
+ * For a schema like `ApiKey.owner` whose oneOf has two variants — one
635
+ * with `type: { const: "organization" }` and one with `type: { const:
636
+ * "user" }` — this returns `ApiKeyOwner` for the first variant (the
637
+ * baseline name that the union TypeRef will reference, so consumer-
638
+ * visible typed fields stay backward-compatible) and `UserApiKeyOwner`
639
+ * for the second instead of `ApiKeyOwner2`. SDK consumers reading the
640
+ * generated types can tell the variants apart at a glance.
641
+ *
642
+ * The first variant always keeps the bare parent name because
643
+ * `schemaToTypeRef` builds union variants whose model refs collapse to
644
+ * `qualifyInlineModelName(field, parent)` — so the first model name
645
+ * must match for the union's degenerate-collapse to land on a valid
646
+ * type. Renaming variant 0 would orphan it.
647
+ *
648
+ * Falls back to numeric suffix when:
649
+ * - The variant has no discriminator property (no shared const
650
+ * property across all object variants), OR
651
+ * - The const value PascalCases to an empty/whitespace name, OR
652
+ * - The derived name collides with the parent name or an already-
653
+ * emitted variant name (numeric keeps uniqueness).
654
+ */
655
+ function nameVariantModel(variant, parentName, alreadyEmitted, discriminator) {
656
+ if (alreadyEmitted.length === 0) return parentName;
657
+ if (discriminator) {
658
+ const constValue = getConstPropertyValue(variant, discriminator.property);
659
+ if (constValue) {
660
+ const prefix = toPascalCase(constValue);
661
+ if (prefix) {
662
+ const candidate = parentName.startsWith(prefix) ? parentName : `${prefix}${parentName}`;
663
+ if (!(candidate === parentName || alreadyEmitted.some((m) => m.name === candidate))) return candidate;
664
+ }
665
+ }
666
+ }
667
+ return `${parentName}${alreadyEmitted.length + 1}`;
668
+ }
669
+ /**
670
+ * Returns a discriminator descriptor when every inline object variant in a
671
+ * oneOf has a string-const value on the same property — the signature of a
672
+ * discriminated union without an explicit `discriminator:` keyword. Used by
673
+ * `nameVariantModel` to derive variant names from the const values.
674
+ *
675
+ * Distinct from `detectConstPropertyDiscriminator` (which requires every
676
+ * variant to carry a `$ref` or `title` so a name mapping can be built):
677
+ * this pass operates on inline anonymous variants, where naming is exactly
678
+ * what we're trying to derive.
679
+ */
680
+ function deriveConstNamingDiscriminator(variants) {
681
+ if (variants.length < 2) return null;
682
+ const candidates = Object.keys(variants[0]?.properties ?? {});
683
+ for (const propName of candidates) {
684
+ const values = variants.map((v) => getConstPropertyValue(v, propName));
685
+ if (values.some((v) => v === null)) continue;
686
+ if (new Set(values).size !== values.length) continue;
687
+ return { property: propName };
688
+ }
689
+ return null;
690
+ }
627
691
  /** Build a single Field from a schema property entry. Shared across all extraction sites. */
628
692
  function buildFieldFromSchema(fieldName, fieldSchema, contextName, requiredSet) {
629
693
  return {