@kubb/adapter-oas 5.24.0 → 5.24.1

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
@@ -16,7 +16,7 @@ var compatibility = {
16
16
  "node": ">=22.22"
17
17
  };
18
18
  // package.json
19
- var version = "5.24.0";
19
+ var version = "5.24.1";
20
20
  // src/constants.ts
21
21
  /**
22
22
  * Default parser options applied when no explicit options are provided.
@@ -391,18 +391,32 @@ function isPathInside(path, parent) {
391
391
  return rel !== "" && !rel.startsWith("..") && !(0, node_path.isAbsolute)(rel);
392
392
  }
393
393
  // src/oas.ts
394
+ const legacyDocuments = /* @__PURE__ */ new WeakSet();
395
+ /**
396
+ * Marks a document upgraded from Swagger 2.0 or OpenAPI 3.0, so its enums keep the loose `nullable` reading.
397
+ */
398
+ function markLegacyDocument(document) {
399
+ legacyDocuments.add(document);
400
+ }
401
+ /**
402
+ * Returns `true` for a document upgraded from Swagger 2.0 or OpenAPI 3.0.
403
+ */
404
+ function isLegacyDocument(document) {
405
+ return !!document && legacyDocuments.has(document);
406
+ }
394
407
  /**
395
408
  * Returns `true` when a schema should be treated as nullable.
396
409
  *
397
410
  * Recognizes all nullable signals across OAS versions: `nullable: true` (OAS 3.0),
398
411
  * `x-nullable: true` (vendor extension), `type: 'null'`, and `type: ['null', ...]` (OAS 3.1).
412
+ * A sibling `enum` without `null` makes the schema non-nullable, except in documents upgraded from 3.0.
399
413
  */
