@workos/oagen-emitters 0.9.1 → 0.11.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.
@@ -709,6 +709,86 @@ describe('generateModels', () => {
709
709
  expect(barrel!.content).toContain('EventSchemaVariant');
710
710
  });
711
711
 
712
+ it('emits strict dispatch (no raw-dict fallback, no hasattr) for fields typed as a discriminated union', () => {
713
+ const service: Service = {
714
+ name: 'ApiKeys',
715
+ operations: [
716
+ {
717
+ name: 'getApiKey',
718
+ httpMethod: 'get',
719
+ path: '/api_keys/{id}',
720
+ pathParams: [{ name: 'id', type: { kind: 'primitive', type: 'string' }, required: true }],
721
+ queryParams: [],
722
+ headerParams: [],
723
+ response: { kind: 'model', name: 'ApiKeyCreatedData' },
724
+ errors: [],
725
+ injectIdempotencyKey: false,
726
+ },
727
+ ],
728
+ };
729
+
730
+ const models: Model[] = [
731
+ {
732
+ name: 'ApiKeyCreatedData',
733
+ fields: [
734
+ { name: 'id', type: { kind: 'primitive', type: 'string' }, required: true },
735
+ {
736
+ name: 'owner',
737
+ type: {
738
+ kind: 'union',
739
+ variants: [
740
+ { kind: 'model', name: 'ApiKeyCreatedDataOwner' },
741
+ { kind: 'model', name: 'UserApiKeyCreatedDataOwner' },
742
+ ],
743
+ discriminator: {
744
+ property: 'type',
745
+ mapping: {
746
+ organization: 'ApiKeyCreatedDataOwner',
747
+ user: 'UserApiKeyCreatedDataOwner',
748
+ },
749
+ },
750
+ },
751
+ required: true,
752
+ },
753
+ ],
754
+ },
755
+ {
756
+ name: 'ApiKeyCreatedDataOwner',
757
+ fields: [
758
+ { name: 'type', type: { kind: 'literal', value: 'organization' }, required: true },
759
+ { name: 'organization_id', type: { kind: 'primitive', type: 'string' }, required: true },
760
+ ],
761
+ },
762
+ {
763
+ name: 'UserApiKeyCreatedDataOwner',
764
+ fields: [
765
+ { name: 'type', type: { kind: 'literal', value: 'user' }, required: true },
766
+ { name: 'user_id', type: { kind: 'primitive', type: 'string' }, required: true },
767
+ ],
768
+ },
769
+ ];
770
+
771
+ const files = generateModels(models, {
772
+ ...ctx,
773
+ spec: { ...emptySpec, services: [service], models },
774
+ });
775
+
776
+ const parent = files.find((f) => f.path.endsWith('api_key_created_data.py'))!;
777
+ expect(parent).toBeDefined();
778
+
779
+ // from_dict performs strict dispatch and raises on unknown discriminator
780
+ expect(parent.content).toContain('"Unknown discriminator');
781
+ expect(parent.content).toContain('ApiKeyCreatedData.owner');
782
+ expect(parent.content).toContain('Expected one of {sorted(');
783
+ // Old lax fallback patterns are gone
784
+ expect(parent.content).not.toContain('else data["owner"]');
785
+ expect(parent.content).not.toContain('else data[');
786
+
787
+ // to_dict no longer needs the hasattr workaround
788
+ expect(parent.content).not.toContain('hasattr');
789
+ expect(parent.content).toContain('result["owner"] = self.owner.to_dict()');
790
+ });
791
+
712
792
  it('deduplicates models with recursively identical sub-model references', () => {
713
793
  const service: Service = {
714
794
  name: 'Events',
@@ -812,4 +892,149 @@ describe('generateModels', () => {
812
892
  expect(contextBFile.content).toContain('TypeAlias');
813
893
  expect(contextBFile.content).not.toContain('@dataclass');
814
894
  });
895
+
896
+ it('places a model in common/ when referenced by 2+ services', () => {
897
+ const sharedModel: Model = {
898
+ name: 'PageInfo',
899
+ fields: [
900
+ { name: 'page_number', type: { kind: 'primitive', type: 'string' }, required: true },
901
+ { name: 'page_size', type: { kind: 'primitive', type: 'string' }, required: true },
902
+ ],
903
+ };
904
+ const orgsService: Service = {
905
+ name: 'Authorization',
906
+ operations: [
907
+ {
908
+ name: 'listAuthz',
909
+ httpMethod: 'get',
910
+ path: '/authz',
911
+ pathParams: [],
912
+ queryParams: [],
913
+ headerParams: [],
914
+ response: { kind: 'model', name: 'PageInfo' },
915
+ errors: [],
916
+ injectIdempotencyKey: false,
917
+ },
918
+ ],
919
+ };
920
+ const usersService: Service = {
921
+ name: 'Organizations',
922
+ operations: [
923
+ {
924
+ name: 'listOrgs',
925
+ httpMethod: 'get',
926
+ path: '/orgs',
927
+ pathParams: [],
928
+ queryParams: [],
929
+ headerParams: [],
930
+ response: { kind: 'model', name: 'PageInfo' },
931
+ errors: [],
932
+ injectIdempotencyKey: false,
933
+ },
934
+ ],
935
+ };
936
+ const models: Model[] = [sharedModel];
937
+
938
+ const ctxWithServices: EmitterContext = {
939
+ ...ctx,
940
+ spec: { ...emptySpec, services: [orgsService, usersService], models },
941
+ };
942
+
943
+ const files = generateModels(models, ctxWithServices);
944
+ const sharedFile = files.find((f) => f.path.endsWith('/page_info.py'));
945
+ expect(sharedFile).toBeDefined();
946
+ expect(sharedFile!.path).toBe('src/workos/common/models/page_info.py');
947
+ // No service-local copy of the shared model.
948
+ expect(files.find((f) => f.path === 'src/workos/authorization/models/page_info.py')).toBeUndefined();
949
+ expect(files.find((f) => f.path === 'src/workos/organizations/models/page_info.py')).toBeUndefined();
950
+ // common/__init__.py and common/models/__init__.py both re-export PageInfo.
951
+ const commonInit = files.find((f) => f.path === 'src/workos/common/__init__.py');
952
+ expect(commonInit).toBeDefined();
953
+ expect(commonInit!.content).toContain('from .models import PageInfo as PageInfo');
954
+ const commonModelsInit = files.find((f) => f.path === 'src/workos/common/models/__init__.py');
955
+ expect(commonModelsInit).toBeDefined();
956
+ expect(commonModelsInit!.content).toContain('from .page_info import PageInfo as PageInfo');
957
+ });
958
+
959
+ it('keeps a model in service dir when only one service references it', () => {
960
+ const onlyModel: Model = {
961
+ name: 'OnlyOrg',
962
+ fields: [{ name: 'id', type: { kind: 'primitive', type: 'string' }, required: true }],
963
+ };
964
+ const service: Service = {
965
+ name: 'Organizations',
966
+ operations: [
967
+ {
968
+ name: 'getOrg',
969
+ httpMethod: 'get',
970
+ path: '/orgs/{id}',
971
+ pathParams: [{ name: 'id', type: { kind: 'primitive', type: 'string' }, required: true }],
972
+ queryParams: [],
973
+ headerParams: [],
974
+ response: { kind: 'model', name: 'OnlyOrg' },
975
+ errors: [],
976
+ injectIdempotencyKey: false,
977
+ },
978
+ ],
979
+ };
980
+
981
+ const ctxWithServices: EmitterContext = {
982
+ ...ctx,
983
+ spec: { ...emptySpec, services: [service], models: [onlyModel] },
984
+ };
985
+
986
+ const files = generateModels([onlyModel], ctxWithServices);
987
+ const file = files.find((f) => f.path.endsWith('/only_org.py'));
988
+ expect(file!.path).toBe('src/workos/organizations/models/only_org.py');
989
+ });
990
+
991
+ it('respects modelHints over the shared rule', () => {
992
+ const sharedModel: Model = {
993
+ name: 'PinnedThing',
994
+ fields: [{ name: 'id', type: { kind: 'primitive', type: 'string' }, required: true }],
995
+ };
996
+ const a: Service = {
997
+ name: 'Authorization',
998
+ operations: [
999
+ {
1000
+ name: 'a',
1001
+ httpMethod: 'get',
1002
+ path: '/a',
1003
+ pathParams: [],
1004
+ queryParams: [],
1005
+ headerParams: [],
1006
+ response: { kind: 'model', name: 'PinnedThing' },
1007
+ errors: [],
1008
+ injectIdempotencyKey: false,
1009
+ },
1010
+ ],
1011
+ };
1012
+ const b: Service = {
1013
+ name: 'Organizations',
1014
+ operations: [
1015
+ {
1016
+ name: 'b',
1017
+ httpMethod: 'get',
1018
+ path: '/b',
1019
+ pathParams: [],
1020
+ queryParams: [],
1021
+ headerParams: [],
1022
+ response: { kind: 'model', name: 'PinnedThing' },
1023
+ errors: [],
1024
+ injectIdempotencyKey: false,
1025
+ },
1026
+ ],
1027
+ };
1028
+
1029
+ // PinnedThing is referenced by 2 services but explicitly pinned to Authorization.
1030
+ const ctxWithServices: EmitterContext = {
1031
+ ...ctx,
1032
+ spec: { ...emptySpec, services: [a, b], models: [sharedModel] },
1033
+ modelHints: { PinnedThing: 'Authorization' },
1034
+ };
1035
+
1036
+ const files = generateModels([sharedModel], ctxWithServices);
1037
+ expect(files.find((f) => f.path === 'src/workos/authorization/models/pinned_thing.py')).toBeDefined();
1038
+ expect(files.find((f) => f.path === 'src/workos/common/models/pinned_thing.py')).toBeUndefined();
1039
+ });
815
1040
  });
@@ -167,6 +167,51 @@ describe('generateResources', () => {
167
167
  'after: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.',
168
168
  );
169
169
  expect(content).toContain('order: Order the results by the creation time.');
170
+ // The spec has no `default` for `order` here, so the SDK must NOT
171
+ // hardcode 'desc' on the client. Server's default applies instead.
172
+ expect(content).toContain('order: Optional[str] = None,');
173
+ expect(content).not.toContain('order: Optional[str] = "desc"');
174
+ });
175
+
176
+ it('reads pagination order default from the spec rather than hardcoding "desc"', () => {
177
+ const models: Model[] = [
178
+ { name: 'Organization', fields: [{ name: 'id', type: { kind: 'primitive', type: 'string' }, required: true }] },
179
+ ];
180
+ const services: Service[] = [
181
+ {
182
+ name: 'Organizations',
183
+ operations: [
184
+ {
185
+ name: 'listOrganizations',
186
+ httpMethod: 'get',
187
+ path: '/organizations',
188
+ pathParams: [],
189
+ queryParams: [
190
+ { name: 'limit', type: { kind: 'primitive', type: 'integer' }, required: false },
191
+ { name: 'after', type: { kind: 'primitive', type: 'string' }, required: false },
192
+ {
193
+ name: 'order',
194
+ type: { kind: 'primitive', type: 'string' },
195
+ required: false,
196
+ default: 'desc',
197
+ },
198
+ ],
199
+ headerParams: [],
200
+ response: { kind: 'model', name: 'OrganizationList' },
201
+ errors: [],
202
+ injectIdempotencyKey: false,
203
+ pagination: {
204
+ strategy: 'cursor',
205
+ param: 'after',
206
+ dataPath: 'data',
207
+ itemType: { kind: 'model', name: 'Organization' },
208
+ },
209
+ },
210
+ ],
211
+ },
212
+ ];
213
+ const content = generateResources(services, { ...ctx, spec: { ...emptySpec, services, models } })[0].content;
214
+ expect(content).toContain('order: Optional[str] = "desc",');
170
215
  });
