@scalar/openapi-upgrader 0.4.11 → 0.5.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/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @scalar/openapi-upgrader
2
2
 
3
+ ## 0.5.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#11105](https://github.com/scalar/scalar/pull/11105): Finish migrating legacy `nullable` schema keywords when upgrading OpenAPI 3.0 descriptions, so downstream request validators can compile the upgraded schemas and enforce the rest of the request contract.
8
+
9
+ ## 0.5.0
10
+
11
+ ### Minor Changes
12
+
13
+ - [#10997](https://github.com/scalar/scalar/pull/10997): Upgrade referenced OpenAPI 2.0 and 3.0 resources before bundling, including bare fragments interpreted in their owning API description's dialect and media types. Preserve authored reference targets when schemas and parameters move, and retain their context through lazy loading, serialization, and export.
14
+
15
+ Add JSON Magic resource and target lifecycle hooks and the typed `upgradeFragment` API. Headerless external resources are traversed through referenced subtrees, including when reached from OpenAPI 3.1/3.2 descriptions. Exports retain compact `x-scalar-upgrade` bookkeeping only when lazy external references still need the original dialect, relocation mappings, or media types.
16
+
17
+ Resources already stored in an input bundle's `x-ext` keep their existing layout so saved references remain valid. Legacy keywords in those stored resources, including OpenAPI 2.0 schema `x-nullable` and `type: file`, deliberately remain unconverted; newly loaded legacy resources are upgraded.
18
+
3
19
  ## 0.4.11
4
20
 
5
21
  ### Patch Changes
@@ -1,4 +1,4 @@
1
- import type { OpenAPIV3 } from '@scalar/openapi-types';
1
+ import type { OpenAPIV2, OpenAPIV3 } from '@scalar/openapi-types';
2
2
  import type { UnknownObject } from '@scalar/types/utils';
3
3
  /**
4
4
  * Upgrade Swagger 2.0 to OpenAPI 3.0
@@ -6,4 +6,25 @@ import type { UnknownObject } from '@scalar/types/utils';
6
6
  * https://swagger.io/blog/news/whats-new-in-openapi-3-0/
7
7
  */
8
8
  export declare function upgradeFromTwoToThree(originalSpecification: UnknownObject): UnknownObject | OpenAPIV3.Document<{}>;
9
+ /**
10
+ * Converts Swagger discriminator and nullability fields to their OpenAPI 3.0 forms.
11
+ * Visit only OpenAPI 2.0 subschema keywords so example/default values and custom extensions remain user data.
12
+ * Set recursive to false when the caller already owns schema traversal.
13
+ */
14
+ export declare const migrateSchema: (schema: unknown, recursive?: boolean) => void;
15
+ /** Schema-valued fields moved from a 2.0 non-body parameter or header into its schema. */
16
+ export declare const parameterSchemaFields: readonly ["type", "format", "default", "items", "maximum", "exclusiveMaximum", "minimum", "exclusiveMinimum", "maxLength", "minLength", "pattern", "maxItems", "minItems", "uniqueItems", "enum", "multipleOf", "x-nullable"];
17
+ /** Convert a 2.0 non-body parameter in place, moving validation fields into its schema. */
18
+ export declare function transformParameterObject(parameter: OpenAPIV2.ParameterObject | OpenAPIV2.ReferenceObject): OpenAPIV3.ParameterObject | OpenAPIV3.ReferenceObject;
19
+ /**
20
+ * Transform OpenAPI 2.0 response header to OpenAPI 3.0 format.
21
+ * Response headers do not have "in", "name", "style", or "explode" properties.
22
+ */
23
+ export declare function transformResponseHeader(header: OpenAPIV2.HeaderObject | OpenAPIV2.ReferenceObject): OpenAPIV3.HeaderObject | OpenAPIV3.ReferenceObject;
24
+ /** Convert a 2.0 body parameter in place using the owning operation's effective request media types. */
25
+ export declare function migrateBodyParameter(bodyParameter: OpenAPIV2.ParameterObject, consumes: string[]): OpenAPIV3.RequestBodyObject;
26
+ /** Combine 2.0 form parameters without modifying them, using the effective request media types. */
27
+ export declare function migrateFormDataParameter(parameters: OpenAPIV2.ParameterObject[], consumes?: string[] | undefined): OpenAPIV3.RequestBodyObject;
28
+ /** Convert a 2.0 response in place using the owning operation's effective response media types. */
29
+ export declare const transformResponseObject: (responseObj: UnknownObject, produces: string[]) => UnknownObject;
9
30
  //# sourceMappingURL=upgrade-from-two-to-three.d.ts.map
@@ -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":"AAGA,OAAO,KAAK,EAAa,SAAS,EAAE,MAAM,uBAAuB,CAAA;AACjE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAmMxD;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,qBAAqB,EAAE,aAAa,0CAsfzE"}
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":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AACjE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAmMxD;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,qBAAqB,EAAE,aAAa,0CAiXzE;AAED;;;;GAIG;AACH,eAAO,MAAM,aAAa,GAAI,QAAQ,OAAO,EAAE,mBAAgB,KAAG,IA0CjE,CAAA;AAED,0FAA0F;AAC1F,eAAO,MAAM,qBAAqB,+NAkBxB,CAAA;AA0BV,2FAA2F;AAC3F,wBAAgB,wBAAwB,CACtC,SAAS,EAAE,SAAS,CAAC,eAAe,GAAG,SAAS,CAAC,eAAe,GAC/D,SAAS,CAAC,eAAe,GAAG,SAAS,CAAC,eAAe,CAuDvD;AAED;;;GAGG;AACH,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,SAAS,CAAC,YAAY,GAAG,SAAS,CAAC,eAAe,GACzD,SAAS,CAAC,YAAY,GAAG,SAAS,CAAC,eAAe,CAmBpD;AAuFD,wGAAwG;AACxG,wBAAgB,oBAAoB,CAClC,aAAa,EAAE,SAAS,CAAC,eAAe,EACxC,QAAQ,EAAE,MAAM,EAAE,GACjB,SAAS,CAAC,iBAAiB,CA8E7B;AAED,mGAAmG;AACnG,wBAAgB,wBAAwB,CACtC,UAAU,EAAE,SAAS,CAAC,eAAe,EAAE,EACvC,QAAQ,GAAE,MAAM,EAAE,GAAG,SAAmC,GACvD,SAAS,CAAC,iBAAiB,CAwD7B;AA6GD,mGAAmG;AACnG,eAAO,MAAM,uBAAuB,GAAI,aAAa,aAAa,EAAE,UAAU,MAAM,EAAE,KAAG,aAwExF,CAAA"}
@@ -310,71 +310,7 @@ export function upgradeFromTwoToThree(originalSpecification) {
310
310
  }
