Skip to content

Commit 26a9c0d

Browse files
committed
fix: JSON schema had incorrect value types
Resolves #1389
1 parent 80c4524 commit 26a9c0d

File tree

18 files changed

+203
-306
lines changed

18 files changed

+203
-306
lines changed

src/lib/converter/symbols.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { convertDefaultValue } from "./convert-expression";
2121
import { ConverterEvents } from "./converter-events";
2222
import { createSignature } from "./factories/signature";
2323

24-
// TODO: implementationOf / overwrites
2524
// TODO: Index signatures
2625

2726
function getSymbolExportsWithFlag(symbol: ts.Symbol, flag: ts.SymbolFlags) {

src/lib/serialization/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export {
1717
IntrinsicTypeSerializer,
1818
LiteralTypeSerializer,
1919
ParameterReflectionSerializer,
20-
ProjectReflectionSerializer,
2120
ReferenceTypeSerializer,
2221
ReflectionCategorySerializer,
2322
ReflectionGroupSerializer,

src/lib/serialization/schema.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,15 @@ export interface SignatureReflection
143143
extends Reflection,
144144
S<
145145
M.SignatureReflection,
146-
"type" | "overwrites" | "inheritedFrom" | "implementationOf"
147-
> {}
146+
| "parameters"
147+
| "type"
148+
| "overwrites"
149+
| "inheritedFrom"
150+
| "implementationOf"
151+
> {
152+
// Weird not to call this typeParameters... preserving backwards compatibility for now.
153+
typeParameter?: ModelToObject<M.SignatureReflection["typeParameters"]>;
154+
}
148155

149156
export interface ParameterReflection
150157
extends Reflection,
@@ -155,15 +162,26 @@ export interface DeclarationReflection
155162
S<
156163
M.DeclarationReflection,
157164
| "type"
165+
| "signatures"
166+
| "indexSignature"
158167
| "defaultValue"
159168
| "overwrites"
160169
| "inheritedFrom"
170+
| "implementationOf"
161171
| "extendedTypes"
162172
| "extendedBy"
163173
| "implementedTypes"
164174
| "implementedBy"
165-
| "implementationOf"
166-
> {}
175+
> {
176+
// Weird not to call this typeParameters... preserving backwards compatibility for now.
177+
typeParameter?: ModelToObject<M.DeclarationReflection["typeParameters"]>;
178+
179+
// Yep... backwards compatibility. This is an optional one-tuple.
180+
getSignature?: [ModelToObject<M.DeclarationReflection["getSignature"]>];
181+
182+
// Yep... backwards compatibility. This is an optional one-tuple.
183+
setSignature?: [ModelToObject<M.DeclarationReflection["setSignature"]>];
184+
}
167185

168186
export interface TypeParameterReflection
169187
extends Reflection,
@@ -174,32 +192,19 @@ export interface ProjectReflection extends ContainerReflection {}
174192

175193
export interface ContainerReflection
176194
extends Reflection,
177-
S<M.ContainerReflection, "groups" | "categories"> {
195+
S<M.ContainerReflection, "children" | "groups" | "categories"> {
178196
sources?: ModelToObject<SourceReferenceWrapper[]>;
179197
}
180198

181-
/**
182-
* If a 3rd party serializer creates a loop when serializing, a pointer will be created
183-
* instead of re-serializing the [[DeclarationReflection]]
184-
*/
185-
export interface ReflectionPointer extends S<M.Reflection, "id"> {}
186-
187199
export interface Reflection
188200
extends S<
189201
M.Reflection,
190202
"id" | "name" | "kind" | "kindString" | "comment" | "decorates"
191203
> {
204+
/** Will not be present if name === originalName */
192205
originalName?: M.Reflection["originalName"];
193206
flags: ReflectionFlags;
194207
decorators?: ModelToObject<DecoratorWrapper[]>;
195-
196-
children?: ModelToObject<M.Reflection>[];
197-
parameters?: ModelToObject<M.Reflection>[];
198-
typeParameter?: ModelToObject<M.Reflection>[];
199-
signatures?: ModelToObject<M.Reflection>[];
200-
indexSignature?: ModelToObject<M.Reflection>[];
201-
getSignature?: ModelToObject<M.Reflection>[];
202-
setSignature?: ModelToObject<M.Reflection>[];
203208
}
204209

205210
// Types

src/lib/serialization/serializer.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ export class Serializer extends EventDispatcher {
4444
}
4545

4646
toObject<T>(value: T, init: object = {}): ModelToObject<T> {
47+
if (value == null || typeof value !== "object") {
48+
return value as any; // Serializing some primitive
49+
}
50+
51+
if (Array.isArray(value)) {
52+
if (value.length === 0) {
53+
return undefined as any;
54+
}
55+
return value.map((val) => this.toObject(val)) as any;
56+
}
57+
4758
// Note: This type *could* potentially lie, if a serializer declares a partial type but fails to provide
4859
// the defined property, but the benefit of being mostly typed is probably worth it.
4960
// TypeScript errors out if init is correctly typed as `Partial<ModelToObject<T>>`
@@ -117,7 +128,6 @@ const serializerComponents: (new (owner: Serializer) => SerializerComponent<
117128
S.ContainerReflectionSerializer,
118129
S.DeclarationReflectionSerializer,
119130
S.ParameterReflectionSerializer,
120-
S.ProjectReflectionSerializer,
121131
S.SignatureReflectionSerializer,
122132
S.TypeParameterReflectionSerializer,
123133

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Reflection, TraverseProperty } from "../../../models";
1+
import { Reflection } from "../../../models";
22

33
import { ReflectionSerializerComponent } from "../../components";
44
import { DecoratorWrapper } from "../models";
5-
import { Reflection as JSONReflection } from "../../schema";
5+
import type { Reflection as JSONReflection } from "../../schema";
66

77
export class ReflectionSerializer extends ReflectionSerializerComponent<
88
Reflection
@@ -24,16 +24,17 @@ export class ReflectionSerializer extends ReflectionSerializerComponent<
2424
kind: reflection.kind,
2525
kindString: reflection.kindString,
2626
flags: {},
27+
comment: this.owner.toObject(reflection.comment),
28+
decorates: this.owner.toObject(reflection.decorates),
29+
decorators: this.owner.toObject(
30+
reflection.decorators?.map((d) => new DecoratorWrapper(d))
31+
),
2732
};
2833

2934
if (reflection.originalName !== reflection.name) {
3035
result.originalName = reflection.originalName;
3136
}
3237

33-
if (reflection.comment) {
34-
result.comment = this.owner.toObject(reflection.comment);
35-
}
36-
3738
const flags = [
3839
"isPrivate",
3940
"isProtected",
@@ -56,30 +57,6 @@ export class ReflectionSerializer extends ReflectionSerializerComponent<
5657
}
5758
}
5859

59-
if (reflection.decorates && reflection.decorates.length > 0) {
60-
result.decorates = reflection.decorates.map((t) =>
61-
this.owner.toObject(t)
62-
);
63-
}
64-
65-
if (reflection.decorators && reflection.decorators.length > 0) {
66-
result.decorators = reflection.decorators.map((d) =>
67-
this.owner.toObject(new DecoratorWrapper(d))
68-
);
69-
}
70-
71-
reflection.traverse((child, property) => {
72-
if (property === TraverseProperty.TypeLiteral) {
73-
return;
74-
}
75-
let name = TraverseProperty[property];
76-
name = name[0].toLowerCase() + name.substr(1);
77-
if (!(result as any)[name]) {
78-
(result as any)[name] = [];
79-
}
80-
(result as any)[name].push(this.owner.toObject(child));
81-
});
82-
8360
return result;
8461
}
8562
}

