svelte-reflector 2.1.5 → 2.1.7
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/dist/core/openapi/InlineSchemaPromoter.d.ts +29 -0
- package/dist/core/openapi/InlineSchemaPromoter.js +102 -1
- package/dist/core/schema/ArraySchemaRenderer.d.ts +18 -0
- package/dist/core/schema/ArraySchemaRenderer.js +39 -0
- package/dist/core/schema/Schema.d.ts +21 -0
- package/dist/core/schema/Schema.js +59 -0
- package/dist/core/schema/SchemaRegistry.js +7 -1
- package/dist/props/primitive.property.js +4 -1
- package/package.json +1 -1
|
@@ -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,107 @@ 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)
|
|
53
|
+
continue;
|
|
54
|
+
// Array-root schema (e.g. a promoted `data: array` response): promote its
|
|
55
|
+
// inline-object items to a named `${schemaName}Item` so they get a typed
|
|
56
|
+
// child schema instead of staying inline and degrading to `string[]`.
|
|
57
|
+
if (!schema.properties && schema.type === "array") {
|
|
58
|
+
const inlineItems = InlineSchemaPromoter.extractableInlineArrayItems(schema);
|
|
59
|
+
if (inlineItems) {
|
|
60
|
+
const name = InlineSchemaPromoter.reserveNestedName(usedNames, schemaName, "item");
|
|
61
|
+
schemas[name] = inlineItems;
|
|
62
|
+
queue.push(name);
|
|
63
|
+
schema.items = { $ref: `#/components/schemas/${name}` };
|
|
64
|
+
}
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (!schema.properties)
|
|
68
|
+
continue;
|
|
69
|
+
for (const [propKey, propValue] of Object.entries(schema.properties)) {
|
|
70
|
+
if (isReferenceObject(propValue))
|
|
71
|
+
continue;
|
|
72
|
+
const inlineObject = InlineSchemaPromoter.extractableInlineObject(propValue);
|
|
73
|
+
if (inlineObject) {
|
|
74
|
+
const name = InlineSchemaPromoter.reserveNestedName(usedNames, schemaName, propKey);
|
|
75
|
+
schemas[name] = inlineObject;
|
|
76
|
+
queue.push(name);
|
|
77
|
+
schema.properties[propKey] = InlineSchemaPromoter.refForProperty(name, propValue.nullable);
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
const inlineItems = InlineSchemaPromoter.extractableInlineArrayItems(propValue);
|
|
81
|
+
if (inlineItems) {
|
|
82
|
+
const name = InlineSchemaPromoter.reserveNestedName(usedNames, schemaName, propKey);
|
|
83
|
+
schemas[name] = inlineItems;
|
|
84
|
+
queue.push(name);
|
|
85
|
+
propValue.items = { $ref: `#/components/schemas/${name}` };
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/** Inline object with own `properties` (not a free-form `additionalProperties` map). */
|
|
91
|
+
static extractableInlineObject(value) {
|
|
92
|
+
if (value.type !== "object" || value.additionalProperties || !value.properties)
|
|
93
|
+
return null;
|
|
94
|
+
return value;
|
|
95
|
+
}
|
|
96
|
+
/** `type: array` whose `items` is an inline object with `properties`. */
|
|
97
|
+
static extractableInlineArrayItems(value) {
|
|
98
|
+
if (value.type !== "array" || !value.items || isReferenceObject(value.items))
|
|
99
|
+
return null;
|
|
100
|
+
const items = value.items;
|
|
101
|
+
if (items.type !== "object" || items.additionalProperties || !items.properties)
|
|
102
|
+
return null;
|
|
103
|
+
return items;
|
|
104
|
+
}
|
|
105
|
+
static reserveNestedName(usedNames, parent, prop) {
|
|
106
|
+
const base = `${parent}${capitalizeFirstLetter(prop.replaceAll(/[^a-zA-Z0-9]/g, ""))}`;
|
|
107
|
+
let name = base;
|
|
108
|
+
let suffix = 2;
|
|
109
|
+
while (usedNames.has(name)) {
|
|
110
|
+
name = `${base}${suffix++}`;
|
|
111
|
+
}
|
|
112
|
+
usedNames.add(name);
|
|
113
|
+
return name;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* OpenAPI 3.0 forbids siblings next to `$ref`, so a nullable object property
|
|
117
|
+
* must wrap the ref in `allOf` + `nullable` — the form `classifyObject`
|
|
118
|
+
* already understands. Non-nullable properties use a bare `$ref`.
|
|
119
|
+
*/
|
|
120
|
+
static refForProperty(name, nullable) {
|
|
121
|
+
const ref = { $ref: `#/components/schemas/${name}` };
|
|
122
|
+
if (nullable)
|
|
123
|
+
return { allOf: [ref], nullable: true };
|
|
124
|
+
return ref;
|
|
24
125
|
}
|
|
25
126
|
static promoteBodies(components, paths) {
|
|
26
127
|
components.schemas ??= {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renders the wrapper class + interface alias for an array-root schema — a
|
|
3
|
+
* component whose top level is `type: array` (e.g. a promoted `data: array`
|
|
4
|
+
* response envelope). Unlike object schemas (handled by `SchemaClassRenderer`),
|
|
5
|
+
* the interface is a bare array alias (`type XInterface = ItemInterface[]`)
|
|
6
|
+
* because the response envelope's `data` is already unwrapped: the consumer
|
|
7
|
+
* calls `new X({ data: response })` with `response` being the array itself.
|
|
8
|
+
*/
|
|
9
|
+
export declare class ArraySchemaRenderer {
|
|
10
|
+
static render(params: {
|
|
11
|
+
name: string;
|
|
12
|
+
elementType: string;
|
|
13
|
+
isRef: boolean;
|
|
14
|
+
}): {
|
|
15
|
+
interface: string;
|
|
16
|
+
schema: string;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renders the wrapper class + interface alias for an array-root schema — a
|
|
3
|
+
* component whose top level is `type: array` (e.g. a promoted `data: array`
|
|
4
|
+
* response envelope). Unlike object schemas (handled by `SchemaClassRenderer`),
|
|
5
|
+
* the interface is a bare array alias (`type XInterface = ItemInterface[]`)
|
|
6
|
+
* because the response envelope's `data` is already unwrapped: the consumer
|
|
7
|
+
* calls `new X({ data: response })` with `response` being the array itself.
|
|
8
|
+
*/
|
|
9
|
+
export class ArraySchemaRenderer {
|
|
10
|
+
static render(params) {
|
|
11
|
+
const { name, elementType, isRef } = params;
|
|
12
|
+
const interfaceElement = isRef ? `${elementType}Interface` : elementType;
|
|
13
|
+
const interfaceAlias = `export type ${name}Interface = ${interfaceElement}[]`;
|
|
14
|
+
const constructorBody = isRef
|
|
15
|
+
? `this.data = params?.data?.map((item) => new ${elementType}({ data: item })) ?? []`
|
|
16
|
+
: `this.data = params?.data ?? []`;
|
|
17
|
+
const staticBody = isRef
|
|
18
|
+
? `return data.map((item) => new ${elementType}({ data: item }))`
|
|
19
|
+
: `return data`;
|
|
20
|
+
const bundleBody = isRef ? `return this.data.map((item) => item.bundle())` : `return this.data`;
|
|
21
|
+
const schema = `
|
|
22
|
+
export class ${name} {
|
|
23
|
+
data = $state<${elementType}[]>([]);
|
|
24
|
+
|
|
25
|
+
constructor(params?: { data?: ${name}Interface | undefined, empty?: boolean }) {
|
|
26
|
+
${constructorBody}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static from(data: ${name}Interface) {
|
|
30
|
+
${staticBody}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
bundle(): ${name}Interface {
|
|
34
|
+
${bundleBody}
|
|
35
|
+
}
|
|
36
|
+
};`;
|
|
37
|
+
return { interface: interfaceAlias, schema };
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -19,6 +19,27 @@ export declare class Schema {
|
|
|
19
19
|
readonly customTypeDeps: string[];
|
|
20
20
|
schema: string;
|
|
21
21
|
interface: string;
|
|
22
|
+
/**
|
|
23
|
+
* Builds a Schema for an array-root component (top-level `type: array`), e.g.
|
|
24
|
+
* a promoted `data: array` response envelope. Renders a wrapper class whose
|
|
25
|
+
* `data` is `Item[]` (hydrated `new Item({ data })` when items are a `$ref`)
|
|
26
|
+
* and whose interface is a bare array alias. Object-root schemas use the
|
|
27
|
+
* regular constructor instead.
|
|
28
|
+
*/
|
|
29
|
+
static forArrayRoot(params: {
|
|
30
|
+
name: string;
|
|
31
|
+
items: SchemaObject | ReferenceObject;
|
|
32
|
+
context: CodegenContext;
|
|
33
|
+
}): Schema;
|
|
34
|
+
/**
|
|
35
|
+
* Resolves the element type of an array-root schema. Inline-object items are
|
|
36
|
+
* already promoted to a `$ref` by `InlineSchemaPromoter`, so by here items is
|
|
37
|
+
* a `$ref`, an enum, a free-form object (→ `Record<string, unknown>`), or a
|
|
38
|
+
* primitive. `ArrayProp` is reused for ref/enum/primitive (it also registers
|
|
39
|
+
* the enum type in the context); the free-form object is special-cased
|
|
40
|
+
* because `ArrayProp.getType` would degrade it to `string`.
|
|
41
|
+
*/
|
|
42
|
+
private static resolveArrayElement;
|
|
22
43
|
constructor(params: {
|
|
23
44
|
properties: Record<string, SchemaObject | ReferenceObject>;
|
|
24
45
|
name: string;
|
|
@@ -2,9 +2,11 @@ import { ArrayProp } from "../../props/array.property.js";
|
|
|
2
2
|
import { EnumProp } from "../../props/enum.property.js";
|
|
3
3
|
import { ObjectProp } from "../../props/object.property.js";
|
|
4
4
|
import { PrimitiveProp } from "../../props/primitive.property.js";
|
|
5
|
+
import { isReferenceObject } from "../../helpers/helpers.js";
|
|
5
6
|
import { SchemaPropertyClassifier } from "./SchemaPropertyClassifier.js";
|
|
6
7
|
import { SchemaDependencyCollector } from "./SchemaDependencyCollector.js";
|
|
7
8
|
import { SchemaClassRenderer } from "./SchemaClassRenderer.js";
|
|
9
|
+
import { ArraySchemaRenderer } from "./ArraySchemaRenderer.js";
|
|
8
10
|
export class Schema {
|
|
9
11
|
name;
|
|
10
12
|
primitiveProps = [];
|
|
@@ -19,6 +21,63 @@ export class Schema {
|
|
|
19
21
|
customTypeDeps;
|
|
20
22
|
schema;
|
|
21
23
|
interface;
|
|
24
|
+
/**
|
|
25
|
+
* Builds a Schema for an array-root component (top-level `type: array`), e.g.
|
|
26
|
+
* a promoted `data: array` response envelope. Renders a wrapper class whose
|
|
27
|
+
* `data` is `Item[]` (hydrated `new Item({ data })` when items are a `$ref`)
|
|
28
|
+
* and whose interface is a bare array alias. Object-root schemas use the
|
|
29
|
+
* regular constructor instead.
|
|
30
|
+
*/
|
|
31
|
+
static forArrayRoot(params) {
|
|
32
|
+
const { name, items, context } = params;
|
|
33
|
+
const element = Schema.resolveArrayElement(items, name, context);
|
|
34
|
+
const schema = Object.create(Schema.prototype);
|
|
35
|
+
schema.name = name;
|
|
36
|
+
schema.primitiveProps = [];
|
|
37
|
+
schema.arrayProps = [];
|
|
38
|
+
schema.objectProps = [];
|
|
39
|
+
schema.enumProps = [];
|
|
40
|
+
schema.schemaDeps = element.kind === "ref" ? [element.type] : [];
|
|
41
|
+
schema.enumDeps = element.kind === "enum" ? [element.type] : [];
|
|
42
|
+
schema.customTypeDeps = [];
|
|
43
|
+
const rendered = ArraySchemaRenderer.render({
|
|
44
|
+
name,
|
|
45
|
+
elementType: element.type,
|
|
46
|
+
isRef: element.kind === "ref",
|
|
47
|
+
});
|
|
48
|
+
schema.interface = rendered.interface;
|
|
49
|
+
schema.schema = rendered.schema;
|
|
50
|
+
return schema;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Resolves the element type of an array-root schema. Inline-object items are
|
|
54
|
+
* already promoted to a `$ref` by `InlineSchemaPromoter`, so by here items is
|
|
55
|
+
* a `$ref`, an enum, a free-form object (→ `Record<string, unknown>`), or a
|
|
56
|
+
* primitive. `ArrayProp` is reused for ref/enum/primitive (it also registers
|
|
57
|
+
* the enum type in the context); the free-form object is special-cased
|
|
58
|
+
* because `ArrayProp.getType` would degrade it to `string`.
|
|
59
|
+
*/
|
|
60
|
+
static resolveArrayElement(items, name, context) {
|
|
61
|
+
if (isReferenceObject(items)) {
|
|
62
|
+
return { kind: "ref", type: items.$ref.split("/").at(-1) };
|
|
63
|
+
}
|
|
64
|
+
if (!items.enum && items.type === "object") {
|
|
65
|
+
return { kind: "raw", type: "Record<string, unknown>" };
|
|
66
|
+
}
|
|
67
|
+
const prop = new ArrayProp({
|
|
68
|
+
name: "data",
|
|
69
|
+
schemaObject: { type: "array", items },
|
|
70
|
+
schemaName: name,
|
|
71
|
+
required: true,
|
|
72
|
+
isParam: undefined,
|
|
73
|
+
isEnum: !!items.enum,
|
|
74
|
+
isNullable: false,
|
|
75
|
+
context,
|
|
76
|
+
});
|
|
77
|
+
if (items.enum)
|
|
78
|
+
return { kind: "enum", type: prop.type };
|
|
79
|
+
return { kind: "raw", type: prop.type };
|
|
80
|
+
}
|
|
22
81
|
constructor(params) {
|
|
23
82
|
const { name, isEmpty, properties, requireds, fieldConfigs, context } = params;
|
|
24
83
|
this.name = `${isEmpty ? "Empty" : ""}${name}`;
|
|
@@ -10,7 +10,13 @@ export class SchemaRegistry {
|
|
|
10
10
|
if (!componentSchemas)
|
|
11
11
|
return;
|
|
12
12
|
for (const [key, object] of Object.entries(componentSchemas)) {
|
|
13
|
-
if (isReferenceObject(object)
|
|
13
|
+
if (isReferenceObject(object))
|
|
14
|
+
continue;
|
|
15
|
+
if (object.type === "array" && object.items) {
|
|
16
|
+
this.schemas.push(Schema.forArrayRoot({ name: key, items: object.items, context }));
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
if (!object.properties)
|
|
14
20
|
continue;
|
|
15
21
|
const properties = object.properties;
|
|
16
22
|
const schema = {
|
|
@@ -69,7 +69,10 @@ export class PrimitiveProp {
|
|
|
69
69
|
};
|
|
70
70
|
if (type === "string")
|
|
71
71
|
return {
|
|
72
|
-
|
|
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 {
|