-
-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
43 changed files
with
540 additions
and
440 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,5 +10,6 @@ module.exports = { | |
reporter: "dot", | ||
spec: [ | ||
"packages/**/*.spec.ts" | ||
] | ||
], | ||
timeout: 3000 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import {JsonLazyRef} from "../domain/JsonLazyRef"; | ||
import {JsonSchemaOptions} from "../interfaces/JsonSchemaOptions"; | ||
import {execMapper, registerJsonSchemaMapper} from "../registries/JsonSchemaMapperContainer"; | ||
import {mapGenericsOptions} from "../utils/generics"; | ||
import {toRef} from "../utils/ref"; | ||
|
||
export function anyMapper(input: any, options: JsonSchemaOptions = {}): any { | ||
options.schemas = options.schemas || {}; | ||
|
||
if (typeof input !== "object" || input === null) { | ||
return input; | ||
} | ||
|
||
if (input instanceof JsonLazyRef) { | ||
return execMapper("lazyRef", input, options); | ||
} | ||
|
||
if ("toJSON" in input) { | ||
const schema = input.toJSON(mapGenericsOptions(options)); | ||
|
||
return input.canRef ? toRef(input, schema, options) : schema; | ||
} | ||
|
||
return execMapper("object", input, options); | ||
} | ||
|
||
registerJsonSchemaMapper("any", anyMapper); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import {JsonEntityStore} from "../domain/JsonEntityStore"; | ||
import {JsonSchema} from "../domain/JsonSchema"; | ||
import {JsonSchemaOptions} from "../interfaces/JsonSchemaOptions"; | ||
import {execMapper, registerJsonSchemaMapper} from "../registries/JsonSchemaMapperContainer"; | ||
import {mapGenericsOptions, popGenerics} from "../utils/generics"; | ||
import {createRef, createRefName} from "../utils/ref"; | ||
|
||
export function classMapper(value: JsonSchema, options: JsonSchemaOptions = {}) { | ||
const store = JsonEntityStore.from(value.class); | ||
const name = createRefName(store.schema.getName() || value.getName(), options); | ||
|
||
if (value.hasGenerics) { | ||
// Inline generic | ||
const {type, properties, additionalProperties, items, ...props} = value.toJSON(options); | ||
const schema = { | ||
...execMapper("any", store.schema, { | ||
...options, | ||
...popGenerics(value), | ||
root: false | ||
}), | ||
...props | ||
}; | ||
|
||
if (schema.title) { | ||
const name = createRefName(schema.title, options); | ||
options.schemas![name] = schema; | ||
delete schema.title; | ||
|
||
return createRef(name, value, options); | ||
} | ||
|
||
return schema; | ||
} | ||
|
||
if (options.schemas && !options.schemas[name]) { | ||
options.schemas[name] = {}; // avoid infinite calls | ||
options.schemas[name] = execMapper( | ||
"any", | ||
store.schema, | ||
mapGenericsOptions({ | ||
...options, | ||
root: false | ||
}) | ||
); | ||
} | ||
|
||
return createRef(name, value, options); | ||
} | ||
|
||
registerJsonSchemaMapper("class", classMapper); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import {isClass, isPrimitiveClass} from "@tsed/core"; | ||
import {JsonEntityStore} from "../domain/JsonEntityStore"; | ||
import {execMapper, registerJsonSchemaMapper} from "../registries/JsonSchemaMapperContainer"; | ||
import {GenericsContext, popGenerics} from "../utils/generics"; | ||
import {getJsonType} from "../utils/getJsonType"; | ||
|
||
/** | ||
* @ignore | ||
*/ | ||
export function genericsMapper(obj: any, options: GenericsContext) { | ||
const {generics} = options; | ||
|
||
if (generics && obj.$ref) { | ||
if (generics.has(obj.$ref)) { | ||
let type = generics.get(obj.$ref); | ||
|
||
if (isPrimitiveClass(type)) { | ||
return { | ||
type: getJsonType(type) | ||
}; | ||
} | ||
|
||
if (type === Date) { | ||
return { | ||
type: "string", | ||
format: "date-time" | ||
}; | ||
} | ||
|
||
if (type.toJSON) { | ||
return type.toJSON({ | ||
...options, | ||
generics: undefined | ||
}); | ||
} | ||
|
||
if (type === Object) { | ||
return { | ||
type: "object" | ||
}; | ||
} | ||
|
||
if (isClass(type)) { | ||
const model = { | ||
class: type | ||
}; | ||
|
||
if (options.nestedGenerics.length === 0) { | ||
return execMapper("class", model as any, { | ||
...options, | ||
generics: undefined | ||
}); | ||
} | ||
|
||
const store = JsonEntityStore.from(model.class); | ||
|
||
return execMapper("schema", store.schema, { | ||
...options, | ||
...popGenerics(options), | ||
root: false | ||
}); | ||
} | ||
} | ||
} | ||
|
||
return obj; | ||
} | ||
|
||
registerJsonSchemaMapper("generics", genericsMapper); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export * from "./anyMapper"; | ||
export * from "./classMapper"; | ||
export * from "./genericsMapper"; | ||
export * from "./inheritedClassMapper"; | ||
export * from "./itemMapper"; | ||
export * from "./lazyRefMapper"; | ||
export * from "./mapMapper"; | ||
export * from "./objectMapper"; | ||
export * from "./schemaMapper"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import {classOf, deepMerge} from "@tsed/core"; | ||
import {JsonSchemaOptions} from "../interfaces/JsonSchemaOptions"; | ||
import {execMapper, registerJsonSchemaMapper} from "../registries/JsonSchemaMapperContainer"; | ||
import {getInheritedStores} from "../utils/getInheritedStores"; | ||
|
||
/** | ||
* @ignore | ||
*/ | ||
export function inheritedClassMapper(obj: any, {target, ...options}: JsonSchemaOptions = {}) { | ||
const stores = Array.from(getInheritedStores(target).entries()).filter(([model]) => classOf(model) !== classOf(target)); | ||
|
||
if (stores.length) { | ||
const schema = stores.reduce((obj, [, store]) => { | ||
return deepMerge(obj, execMapper("schema", store.schema, options)); | ||
}, {}); | ||
|
||
obj = deepMerge(schema, obj); | ||
} | ||
|
||
return obj; | ||
} | ||
|
||
registerJsonSchemaMapper("inheritedClass", inheritedClassMapper); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {JsonSchemaOptions} from "../interfaces/JsonSchemaOptions"; | ||
import {execMapper, registerJsonSchemaMapper} from "../registries/JsonSchemaMapperContainer"; | ||
|
||
export function itemMapper(value: any, options: JsonSchemaOptions) { | ||
return value && value.isClass ? execMapper("class", value, options) : execMapper("any", value, options); | ||
} | ||
|
||
registerJsonSchemaMapper("item", itemMapper); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {JsonLazyRef} from "../domain/JsonLazyRef"; | ||
import {JsonSchemaOptions} from "../interfaces/JsonSchemaOptions"; | ||
import {registerJsonSchemaMapper} from "../registries/JsonSchemaMapperContainer"; | ||
import {mapGenericsOptions} from "../utils/generics"; | ||
import {createRef, toRef} from "../utils/ref"; | ||
|
||
export function lazyRefMapper(input: JsonLazyRef, options: JsonSchemaOptions) { | ||
const name = input.name; | ||
|
||
if (options.$refs?.find((t: any) => t === input.target)) { | ||
return createRef(name, input.schema, options); | ||
} | ||
|
||
options.$refs = [...(options.$refs || []), input.target]; | ||
|
||
const schema = input.toJSON(mapGenericsOptions(options)); | ||
|
||
return toRef(input.schema, schema, options); | ||
} | ||
|
||
registerJsonSchemaMapper("lazyRef", lazyRefMapper); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {mapGenericsOptions} from "../utils/generics"; | ||
import {JsonSchemaOptions} from "../interfaces/JsonSchemaOptions"; | ||
import {execMapper, registerJsonSchemaMapper} from "../registries/JsonSchemaMapperContainer"; | ||
|
||
/** | ||
* Serialize class which inherit from Map like JsonMap, JsonOperation, JsonParameter. | ||
* @param input | ||
* @param ignore | ||
* @param options | ||
* @ignore | ||
*/ | ||
export function mapMapper(input: Map<string, any>, {ignore = [], ...options}: JsonSchemaOptions = {}): any { | ||
options = mapGenericsOptions(options); | ||
|
||
return Array.from(input.entries()).reduce((obj: any, [key, value]) => { | ||
if (ignore.includes(key)) { | ||
return obj; | ||
} | ||
|
||
obj[key] = execMapper("item", value, options); | ||
return obj; | ||
}, {}); | ||
} | ||
|
||
registerJsonSchemaMapper("map", mapMapper); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import {isArray} from "@tsed/core"; | ||
import {JsonSchema} from "../domain/JsonSchema"; | ||
import {alterIgnore} from "../hooks/alterIgnore"; | ||
import {JsonSchemaOptions} from "../interfaces/JsonSchemaOptions"; | ||
import {execMapper, registerJsonSchemaMapper} from "../registries/JsonSchemaMapperContainer"; | ||
|
||
/** | ||
* Serialize Any object to a json schema | ||
* @param input | ||
* @param options | ||
* @ignore | ||
*/ | ||
export function objectMapper(input: any, options: JsonSchemaOptions) { | ||
const {specType, operationIdFormatter, root, schemas, genericTypes, nestedGenerics, useAlias, genericLabels, ...ctx} = options; | ||
|
||
return Object.entries(input).reduce<any>( | ||
(obj, [key, value]: [string, any | JsonSchema]) => { | ||
if (options.withIgnoredProps !== false && !alterIgnore(value, ctx)) { | ||
// remove groups to avoid bad schema generation over children models | ||
obj[key] = execMapper("item", value, {...options, groups: value?.$forwardGroups ? options.groups : undefined}); | ||
} | ||
|
||
return obj; | ||
}, | ||
isArray(input) ? [] : {} | ||
); | ||
} | ||
|
||
registerJsonSchemaMapper("object", objectMapper); |
Oops, something went wrong.