src/lib/serialization/serializers/reflections/container.ts

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,14 @@ export class ContainerReflectionSerializer extends ReflectionSerializerComponent
2323
container: ContainerReflection,
2424
obj: JSONReflection
2525
): JSONContainerReflection {
26-
const result: JSONContainerReflection = {
26+
return {
2727
...obj,
28+
children: this.owner.toObject(container.children),
29+
groups: this.owner.toObject(container.groups),
30+
categories: this.owner.toObject(container.categories),
31+
sources: this.owner.toObject(
32+
container.sources?.map((s) => new SourceReferenceWrapper(s))
33+
),
2834
};
29-
30-
if (container.groups && container.groups.length > 0) {
31-
result.groups = container.groups.map((group) =>
32-
this.owner.toObject(group)
33-
);
34-
}
35-
36-
if (container.categories && container.categories.length > 0) {
37-
result.categories = container.categories.map((category) =>
38-
this.owner.toObject(category)
39-
);
40-
}
41-
42-
if (container.sources && container.sources.length > 0) {
43-
result.sources = container.sources.map((source) =>
44-
this.owner.toObject(
45-
new SourceReferenceWrapper({
46-
fileName: source.fileName,
47-
line: source.line,
48-
character: source.character,
49-
})
50-
)
51-
);
52-
}
53-
54-
return result;
5535
}
5636
}

src/lib/serialization/serializers/reflections/declaration.ts

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,61 +17,33 @@ export class DeclarationReflectionSerializer extends ReflectionSerializerCompone
1717
}
1818