400
- function isNullable(schema) {
414
+ function isNullable(schema, document) {
401
415
  if ((schema?.nullable ?? schema?.["x-nullable"]) === true) return true;
402
416
  const schemaType = schema?.type;
403
- if (schemaType === "null") return true;
404
- if (Array.isArray(schemaType)) return schemaType.includes("null");
405
- return false;
417
+ const typeAllowsNull = schemaType === "null" || Array.isArray(schemaType) && schemaType.includes("null");
418
+ if (typeAllowsNull && Array.isArray(schema?.enum) && !schema.enum.includes(null)) return isLegacyDocument(document);
419
+ return typeAllowsNull;
406
420
  }
407
421
  /**
408
422
  * Returns `true` for an OpenAPI 3.2 or later document. Swagger 2.0 and OpenAPI 3.0 documents are
@@ -1216,10 +1230,13 @@ function upgradeDocument(raw, { pathRefs = true } = {}) {
1216
1230
  const refs = createRefs(raw);
1217
1231
  inlinePathRefs(raw, (ref) => refs.resolve(ref, { report: false }));
1218
1232
  }
1233
+ const declared = raw && typeof raw === "object" ? raw.openapi ?? raw.swagger : void 0;
1234
+ const legacy = declared !== void 0 && /^(2|3\.0)(\.|$)/.test(String(declared));
1219
1235
  try {
1220
- return (0, _scalar_openapi_upgrader.upgrade)(raw, "3.1");
1236
+ const document = (0, _scalar_openapi_upgrader.upgrade)(raw, "3.1");
1237
+ if (legacy) markLegacyDocument(document);
1238
+ return document;
1221
1239
  } catch (error) {
1222
- const declared = raw && typeof raw === "object" ? raw.openapi ?? raw.swagger : void 0;
1223
1240
  throw new _kubb_kit.Diagnostics.Error({
1224
1241
  code: _kubb_kit.Diagnostics.code.invalidDocument,
1225
1242
  severity: "error",
@@ -3092,10 +3109,48 @@ function convertAllOf({ schema: original, name, nullable, defaultValue, parse, r
3092
3109
  members: [...(0, _kubb_kit.mergeAdjacentObjectsLazy)(allOfMembers.slice(0, syntheticStart)), ...(0, _kubb_kit.mergeAdjacentObjectsLazy)(allOfMembers.slice(syntheticStart))]
3093
3110
  });
3094
3111
  }
3112
+ const scalarTypes = /* @__PURE__ */ new Set([
3113
+ "string",
3114
+ "number",
3115
+ "integer",
3116
+ "boolean"
3117
+ ]);
3118
+ /**
3119
+ * Copies a scalar parent `type` and `format` onto inline members without a `type`. JSON Schema applies
3120
+ * sibling keywords to every member, so `type: 'integer', anyOf: [{ minimum: 10 }]` keeps each member an integer.
3121
+ */
3122
+ function inheritScalarType(members, { schema, type }) {
3123
+ if (!type || !scalarTypes.has(type)) return members;
3124
+ if (Array.isArray(schema.type) && schema.type.filter((t) => t !== "null").length > 1) return members;
3125
+ return members.map((member) => {
3126
+ if (!member || typeof member !== "object" || isReference(member) || member.type !== void 0) return member;
3127
+ const obj = member;
3128
+ return {
3129
+ type,
3130
+ ...schema.format !== void 0 && obj.format === void 0 && { format: schema.format },
3131
+ ...obj
3132
+ };
3133
+ });
3134
+ }
3095
3135
  /**
3096
3136
  * Converts a `oneOf` / `anyOf` schema into a `UnionSchemaNode`.
3097
3137
  */
3098
3138
  function convertUnion(context) {
3139
+ if (context.schema.enum?.length) {
3140
+ const { enum: values, ...union } = context.schema;
3141
+ const enumSchema = {
3142
+ type: union.type,
3143
+ enum: isLegacyDocument(context.document) ? [...values, null] : values
3144
+ };
3145
+ const members = [convertUnion({
3146
+ ...context,
3147
+ schema: union
3148
+ }), context.parse({ schema: enumSchema })];
3149
+ return createNode(context, {
3150
+ type: _kubb_kit.ast.schema.types.intersection,
3151
+ members
3152
+ });
3153
+ }
3099
3154
  const annotatedEnum = convertAnnotatedEnum(context);
3100
3155
  if (annotatedEnum) return annotatedEnum;
3101
3156
  const { schema, name, nullable, defaultValue, parse, refs } = context;
@@ -3108,7 +3163,7 @@ function convertUnion(context) {
3108
3163
  const discriminator = isDiscriminator(schema) ? schema.discriminator : void 0;
3109
3164
  const declaredMembers = [...schema.oneOf ?? [], ...schema.anyOf ?? []];
3110
3165
  const defaultRef = getDefaultMappingRef(typeof schema.discriminator === "object" ? schema.discriminator : void 0);
3111
- const unionMembers = resolveUnionMembers(defaultRef && !declaredMembers.some((member) => isReference(member) && member.$ref === defaultRef) ? [...declaredMembers, { $ref: defaultRef }] : declaredMembers, schema.properties ?? {});
3166
+ const unionMembers = resolveUnionMembers(inheritScalarType(defaultRef && !declaredMembers.some((member) => isReference(member) && member.$ref === defaultRef) ? [...declaredMembers, { $ref: defaultRef }] : declaredMembers, context), schema.properties ?? {});
3112
3167
  const strategy = schema.oneOf ? "one" : "any";
3113
3168
  const explicitDiscriminatorPropertyName = discriminator?.propertyName;
3114
3169
  const { oneOf: _o, anyOf: _a, discriminator: _d, ...memberBaseSchema } = schema;
@@ -3172,12 +3227,11 @@ function convertUnion(context) {
3172
3227
  * with nullability already folded in.
3173
3228
  */
3174
3229
  function convertMultiType({ schema, name, nullable, defaultValue, parse }) {
3175
- const types = schema.type;
3176
- const nonNullTypes = types.filter((t) => t !== "null");
3230
+ const nonNullTypes = schema.type.filter((t) => t !== "null");
3177
3231
  return createNode({
3178
3232
  schema,
3179
3233
  name,
3180
- nullable: types.includes("null") || nullable || void 0,
3234
+ nullable,
3181
3235
  defaultValue
3182
3236
  }, {
3183
3237
  type: _kubb_kit.ast.schema.types.union,
@@ -3483,12 +3537,12 @@ function nameEnums(node, options) {
3483
3537
  /**
3484
3538
  * Converts an object-like schema into an `ObjectSchemaNode`.
3485
3539
  */
3486
- function convertObject({ schema, name, nullable, defaultValue, options, parse, refs }) {
3540
+ function convertObject({ schema, name, nullable, defaultValue, options, parse, refs, document }) {
3487
3541
  const requiredNames = Array.isArray(schema.required) ? new Set(schema.required) : void 0;
3488
3542
  const properties = schema.properties ? Object.entries(schema.properties).map(([propName, propSchema]) => {
3489
3543
  const required = requiredNames ? requiredNames.has(propName) : !!schema.required;
3490
3544
  const resolvedPropSchema = propSchema;
3491
- const propNullable = isNullable(resolvedPropSchema);
3545
+ const propNullable = isNullable(resolvedPropSchema, document);
3492
3546
  const schemaNode = nameEnums(parse({
3493
3547
  schema: resolvedPropSchema,
3494
3548
  name: (0, _kubb_kit.childName)(name, propName)
@@ -3778,7 +3832,7 @@ function createSchemaParser(ctx) {
3778
3832
  name
3779
3833
  });
3780
3834
  }
3781
- const nullable = isNullable(schema) || void 0;
3835
+ const nullable = isNullable(schema, document) || void 0;
3782
3836
  const context = {
3783
3837
  schema,
3784
3838
  name,