osury 0.20.1 → 0.22.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.20.1",
5
+ "version": "0.22.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
@@ -139,15 +139,22 @@ function convertToIrTypeDef(namedSchema, schemasDict, tagsDict, skipSchemaSet) {
139
139
  } else {
140
140
  let refName$1 = refName._0;
141
141
  let other = schemasDict[refName$1];
142
- payload = other !== undefined ? (
143
- typeof other !== "object" || other._tag !== "Object" ? convertType(other) : ({
144
- TAG: "InlineRecord",
145
- _0: other._0.map(convertField)
146
- })
147
- ) : ({
142
+ if (other !== undefined) {
143
+ if (typeof other !== "object" || other._tag !== "Object") {
144
+ payload = convertType(other);
145
+ } else {
146
+ let filtered = other._0.filter(f => f.name !== tagName);
147
+ payload = {
148
+ TAG: "InlineRecord",
149
+ _0: filtered.map(convertField)
150
+ };
151
+ }
152
+ } else {
153
+ payload = {
148
154
  TAG: "Named",
149
155
  _0: CodegenHelpers.lcFirst(refName$1)
150
- });
156
+ };
157
+ }
151
158
  }
152
159
  return {
153
160
  tag: CodegenHelpers.ucFirst(c._tag),
@@ -203,15 +210,23 @@ function convertToIrTypeDef(namedSchema, schemasDict, tagsDict, skipSchemaSet) {
203
210
  let tagValue = tagsDict[name];
204
211
  let tag = tagValue !== undefined ? CodegenHelpers.ucFirst(tagValue) : CodegenHelpers.ucFirst(name);
205
212
  let other = schemasDict[name];
206
- let payload = other !== undefined ? (
207
- typeof other !== "object" || other._tag !== "Object" ? convertType(other) : ({
208
- TAG: "InlineRecord",
209
- _0: other._0.map(convertField)
210
- })
211
- ) : ({
213
+ let payload;
214
+ if (other !== undefined) {
215
+ if (typeof other !== "object" || other._tag !== "Object") {
216
+ payload = convertType(other);
217
+ } else {
218
+ let filtered = other._0.filter(f => f.name !== tagName);
219
+ payload = {
220
+ TAG: "InlineRecord",
221
+ _0: filtered.map(convertField)
222
+ };
223
+ }
224
+ } else {
225
+ payload = {
212
226
  TAG: "Named",
213
227
  _0: CodegenHelpers.lcFirst(name)
214
- });
228
+ };
229
+ }
215
230
  return {
216
231
  tag: tag,
217
232
  payload: payload
@@ -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,