1919
toObject(
20-
declaration: DeclarationReflection,
20+
d: DeclarationReflection,
2121
obj: JSONContainerReflection
2222
): JSONDeclarationReflection {
2323
const result: JSONDeclarationReflection = {
2424
...obj,
25+
typeParameter: this.owner.toObject(d.typeParameters),
26+
type: this.owner.toObject(d.type),
27+
signatures: this.owner.toObject(d.signatures),
28+
indexSignature: this.owner.toObject(d.indexSignature),
2529
};
2630

27-
if (declaration.type) {
28-
result.type = this.owner.toObject(declaration.type);
31+
if (d.getSignature) {
32+
result.getSignature = [this.owner.toObject(d.getSignature)];
2933
}
30-
31-
if (declaration.defaultValue) {
32-
result.defaultValue = declaration.defaultValue;
33-
}
34-
35-
if (declaration.overwrites) {
36-
result.overwrites = this.owner.toObject(declaration.overwrites);
37-
}
38-
39-
if (declaration.inheritedFrom) {
40-
result.inheritedFrom = this.owner.toObject(
41-
declaration.inheritedFrom
42-
);
43-
}
44-
45-
if (declaration.extendedTypes) {
46-
result.extendedTypes = declaration.extendedTypes.map((t) =>
47-
this.owner.toObject(t)
48-
);
49-
}
50-
51-
if (declaration.extendedBy) {
52-
result.extendedBy = declaration.extendedBy.map((t) =>
53-
this.owner.toObject(t)
54-
);
55-
}
56-
57-
if (declaration.implementedTypes) {
58-
result.implementedTypes = declaration.implementedTypes.map((t) =>
59-
this.owner.toObject(t)
60-
);
61-
}
62-
63-
if (declaration.implementedBy) {
64-
result.implementedBy = declaration.implementedBy.map((t) =>
65-
this.owner.toObject(t)
66-
);
67-
}
68-
69-
if (declaration.implementationOf) {
70-
result.implementationOf = this.owner.toObject(
71-
declaration.implementationOf
72-
);
34+
if (d.setSignature) {
35+
result.setSignature = [this.owner.toObject(d.setSignature)];
7336
}
7437

75-
return result;
38+
return Object.assign(result, {
39+
defaultValue: this.owner.toObject(d.defaultValue),
40+
overwrites: this.owner.toObject(d.overwrites),
41+
inheritedFrom: this.owner.toObject(d.inheritedFrom),
42+
implementationOf: this.owner.toObject(d.implementationOf),
43+
extendedTypes: this.owner.toObject(d.extendedTypes),
44+
extendedBy: this.owner.toObject(d.extendedBy),
45+
implementedTypes: this.owner.toObject(d.implementedTypes),
46+
implementedBy: this.owner.toObject(d.implementedBy),
47+
});
7648
}
7749
}

src/lib/serialization/serializers/reflections/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ export * from "./abstract";
22
export * from "./container";
33
export * from "./declaration";
44
export * from "./parameter";
5-
export * from "./project";
65
export * from "./reference";
76
export * from "./signature";
87
export * from "./type-parameter";

src/lib/serialization/serializers/reflections/parameter.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,10 @@ export class ParameterReflectionSerializer extends ReflectionSerializerComponent
1919
): JSONParameterReflection {
2020
const result: JSONParameterReflection = {
2121
...obj,
22+
type: this.owner.toObject(parameter.type),
23+
defaultValue: this.owner.toObject(parameter.defaultValue),
2224
};
2325

24-
if (parameter.type) {
25-
result.type = this.owner.toObject(parameter.type);
26-
}
27-
28-
if (parameter.defaultValue) {
29-
result.defaultValue = parameter.defaultValue;
30-
}
31-
3226
return result;
3327
}
3428
}

src/lib/serialization/serializers/reflections/project.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/lib/serialization/serializers/reflections/signature.ts

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,14 @@ export class SignatureReflectionSerializer extends ReflectionSerializerComponent
1717
signature: SignatureReflection,
1818
obj: JSONReflection
1919
): JSONSignatureReflection {
20-
const result: JSONSignatureReflection = { ...obj };
21-
22-
if (signature.type) {
23-
result.type = this.owner.toObject(signature.type);
24-
}
25-
26-
if (signature.overwrites) {
27-
result.overwrites = this.owner.toObject(signature.overwrites);
28-
}
29-
30-
if (signature.inheritedFrom) {
31-
result.inheritedFrom = this.owner.toObject(signature.inheritedFrom);
32-
}
33-
34-
if (signature.implementationOf) {
35-
result.implementationOf = this.owner.toObject(
36-
signature.implementationOf
37-
);
38-
}
39-
40-
return result;
20+
return {
21+
...obj,
22+
typeParameter: this.owner.toObject(signature.typeParameters),
23+
parameters: this.owner.toObject(signature.parameters),
24+
type: this.owner.toObject(signature.type),
25+
overwrites: this.owner.toObject(signature.overwrites),
26+
inheritedFrom: this.owner.toObject(signature.inheritedFrom),
27+
implementationOf: this.owner.toObject(signature.implementationOf),
28+
};
4129
}
4230
}

0 commit comments

Comments
 (0)