171
216
 
172
217
  it('indents multiline argument descriptions in docstrings', () => {
@@ -187,6 +187,64 @@ describe('ruby/resources', () => {
187
187
  expect(content).toContain('ListStruct.from_response(');
188
188
  });
189
189
 
190
+ it('reads pagination order default from the spec rather than hardcoding "desc"', () => {
191
+ const services: Service[] = [
192
+ {
193
+ name: 'Organizations',
194
+ operations: [
195
+ makeOp({
196
+ name: 'listOrganizations',
197
+ queryParams: [
198
+ { name: 'after', type: { kind: 'primitive', type: 'string' }, required: false },
199
+ { name: 'limit', type: { kind: 'primitive', type: 'integer' }, required: false },
200
+ {
201
+ name: 'order',
202
+ type: { kind: 'primitive', type: 'string' },
203
+ required: false,
204
+ default: 'desc',
205
+ },
206
+ ],
207
+ pagination: {
208
+ strategy: 'cursor',
209
+ param: 'after',
210
+ dataPath: 'data',
211
+ itemType: { kind: 'model', name: 'Organization' },
212
+ },
213
+ }),
214
+ ],
215
+ },
216
+ ];
217
+ const content = generateResources(services, makeCtx(makeSpec(services)))[0].content;
218
+ expect(content).toContain("order: 'desc'");
219
+ });
220
+
221
+ it("emits order: nil when the spec carries no default (no 'desc' hardcode)", () => {
222
+ const services: Service[] = [
223
+ {
224
+ name: 'Organizations',
225
+ operations: [
226
+ makeOp({
227
+ name: 'listOrganizations',
228
+ queryParams: [
229
+ { name: 'after', type: { kind: 'primitive', type: 'string' }, required: false },
230
+ { name: 'limit', type: { kind: 'primitive', type: 'integer' }, required: false },
231
+ { name: 'order', type: { kind: 'primitive', type: 'string' }, required: false },
232
+ ],
233
+ pagination: {
234
+ strategy: 'cursor',
235
+ param: 'after',
236
+ dataPath: 'data',
237
+ itemType: { kind: 'model', name: 'Organization' },
238
+ },
239
+ }),
240
+ ],
241
+ },
242
+ ];
243
+ const content = generateResources(services, makeCtx(makeSpec(services)))[0].content;
244
+ expect(content).toContain('order: nil');
245
+ expect(content).not.toContain("order: 'desc'");
246
+ });
247
+
190
248
  // ── P0-3: paginated response shape detection ──────────────────────────
191
249
 
192
250
  it('generates ListStruct for paginated endpoints with array response type', () => {