@workos/oagen-emitters 0.12.5 → 0.13.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.
@@ -2,6 +2,10 @@ import type { Operation, Service, EmitterContext } from '@workos/oagen';
2
2
  import { toPascalCase, toSnakeCase } from '@workos/oagen';
3
3
  import { buildResolvedLookup, lookupMethodName, getMountTarget } from '../shared/resolved-ops.js';
4
4
  import { stripUrnPrefix, applyAcronymFixes } from '../shared/naming-utils.js';
5
+ import {
6
+ buildExportedClassNameSet as buildExportedClassNameSetShared,
7
+ resolveServiceTarget as resolveServiceTargetShared,
8
+ } from '../shared/service-name-collision.js';
5
9
 
6
10
  /**
7
11
  * Ruby class names that collide with core classes. When a model name resolves
@@ -117,6 +121,32 @@ export function moduleName(name: string): string {
117
121
  return toSnakeCase(name);
118
122
  }
119
123
 
124
+ /**
125
+ * Build the set of model + enum Ruby class names that the SDK exposes under
126
+ * `WorkOS::`. Used to detect collisions with operation-client class names —
127
+ * a colliding service gets a `Service` suffix (`OrganizationMembershipService`)
128
+ * so it doesn't shadow the model class under Zeitwerk's collapsed namespace.
129
+ */
130
+ export function buildExportedClassNameSet(ctx: EmitterContext): Set<string> {
131
+ return buildExportedClassNameSetShared(ctx, className);
132
+ }
133
+
134
+ /**
135
+ * Resolve a service's mount-target identifier, appending `Service` on
136
+ * collision with an exported model/enum class name. The returned PascalCase
137
+ * value feeds `className`/`fileName` to derive matching class + file names
138
+ * (e.g. `OrganizationMembershipService` / `organization_membership_service`).
139
+ *
140
+ * Accessor names (`servicePropertyName`) intentionally use the RAW target —
141
+ * `client.organization_membership` is more readable than the suffixed form.
142
+ *
143
+ * The directory used by `loader.collapse` (the model home) likewise uses the
144
+ * raw target.
145
+ */
146
+ export function resolveServiceTarget(target: string, exportedClasses: Set<string>): string {
147
+ return resolveServiceTargetShared(target, exportedClasses, className);
148
+ }
149
+
120
150
  /**
121
151
  * PascalCase class name for a parameter-group variant. Mirrors the Python
122
152
  * convention: group "password" + variant "plaintext" → `PasswordPlaintext`.
package/src/ruby/rbi.ts CHANGED
@@ -7,6 +7,9 @@ import {
7
7
  safeParamName,
8
8
  scopedGroupVariantClassName,
9
9
  resolveMethodName,
10
+ servicePropertyName,
11
+ buildExportedClassNameSet,
12
+ resolveServiceTarget,
10
13
  } from './naming.js';
11
14
  import {
12
15
  buildResolvedLookup,
@@ -129,9 +132,11 @@ export function generateRbiFiles(spec: ApiSpec, ctx: EmitterContext): GeneratedF
129
132
  if (isListWrapperModel(m)) listWrapperModels.set(m.name, m);
130
133
  }
131
134
  const groupOwners = buildGroupOwnerMap(ctx);
135
+ const exportedClasses = buildExportedClassNameSet(ctx);
132
136
 
133
137
  for (const [mountTarget, group] of groups) {
134
- const cls = className(mountTarget);
138
+ const resolvedTarget = resolveServiceTarget(mountTarget, exportedClasses);
139
+ const cls = className(resolvedTarget);
135
140
  const lines: string[] = [];
136
141
  lines.push('# typed: strong');
137
142
  lines.push('');
@@ -143,6 +148,9 @@ export function generateRbiFiles(spec: ApiSpec, ctx: EmitterContext): GeneratedF
143
148
  // layout in lib/workos/<service>.rb.
144
149
  const variants = collectVariantsForMountTarget(ctx, spec.models as Model[], mountTarget);
145
150
  for (const v of variants) {
151
+ // Rewrite mountTarget to the (possibly pluralized) service class so the
152
+ // RBI's `WorkOS::<Service>::<Variant>` reference matches the runtime.
153
+ v.mountTarget = resolvedTarget;
146
154
  for (const line of emitInlineVariantRbi(v)) lines.push(line);
147
155
  lines.push('');
148
156
  }
@@ -183,7 +191,8 @@ export function generateRbiFiles(spec: ApiSpec, ctx: EmitterContext): GeneratedF
183
191
  if (!owner) {
184
192
  throw new Error(`No owner mount target found for parameter group '${group.name}'`);
185
193
  }
186
- const variants = group.variants.map((v) => scopedGroupVariantClassName(owner, group.name, v.name));
194
+ const resolvedOwner = resolveServiceTarget(owner, exportedClasses);
195
+ const variants = group.variants.map((v) => scopedGroupVariantClassName(resolvedOwner, group.name, v.name));
187
196
  if (variants.length === 1) return variants[0];
188
197
  return `T.any(${variants.join(', ')})`;
189
198
  };
@@ -267,7 +276,7 @@ export function generateRbiFiles(spec: ApiSpec, ctx: EmitterContext): GeneratedF
267
276
  lines.push('end');
268
277
 
269
278
  files.push({
270
- path: `rbi/workos/${fileName(mountTarget)}.rbi`,
279
+ path: `rbi/workos/${fileName(resolvedTarget)}.rbi`,
271
280
  content: lines.join('\n'),
272
281
  integrateTarget: true,
273
282
  overwriteExisting: true,
@@ -283,11 +292,9 @@ export function generateRbiFiles(spec: ApiSpec, ctx: EmitterContext): GeneratedF
283
292
  lines.push(' class Client < BaseClient');
284
293
 
285
294
  for (const [mountTarget] of groups) {
286
- const cls = className(mountTarget);
287
- const prop = mountTarget
288
- .replace(/-/g, '_')
289
- .replace(/[A-Z]/g, (ch) => `_${ch.toLowerCase()}`)
290
- .replace(/^_/, '');
295
+ const resolvedTarget = resolveServiceTarget(mountTarget, exportedClasses);
296
+ const cls = className(resolvedTarget);
297
+ const prop = servicePropertyName(mountTarget);
291
298
  lines.push(` sig { returns(WorkOS::${cls}) }`);
292
299
  lines.push(` def ${prop}; end`);
293
300
  lines.push('');
@@ -8,6 +8,8 @@ import {
8
8
  safeParamName,
9
9
  resolveMethodName,
10
10
  scopedGroupVariantClassName,
11
+ buildExportedClassNameSet,
12
+ resolveServiceTarget,
11
13
  } from './naming.js';
12
14
  import { mapTypeRefForYard } from './type-map.js';
13
15
  import {
@@ -48,10 +50,12 @@ export function generateResources(services: Service[], ctx: EmitterContext): Gen
48
50
  // dispatcher and YARD `@param` reference resolves variant classes through
49
51
  // this map so cross-resource references stay consistent.
50
52
  const groupOwners = buildGroupOwnerMap(ctx);
53
+ const exportedClasses = buildExportedClassNameSet(ctx);
51
54
 
52
55
  for (const [mountTarget, group] of groups) {
53
- const cls = className(mountTarget);
54
- const file = fileName(mountTarget);
56
+ const resolvedTarget = resolveServiceTarget(mountTarget, exportedClasses);
57
+ const cls = className(resolvedTarget);
58
+ const file = fileName(resolvedTarget);
55
59
 
56
60
  const operations = group.operations;
57
61
  if (operations.length === 0) continue;
@@ -103,6 +107,7 @@ export function generateResources(services: Service[], ctx: EmitterContext): Gen
103
107
  listWrapperModels,
104
108
  requires,
105
109
  groupOwners,
110
+ exportedClasses,
106
111
  });
107
112
  methodBodies.push(body);
108
113
 
@@ -182,6 +187,7 @@ function emitMethod(args: {
182
187
  listWrapperModels: Map<string, Model>;
183
188
  requires: Set<string>;
184
189
  groupOwners: Map<string, string>;
190
+ exportedClasses: Set<string>;
185
191
  }): string {
186
192
  const {
187
193
  op,
@@ -195,6 +201,7 @@ function emitMethod(args: {
195
201
  listWrapperModels,
196
202
  requires,
197
203
  groupOwners,
204
+ exportedClasses,
198
205
  } = args;
199
206
  void enumNames;
200
207
 
@@ -204,7 +211,7 @@ function emitMethod(args: {
204
211
  if (!owner) {
205
212
  throw new Error(`No owner mount target found for parameter group '${group.name}'`);
206
213
  }
207
- return scopedGroupVariantClassName(owner, group.name, variantName);
214
+ return scopedGroupVariantClassName(resolveServiceTarget(owner, exportedClasses), group.name, variantName);
208
215
  };
209
216
 
210
217
  const plan = planOperation(op);
package/src/ruby/tests.ts CHANGED
@@ -7,6 +7,8 @@ import {
7
7
  scopedGroupVariantClassName,
8
8
  servicePropertyName,
9
9
  resolveMethodName,
10
+ buildExportedClassNameSet,
11
+ resolveServiceTarget,
10
12
  } from './naming.js';
11
13
  import {
12
14
  buildResolvedLookup,
@@ -38,11 +40,13 @@ export function generateTests(spec: ApiSpec, ctx: EmitterContext): GeneratedFile
38
40
 
39
41
  const lookup = buildResolvedLookup(ctx);
40
42
  const groupOwners = buildGroupOwnerMap(ctx);
43
+ const exportedClasses = buildExportedClassNameSet(ctx);
41
44
 
42
45
  for (const [mountTarget, group] of groups) {
43
- const cls = className(mountTarget);
46
+ const resolvedTarget = resolveServiceTarget(mountTarget, exportedClasses);
47
+ const cls = className(resolvedTarget);
44
48
  const prop = servicePropertyName(mountTarget);
45
- const file = fileName(mountTarget);
49
+ const file = fileName(resolvedTarget);
46
50
 
47
51
  const lines: string[] = [];
48
52
  lines.push(`require 'test_helper'`);
@@ -85,7 +89,7 @@ export function generateTests(spec: ApiSpec, ctx: EmitterContext): GeneratedFile
85
89
 
86
90
  const resolved = lookupResolved(op, lookup);
87
91
  const hiddenParams = buildHiddenParams(resolved);
88
- const callArgs = buildCallArgsStub(op, modelByName, hiddenParams, groupOwners, models);
92
+ const callArgs = buildCallArgsStub(op, modelByName, hiddenParams, groupOwners, models, exportedClasses);
89
93
  const bodyMatcher = buildBodyMatcher(op, modelByName, hiddenParams, models);
90
94
 
91
95
  // Collect method info for the parameterized 401 test (T20).
@@ -118,7 +122,15 @@ export function generateTests(spec: ApiSpec, ctx: EmitterContext): GeneratedFile
118
122
  for (let vi = 1; vi < group.variants.length; vi++) {
119
123
  const variant = group.variants[vi];
120
124
  const overrides = new Map<string, number>([[group.name, vi]]);
121
- const variantCallArgs = buildCallArgsStub(op, modelByName, hiddenParams, groupOwners, models, overrides);
125
+ const variantCallArgs = buildCallArgsStub(
126
+ op,
127
+ modelByName,
128
+ hiddenParams,
129
+ groupOwners,
130
+ models,
131
+ exportedClasses,
132
+ overrides,
133
+ );
122
134
  const variantBodyMatcher = buildBodyMatcher(op, modelByName, hiddenParams, models, overrides);
123
135
  const suffix = `with_${fieldName(group.name)}_${fieldName(variant.name)}`;
124
136
  lines.push('');
@@ -338,6 +350,7 @@ function buildCallArgsStub(
338
350
  hiddenParams: Set<string>,
339
351
  groupOwners: Map<string, string>,
340
352
  models: Model[],
353
+ exportedClasses: Set<string>,
341
354
  variantOverrides: Map<string, number> = new Map(),
342
355
  ): string {
343
356
  const parts: string[] = [];
@@ -406,7 +419,11 @@ function buildCallArgsStub(
406
419
  if (!owner) {
407
420
  throw new Error(`No owner mount target found for parameter group '${group.name}'`);
408
421
  }
409
- const variantClass = scopedGroupVariantClassName(owner, group.name, variant.name);
422
+ const variantClass = scopedGroupVariantClassName(
423
+ resolveServiceTarget(owner, exportedClasses),
424
+ group.name,
425
+ variant.name,
426
+ );
410
427
  const fieldStubs = variant.parameters
411
428
  .map((p) => `${fieldName(p.name)}: ${stubValueFor(pickVariantParamType(p.type, bodyFieldTypes.get(p.name)))}`)
412
429
  .join(', ');
@@ -0,0 +1,56 @@
1
+ import type { EmitterContext, Model, Enum } from '@workos/oagen';
2
+ import { isListWrapperModel, isListMetadataModel } from './model-utils.js';
3
+
4
+ /**
5
+ * Suffix applied to an operation-client class name when it collides with an
6
+ * exported model/enum class name in the same SDK namespace. Standardized
7
+ * across emitters so colliding services look the same in every language —
8
+ * e.g. `OrganizationMembershipService` regardless of language.
9
+ *
10
+ * Languages whose operation clients already carry a unique suffix (Go's
11
+ * `…Service`, Rust's `…Api`, .NET's `…Service`) skip this helper entirely.
12
+ */
13
+ export const SERVICE_COLLISION_SUFFIX = 'Service';
14
+
15
+ /**
16
+ * Build the set of model + enum class names that the SDK exports under its
17
+ * top-level namespace. Each emitter passes its own `classNameFn` so the
18
+ * comparison happens on the language-specific class-name form (e.g. Ruby's
19
+ * `RoleList`, Python's `RoleList`).
20
+ *
21
+ * List-wrapper and list-metadata models are excluded — they aren't exposed
22
+ * as user-facing types.
23
+ */
24
+ export function buildExportedClassNameSet(ctx: EmitterContext, classNameFn: (name: string) => string): Set<string> {
25
+ const out = new Set<string>();
26
+ for (const model of ctx.spec.models as Model[]) {
27
+ if (isListWrapperModel(model) || isListMetadataModel(model)) continue;
28
+ out.add(classNameFn(model.name));
29
+ }
30
+ for (const enumDef of ctx.spec.enums as Enum[]) {
31
+ out.add(classNameFn(enumDef.name));
32
+ }
33
+ return out;
34
+ }
35
+
36
+ /**
37
+ * Resolve the PascalCase mount-target identifier for an operation client,
38
+ * appending `Service` when the un-suffixed class name would shadow an
39
+ * exported model or enum.
40
+ *
41
+ * Operates on the PascalCase target (the mount-target string the IR carries),
42
+ * so the returned value feeds cleanly into each language's `className` and
43
+ * `fileName` helpers — e.g. `OrganizationMembership` → `OrganizationMembershipService`,
44
+ * then `fileName` → `organization_membership_service` / `organization-membership-service`.
45
+ *
46
+ * The accessor on the client (`client.organization_membership`) is intentionally
47
+ * NOT suffixed — callers should keep using the raw target for `servicePropertyName`
48
+ * so the accessor reads naturally.
49
+ */
50
+ export function resolveServiceTarget(
51
+ target: string,
52
+ exportedClasses: Set<string>,
53
+ classNameFn: (name: string) => string,
54
+ ): string {
55
+ return exportedClasses.has(classNameFn(target)) ? `${target}${SERVICE_COLLISION_SUFFIX}` : target;
56
+ }