@scalar/openapi-upgrader 0.3.0 → 0.4.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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @scalar/openapi-upgrader
2
2
 
3
+ ## 0.4.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#10346](https://github.com/scalar/scalar/pull/10346): Add `onIncompatible: 'ignore'` for best-effort OpenAPI 3.2 upgrades. Apply available migrations and return the document despite compatibility issues, without mutating the input. Strict mode remains the default; malformed versions and clone errors still throw.
8
+
9
+ ## 0.3.1
10
+
11
+ ### Patch Changes
12
+
13
+ - [#10306](https://github.com/scalar/scalar/pull/10306): Preserve Swagger 2.0 `x-nullable` when upgrading schemas, parameters, and headers.
14
+
3
15
  ## 0.3.0
4
16
 
5
17
  ### Minor Changes
@@ -1 +1 @@
1
- {"version":3,"file":"upgrade-from-two-to-three.d.ts","sourceRoot":"","sources":["../../src/2.0-to-3.0/upgrade-from-two-to-three.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAa,SAAS,EAAE,MAAM,uBAAuB,CAAA;AACjE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AA2LxD;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,qBAAqB,EAAE,aAAa,0CA4bzE"}
1
+ {"version":3,"file":"upgrade-from-two-to-three.d.ts","sourceRoot":"","sources":["../../src/2.0-to-3.0/upgrade-from-two-to-three.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAa,SAAS,EAAE,MAAM,uBAAuB,CAAA;AACjE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AA2LxD;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,qBAAqB,EAAE,aAAa,0CAkczE"}
@@ -185,6 +185,9 @@ export function upgradeFromTwoToThree(originalSpecification) {
185
185
  }
186
186
  // Schemas
187
187
  if (document.definitions) {
188
+ for (const schema of Object.values(document.definitions)) {
189
+ migrateSchemaNullability(schema);
190
+ }
188
191
  document.components = Object.assign({}, document.components, {
189
192
  schemas: document.definitions,
190
193
  });
@@ -285,6 +288,7 @@ export function upgradeFromTwoToThree(originalSpecification) {
285
288
  const produces = document.produces ?? [DEFAULT_MEDIA_TYPE];
286
289
  // Transform schema to content
287
290
  if (responseObj.schema) {
291
+ migrateSchemaNullability(responseObj.schema);
288
292
  if (typeof responseObj.content !== 'object') {
289
293
  responseObj.content = {};
290
294
  }
@@ -400,6 +404,7 @@ export function upgradeFromTwoToThree(originalSpecification) {
400
404
  }, {});
401
405
  }
402
406
  if (responseItem.schema) {
407
+ migrateSchemaNullability(responseItem.schema);
403
408
  const produces = document.produces ?? operationItem.produces ?? [DEFAULT_MEDIA_TYPE];
404
409
  if (typeof responseItem.content !== 'object') {
405
410
  responseItem.content = {};
@@ -516,6 +521,34 @@ export function upgradeFromTwoToThree(originalSpecification) {
516
521
  delete document.produces;
517
522
  return document;
518
523
  }
524
+ /**
525
+ * Swagger tooling uses x-nullable for the nullability keyword introduced in OpenAPI 3.0.
526
+ * Visit only subschemas so example/default values and custom extensions remain user data.
527
+ */
528
+ const migrateSchemaNullability = (schema) => {
529
+ if (!isObjectLike(schema)) {
530
+ return;
531
+ }
532
+ if (typeof schema['x-nullable'] === 'boolean') {
533
+ // False is already the default in OpenAPI 3.0 and needs no keyword in later versions.
534
+ if (schema['x-nullable']) {
535
+ schema.nullable = true;
536
+ }
537
+ delete schema['x-nullable'];
538
+ }
539
+ if (isObjectLike(schema.properties)) {
540
+ for (const property of Object.values(schema.properties)) {
541
+ migrateSchemaNullability(property);
542
+ }
543
+ }
544
+ migrateSchemaNullability(schema.items);
545
+ migrateSchemaNullability(schema.additionalProperties);
546
+ if (Array.isArray(schema.allOf)) {
547
+ for (const member of schema.allOf) {
548
+ migrateSchemaNullability(member);
549
+ }
550
+ }
551
+ };
519
552
  function transformItemsObject(obj) {
520
553
  const schemaProperties = [
521
554
  'type',
@@ -534,14 +567,17 @@ function transformItemsObject(obj) {
534
567
  'uniqueItems',
535
568
  'enum',
536
569
  'multipleOf',
570
+ 'x-nullable',
537
571
  ];
538
- return schemaProperties.reduce((acc, property) => {
572
+ const schema = schemaProperties.reduce((acc, property) => {
539
573
  if (Object.hasOwn(obj, property)) {
540
574
  acc[property] = obj[property];
541
575
  delete obj[property];
542
576
  }
543
577
  return acc;
544
578
  }, {});
579
+ migrateSchemaNullability(schema);
580
+ return schema;
545
581
  }
546
582
  function getParameterLocation(location) {
547
583
  if (location === 'formData') {
@@ -683,6 +719,7 @@ function migrateBodyParameter(bodyParameter, consumes) {
683
719
  delete bodyParameter.name;
684
720
  delete bodyParameter.in;
685
721
  const { schema, ...requestBody } = bodyParameter;
722
+ migrateSchemaNullability(schema);
686
723
  const requestBodyObject = {
687
724
  content: {},
688
725
  ...requestBody,
@@ -1,4 +1,4 @@
1
1
  import type { UnknownObject } from '@scalar/types/utils';
2
- /** Reject only incompatibilities that can be established within this document. */
3
- export declare const migrateObjects: (document: UnknownObject) => Set<string>;
2
+ /** Apply migrations, optionally tolerating incompatibilities established within this document. */
3
+ export declare const migrateObjects: (document: UnknownObject, onIncompatible?: "throw" | "ignore") => Set<string>;
4
4
  //# sourceMappingURL=migrate-objects.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"migrate-objects.d.ts","sourceRoot":"","sources":["../../src/3.1-to-3.2/migrate-objects.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AA2CxD,kFAAkF;AAClF,eAAO,MAAM,cAAc,GAAI,UAAU,aAAa,KAAG,GAAG,CAAC,MAAM,CAmalE,CAAA"}
1
+ {"version":3,"file":"migrate-objects.d.ts","sourceRoot":"","sources":["../../src/3.1-to-3.2/migrate-objects.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AA2CxD,kGAAkG;AAClG,eAAO,MAAM,cAAc,GAAI,UAAU,aAAa,EAAE,iBAAgB,OAAO,GAAG,QAAkB,KAAG,GAAG,CAAC,MAAM,CAmahH,CAAA"}
@@ -17,8 +17,8 @@ const schemaSingles = [
17
17
  'else',
18
18
  'contentSchema',
19
19
  ];
20
- /** Reject only incompatibilities that can be established within this document. */
21
- export const migrateObjects = (document) => {
20
+ /** Apply migrations, optionally tolerating incompatibilities established within this document. */
21
+ export const migrateObjects = (document, onIncompatible = 'throw') => {
22
22
  // A cloned JSON object still inherits Object.prototype. Keep traversal in own
23
23
  // data properties, including when the host already has polluted prototypes.
24
24
  // Maps also allow literal __proto__ and constructor reference segments safely.
@@ -404,7 +404,7 @@ export const migrateObjects = (document) => {
404
404
  for (const { schema, path, dialect } of xmlRoots) {
405
405
  validateXmlNames(schema, path, { inferredName: false, propertyName: false, dialect });
406
406
  }
407
- if (errors.length > 0) {
407
+ if (errors.length > 0 && onIncompatible === 'throw') {
408
408
  throw new UpgradeIncompatibilityError(errors);
409
409
  }
410
410
  return operationTags;
@@ -1,6 +1,6 @@
1
1
  import type { UnknownObject } from '@scalar/types/utils';
2
2
  /** Apply the final migration to a document already owned by the upgrade pipeline. */
3
- export declare const migrateThreeOneToThreeTwo: (document: UnknownObject) => UnknownObject;
3
+ export declare const migrateThreeOneToThreeTwo: (document: UnknownObject, onIncompatible?: "throw" | "ignore") => UnknownObject;
4
4
  /**
5
5
  * Upgrade OpenAPI 3.1.x to 3.2 without changing the input.
6
6
  *
@@ -1 +1 @@
1
- {"version":3,"file":"upgrade-from-three-one-to-three-two.d.ts","sourceRoot":"","sources":["../../src/3.1-to-3.2/upgrade-from-three-one-to-three-two.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAsBxD,qFAAqF;AACrF,eAAO,MAAM,yBAAyB,GAAI,UAAU,aAAa,KAAG,aAQnE,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,6BAA6B,GAAI,kBAAkB,aAAa,KAAG,aAK/E,CAAA"}
1
+ {"version":3,"file":"upgrade-from-three-one-to-three-two.d.ts","sourceRoot":"","sources":["../../src/3.1-to-3.2/upgrade-from-three-one-to-three-two.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAsBxD,qFAAqF;AACrF,eAAO,MAAM,yBAAyB,GACpC,UAAU,aAAa,EACvB,iBAAgB,OAAO,GAAG,QAAkB,KAC3C,aAQF,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,6BAA6B,GAAI,kBAAkB,aAAa,KAAG,aAK/E,CAAA"}
@@ -16,11 +16,11 @@ const isThreeOneDocument = (document) => {
16
16
  return false;
17
17
  };
18
18
  /** Apply the final migration to a document already owned by the upgrade pipeline. */
19
- export const migrateThreeOneToThreeTwo = (document) => {
19
+ export const migrateThreeOneToThreeTwo = (document, onIncompatible = 'throw') => {
20
20
  if (!isThreeOneDocument(document)) {
21
21
  return document;
22
22
  }
23
- const operationTags = migrateObjects(document);
23
+ const operationTags = migrateObjects(document, onIncompatible);
24
24
  migrateTagGroups(document, operationTags);
25
25
  document.openapi = '3.2.0';
26
26
  return document;
package/dist/upgrade.d.ts CHANGED
@@ -2,8 +2,8 @@ import type { OpenAPIV3, OpenAPIV3_1, OpenAPIV3_2 } from '@scalar/openapi-types'
2
2
  import type { UnknownObject } from '@scalar/types/utils';
3
3
  /** Control how compatibility issues in the OpenAPI 3.2 migration are reported. */
4
4
  export type UpgradeOptions = {
5
- /** Defaults to throwing. Collect mode retains OpenAPI 3.1 when migration is incompatible. */
6
- onIncompatible: 'throw' | 'collect';
5
+ /** Defaults to throwing. Collect retains 3.1; ignore applies best-effort migrations despite incompatibilities. */
6
+ onIncompatible: 'throw' | 'collect' | 'ignore';
7
7
  };
8
8
  /** A complete document and the compatibility issues that prevented upgrading it to OpenAPI 3.2. */
9
9
  export type UpgradeResult = {
@@ -16,11 +16,12 @@ export type UpgradeResult = {
16
16
  * Upgrade a Swagger 2.0 or OpenAPI description to the specified target version.
17
17
  * Targeting 3.2 is strict by default. Collect mode returns compatibility diagnostics
18
18
  * with a complete 3.1 fallback instead of throwing for migration incompatibilities.
19
+ * Ignore mode returns a best-effort 3.2 document that may change meaning or be invalid.
19
20
  */
20
21
  export declare function upgrade(value: UnknownObject, targetVersion: '3.0'): OpenAPIV3.Document;
21
22
  export declare function upgrade(value: UnknownObject, targetVersion: '3.1'): OpenAPIV3_1.Document;
22
23
  export declare function upgrade(value: UnknownObject, targetVersion: '3.2', options?: {
23
- onIncompatible: 'throw';
24
+ onIncompatible: 'throw' | 'ignore';
24
25
  }): OpenAPIV3_2.Document;
25
26
  export declare function upgrade(value: UnknownObject, targetVersion: '3.2', options: {
26
27
  onIncompatible: 'collect';
@@ -1 +1 @@
1
- {"version":3,"file":"upgrade.d.ts","sourceRoot":"","sources":["../src/upgrade.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAChF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AASxD,kFAAkF;AAClF,MAAM,MAAM,cAAc,GAAG;IAC3B,6FAA6F;IAC7F,cAAc,EAAE,OAAO,GAAG,SAAS,CAAA;CACpC,CAAA;AAED,mGAAmG;AACnG,MAAM,MAAM,aAAa,GAAG;IAC1B,iGAAiG;IACjG,QAAQ,EAAE,WAAW,CAAC,QAAQ,GAAG,WAAW,CAAC,QAAQ,CAAA;IACrD,0FAA0F;IAC1F,WAAW,EAAE,KAAK,EAAE,CAAA;CACrB,CAAA;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,KAAK,GAAG,SAAS,CAAC,QAAQ,CAAA;AACvF,wBAAgB,OAAO,CAAC,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,KAAK,GAAG,WAAW,CAAC,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AE,aAAa,EACpB,aAAa,EAAE,KAAK,EACpB,OAAO,CAAC,EAAE;IAAE,cAAc,EAAE,OAAO,GAAG,QAAQ,CAAA;CAAE,GAC/C,WAAW,CAAC,QAAQ,CAAA;AACvB,wBAAgB,OAAO,CACrB,KAAK,EAAE,aAAa,EACpB,aAAa,EAAE,KAAK,EACpB,OAAO,EAAE;IAAE,cAAc,EAAE,SAAS,CAAA;CAAE,GACrC,aAAa,CAAA;AAChB,wBAAgB,OAAO,CACrB,KAAK,EAAE,aAAa,EACpB,aAAa,EAAE,KAAK,EACpB,OAAO,EAAE,cAAc,GACtB,WAAW,CAAC,QAAQ,GAAG,aAAa,CAAA"}
package/dist/upgrade.js CHANGED
@@ -10,7 +10,7 @@ export function upgrade(value, targetVersion, options) {
10
10
  const openapi30 = upgradeFromTwoToThree(input);
11
11
  const openapi31 = upgradeFromThreeToThreeOne(openapi30);
12
12
  try {
13
- const document = migrateThreeOneToThreeTwo(openapi31);
13
+ const document = migrateThreeOneToThreeTwo(openapi31, options?.onIncompatible === 'ignore' ? 'ignore' : 'throw');
14
14
  return options?.onIncompatible === 'collect' ? { document, diagnostics: [] } : document;
15
15
  }
16
16
  catch (error) {
package/package.json CHANGED
@@ -20,7 +20,7 @@
20
20
  "openapi 3.0",
21
21
  "openapi 3.1"
22
22
  ],
23
- "version": "0.3.0",
23
+ "version": "0.4.0",
24
24
  "engines": {
25
25
  "node": ">=22"
26
26
  },
@@ -71,11 +71,11 @@
71
71
  },
72
72
  "sideEffects": false,
73
73
  "dependencies": {
74
- "@scalar/helpers": "0.13.0",
74
+ "@scalar/helpers": "0.15.0",
75
75
  "@scalar/openapi-types": "0.9.7"
76
76
  },
77
77
  "devDependencies": {
78
- "@scalar/types": "0.21.0",
78
+ "@scalar/types": "0.22.1",
79
79
  "vite": "8.1.5"
80
80
  },
81
81
  "scripts": {