311
311
  else {
312
312
  // Transform the response object
313
- const responseObj = response;
314
- const produces = document.produces ?? [DEFAULT_MEDIA_TYPE];
315
- // Transform schema to content
316
- if (responseObj.schema) {
317
- migrateSchema(responseObj.schema);
318
- if (typeof responseObj.content !== 'object') {
319
- responseObj.content = {};
320
- }
321
- for (const type of produces) {
322
- ;
323
- responseObj.content[type] = {
324
- schema: responseObj.schema,
325
- };
326
- }
327
- delete responseObj.schema;
328
- }
329
- // Transform examples from Swagger 2.0 to OpenAPI 3.0 format
330
- if (responseObj.examples && typeof responseObj.examples === 'object') {
331
- if (typeof responseObj.content !== 'object') {
332
- responseObj.content = {};
333
- }
334
- const defaultMediaType = produces[0] ?? DEFAULT_MEDIA_TYPE;
335
- for (const [key, exampleValue] of Object.entries(responseObj.examples)) {
336
- // Media-type keys (e.g. application/json) go to content[key]; named example keys go to content[defaultMediaType].examples[key].
337
- if (isMediaTypeKey(key, produces)) {
338
- if (typeof responseObj.content[key] !== 'object') {
339
- ;
340
- responseObj.content[key] = {};
341
- }
342
- ;
343
- responseObj.content[key].example =
344
- exampleValue;
345
- }
346
- else {
347
- if (typeof responseObj.content[defaultMediaType] !== 'object') {
348
- ;
349
- responseObj.content[defaultMediaType] = {};
350
- }
351
- const mediaEntry = responseObj.content[defaultMediaType];
352
- if (typeof mediaEntry.examples !== 'object') {
353
- mediaEntry.examples = {};
354
- }
355
- ;
356
- mediaEntry.examples[key] =
357
- wrapAsExampleObject(exampleValue);
358
- }
359
- }
360
- delete responseObj.examples;
361
- }
362
- // Clean up content entries that only have an empty schema when other entries have examples
363
- if (responseObj.content && typeof responseObj.content === 'object') {
364
- removeEmptySchemaOnlyContentEntries(responseObj.content);
365
- }
366
- // Transform headers if present
367
- if (isObjectLike(responseObj.headers)) {
368
- responseObj.headers = Object.entries(responseObj.headers).reduce((acc, [headerName, header]) => {
369
- if (header && typeof header === 'object') {
370
- return {
371
- [headerName]: transformResponseHeader(header),
372
- ...acc,
373
- };
374
- }
375
- return acc;
376
- }, {});
377
- }
313
+ const responseObj = transformResponseObject(response, document.produces ?? [DEFAULT_MEDIA_TYPE]);
378
314
  migratedResponses[name] = responseObj;
379
315
  }
