@workos/oagen 0.15.0 → 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.
@@ -628,6 +628,7 @@ function findNestedFieldSchema(fieldName, parentSchema, schemas) {
628
628
  function extractNestedSchema(name, schema) {
629
629
  if (schema.oneOf) {
630
630
  const models = [];
631
+ const constNamingDiscriminator = deriveConstNamingDiscriminator(schema.oneOf.filter((v) => !v.$ref && v.properties && (v.type === "object" || !v.type)));
631
632
  for (const variant of schema.oneOf) {
632
633
  if (variant.$ref) continue;
633
634
  if (variant.properties && (variant.type === "object" || !variant.type)) {
@@ -637,7 +638,7 @@ function extractNestedSchema(name, schema) {
637
638
  if (!fs) continue;
638
639
  fields.push(buildFieldFromSchema(fn, fs, name, requiredSet));
639
640
  }
640
- const variantName = models.length === 0 ? name : `${name}${models.length + 1}`;
641
+ const variantName = nameVariantModel(variant, name, models, constNamingDiscriminator);
641
642
  models.push({
642
643
  name: variantName,
643
644
  description: variant.description,
@@ -675,6 +676,69 @@ function extractNestedSchema(name, schema) {
675
676
  }
676
677
  return [];
677
678
  }
679
+ /**
680
+ * Derive a meaningful name for a oneOf object variant past the first,
681
+ * falling back to the numeric-suffix scheme (`Foo2`, `Foo3`) when no
682
+ * better signal is available. The "better signal" is a const-valued
683
+ * discriminator property that distinguishes one variant from the others.
684
+ *
685
+ * For a schema like `ApiKey.owner` whose oneOf has two variants — one
686
+ * with `type: { const: "organization" }` and one with `type: { const:
687
+ * "user" }` — this returns `ApiKeyOwner` for the first variant (the
688
+ * baseline name that the union TypeRef will reference, so consumer-
689
+ * visible typed fields stay backward-compatible) and `UserApiKeyOwner`
690
+ * for the second instead of `ApiKeyOwner2`. SDK consumers reading the
691
+ * generated types can tell the variants apart at a glance.
692
+ *
693
+ * The first variant always keeps the bare parent name because
694
+ * `schemaToTypeRef` builds union variants whose model refs collapse to
695
+ * `qualifyInlineModelName(field, parent)` — so the first model name
696
+ * must match for the union's degenerate-collapse to land on a valid
697
+ * type. Renaming variant 0 would orphan it.
698
+ *
699
+ * Falls back to numeric suffix when:
700
+ * - The variant has no discriminator property (no shared const
701
+ * property across all object variants), OR
702
+ * - The const value PascalCases to an empty/whitespace name, OR
703
+ * - The derived name collides with the parent name or an already-
704
+ * emitted variant name (numeric keeps uniqueness).
705
+ */
706
+ function nameVariantModel(variant, parentName, alreadyEmitted, discriminator) {
707
+ if (alreadyEmitted.length === 0) return parentName;
708
+ if (discriminator) {
709
+ const constValue = getConstPropertyValue(variant, discriminator.property);
710
+ if (constValue) {
711
+ const prefix = toPascalCase(constValue);
712
+ if (prefix) {
713
+ const candidate = parentName.startsWith(prefix) ? parentName : `${prefix}${parentName}`;
714
+ if (!(candidate === parentName || alreadyEmitted.some((m) => m.name === candidate))) return candidate;
715
+ }
716
+ }
717
+ }
718
+ return `${parentName}${alreadyEmitted.length + 1}`;
719
+ }
720
+ /**
721
+ * Returns a discriminator descriptor when every inline object variant in a
722
+ * oneOf has a string-const value on the same property — the signature of a
723
+ * discriminated union without an explicit `discriminator:` keyword. Used by
724
+ * `nameVariantModel` to derive variant names from the const values.
725
+ *
726
+ * Distinct from `detectConstPropertyDiscriminator` (which requires every
727
+ * variant to carry a `$ref` or `title` so a name mapping can be built):
728
+ * this pass operates on inline anonymous variants, where naming is exactly
729
+ * what we're trying to derive.
730
+ */
731
+ function deriveConstNamingDiscriminator(variants) {
732
+ if (variants.length < 2) return null;
733
+ const candidates = Object.keys(variants[0]?.properties ?? {});
734
+ for (const propName of candidates) {
735
+ const values = variants.map((v) => getConstPropertyValue(v, propName));
736
+ if (values.some((v) => v === null)) continue;
737
+ if (new Set(values).size !== values.length) continue;
738
+ return { property: propName };
739
+ }
740
+ return null;
741
+ }
678
742
  /** Build a single Field from a schema property entry. Shared across all extraction sites. */
679
743
  function buildFieldFromSchema(fieldName, fieldSchema, contextName, requiredSet) {
680
744
  return {