@scalar/openapi-upgrader 0.2.1 → 0.2.3

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.2.3
4
+
5
+ ### Patch Changes
6
+
7
+ - [#8622](https://github.com/scalar/scalar/pull/8622): fix: swagger 2.0 mixed media types for produces & examples
8
+
9
+ ## 0.2.2
10
+
11
+ ### Patch Changes
12
+
13
+ - [#8502](https://github.com/scalar/scalar/pull/8502): feat: transforms x-examples keyed by example name
14
+
3
15
  ## 0.2.1
4
16
 
5
17
  ### Patch 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":"AAAA,OAAO,KAAK,EAAa,SAAS,EAAE,MAAM,uBAAuB,CAAA;AACjE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAgHxD;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,qBAAqB,EAAE,aAAa,0CA0bzE"}
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":"AAAA,OAAO,KAAK,EAAa,SAAS,EAAE,MAAM,uBAAuB,CAAA;AACjE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AA8LxD;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,qBAAqB,EAAE,aAAa,0CAoczE"}
@@ -22,6 +22,73 @@ function isNamedExamplesCollection(value) {
22
22
  return (isNonEmptyObject(value) &&
23
23
  Object.values(value).every((v) => typeof v === 'object' && v !== null && !Array.isArray(v)));
24
24
  }
25
+ /** Checks if a schema is empty (no meaningful properties defined) */
26
+ function isEmptySchema(schema) {
27
+ if (typeof schema !== 'object' || schema === null) {
28
+ return true;
29
+ }
30
+ const s = schema;
31
+ const substantiveValidationKeywords = [
32
+ 'enum',
33
+ 'const',
34
+ 'not',
35
+ 'format',
36
+ 'multipleOf',
37
+ 'maximum',
38
+ 'exclusiveMaximum',
39
+ 'minimum',
40
+ 'exclusiveMinimum',
41
+ 'maxLength',
42
+ 'minLength',
43
+ 'pattern',
44
+ 'maxItems',
45
+ 'minItems',
46
+ 'uniqueItems',
47
+ 'maxProperties',
48
+ 'minProperties',
49
+ 'required',
50
+ ];
51
+ // Has substantive schema keywords — not empty
52
+ if (s.allOf ||
53
+ s.oneOf ||
54
+ s.anyOf ||
55
+ s.items ||
56
+ s.$ref ||
57
+ 'additionalProperties' in s ||
58
+ substantiveValidationKeywords.some((key) => key in s)) {
59
+ return false;
60
+ }
61
+ // Has properties with at least one defined property — not empty
62
+ if (typeof s.properties === 'object' && s.properties !== null && Object.keys(s.properties).length > 0) {
63
+ return false;
64
+ }
65
+ return true;
66
+ }
67
+ /**
68
+ * Removes content entries that only have an empty schema (no example/examples)
69
+ * when other entries with actual examples exist. This prevents the UI from
70
+ * selecting a meaningless produces-based entry over one with a real example.
71
+ */
72
+ function removeEmptySchemaOnlyContentEntries(content) {
73
+ const keys = Object.keys(content);
74
+ const hasEntryWithExample = keys.some((key) => {
75
+ const entry = content[key];
76
+ return entry?.example !== undefined || entry?.examples !== undefined;
77
+ });
78
+ if (!hasEntryWithExample) {
79
+ return;
80
+ }
81
+ for (const key of keys) {
82
+ const entry = content[key];
83
+ if (!entry)
84
+ continue;
85
+ const hasExample = entry.example !== undefined || entry.examples !== undefined;
86
+ const hasOnlySchema = entry.schema !== undefined && !hasExample && Object.keys(entry).length === 1;
87
+ if (hasOnlySchema && isEmptySchema(entry.schema)) {
88
+ delete content[key];
89
+ }
90
+ }
91
+ }
25
92
  /** The allowed properties for an OpenAPI 3.x ExampleObject */
26
93
  const EXAMPLE_OBJECT_PROPERTIES = new Set(['summary', 'description', 'value', 'externalValue']);
27
94
  /**
@@ -263,6 +330,10 @@ export function upgradeFromTwoToThree(originalSpecification) {
263
330
  }
264
331
  delete responseObj.examples;
265
332
  }
333
+ // Clean up content entries that only have an empty schema when other entries have examples
334
+ if (responseObj.content && typeof responseObj.content === 'object') {
335
+ removeEmptySchemaOnlyContentEntries(responseObj.content);
336
+ }
266
337
  // Transform headers if present
267
338
  if (responseObj.headers && typeof responseObj.headers === 'object') {
268
339
  responseObj.headers = Object.entries(responseObj.headers).reduce((acc, [headerName, header]) => {
@@ -373,6 +444,10 @@ export function upgradeFromTwoToThree(originalSpecification) {
373
444
  }
374
445
  delete responseItem.examples;
375
446
  }
447
+ // Clean up content entries that only have an empty schema when other entries have examples
448
+ if (responseItem.content && typeof responseItem.content === 'object') {
449
+ removeEmptySchemaOnlyContentEntries(responseItem.content);
450
+ }
376
451
  }
377
452
  }
378
453
  }
@@ -604,13 +679,10 @@ function migrateBodyParameter(bodyParameter, consumes) {
604
679
  requestBodyObject.content[type] = {
605
680
  schema: schema,
606
681
  };
607
- // Handle x-example (singular) - Redocly extension for Swagger 2.0
608
- // Transforms to OpenAPI 3.x `example` field
609
- if (isNonEmptyObject(xExample) && type in xExample) {
610
- requestBodyObject.content[type].example = xExample[type];
611
- }
612
- // Handle x-examples (plural) - Redocly extension for Swagger 2.0
613
- // Transforms to OpenAPI 3.x `examples` field (named examples with summary/value)
682
+ // Handle x-examples (plural) first - Redocly extension for Swagger 2.0
683
+ // Transforms to OpenAPI 3.x `examples` field (named examples with summary/value).
684
+ // When both x-example and x-examples exist, we prefer x-examples so each media type
685
+ // has only one of `example` or `examples` (OpenAPI 3 does not use both on the same type).
614
686
  if (isNonEmptyObject(xExamples) && type in xExamples) {
615
687
  const examples = xExamples[type];
616
688
  // Check if examples is already in proper OpenAPI 3.x format (object with entries that are valid ExampleObjects)
@@ -635,17 +707,15 @@ function migrateBodyParameter(bodyParameter, consumes) {
635
707
  // Fallback: x-examples keyed by example name instead of media type
636
708
  // e.g. x-examples: { Request: { email: "test@example.com" } }
637
709
  else if (isNonEmptyObject(xExamples) && !Object.keys(xExamples).some(isMediaTypeKey)) {
638
- if (isNamedExamplesCollection(xExamples)) {
639
- requestBodyObject.content[type].examples = Object.entries(xExamples).reduce((acc, [key, exampleValue]) => {
640
- acc[key] = wrapAsExampleObject(exampleValue);
641
- return acc;
642
- }, {});
643
- }
644
- else {
645
- requestBodyObject.content[type].examples = {
646
- default: wrapAsExampleObject(xExamples),
647
- };
648
- }
710
+ requestBodyObject.content[type].examples = Object.entries(xExamples).reduce((acc, [key, exampleValue]) => {
711
+ acc[key] = wrapAsExampleObject(exampleValue);
712
+ return acc;
713
+ }, {});
714
+ }
715
+ // Handle x-example (singular) only when we did not set examples for this type
716
+ // (avoids having both example and examples on the same media type)
717
+ if (!requestBodyObject.content[type].examples && isNonEmptyObject(xExample) && type in xExample) {
718
+ requestBodyObject.content[type].example = xExample[type];
649
719
  }
650
720
  }
651
721
  }
package/package.json CHANGED
@@ -20,7 +20,7 @@
20
20
  "openapi 3.0",
21
21
  "openapi 3.1"
22
22
  ],
23
- "version": "0.2.1",
23
+ "version": "0.2.3",
24
24
  "engines": {
25
25
  "node": ">=22"
26
26
  },
@@ -75,7 +75,7 @@
75
75
  },
76
76
  "devDependencies": {
77
77
  "vite": "8.0.0",
78
- "@scalar/types": "0.7.4"
78
+ "@scalar/types": "0.7.5"
79
79
  },
80
80
  "scripts": {
81
81
  "build": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json",