svelte-reflector 2.1.5 → 2.1.6

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.
@@ -18,6 +18,35 @@ import type { ComponentsObject, PathsObject } from "../../types/open-api-spec.in
18
18
  */
19
19
  export declare class InlineSchemaPromoter {
20
20
  static promote(components: ComponentsObject, paths: PathsObject): void;
21
+ /**
22
+ * Recursively promotes inline object and array-of-object schemas nested
23
+ * inside named component schemas to their own named components, replacing
24
+ * them with a `$ref`.
25
+ *
26
+ * The rest of the pipeline already resolves `$ref` correctly (`ObjectProp`
27
+ * for object properties, `ArrayProp.getType` for array items), but it
28
+ * silently DROPS inline objects (`SchemaPropertyClassifier.classify` returns
29
+ * `null` for `type: "object"` without `$ref`) and DEGRADES inline
30
+ * arrays-of-object to `string[]` (`array.property.ts` fallback). Promoting
31
+ * them upfront routes both through the working `$ref` path, producing typed
32
+ * child classes instead of `string[]` / dropped fields.
33
+ *
34
+ * Runs after body/response promotion so it also covers the freshly promoted
35
+ * envelope schemas. Uses a worklist so newly created child schemas are
36
+ * themselves scanned for deeper nesting.
37
+ */
38
+ private static promoteNestedObjects;
39
+ /** Inline object with own `properties` (not a free-form `additionalProperties` map). */
40
+ private static extractableInlineObject;
41
+ /** `type: array` whose `items` is an inline object with `properties`. */
42
+ private static extractableInlineArrayItems;
43
+ private static reserveNestedName;
44
+ /**
45
+ * OpenAPI 3.0 forbids siblings next to `$ref`, so a nullable object property
46
+ * must wrap the ref in `allOf` + `nullable` — the form `classifyObject`
47
+ * already understands. Non-nullable properties use a bare `$ref`.
48
+ */
49
+ private static refForProperty;
21
50
  private static promoteBodies;
22
51
  private static promoteInlineBody;
23
52
  private static promoteResponses;
@@ -1,4 +1,4 @@
1
- import { capitalizeFirstLetter } from "../../helpers/helpers.js";
1
+ import { capitalizeFirstLetter, isReferenceObject } from "../../helpers/helpers.js";
2
2
  const HTTP_METHODS = ["get", "put", "post", "delete", "patch", "options", "head", "trace"];
3
3
  /**
4
4
  * Promotes inline request-body and response `data` schemas to named
@@ -21,6 +21,92 @@ export class InlineSchemaPromoter {
21
21
  static promote(components, paths) {
22
22
  InlineSchemaPromoter.promoteBodies(components, paths);
23
23
  InlineSchemaPromoter.promoteResponses(components, paths);
24
+ InlineSchemaPromoter.promoteNestedObjects(components);
25
+ }
26
+ /**
27
+ * Recursively promotes inline object and array-of-object schemas nested
28
+ * inside named component schemas to their own named components, replacing
29
+ * them with a `$ref`.
30
+ *
31
+ * The rest of the pipeline already resolves `$ref` correctly (`ObjectProp`
32
+ * for object properties, `ArrayProp.getType` for array items), but it
33
+ * silently DROPS inline objects (`SchemaPropertyClassifier.classify` returns
34
+ * `null` for `type: "object"` without `$ref`) and DEGRADES inline
35
+ * arrays-of-object to `string[]` (`array.property.ts` fallback). Promoting
36
+ * them upfront routes both through the working `$ref` path, producing typed
37
+ * child classes instead of `string[]` / dropped fields.
38
+ *
39
+ * Runs after body/response promotion so it also covers the freshly promoted
40
+ * envelope schemas. Uses a worklist so newly created child schemas are
41
+ * themselves scanned for deeper nesting.
42
+ */
43
+ static promoteNestedObjects(components) {
44
+ const schemas = components.schemas;
45
+ if (!schemas)
46
+ return;
47
+ const usedNames = new Set(Object.keys(schemas));
48
+ const queue = Object.keys(schemas);
49
+ while (queue.length > 0) {
50
+ const schemaName = queue.shift();
51
+ const schema = schemas[schemaName];
52
+ if (!schema || "$ref" in schema || !schema.properties)
53
+ continue;
54
+ for (const [propKey, propValue] of Object.entries(schema.properties)) {
55
+ if (isReferenceObject(propValue))
56
+ continue;
57
+ const inlineObject = InlineSchemaPromoter.extractableInlineObject(propValue);
58
+ if (inlineObject) {
59
+ const name = InlineSchemaPromoter.reserveNestedName(usedNames, schemaName, propKey);
60
+ schemas[name] = inlineObject;
61
+ queue.push(name);
62
+ schema.properties[propKey] = InlineSchemaPromoter.refForProperty(name, propValue.nullable);
63
+ continue;
64
+ }
65
+ const inlineItems = InlineSchemaPromoter.extractableInlineArrayItems(propValue);
66
+ if (inlineItems) {
67
+ const name = InlineSchemaPromoter.reserveNestedName(usedNames, schemaName, propKey);
68
+ schemas[name] = inlineItems;
69
+ queue.push(name);
70
+ propValue.items = { $ref: `#/components/schemas/${name}` };
71
+ }
72
+ }
73
+ }
74
+ }
75
+ /** Inline object with own `properties` (not a free-form `additionalProperties` map). */
76
+ static extractableInlineObject(value) {
77
+ if (value.type !== "object" || value.additionalProperties || !value.properties)
78
+ return null;
79
+ return value;
80
+ }
81
+ /** `type: array` whose `items` is an inline object with `properties`. */
82
+ static extractableInlineArrayItems(value) {
83
+ if (value.type !== "array" || !value.items || isReferenceObject(value.items))
84
+ return null;
85
+ const items = value.items;
86
+ if (items.type !== "object" || items.additionalProperties || !items.properties)
87
+ return null;
88
+ return items;
89
+ }
90
+ static reserveNestedName(usedNames, parent, prop) {
91
+ const base = `${parent}${capitalizeFirstLetter(prop.replaceAll(/[^a-zA-Z0-9]/g, ""))}`;
92
+ let name = base;
93
+ let suffix = 2;
94
+ while (usedNames.has(name)) {
95
+ name = `${base}${suffix++}`;
96
+ }
97
+ usedNames.add(name);
98
+ return name;
99
+ }
100
+ /**
101
+ * OpenAPI 3.0 forbids siblings next to `$ref`, so a nullable object property
102
+ * must wrap the ref in `allOf` + `nullable` — the form `classifyObject`
103
+ * already understands. Non-nullable properties use a bare `$ref`.
104
+ */
105
+ static refForProperty(name, nullable) {
106
+ const ref = { $ref: `#/components/schemas/${name}` };
107
+ if (nullable)
108
+ return { allOf: [ref], nullable: true };
109
+ return ref;
24
110
  }
25
111
  static promoteBodies(components, paths) {
26
112
  components.schemas ??= {};
@@ -69,7 +69,10 @@ export class PrimitiveProp {
69
69
  };
70
70
  if (type === "string")
71
71
  return {
72
- example: `"${example}"`,
72
+ // JSON.stringify escapes embedded quotes/backslashes/newlines so a
73
+ // string example like `{"64":true}` doesn't emit invalid JS. For
74
+ // plain strings the output is identical to `"${example}"`.
75
+ example: JSON.stringify(String(example)),
73
76
  emptyExample,
74
77
  };
75
78
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-reflector",
3
- "version": "2.1.5",
3
+ "version": "2.1.6",
4
4
  "description": "Reflects zod types from openAPI schemas",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",