@scalar/openapi-upgrader 0.4.2 → 0.4.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,11 @@
1
1
  # @scalar/openapi-upgrader
2
2
 
3
+ ## 0.4.3
4
+
5
+ ### Patch Changes
6
+
7
+ - [#10501](https://github.com/scalar/scalar/pull/10501): Keep Swagger 2.0 named examples whose name contains a slash, such as `Coupons/Promos`. Only registered media types are treated as media-type keys, so one such name no longer drops every request body example.
8
+
3
9
  ## 0.4.2
4
10
 
5
11
  ## 0.4.1
@@ -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,0CAkczE"}
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;AA8LxD;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,qBAAqB,EAAE,aAAa,0CAkczE"}
@@ -120,14 +120,16 @@ function wrapAsExampleObject(value) {
120
120
  /**
121
121
  * True if the key looks like a MIME media type (e.g. application/json, text/plain).
122
122
  * Used to distinguish media-type example keys from named example keys when migrating
123
- * Swagger 2.0 examples to OpenAPI 3.0 content. Requires exactly one slash and
124
- * token-style type/subtype (no spaces or semicolons) to avoid false positives
125
- * for named keys that contain a slash (e.g. "Error 404/Not Found").
123
+ * Swagger 2.0 examples to OpenAPI 3.0 content. The type must be a registered top-level
124
+ * type (or `*`) and the subtype a single token, so named keys that contain a slash
125
+ * ("Error 404/Not Found", "Coupons/Promos") stay named examples. Matching is
126
+ * case-sensitive for undeclared keys so "Image/Before" stays a name. Explicit
127
+ * consumes/produces entries take precedence, including mixed-case or private types.
126
128
  */
127
- const MEDIA_TYPE_KEY_PATTERN = /^[a-zA-Z0-9*+.-]+\/[a-zA-Z0-9*+.+-]+$/;
128
- function isMediaTypeKey(key) {
129
- return MEDIA_TYPE_KEY_PATTERN.test(key);
130
- }
129
+ const MEDIA_TYPE_KEY_PATTERN = /^(\*|application|audio|example|font|haptics|image|message|model|multipart|text|video)\/[a-zA-Z0-9*+.-]+$/;
130
+ const isMediaTypeKey = (key, declaredMediaTypes) => {
131
+ return declaredMediaTypes.includes(key) || MEDIA_TYPE_KEY_PATTERN.test(key);
132
+ };
131
133
  /** Transforms x-example entries to OpenAPI 3.x examples format */
132
134
  function transformXExampleToExamples(xExample) {
133
135
  return Object.entries(xExample).reduce((acc, [key, value]) => {
@@ -308,7 +310,7 @@ export function upgradeFromTwoToThree(originalSpecification) {
308
310
  const defaultMediaType = produces[0] ?? DEFAULT_MEDIA_TYPE;
309
311
  for (const [key, exampleValue] of Object.entries(responseObj.examples)) {
310
312
  // Media-type keys (e.g. application/json) go to content[key]; named example keys go to content[defaultMediaType].examples[key].
311
- if (isMediaTypeKey(key)) {
313
+ if (isMediaTypeKey(key, produces)) {
312
314
  if (typeof responseObj.content[key] !== 'object') {
313
315
  ;
314
316
  responseObj.content[key] = {};
@@ -427,7 +429,7 @@ export function upgradeFromTwoToThree(originalSpecification) {
427
429
  const defaultMediaType = produces[0] ?? DEFAULT_MEDIA_TYPE;
428
430
  for (const [key, exampleValue] of Object.entries(responseItem.examples)) {
429
431
  // Media-type keys (e.g. application/json) go to content[key]; named example keys go to content[defaultMediaType].examples[key].
430
- if (isMediaTypeKey(key)) {
432
+ if (isMediaTypeKey(key, operationItem.produces ?? produces)) {
431
433
  if (typeof responseItem.content[key] !== 'object') {
432
434
  responseItem.content[key] = {};
433
435
  }
@@ -756,11 +758,15 @@ function migrateBodyParameter(bodyParameter, consumes) {
756
758
  }
757
759
  // Fallback: x-examples keyed by example name instead of media type
758
760
  // e.g. x-examples: { Request: { email: "test@example.com" } }
759
- else if (isNonEmptyObject(xExamples) && !Object.keys(xExamples).some(isMediaTypeKey)) {
760
- requestBodyObject.content[type].examples = Object.entries(xExamples).reduce((acc, [key, exampleValue]) => {
761
- acc[key] = wrapAsExampleObject(exampleValue);
762
- return acc;
763
- }, {});
761
+ // Decided per key, so a key that belongs to another media type never drops the named ones
762
+ else if (isNonEmptyObject(xExamples)) {
763
+ const namedExamples = Object.entries(xExamples).filter(([key]) => !isMediaTypeKey(key, consumes));
764
+ if (namedExamples.length) {
765
+ requestBodyObject.content[type].examples = namedExamples.reduce((acc, [key, exampleValue]) => {
766
+ acc[key] = wrapAsExampleObject(exampleValue);
767
+ return acc;
768
+ }, {});
769
+ }
764
770
  }
765
771
  // Handle x-example (singular) only when we did not set examples for this type
766
772
  // (avoids having both example and examples on the same media type)
package/package.json CHANGED
@@ -20,7 +20,7 @@
20
20
  "openapi 3.0",
21
21
  "openapi 3.1"
22
22
  ],
23
- "version": "0.4.2",
23
+ "version": "0.4.3",
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.16.1",
74
+ "@scalar/helpers": "0.16.2",
75
75
  "@scalar/openapi-types": "0.9.7"
76
76
  },
77
77
  "devDependencies": {
78
- "@scalar/types": "0.23.0",
78
+ "@scalar/types": "0.23.1",
79
79
  "vite": "8.1.5"
80
80
  },
81
81
  "scripts": {