@workos/oagen 0.23.0 → 0.24.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.mjs CHANGED
@@ -2105,6 +2105,10 @@ function extractRequestBody(body, op, inlineModels) {
2105
2105
  return {};
2106
2106
  }
2107
2107
  const contextName = cleanSchemaName(op?.operationId ? toPascalCase(normalizeOperationIdForNaming(op.operationId)) + "Request" : "RequestBody");
2108
+ if (schema.oneOf && schema.discriminator) {
2109
+ const merged = flattenDiscriminatedUnionBody(schema);
2110
+ if (merged) schema = merged;
2111
+ }
2108
2112
  if (schema.properties && (schema.type === "object" || !schema.type)) {
2109
2113
  const requiredSet = new Set(schema.required ?? []);
2110
2114
  const fields = [];
@@ -2170,6 +2174,67 @@ function extractRequestBody(body, op, inlineModels) {
2170
2174
  encoding
2171
2175
  };
2172
2176
  }
2177
+ /**
2178
+ * Fold a discriminated-union request body into the equivalent single object so
2179
+ * its fields can be spread into method arguments instead of forcing callers to
2180
+ * construct an opaque union.
2181
+ *
2182
+ * The result is the union of every variant's properties: the discriminator is
2183
+ * collapsed to an enum of the pinned const values, and any field that isn't
2184
+ * present-and-required in *every* variant becomes optional (it varies by
2185
+ * branch, so it can't be unconditionally required). The discriminator itself is
2186
+ * always required. This mirrors what a spec author would write by hand to model
2187
+ * the same shape as a flat object.
2188
+ *
2189
+ * Returns null — leaving the union intact — unless the body is a clean
2190
+ * inline-object discriminated union: a variant that is a `$ref`, a non-object,
2191
+ * or that doesn't pin the discriminator to a string const can't be safely
2192
+ * merged. Gating on an *explicit* `discriminator` keeps undiscriminated `oneOf`
2193
+ * bodies untouched.
2194
+ */
2195
+ function flattenDiscriminatedUnionBody(schema) {
2196
+ const discProp = schema.discriminator?.propertyName;
2197
+ if (!discProp || !Array.isArray(schema.oneOf) || schema.oneOf.length === 0) return null;
2198
+ const variants = schema.oneOf;
2199
+ const discValues = [];
2200
+ let discPropSchema;
2201
+ for (const variant of variants) {
2202
+ if (variant.$ref || !variant.properties || !(variant.type === "object" || variant.type === void 0)) return null;
2203
+ const dp = variant.properties[discProp];
2204
+ const constVal = typeof dp?.const === "string" ? dp.const : Array.isArray(dp?.enum) && dp.enum.length === 1 && typeof dp.enum[0] === "string" ? dp.enum[0] : void 0;
2205
+ if (constVal === void 0) return null;
2206
+ if (!discValues.includes(constVal)) discValues.push(constVal);
2207
+ if (!discPropSchema) discPropSchema = dp;
2208
+ }
2209
+ const variantCount = variants.length;
2210
+ const mergedProps = {};
2211
+ const requiredCounts = {};
2212
+ const order = [discProp];
2213
+ for (const variant of variants) {
2214
+ const required = new Set(variant.required ?? []);
2215
+ for (const [name, propSchema] of Object.entries(variant.properties)) {
2216
+ if (name === discProp || !propSchema) continue;
2217
+ if (!(name in mergedProps)) {
2218
+ mergedProps[name] = propSchema;
2219
+ order.push(name);
2220
+ }
2221
+ if (required.has(name)) requiredCounts[name] = (requiredCounts[name] ?? 0) + 1;
2222
+ }
2223
+ }
2224
+ mergedProps[discProp] = {
2225
+ type: "string",
2226
+ enum: discValues,
2227
+ ...discPropSchema?.description ? { description: discPropSchema.description } : {}
2228
+ };
2229
+ const properties = {};
2230
+ for (const name of order) properties[name] = mergedProps[name];
2231
+ return {
2232
+ type: "object",
2233
+ properties,
2234
+ required: order.filter((name) => name === discProp || requiredCounts[name] === variantCount),
2235
+ ...schema.description ? { description: schema.description } : {}
2236
+ };
2237
+ }
2173
2238
  /** Derive a unique variant name for a oneOf inline model. */
2174
2239
  function deriveOneOfVariantName(variant, baseName, existingModels) {
2175
2240
  if (variant.properties) {