@readme/oas-to-har 32.1.14 → 33.0.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/dist/index.cjs CHANGED
@@ -44,11 +44,12 @@ function hasSchemaType(schema, discriminator) {
44
44
  }
45
45
  return schema.type === discriminator;
46
46
  }
47
- function getSafeRequestBody(obj) {
48
- if ("oneOf" in obj) {
49
- return getSafeRequestBody(obj.oneOf[0]);
50
- } else if ("anyOf" in obj) {
51
- return getSafeRequestBody(obj.anyOf[0]);
47
+ function unwrapFirstPolymorphicBranch(obj) {
48
+ if (obj.oneOf && Array.isArray(obj.oneOf) && obj.oneOf.length) {
49
+ return unwrapFirstPolymorphicBranch(obj.oneOf[0]);
50
+ }
51
+ if (obj.anyOf && Array.isArray(obj.anyOf) && obj.anyOf.length) {
52
+ return unwrapFirstPolymorphicBranch(obj.anyOf[0]);
52
53
  }
53
54
  return obj;
54
55
  }
@@ -116,7 +117,7 @@ function getSubschemas(schema, api, opts, seenRefs = /* @__PURE__ */ new Set())
116
117
  if (!("properties" in resolvedSchema) && typeof resolvedSchema === "object" && !("type" in resolvedSchema && resolvedSchema.type !== "object")) {
117
118
  for (const [propName, propSchema] of Object.entries(resolvedSchema)) {
118
119
  if (propSchema && (Array.isArray(propSchema) || typeof propSchema === "object" && propSchema !== null)) {
119
- const raw = getSafeRequestBody(propSchema);
120
+ const raw = unwrapFirstPolymorphicBranch(propSchema);
120
121
  const resolved = _types.isRef.call(void 0, raw) ? _utils.dereferenceRef.call(void 0, raw, api) : raw;
121
122
  const toPush = resolved && !_types.isRef.call(void 0, resolved) ? resolved : raw;
122
123
  if (toPush && typeof toPush === "object") {
@@ -290,7 +291,7 @@ function collectSchemaObjectProperties(schema, api, seenRefs) {
290
291
  }
291
292
  node = deref;
292
293
  }
293
- const safe = getSafeRequestBody(node);
294
+ const safe = unwrapFirstPolymorphicBranch(node);
294
295
  if (_types.isRef.call(void 0, safe)) {
295
296
  return collectSchemaObjectProperties(safe, api, seenRefs);
296
297
  }
@@ -305,6 +306,54 @@ function collectSchemaObjectProperties(schema, api, seenRefs) {
305
306
  }
306
307
  return void 0;
307
308
  }
309
+ function getPayloadPropertyKeys(payload) {
310
+ if (typeof payload !== "object" || payload === null || Array.isArray(payload)) {
311
+ return [];
312
+ }
313
+ return Object.keys(payload).filter(
314
+ (k) => typeof payload[k] !== "undefined"
315
+ );
316
+ }
317
+ function getPolymorphicSchema(node) {
318
+ return node.oneOf && Array.isArray(node.oneOf) && node.oneOf.length ? node.oneOf : node.anyOf && Array.isArray(node.anyOf) && node.anyOf.length ? node.anyOf : null;
319
+ }
320
+ function pickPolymorphicBranch(alternatives, keys, api) {
321
+ if (!keys.length) {
322
+ return alternatives[0];
323
+ }
324
+ const scored = alternatives.map((branch, idx) => {
325
+ const props = collectSchemaObjectProperties(branch, api, /* @__PURE__ */ new Set());
326
+ const allMatch = Boolean(props && keys.every((k) => Object.prototype.hasOwnProperty.call(props, k)));
327
+ const propCount = props ? Object.keys(props).length : Number.POSITIVE_INFINITY;
328
+ return { idx, branch, allMatch, propCount };
329
+ });
330
+ const matches = scored.filter((entry) => entry.allMatch);
331
+ if (matches.length === 1) return matches[0].branch;
332
+ if (matches.length > 1) {
333
+ matches.sort((a, b) => a.propCount - b.propCount || a.idx - b.idx);
334
+ return matches[0].branch;
335
+ }
336
+ return scored[0].branch;
337
+ }
338
+ function getSafeRequestBody(schema, payload, api) {
339
+ let resolved = _utils.dereferenceRefDeep.call(void 0, schema, api);
340
+ const keys = getPayloadPropertyKeys(payload);
341
+ for (let alternatives = getPolymorphicSchema(resolved); alternatives != null; alternatives = getPolymorphicSchema(resolved)) {
342
+ resolved = pickPolymorphicBranch(alternatives, keys, api);
343
+ }
344
+ if ("allOf" in resolved && Array.isArray(resolved.allOf) && resolved.allOf.length) {
345
+ const fromAllOf = mergePropertiesFromAllOf(resolved.allOf, api, /* @__PURE__ */ new Set());
346
+ if (fromAllOf && Object.keys(fromAllOf).length) {
347
+ const existing = resolved.properties && typeof resolved.properties === "object" && resolved.properties !== null ? resolved.properties : {};
348
+ resolved = {
349
+ ...resolved,
350
+ type: "object",
351
+ properties: { ...fromAllOf, ...existing }
352
+ };
353
+ }
354
+ }
355
+ return resolved;
356
+ }
308
357
  function parseJSONStringsInBodyWithSchema(obj, schema, api, seenRefs = /* @__PURE__ */ new Set()) {
309
358
  if (schema === void 0) return parseJSONStrings(obj);
310
359
  let resolved = schema;
@@ -319,7 +368,7 @@ function parseJSONStringsInBodyWithSchema(obj, schema, api, seenRefs = /* @__PUR
319
368
  }
320
369
  resolved = deref;
321
370
  }
322
- const safe = getSafeRequestBody(resolved);
371
+ const safe = getSafeRequestBody(resolved, obj, api);
323
372
  if (_types.isRef.call(void 0, safe)) {
324
373
  return parseJSONStringsInBodyWithSchema(obj, safe, api, seenRefs);
325
374
  }
@@ -1062,8 +1111,11 @@ function oasToHar(oas, operationSchema, values = {}, auth = {}, opts = { proxyUr
1062
1111
  let cleanBody = _removeundefinedobjects2.default.call(void 0, formData.body, { preserveNullishArrays: true });
1063
1112
  if (isMultipart) {
1064
1113
  har.postData = { params: [], mimeType: "multipart/form-data" };
1065
- const safeBodySchema = getSafeRequestBody(requestBodySchema);
1066
- const binaryTypes = Object.keys(safeBodySchema.properties).filter((key) => {
1114
+ const safeBodySchema = getSafeRequestBody(requestBodySchema, formData.body, operation.api);
1115
+ const binaryTypes = Object.keys(_nullishCoalesce(safeBodySchema.properties, () => ( {}))).filter((key) => {
1116
+ if (!_optionalChain([safeBodySchema, 'access', _29 => _29.properties, 'optionalAccess', _30 => _30[key]]) || typeof safeBodySchema.properties[key] !== "object" || safeBodySchema.properties[key] === null) {
1117
+ return false;
1118
+ }
1067
1119
  const propData = safeBodySchema.properties[key];
1068
1120
  if (propData.format === "binary") {
1069
1121
  return true;
@@ -1075,12 +1127,12 @@ function oasToHar(oas, operationSchema, values = {}, auth = {}, opts = { proxyUr
1075
1127
  if (cleanBody !== void 0) {
1076
1128
  let multipartParams = [];
1077
1129
  const multipartContent = operation.getRequestBody("multipart/form-data");
1078
- if (typeof multipartContent === "object" && multipartContent !== null && // `getRequestBody()` will return an array if there are multiple content types that
1079
- // match the one we're looking for but because we're looking for an exact
1080
- // `multipart/form-data` match `getRequestBody()` will only ever return a single
1081
- // object back for us.
1082
- !Array.isArray(multipartContent)) {
1083
- multipartParams = multipartBodyToFormatterParams(formData.body, multipartContent, safeBodySchema);
1130
+ if (multipartContent) {
1131
+ multipartParams = multipartBodyToFormatterParams(
1132
+ formData.body,
1133
+ multipartContent.mediaTypeObject,
1134
+ safeBodySchema
1135
+ );
1084
1136
  }
1085
1137
  if (multipartParams.length) {
1086
1138
  Object.keys(cleanBody).forEach((name) => {
@@ -1101,7 +1153,7 @@ function oasToHar(oas, operationSchema, values = {}, auth = {}, opts = { proxyUr
1101
1153
  }
1102
1154
  }
1103
1155
  }
1104
- appendHarValue(_optionalChain([har, 'access', _29 => _29.postData, 'optionalAccess', _30 => _30.params]) || [], name, val, addtlData);
1156
+ appendHarValue(_optionalChain([har, 'access', _31 => _31.postData, 'optionalAccess', _32 => _32.params]) || [], name, val, addtlData);
1105
1157
  });
1106
1158
  }
1107
1159
  });
@@ -1133,7 +1185,7 @@ function oasToHar(oas, operationSchema, values = {}, auth = {}, opts = { proxyUr
1133
1185
  } else {
1134
1186
  try {
1135
1187
  const parsed = parseJSONStringsInBodyWithSchema(cleanBody, requestBodySchema, operation.api);
1136
- if (typeof _optionalChain([parsed, 'optionalAccess', _31 => _31.RAW_BODY]) !== "undefined") {
1188
+ if (typeof _optionalChain([parsed, 'optionalAccess', _33 => _33.RAW_BODY]) !== "undefined") {
1137
1189
  har.postData.text = isPrimitive(parsed.RAW_BODY) ? String(parsed.RAW_BODY) : stringify(parsed.RAW_BODY);
1138
1190
  } else {
1139
1191
  har.postData.text = JSON.stringify(parsed);
@@ -1152,14 +1204,14 @@ function oasToHar(oas, operationSchema, values = {}, auth = {}, opts = { proxyUr
1152
1204
  }
1153
1205
  }
1154
1206
  }
1155
- if ((_optionalChain([har, 'access', _32 => _32.postData, 'optionalAccess', _33 => _33.text]) || _optionalChain([requestBody, 'optionalAccess', _34 => _34.schema]) && Object.keys(requestBody.schema).length) && !hasContentType) {
1207
+ if ((_optionalChain([har, 'access', _34 => _34.postData, 'optionalAccess', _35 => _35.text]) || _optionalChain([requestBody, 'optionalAccess', _36 => _36.schema]) && Object.keys(requestBody.schema).length) && !hasContentType) {
1156
1208
  har.headers.push({
1157
1209
  name: "content-type",
1158
1210
  value: contentType
1159
1211
  });
1160
1212
  }
1161
1213
  const securityRequirements = operation.getSecurity();
1162
- if (_optionalChain([securityRequirements, 'optionalAccess', _35 => _35.length])) {
1214
+ if (_optionalChain([securityRequirements, 'optionalAccess', _37 => _37.length])) {
1163
1215
  securityRequirements.forEach((schemes) => {
1164
1216
  Object.keys(schemes).forEach((security) => {
1165
1217
  const securityValue = _chunkZYPAVWY2cjs.configureSecurity.call(void 0, apiDefinition, auth, security);