380
316
  }
@@ -439,65 +375,7 @@ export function upgradeFromTwoToThree(originalSpecification) {
439
375
  if (operationItem.responses) {
440
376
  for (const response in operationItem.responses) {
441
377
  if (Object.hasOwn(operationItem.responses, response)) {
442
- const responseItem = operationItem.responses[response];
443
- if (responseItem.headers && typeof responseItem.headers === 'object') {
444
- responseItem.headers = Object.entries(responseItem.headers).reduce((acc, [name, header]) => {
445
- if (header && typeof header === 'object') {
446
- return {
447
- [name]: transformResponseHeader(header),
448
- ...acc,
449
- };
450
- }
451
- return acc;
452
- }, {});
453
- }
454
- if (responseItem.schema) {
455
- migrateSchema(responseItem.schema);
456
- const produces = operationItem.produces ?? document.produces ?? [DEFAULT_MEDIA_TYPE];
457
- if (typeof responseItem.content !== 'object') {
458
- responseItem.content = {};
459
- }
460
- for (const type of produces) {
461
- responseItem.content[type] = {
462
- schema: responseItem.schema,
463
- };
464
- }
465
- delete responseItem.schema;
466
- }
467
- // Transform response examples from Swagger 2.0 to OpenAPI 3.0 format
468
- // In Swagger 2.0, examples are at response level: examples: { DEFAULT_MEDIA_TYPE: {...} } or named: { 'Example': {...} }
469
- // In OpenAPI 3.0, examples move inside content: content: { DEFAULT_MEDIA_TYPE: { example: {...} } } or examples: { Example: { value: {...} } }
470
- if (responseItem.examples && typeof responseItem.examples === 'object') {
471
- if (typeof responseItem.content !== 'object') {
472
- responseItem.content = {};
473
- }
474
- const produces = operationItem.produces ?? document.produces ?? [DEFAULT_MEDIA_TYPE];
475
- const defaultMediaType = produces[0] ?? DEFAULT_MEDIA_TYPE;
476
- for (const [key, exampleValue] of Object.entries(responseItem.examples)) {
477
- // Media-type keys (e.g. application/json) go to content[key]; named example keys go to content[defaultMediaType].examples[key].
478
- if (isMediaTypeKey(key, produces)) {
479
- if (typeof responseItem.content[key] !== 'object') {
480
- responseItem.content[key] = {};
481
- }
482
- responseItem.content[key].example = exampleValue;
483
- }
484
- else {
485
- if (typeof responseItem.content[defaultMediaType] !== 'object') {
486
- responseItem.content[defaultMediaType] = {};
487
- }
488
- const mediaEntry = responseItem.content[defaultMediaType];
489
- if (typeof mediaEntry.examples !== 'object') {
490
- mediaEntry.examples = {};
491
- }
492
- mediaEntry.examples[key] = wrapAsExampleObject(exampleValue);
493
- }
494
- }
495
- delete responseItem.examples;
496
- }
497
- // Clean up content entries that only have an empty schema when other entries have examples
498
- if (responseItem.content && typeof responseItem.content === 'object') {
499
- removeEmptySchemaOnlyContentEntries(responseItem.content);
500
- }
378
+ operationItem.responses[response] = transformResponseObject(operationItem.responses[response], operationItem.produces ?? document.produces ?? [DEFAULT_MEDIA_TYPE]);
501
379
  }
502
380
  }
503
381
  }
@@ -575,8 +453,9 @@ export function upgradeFromTwoToThree(originalSpecification) {
575
453
  /**
576
454
  * Converts Swagger discriminator and nullability fields to their OpenAPI 3.0 forms.
577
455
  * Visit only OpenAPI 2.0 subschema keywords so example/default values and custom extensions remain user data.
456
+ * Set recursive to false when the caller already owns schema traversal.
578
457
  */
579
- const migrateSchema = (schema) => {
458
+ export const migrateSchema = (schema, recursive = true) => {
580
459
  if (!isObjectLike(schema)) {
581
460
  return;
582
461
  }
@@ -598,6 +477,9 @@ const migrateSchema = (schema) => {
598
477
  }
599
478
  delete schema['x-nullable'];
600
479
  }
480
+ if (!recursive) {
481
+ return;
482
+ }
601
483
  if (isObjectLike(schema.properties)) {
602
484
  for (const property of Object.values(schema.properties)) {
603
485
  migrateSchema(property);
@@ -611,27 +493,28 @@ const migrateSchema = (schema) => {
611
493
  }
612
494
  }
613
495
  };
496
+ /** Schema-valued fields moved from a 2.0 non-body parameter or header into its schema. */
497
+ export const parameterSchemaFields = [
498
+ 'type',
499
+ 'format',
500
+ 'default',
501
+ 'items',
502
+ 'maximum',
503
+ 'exclusiveMaximum',
504
+ 'minimum',
505
+ 'exclusiveMinimum',
506
+ 'maxLength',
507
+ 'minLength',
508
+ 'pattern',
509
+ 'maxItems',
510
+ 'minItems',
511
+ 'uniqueItems',
512
+ 'enum',
513
+ 'multipleOf',
514
+ 'x-nullable',
515
+ ];
614
516
  function transformItemsObject(obj) {
615
- const schemaProperties = [
616
- 'type',
617
- 'format',
618
- 'default',
619
- 'items',
620
- 'maximum',
621
- 'exclusiveMaximum',
622
- 'minimum',
623
- 'exclusiveMinimum',
624
- 'maxLength',
625
- 'minLength',
626
- 'pattern',
627
- 'maxItems',
628
- 'minItems',
629
- 'uniqueItems',
630
- 'enum',
631
- 'multipleOf',
632
- 'x-nullable',
633
- ];
634
- const schema = schemaProperties.reduce((acc, property) => {
517
+ const schema = parameterSchemaFields.reduce((acc, property) => {
635
518
  if (Object.hasOwn(obj, property)) {
636
519
  acc[property] = obj[property];
637
520
  delete obj[property];
@@ -650,7 +533,8 @@ function getParameterLocation(location) {
650
533
  }
651
534
  return location;
652
535
  }
653
- function transformParameterObject(parameter) {
536
+ /** Convert a 2.0 non-body parameter in place, moving validation fields into its schema. */
537
+ export function transformParameterObject(parameter) {
654
538
  if (Object.hasOwn(parameter, '$ref') && typeof parameter.$ref === 'string') {
655
539
  return {
656
540
  $ref: parameter.$ref,
@@ -700,7 +584,7 @@ function transformParameterObject(parameter) {
700
584
  * Transform OpenAPI 2.0 response header to OpenAPI 3.0 format.
701
585
  * Response headers do not have "in", "name", "style", or "explode" properties.
702
586
  */
703
- function transformResponseHeader(header) {
587
+ export function transformResponseHeader(header) {
704
588
  if (Object.hasOwn(header, '$ref') && typeof header.$ref === 'string') {
705
589
  return {
706
590
  $ref: header.$ref,
@@ -780,7 +664,8 @@ function getFormDataEncoding(parameter) {
780
664
  }
781
665
  return encoding;
782
666
  }
783
- function migrateBodyParameter(bodyParameter, consumes) {
667
+ /** Convert a 2.0 body parameter in place using the owning operation's effective request media types. */
668
+ export function migrateBodyParameter(bodyParameter, consumes) {
784
669
  // Extract x-example and x-examples before deleting other properties
785
670
  // @see https://redocly.com/docs-legacy/api-reference-docs/specification-extensions/x-examples
786
671
  const { xExample, xExamples } = extractXExampleExtensions(bodyParameter);
@@ -843,7 +728,8 @@ function migrateBodyParameter(bodyParameter, consumes) {
843
728
  }
844
729
  return requestBodyObject;
845
730
  }
846
- function migrateFormDataParameter(parameters, consumes = ['multipart/form-data']) {
731
+ /** Combine 2.0 form parameters without modifying them, using the effective request media types. */
732
+ export function migrateFormDataParameter(parameters, consumes = ['multipart/form-data']) {
847
733
  const requestBodyObject = {
848
734
  content: {},
849
735
  };
@@ -973,3 +859,71 @@ function migrateParameters(parameters, consumes, sharedBodyParameters, globalCon
973
859
  }
974
860
  return result;
975
861
  }
862
+ /** Convert a 2.0 response in place using the owning operation's effective response media types. */
863
+ export const transformResponseObject = (responseObj, produces) => {
864
+ if (typeof responseObj.$ref === 'string') {
865
+ return responseObj;
866
+ }
867
+ // Transform schema to content
868
+ if (responseObj.schema) {
869
+ migrateSchema(responseObj.schema);
870
+ if (typeof responseObj.content !== 'object') {
871
+ responseObj.content = {};
872
+ }
873
+ for (const type of produces) {
874
+ ;
875
+ responseObj.content[type] = {
876
+ schema: responseObj.schema,
877
+ };
878
+ }
879
+ delete responseObj.schema;
880
+ }
881
+ // Transform examples from Swagger 2.0 to OpenAPI 3.0 format
882
+ if (responseObj.examples && typeof responseObj.examples === 'object') {
883
+ if (typeof responseObj.content !== 'object') {
884
+ responseObj.content = {};
885
+ }
886
+ const defaultMediaType = produces[0] ?? DEFAULT_MEDIA_TYPE;
887
+ for (const [key, exampleValue] of Object.entries(responseObj.examples)) {
888
+ // Media-type keys (e.g. application/json) go to content[key]; named example keys go to content[defaultMediaType].examples[key].
889
+ if (isMediaTypeKey(key, produces)) {
890
+ if (typeof responseObj.content[key] !== 'object') {
891
+ ;
892
+ responseObj.content[key] = {};
893
+ }
894
+ ;
895
+ responseObj.content[key].example = exampleValue;
896
+ }
897
+ else {
898
+ if (typeof responseObj.content[defaultMediaType] !== 'object') {
899
+ ;
900
+ responseObj.content[defaultMediaType] = {};
901
+ }
902
+ const mediaEntry = responseObj.content[defaultMediaType];
903
+ if (typeof mediaEntry.examples !== 'object') {
904
+ mediaEntry.examples = {};
905
+ }
906
+ ;
907
+ mediaEntry.examples[key] = wrapAsExampleObject(exampleValue);
908
+ }
909
+ }
910
+ delete responseObj.examples;
911
+ }
912
+ // Clean up content entries that only have an empty schema when other entries have examples
913
+ if (responseObj.content && typeof responseObj.content === 'object') {
914
+ removeEmptySchemaOnlyContentEntries(responseObj.content);
915
+ }
916
+ // Transform headers if present
917
+ if (isObjectLike(responseObj.headers)) {
918
+ responseObj.headers = Object.entries(responseObj.headers).reduce((acc, [headerName, header]) => {
919
+ if (header && typeof header === 'object') {
920
+ return {
921
+ [headerName]: transformResponseHeader(header),
922
+ ...acc,
923
+ };
924
+ }
925
+ return acc;
926
+ }, {});
927
+ }
928
+ return responseObj;
929
+ };
@@ -1,5 +1,12 @@
1
1
  import type { OpenAPIV3_1 } from '@scalar/openapi-types';
2
2
  import type { UnknownObject } from '@scalar/types/utils';
3
+ type NullableComposition = {
4
+ base: unknown;
5
+ keywords: string[];
6
+ unwrapAllOf: boolean;
7
+ };
8
+ /** Plan nullable composition wrapping consistently for migrations and reference locations. */
9
+ export declare const getNullableComposition: (schema: UnknownObject) => NullableComposition | undefined;
3
10
  /**
4
11
  * Upgrade from OpenAPI 3.0.x to 3.1.1
5
12
  *
@@ -18,4 +25,7 @@ export declare function upgradeFromThreeToThreeOne(originalContent: UnknownObjec
18
25
  [customExtension: `x-${string}`]: any;
19
26
  [key: string]: any;
20
27
  });
28
+ /** Apply one migration step to a node at its semantic OpenAPI location. */
29
+ export declare const applyChangesToDocument: (schema: UnknownObject, path?: string[]) => UnknownObject;
30
+ export {};
21
31
  //# sourceMappingURL=upgrade-from-three-to-three-one.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"upgrade-from-three-to-three-one.d.ts","sourceRoot":"","sources":["../../src/3.0-to-3.1/upgrade-from-three-to-three-one.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAmDxD;;;;GAIG;AACH,wBAAgB,0BAA0B,CAAC,eAAe,EAAE,aAAa;;;;;;;;;;;;GAcxE"}
1
+ {"version":3,"file":"upgrade-from-three-to-three-one.d.ts","sourceRoot":"","sources":["../../src/3.0-to-3.1/upgrade-from-three-to-three-one.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAoJxD,KAAK,mBAAmB,GAAG;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,QAAQ,EAAE,MAAM,EAAE,CAAC;IAAC,WAAW,EAAE,OAAO,CAAA;CAAE,CAAA;AAEtF,8FAA8F;AAC9F,eAAO,MAAM,sBAAsB,GAAI,QAAQ,aAAa,KAAG,mBAAmB,GAAG,SAgBpF,CAAA;AA4BD;;;;GAIG;AACH,wBAAgB,0BAA0B,CAAC,eAAe,EAAE,aAAa;;;;;;;;;;;;GAwCxE;AAED,2EAA2E;AAC3E,eAAO,MAAM,sBAAsB,GAAI,QAAQ,aAAa,EAAE,OAAO,MAAM,EAAE,kBA2G5E,CAAA"}