osury 0.19.0 → 0.21.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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "osury",
3
3
  "type": "module",
4
4
  "description": "Generate ReScript types with Sury schemas from OpenAPI specifications",
5
- "version": "0.19.0",
5
+ "version": "0.21.0",
6
6
  "license": "MIT",
7
7
  "bin": {
8
8
  "osury": "bin/osury.mjs"
@@ -66,6 +66,8 @@ let isPrimitivePlusDictUnion = CodegenTransforms.isPrimitivePlusDictUnion;
66
66
 
67
67
  let getUnionName = CodegenTransforms.getUnionName;
68
68
 
69
+ let getPolyVariantName = CodegenTransforms.getPolyVariantName;
70
+
69
71
  let extractUnions = CodegenTransforms.extractUnions;
70
72
 
71
73
  let extractUnionsFromType = CodegenTransforms.extractUnionsFromType;
@@ -115,6 +117,7 @@ export {
115
117
  isRefPlusDictUnion,
116
118
  isPrimitivePlusDictUnion,
117
119
  getUnionName,
120
+ getPolyVariantName,
118
121
  extractUnions,
119
122
  extractUnionsFromType,
120
123
  replaceUnions,
@@ -113,6 +113,10 @@ function getUnionName(types) {
113
113
  return first + rest.map(n => "Or" + CodegenHelpers.ucFirst(n)).join("");
114
114
  }
115
115
 
116
+ function getPolyVariantName(cases) {
117
+ return getUnionName(cases.map(c => c.payload));
118
+ }
119
+
116
120
  function extractUnionsFromType(_schema) {
117
121
  while (true) {
118
122
  let schema = _schema;
@@ -122,6 +126,12 @@ function extractUnionsFromType(_schema) {
122
126
  switch (schema._tag) {
123
127
  case "Object" :
124
128
  return schema._0.flatMap(field => extractUnionsFromType(field.type));
129
+ case "PolyVariant" :
130
+ let name = getPolyVariantName(schema._0);
131
+ return [{
132
+ name: name,
133
+ schema: schema
134
+ }];
125
135
  case "Optional" :
126
136
  case "Nullable" :
127
137
  case "Array" :
@@ -134,9 +144,9 @@ function extractUnionsFromType(_schema) {
134
144
  if (match !== undefined) {
135
145
  return [];
136
146
  }
137
- let name = getUnionName(types);
147
+ let name$1 = getUnionName(types);
138
148
  return [{
139
- name: name,
149
+ name: name$1,
140
150
  schema: schema
141
151
  }];
142
152
  default:
@@ -188,6 +198,11 @@ function replaceUnionInType(schema) {
188
198
  _tag: "Array",
189
199
  _0: replaceUnionInType(schema._0)
190
200
  };
201
+ case "PolyVariant" :
202
+ return {
203
+ _tag: "Ref",
204
+ _0: getPolyVariantName(schema._0)
205
+ };
191
206
  case "Dict" :
192
207
  return {
193
208
  _tag: "Dict",
@@ -499,6 +514,7 @@ export {
499
514
  isRefPlusDictUnion,
500
515
  isPrimitivePlusDictUnion,
501
516
  getUnionName,
517
+ getPolyVariantName,
502
518
  extractUnions,
503
519
  extractUnionsFromType,
504
520
  replaceUnions,
package/src/IRGen.res.mjs CHANGED
@@ -154,23 +154,16 @@ function convertToIrTypeDef(namedSchema, schemasDict, tagsDict, skipSchemaSet) {
154
154
  payload: payload
155
155
  };
156
156
  });
157
- let annotations = shouldSkipSchema ? [
158
- "GenType",
159
- {
160
- TAG: "Tag",
161
- _0: tagName
162
- }
163
- ] : [
157
+ return {
158
+ name: typeName,
159
+ annotations: [
164
160
  "GenType",
165
161
  {
166
162
  TAG: "Tag",
167
163
  _0: tagName
168
164
  },
169
165
  "Schema"
170
- ];
171
- return {
172
- name: typeName,
173
- annotations: annotations,
166
+ ],
174
167
  kind: {
175
168
  TAG: "VariantDef",
176
169
  _0: irCases
@@ -187,14 +180,9 @@ function convertToIrTypeDef(namedSchema, schemasDict, tagsDict, skipSchemaSet) {
187
180
  payload: payload
188
181
  };
189
182
  });
190
- let annotations$1 = shouldSkipSchema ? [
191
- "GenType",
192
- {
193
- TAG: "Tag",
194
- _0: tagName
195
- },
196
- "Unboxed"
197
- ] : [
183
+ return {
184
+ name: typeName,
185
+ annotations: [
198
186
  "GenType",
199
187
  {
200
188
  TAG: "Tag",
@@ -202,10 +190,7 @@ function convertToIrTypeDef(namedSchema, schemasDict, tagsDict, skipSchemaSet) {
202
190
  },
203
191
  "Unboxed",
204
192
  "Schema"
205
- ];
206
- return {
207
- name: typeName,
208
- annotations: annotations$1,
193
+ ],
209
194
  kind: {
210
195
  TAG: "VariantDef",
211
196
  _0: irCases$1
@@ -239,23 +224,16 @@ function convertToIrTypeDef(namedSchema, schemasDict, tagsDict, skipSchemaSet) {
239
224
  payload: payload$1
240
225
  };
241
226
  });
242
- let annotations$2 = shouldSkipSchema ? [
243
- "GenType",
244
- {
245
- TAG: "Tag",
246
- _0: tagName
247
- }
248
- ] : [
227
+ return {
228
+ name: typeName,
229
+ annotations: [
249
230
  "GenType",
250
231
  {
251
232
  TAG: "Tag",
252
233
  _0: tagName
253
234
  },
254
235
  "Schema"
255
- ];
256
- return {
257
- name: typeName,
258
- annotations: annotations$2,
236
+ ],
259
237
  kind: {
260
238
  TAG: "VariantDef",
261
239
  _0: irCases$2
@@ -272,13 +250,13 @@ function convertToIrTypeDef(namedSchema, schemasDict, tagsDict, skipSchemaSet) {
272
250
  TAG: "RecordDef",
273
251
  _0: fields._0.map(convertField)
274
252
  });
275
- let annotations$3 = shouldSkipSchema ? ["GenType"] : [
253
+ let annotations = shouldSkipSchema ? ["GenType"] : [
276
254
  "GenType",
277
255
  "Schema"
278
256
  ];
279
257
  return {
280
258
  name: typeName,
281
- annotations: annotations$3,
259
+ annotations: annotations,
282
260
  kind: kind
283
261
  };
284
262
  }
@@ -133,6 +133,45 @@ function parsePathResponses(pathsJson) {
133
133
  };
134
134
  }
135
135
 
136
+ function extractDiscriminatorFromPair(items, discDict) {
137
+ let match = discDict["propertyName"];
138
+ if (match === undefined) {
139
+ return;
140
+ }
141
+ if (typeof match !== "string") {
142
+ return;
143
+ }
144
+ let memberNames = Core__Array.filterMap(items, item => {
145
+ if (typeof item !== "object" || item === null || Array.isArray(item)) {
146
+ return;
147
+ }
148
+ let match = item["$ref"];
149
+ if (match === undefined) {
150
+ return;
151
+ }
152
+ if (typeof match !== "string") {
153
+ return;
154
+ }
155
+ let parts = match.split("/");
156
+ return parts[parts.length - 1 | 0];
157
+ });
158
+ if (memberNames.length < 2) {
159
+ return;
160
+ }
161
+ let lcNames = memberNames.map(n => {
162
+ let first = n.charAt(0).toLowerCase();
163
+ let rest = n.slice(1);
164
+ return first + rest;
165
+ });
166
+ let firstName = Core__Option.getOr(lcNames[0], "unknown");
167
+ let restNames = lcNames.slice(1);
168
+ let unionName = firstName + restNames.map(n => "Or" + ucFirst(n)).join("");
169
+ return [
170
+ unionName,
171
+ match
172
+ ];
173
+ }
174
+
136
175
  function extractFieldDiscriminators(schemaJson) {
137
176
  let result = {};
138
177
  if (typeof schemaJson === "object" && schemaJson !== null && !Array.isArray(schemaJson)) {
@@ -144,52 +183,71 @@ function extractFieldDiscriminators(schemaJson) {
144
183
  return;
145
184
  }
146
185
  let match = propJson["anyOf"];
147
- let match$1 = propJson["discriminator"];
148
- if (match === undefined) {
149
- return;
150
- }
151
- if (!Array.isArray(match)) {
152
- return;
153
- }
154
- if (match$1 === undefined) {
155
- return;
156
- }
157
- if (typeof match$1 !== "object" || match$1 === null || Array.isArray(match$1)) {
158
- return;
186
+ let directItems;
187
+ let exit = 0;
188
+ if (Array.isArray(match)) {
189
+ directItems = match;
190
+ } else {
191
+ exit = 1;
159
192
  }
160
- let match$2 = match$1["propertyName"];
161
- if (match$2 === undefined) {
162
- return;
193
+ if (exit === 1) {
194
+ let match$1 = propJson["oneOf"];
195
+ directItems = Array.isArray(match$1) ? match$1 : undefined;
163
196
  }
164
- if (typeof match$2 !== "string") {
165
- return;
197
+ let match$2 = propJson["discriminator"];
198
+ let exit$1 = 0;
199
+ if (directItems !== undefined && match$2 !== undefined) {
200
+ if (typeof match$2 === "object" && match$2 !== null && !Array.isArray(match$2)) {
201
+ let match$3 = extractDiscriminatorFromPair(directItems, match$2);
202
+ if (match$3 !== undefined) {
203
+ result[match$3[0]] = match$3[1];
204
+ return;
205
+ } else {
206
+ return;
207
+ }
208
+ }
209
+ exit$1 = 1;
210
+ } else {
211
+ exit$1 = 1;
166
212
  }
167
- let memberNames = Core__Array.filterMap(match, item => {
168
- if (typeof item !== "object" || item === null || Array.isArray(item)) {
213
+ if (exit$1 === 1) {
214
+ let match$4 = propJson["items"];
215
+ if (match$4 === undefined) {
216
+ return;
217
+ }
218
+ if (typeof match$4 !== "object" || match$4 === null || Array.isArray(match$4)) {
169
219
  return;
170
220
  }
171
- let match = item["$ref"];
172
- if (match === undefined) {
221
+ let match$5 = match$4["anyOf"];
222
+ let nestedItems;
223
+ let exit$2 = 0;
224
+ if (Array.isArray(match$5)) {
225
+ nestedItems = match$5;
226
+ } else {
227
+ exit$2 = 2;
228
+ }
229
+ if (exit$2 === 2) {
230
+ let match$6 = match$4["oneOf"];
231
+ nestedItems = Array.isArray(match$6) ? match$6 : undefined;
232
+ }
233
+ let match$7 = match$4["discriminator"];
234
+ if (nestedItems === undefined) {
173
235
  return;
174
236
  }
175
- if (typeof match !== "string") {
237
+ if (match$7 === undefined) {
238
+ return;
239
+ }
240
+ if (typeof match$7 !== "object" || match$7 === null || Array.isArray(match$7)) {
241
+ return;
242
+ }
243
+ let match$8 = extractDiscriminatorFromPair(nestedItems, match$7);
244
+ if (match$8 !== undefined) {
245
+ result[match$8[0]] = match$8[1];
246
+ return;
247
+ } else {
176
248
  return;
177
249
  }
178
- let parts = match.split("/");
179
- return parts[parts.length - 1 | 0];
180
- });
181
- if (memberNames.length < 2) {
182
- return;
183
250
  }
184
- let lcNames = memberNames.map(n => {
185
- let first = n.charAt(0).toLowerCase();
186
- let rest = n.slice(1);
187
- return first + rest;
188
- });
189
- let firstName = Core__Option.getOr(lcNames[0], "unknown");
190
- let restNames = lcNames.slice(1);
191
- let unionName = firstName + restNames.map(n => "Or" + ucFirst(n)).join("");
192
- result[unionName] = match$2;
193
251
  });
194
252
  }
195
253
  }
@@ -368,6 +426,7 @@ export {
368
426
  pathToName,
369
427
  ucFirst,
370
428
  parsePathResponses,
429
+ extractDiscriminatorFromPair,
371
430
  extractFieldDiscriminators,
372
431
  extractDiscriminatorPropertyName,
373
432
  extractDiscriminatorTag,