@tsed/schema 8.15.4 → 8.16.0-rc.2

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.
@@ -195,7 +195,8 @@ class ReturnDecoratorContext extends DecoratorContext {
195
195
  }
196
196
  }
197
197
  map() {
198
- const { decoratorType } = this;
198
+ const model = this.get("model");
199
+ const { entity, decoratorType } = this;
199
200
  const operation = this.entity.operation;
200
201
  const currentStatus = this.getStatus();
201
202
  const response = operation.ensureResponseOf(currentStatus);
@@ -210,6 +211,12 @@ class ReturnDecoratorContext extends DecoratorContext {
210
211
  response.headers(headers);
211
212
  }
212
213
  this.mapMedia(response);
214
+ // if (isSuccessStatus(this.get("status")) || currentStatus === "default") {
215
+ // if (model) {
216
+ // // TODO Check why we need that ?
217
+ // // entity.type = model;
218
+ // }
219
+ // }
213
220
  // additional info for OS2
214
221
  contentType && operation.addProduce(contentType);
215
222
  // run additional actions
@@ -17,12 +17,6 @@ export class JsonEntityStore {
17
17
  this.store = options.store;
18
18
  this.parent = this;
19
19
  }
20
- get collectionType() {
21
- return this._collectionType;
22
- }
23
- set collectionType(value) {
24
- this._collectionType = value;
25
- }
26
20
  get type() {
27
21
  return this._type;
28
22
  }
@@ -50,10 +44,10 @@ export class JsonEntityStore {
50
44
  return nameOf(this.token);
51
45
  }
52
46
  get isCollection() {
53
- return !!this._collectionType;
47
+ return !!this.collectionType;
54
48
  }
55
49
  get isArray() {
56
- return isArrayOrArrayClass(this._collectionType);
50
+ return isArrayOrArrayClass(this.collectionType);
57
51
  }
58
52
  get discriminatorAncestor() {
59
53
  const ancestors = ancestorsOf(this.target);
@@ -61,7 +55,7 @@ export class JsonEntityStore {
61
55
  return ancestor && JsonEntityStore.from(ancestor);
62
56
  }
63
57
  get isPrimitive() {
64
- return isPrimitiveOrPrimitiveClass(this.type);
58
+ return isPrimitiveOrPrimitiveClass(this._type);
65
59
  }
66
60
  get isDate() {
67
61
  return isDate(this.computedType);
@@ -151,7 +145,7 @@ export class JsonEntityStore {
151
145
  }
152
146
  buildType(type) {
153
147
  if (isCollection(type)) {
154
- this._collectionType = type;
148
+ this.collectionType = type;
155
149
  }
156
150
  else if (!(type && "$schema" in type && type.$schema.skip)) {
157
151
  this._type = type;
@@ -1,7 +1,6 @@
1
1
  import { __decorate, __metadata } from "tslib";
2
- import { DecoratorTypes, deepMerge, descriptorOf, isFunction, prototypeOf, Store } from "@tsed/core";
2
+ import { DecoratorTypes, deepMerge, descriptorOf, isClass, isCollection, isFunction, isPromise, Metadata, prototypeOf, Store } from "@tsed/core";
3
3
  import { JsonEntityComponent } from "../decorators/config/jsonEntityComponent.js";
4
- import { isSuccessStatus } from "../utils/isSuccessStatus.js";
5
4
  import { JsonEntityStore } from "./JsonEntityStore.js";
6
5
  import { JsonOperation } from "./JsonOperation.js";
7
6
  import { JsonSchema } from "./JsonSchema.js";
@@ -50,34 +49,6 @@ let JsonMethodStore = class JsonMethodStore extends JsonEntityStore {
50
49
  get operationPaths() {
51
50
  return this.operation.operationPaths;
52
51
  }
53
- get collectionType() {
54
- return this.schema.getTarget();
55
- }
56
- set collectionType(type) {
57
- console.trace("collectionType is deprecated, use schema.collectionClass instead");
58
- }
59
- get isCollection() {
60
- return this.schema.isCollection;
61
- }
62
- get schema() {
63
- if (this._schema) {
64
- return this._schema;
65
- }
66
- const responses = this.operation.getResponses();
67
- const [, response] = [...responses.entries()].find(([key, response]) => {
68
- return isSuccessStatus(key);
69
- }) || [];
70
- if (response) {
71
- const firstMediaType = response.getContent().values().next().value;
72
- if (firstMediaType) {
73
- this._schema = firstMediaType.schema();
74
- }
75
- }
76
- else {
77
- this._schema = new JsonSchema();
78
- }
79
- return this._schema;
80
- }
81
52
  /**
82
53
  * Get an endpoint.
83
54
  * @param target
@@ -104,7 +75,7 @@ let JsonMethodStore = class JsonMethodStore extends JsonEntityStore {
104
75
  }
105
76
  return { type: media.schema().collectionClass, groups };
106
77
  }
107
- return { type: this.type || Object };
78
+ return { type: this.type };
108
79
  }
109
80
  /**
110
81
  * Append middlewares to the beforeMiddlewares list.
@@ -149,7 +120,38 @@ let JsonMethodStore = class JsonMethodStore extends JsonEntityStore {
149
120
  }), {});
150
121
  }
151
122
  build() {
123
+ if (!this._type) {
124
+ let type = Metadata.getReturnType(this.target, this.propertyKey);
125
+ type = isPromise(type) ? undefined : type;
126
+ this.buildType(type);
127
+ }
128
+ this._type = this._type || Object;
152
129
  this.parent.children.set(this.propertyName, this);
130
+ if (isCollection(this._type)) {
131
+ this.collectionType = this._type;
132
+ // @ts-ignore
133
+ delete this._type;
134
+ }
135
+ // TODO est-ce que c'est tjrs d'actualité car on doit utiliser le decorator @Returns pour affecter correctement les data
136
+ // peut etre que c'est pour les get/setter ou méthod n'étant pas un endpoint?
137
+ if (this.collectionType) {
138
+ this._schema = JsonSchema.from({
139
+ type: this.collectionType
140
+ });
141
+ this._schema.itemSchema(this.type);
142
+ }
143
+ else if (isClass(this.type)) {
144
+ this._schema = JsonSchema.from({
145
+ type: "object"
146
+ });
147
+ this._schema.itemSchema(this.type);
148
+ }
149
+ else {
150
+ this._schema = JsonSchema.from({
151
+ type: this.type
152
+ });
153
+ }
154
+ this.parent.schema.addProperty(this.propertyName, this.schema);
153
155
  }
154
156
  };
155
157
  JsonMethodStore = __decorate([
@@ -1,6 +1,4 @@
1
- import { cleanObject } from "@tsed/core/utils/cleanObject.js";
2
- import { isObject } from "@tsed/core/utils/isObject.js";
3
- import { uniq } from "@tsed/core/utils/uniq.js";
1
+ import { isObject, uniq } from "@tsed/core";
4
2
  function mergeProps(properties1, properties2) {
5
3
  if (!properties1 || !properties2) {
6
4
  return properties1 || properties2 || {};
@@ -19,58 +17,40 @@ function mergeProps(properties1, properties2) {
19
17
  return acc;
20
18
  }, {});
21
19
  }
22
- function getSchemaType(schema) {
23
- return schema.type || (schema.items && "array") || ((schema.properties || schema.additionalProperties) && "object") || "$any";
24
- }
25
20
  function mergeManyOf(kind, schema1, schema2) {
26
- const { [kind]: kind1, ...$rest1 } = schema1;
27
- const { [kind]: kind2, ...$rest2 } = schema2;
28
- let hasRef = false;
29
- const map = [kind1, $rest1, kind2, $rest2]
30
- .flat()
31
- .filter(Boolean)
32
- .reduce((map, schema, index) => {
33
- schema = cleanObject(schema);
34
- if (schema.$ref) {
35
- hasRef = true;
36
- return map.set(schema.$ref, schema);
37
- }
38
- if (Object.keys(schema).length === 0) {
39
- return map;
40
- }
41
- if (schema.type === "object" && Object.keys(schema).length === 2) {
42
- if ("writeOnly" in schema || "readOnly" in schema || "deprecated" in schema) {
43
- schema = { ...schema, type: undefined };
21
+ const refs = new Set();
22
+ if (schema1.$ref && schema2.type && (schema2.readOnly || schema2.writeOnly) && kind === "allOf") {
23
+ const { type, ...rest } = schema2;
24
+ schema2 = rest;
25
+ }
26
+ let of = [...(schema1[kind] || [schema1]), ...(schema2[kind] || [schema2])].filter((schema) => {
27
+ if ("$ref" in schema) {
28
+ if (refs.has(schema.$ref)) {
29
+ return false;
44
30
  }
31
+ refs.add(schema.$ref);
32
+ return true;
45
33
  }
46
- const type = getSchemaType(schema);
47
- if (type === "array") {
48
- map.set("array_" + index, schema);
49
- return map;
50
- }
51
- if (map.has(type)) {
52
- if (kind === "allOf") {
53
- // we can merge allOf schemas to optimize the schema
54
- map.set(type, mergeSchema(map.get(type), schema));
55
- return map;
34
+ return Object.keys(schema).length > 0;
35
+ });
36
+ if (of.length > 2) {
37
+ of = of.reduce((current, item, index) => {
38
+ if (item.$ref) {
39
+ return current.concat(item);
56
40
  }
57
- map.set(type + "_" + index, schema);
58
- return map;
59
- }
60
- return map.set(type, schema);
61
- }, new Map());
62
- const rest = map.get("$any");
63
- map.delete("$any");
64
- const of = Array.from(map.values());
65
- return cleanObject(of.length > 1 || (hasRef && rest)
41
+ if (current.at(-1)?.type === item.type && item.type !== "array") {
42
+ current[current.length - 1] = mergeSchema(current.at(-1), item);
43
+ return current;
44
+ }
45
+ return current.concat(item);
46
+ }, []);
47
+ }
48
+ return of.length > 1
66
49
  ? {
67
- ...rest,
68
- [kind]: of
50
+ [kind]: of,
51
+ ...(schema1.nullable || schema2.nullable ? { nullable: true } : {})
69
52
  }
70
- : {
71
- ...rest,
72
- ...of[0]
73
- });
53
+ : of[0];
74
54
  }
75
55
  export function mergeSchema(schema1, schema2) {
76
56
  if (schema1.$ref && schema2.$ref && schema1.$ref === schema2.$ref) {
@@ -42,23 +42,21 @@ export declare abstract class JsonEntityStore implements JsonEntityStoreOptions
42
42
  * Decorator type used to declare the JsonSchemaStore.
43
43
  */
44
44
  readonly decoratorType: DecoratorTypes;
45
+ /**
46
+ * Type of the collection (Array, Map, Set, etc...)
47
+ */
48
+ collectionType: Type<any>;
45
49
  token: Type<any>;
46
50
  readonly store: Store;
47
51
  readonly isStore = true;
48
52
  readonly parent: JsonEntityStore;
49
53
  readonly target: Type<any>;
50
- [key: string]: any;
51
54
  constructor(options: JsonEntityStoreOptions);
52
- /**
53
- * Type of the collection (Array, Map, Set, etc...)
54
- */
55
- private _collectionType;
56
- get collectionType(): Type<any> | undefined;
57
- set collectionType(value: Type<any>);
58
55
  /**
59
56
  *
60
57
  */
61
58
  protected _type: Type<any>;
59
+ [key: string]: any;
62
60
  get type(): Type<any> | any;
63
61
  /**
64
62
  * Get original type without transformation
@@ -1,7 +1,7 @@
1
1
  import { OS3MediaType } from "@tsed/openspec";
2
2
  import { JsonMap } from "./JsonMap.js";
3
3
  import { JsonSchema } from "./JsonSchema.js";
4
- export declare class JsonMedia extends JsonMap<any> {
4
+ export declare class JsonMedia extends JsonMap<OS3MediaType<JsonSchema>> {
5
5
  $kind: string;
6
6
  constructor(obj?: Partial<OS3MediaType<JsonSchema>>);
7
7
  schema(schema?: JsonSchema): JsonSchema;
@@ -1,9 +1,8 @@
1
1
  import { Type } from "@tsed/core";
2
- import { type JsonClassStore } from "./JsonClassStore.js";
2
+ import type { JsonClassStore } from "./JsonClassStore.js";
3
3
  import { JsonEntityStore, JsonEntityStoreOptions } from "./JsonEntityStore.js";
4
4
  import { JsonOperation } from "./JsonOperation.js";
5
- import { type JsonParameterStore } from "./JsonParameterStore.js";
6
- import { JsonSchema } from "./JsonSchema.js";
5
+ import type { JsonParameterStore } from "./JsonParameterStore.js";
7
6
  export interface JsonViewOptions {
8
7
  path: string;
9
8
  options: any;
@@ -33,10 +32,6 @@ export declare class JsonMethodStore extends JsonEntityStore {
33
32
  set acceptMimes(mimes: string[]);
34
33
  get parameters(): JsonParameterStore[];
35
34
  get operationPaths(): Map<string, import("./JsonOperation.js").JsonMethodPath>;
36
- get collectionType(): Type<any>;
37
- set collectionType(type: Type<any>);
38
- get isCollection(): boolean;
39
- get schema(): JsonSchema;
40
35
  /**
41
36
  * Get an endpoint.
42
37
  * @param target
@@ -1,8 +1,7 @@
1
- import type { Type } from "@tsed/core";
2
1
  import { JsonMap } from "../domain/JsonMap.js";
3
2
  /**
4
3
  * @ignore
5
4
  */
6
5
  export declare function toJsonMapCollection(content: {
7
6
  [key: string]: any;
8
- }, klass?: Type<JsonMap<any>>): JsonMap<any>;
7
+ }, klass?: typeof JsonMap): JsonMap<any>;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@tsed/schema",
3
3
  "description": "JsonSchema module for Ts.ED Framework",
4
4
  "type": "module",
5
- "version": "8.15.4",
5
+ "version": "8.16.0-rc.2",
6
6
  "source": "./src/index.ts",
7
7
  "main": "./lib/esm/index.js",
8
8
  "module": "./lib/esm/index.js",
@@ -34,7 +34,7 @@
34
34
  "tsed"
35
35
  ],
36
36
  "dependencies": {
37
- "@tsed/openspec": "8.15.4",
37
+ "@tsed/openspec": "8.16.0-rc.2",
38
38
  "change-case": "^5.4.4",
39
39
  "json-schema": "0.4.0",
40
40
  "picomatch": "4.0.2",
@@ -43,11 +43,11 @@
43
43
  },
44
44
  "devDependencies": {
45
45
  "@apidevtools/swagger-parser": "10.1.0",
46
- "@tsed/barrels": "8.15.4",
47
- "@tsed/core": "8.15.4",
48
- "@tsed/hooks": "8.15.4",
49
- "@tsed/openspec": "8.15.4",
50
- "@tsed/typescript": "8.15.4",
46
+ "@tsed/barrels": "8.16.0-rc.2",
47
+ "@tsed/core": "8.16.0-rc.2",
48
+ "@tsed/hooks": "8.16.0-rc.2",
49
+ "@tsed/openspec": "8.16.0-rc.2",
50
+ "@tsed/typescript": "8.16.0-rc.2",
51
51
  "@types/fs-extra": "11.0.4",
52
52
  "@types/json-schema": "7.0.15",
53
53
  "@types/picomatch": "3.0.2",
@@ -76,5 +76,8 @@
76
76
  },
77
77
  "homepage": "https://github.com/tsedio/tsed/tree/production/packages/specs/schema",
78
78
  "author": "Romain Lenzotti",
79
- "license": "MIT"
79
+ "license": "MIT",
80
+ "publishConfig": {
81
+ "tag": "rc"
82
+ }
80
83
  }