@scalar/openapi-upgrader 0.2.11 → 0.2.13

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,22 @@
1
1
  # @scalar/openapi-upgrader
2
2
 
3
+ ## 0.2.13
4
+
5
+ ### Patch Changes
6
+
7
+ - [#9827](https://github.com/scalar/scalar/pull/9827): Convert nullable `$ref` patterns to the 3.1 null-union form when upgrading from OpenAPI 3.0 to 3.1
8
+
9
+ ## 0.2.12
10
+
11
+ ### Patch Changes
12
+
13
+ - [#9758](https://github.com/scalar/scalar/pull/9758): fix: stop the 3.0 to 3.1 upgrade from mangling non-schema nodes
14
+
15
+ The upgrade applied its schema transforms to every object node, regardless of where it sat in the document. Three cases are now handled correctly:
16
+ - Members named `example` inside a map of named subschemas (`properties`, `patternProperties`, `$defs`, `definitions`, and `components/schemas`) are names, not the `example` keyword, so they are no longer rewritten into an `examples` array.
17
+ - Data held by the `example`, `default`, `const`, and `enum` keywords, and by an Example Object's `value`, is left untouched instead of being walked as if it were a schema (which turned `nullable: true` into a type array, collapsed `exclusiveMinimum`, renamed a nested `x-webhooks`, and so on).
18
+ - `x-webhooks` is only renamed to `webhooks` at the document root, not wherever a key happens to share that name.
19
+
3
20
  ## 0.2.11
4
21
 
5
22
  ### Patch Changes
package/README.md CHANGED
@@ -20,7 +20,7 @@ Scalar is an open-source API platform for teams who want beautiful developer int
20
20
 
21
21
  - **[API References](https://scalar.com/products/api-references/getting-started)** — Interactive API documentation from OpenAPI and AsyncAPI specs.
22
22
  - **[Developer Docs](https://scalar.com/products/docs/getting-started)** — Write in Markdown/MDX, generate API references, sync with two-way Git.
23
- - **[SDK Generator](https://scalar.com/products/sdks/getting-started)** — Type-safe SDKs and CLIs in TypeScript, Python, Go, PHP, Java, and Ruby.
23
+ - **[SDK Generator](https://scalar.com/products/sdk-generator/getting-started)** — Type-safe SDKs and CLIs in TypeScript, Python, Go, PHP, Java, and Ruby.
24
24
  - **[API Client](https://scalar.com/products/api-client/getting-started)** — Open-source, offline-first Postman alternative built on OpenAPI.
25
25
 
26
26
  20M+ monthly npm installs · 15,500+ GitHub stars · MIT licensed · [scalar.com](https://scalar.com)
@@ -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":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAgBxD,2EAA2E;AAC3E,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,SAAS,GAAG,OAAO,CAsBhE;AAED;;;;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":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAgBxD,2EAA2E;AAC3E,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,SAAS,GAAG,OAAO,CAsBhE;AA8DD;;;;GAIG;AACH,wBAAgB,0BAA0B,CAAC,eAAe,EAAE,aAAa;;;;;;;;;;;;GAcxE"}
@@ -30,6 +30,56 @@ export function isSchemaPath(path) {
30
30
  }
31
31
  return false;
32
32
  }
33
+ // Segments whose value is a map of *named* subschemas. Keys inside these maps are
34
+ // arbitrary names (property names, definition names), never schema keywords.
35
+ const NAMED_SCHEMA_MAP_SEGMENTS = new Set(['properties', 'patternProperties', '$defs', 'definitions']);
36
+ /**
37
+ * Determine if the node at `path` is a map of named subschemas rather than a schema object.
38
+ *
39
+ * This matters because the 3.0 to 3.1 upgrade rewrites the `example` keyword into `examples`.
40
+ * A key called `example` (or `examples`) inside one of these maps is a member *name*, not the
41
+ * schema keyword, so it must be left untouched.
42
+ */
43
+ function isNamedSchemaMap(path) {
44
+ const last = path?.[path.length - 1];
45
+ if (last === undefined) {
46
+ return false;
47
+ }
48
+ // OpenAPI's named-schema map lives at components/schemas
49
+ if (last === 'schemas' && path?.[path.length - 2] === 'components') {
50
+ return true;
51
+ }
52
+ return NAMED_SCHEMA_MAP_SEGMENTS.has(last);
53
+ }
54
+ // Keywords whose value is arbitrary data rather than a schema. The upgrade rules only make
55
+ // sense for schema keywords, so nothing nested under these may be rewritten.
56
+ const DATA_KEYWORDS = new Set(['example', 'default', 'const', 'enum']);
57
+ /**
58
+ * Determine if the node at `path` lives inside a data value rather than a schema.
59
+ *
60
+ * The traversal visits every object node, but a value held by keywords like `example` or
61
+ * `default` is user data, not a schema. Applying the schema transforms to it would mangle it
62
+ * (turning `nullable: true` into a type array, renaming a nested `x-webhooks`, and so on).
63
+ */
64
+ function isInsideDataValue(path) {
65
+ if (!path) {
66
+ return false;
67
+ }
68
+ for (const [index, segment] of path.entries()) {
69
+ // Only treat it as data when the keyword is a real keyword. Inside a named-schema map the
70
+ // same word (e.g. a property literally called `default`) is just a member name.
71
+ if (DATA_KEYWORDS.has(segment) && !isNamedSchemaMap(path.slice(0, index))) {
72
+ return true;
73
+ }
74
+ // The `value` of an Example Object (`examples/<name>/value`) holds arbitrary data too.
75
+ // Require a real examples map two segments up so a property named `value` inside a schema
76
+ // named `examples` is not mistaken for it.
77
+ if (segment === 'value' && path[index - 2] === 'examples' && !isNamedSchemaMap(path.slice(0, index - 2))) {
78
+ return true;
79
+ }
80
+ }
81
+ return false;
82
+ }
33
83
  /**
34
84
  * Upgrade from OpenAPI 3.0.x to 3.1.1
35
85
  *
@@ -47,11 +97,34 @@ export function upgradeFromThreeToThreeOne(originalContent) {
47
97
  return content;
48
98
  }
49
99
  const applyChangesToDocument = (schema, path) => {
100
+ // 0. Bail out inside data values (example, default, const, enum). These hold user data, not a
101
+ // schema, so none of the schema transforms below apply and running them would corrupt the data.
102
+ if (isInsideDataValue(path)) {
103
+ return schema;
104
+ }
50
105
  // 1. Handle nullable types
51
106
  if (schema.type !== undefined && schema.nullable === true) {
52
107
  schema.type = [schema.type, 'null'];
53
108
  delete schema.nullable;
54
109
  }
110
+ // 1b. Handle nullable `$ref` patterns.
111
+ // OpenAPI 3.0 had no way to mark a `$ref` as nullable, so people wrote `nullable: true` next to
112
+ // a `$ref`, or next to an `allOf` wrapping one. `nullable` does not exist in 3.1, so leaving these
113
+ // untouched would silently drop the null option. Rewrite them to the 3.1 null-union form. This only
114
+ // runs when there is no sibling `type` (that case is handled above).
115
+ else if (schema.nullable === true && schema.type === undefined) {
116
+ if (typeof schema.$ref === 'string') {
117
+ const { nullable: _nullable, $ref, ...rest } = schema;
118
+ return { ...rest, anyOf: [{ $ref }, { type: 'null' }] };
119
+ }
120
+ if (Array.isArray(schema.allOf)) {
121
+ const { nullable: _nullable, allOf, ...rest } = schema;
122
+ // A single-member `allOf` unwraps so the union reads `anyOf: [<member>, { type: 'null' }]`.
123
+ const base = allOf.length === 1 ? allOf[0] : { allOf };
124
+ return { ...rest, anyOf: [base, { type: 'null' }] };
125
+ }
126
+ // Otherwise there is nothing for `nullable` to attach to, so leave the schema untouched.
127
+ }
55
128
  // 2. Handle exclusiveMinimum and exclusiveMaximum
56
129
  if (schema.exclusiveMinimum === true) {
57
130
  schema.exclusiveMinimum = schema.minimum;
@@ -70,17 +143,17 @@ const applyChangesToDocument = (schema, path) => {
70
143
  // 3. Handle examples
71
144
  // Skip conversion if we're already inside an examples map to avoid double nesting
72
145
  // Check if 'examples' appears as an exact path segment (not just a substring)
73
- // BUT exclude cases where 'examples' is just a schema property name
146
+ // BUT exclude cases where 'examples' is just a named-schema-map member (e.g. a property named 'examples')
74
147
  const isInsideExamplesMap = path?.some((segment, index) => {
75
- // Only consider it an examples map if 'examples' is not a schema property name
76
148
  if (segment === 'examples' && index > 0) {
77
- const parent = path[index - 1];
78
- // If parent is 'properties', then 'examples' is just a property name, not an examples map
79
- return parent !== 'properties';
149
+ // If the parent node is a named-schema map, 'examples' is a member name, not an examples map
150
+ return !isNamedSchemaMap(path.slice(0, index));
80
151
  }
81
152
  return false;
82
153
  });
83
- if (schema.example !== undefined && !isInsideExamplesMap) {
154
+ // If the current node is itself a map of named subschemas, a key called 'example' is a
155
+ // member name (property, $defs entry, components/schemas entry) and not the schema keyword.
156
+ if (schema.example !== undefined && !isInsideExamplesMap && !isNamedSchemaMap(path)) {
84
157
  if (isSchemaPath(path)) {
85
158
  schema.examples = [schema.example];
86
159
  }
@@ -146,7 +219,9 @@ const applyChangesToDocument = (schema, path) => {
146
219
  }
147
220
  }
148
221
  // 7. Handle x-webhooks
149
- if (schema['x-webhooks'] !== undefined) {
222
+ // `x-webhooks` is a document-root extension, so only rename it there. Anywhere else (a schema
223
+ // property, an example) a key named `x-webhooks` is unrelated and must be left alone.
224
+ if (schema['x-webhooks'] !== undefined && (path === undefined || path.length === 0)) {
150
225
  schema.webhooks = schema['x-webhooks'];
151
226
  delete schema['x-webhooks'];
152
227
  }
package/package.json CHANGED
@@ -20,7 +20,7 @@
20
20
  "openapi 3.0",
21
21
  "openapi 3.1"
22
22
  ],
23
- "version": "0.2.11",
23
+ "version": "0.2.13",
24
24
  "engines": {
25
25
  "node": ">=22"
26
26
  },
@@ -71,12 +71,12 @@
71
71
  },
72
72
  "sideEffects": false,
73
73
  "dependencies": {
74
- "@scalar/openapi-types": "0.9.3"
74
+ "@scalar/openapi-types": "0.9.4"
75
75
  },
76
76
  "devDependencies": {
77
- "vite": "8.0.0",
78
- "@scalar/helpers": "0.9.2",
79
- "@scalar/types": "0.16.4"
77
+ "vite": "8.1.5",
78
+ "@scalar/helpers": "0.10.0",
79
+ "@scalar/types": "0.17.1"
80
80
  },
81
81
  "scripts": {
82
82
  "build": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json",