@tsed/schema 8.15.1 → 8.15.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.
@@ -1,4 +1,6 @@
1
- import { isObject, uniq } from "@tsed/core";
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";
2
4
  function mergeProps(properties1, properties2) {
3
5
  if (!properties1 || !properties2) {
4
6
  return properties1 || properties2 || {};
@@ -17,40 +19,58 @@ function mergeProps(properties1, properties2) {
17
19
  return acc;
18
20
  }, {});
19
21
  }
22
+ function getSchemaType(schema) {
23
+ return schema.type || (schema.items && "array") || ((schema.properties || schema.additionalProperties) && "object") || "$any";
24
+ }
20
25
  function mergeManyOf(kind, schema1, schema2) {
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;
30
- }
31
- refs.add(schema.$ref);
32
- return true;
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;
33
40
  }
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);
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 };
40
44
  }
41
- if (current.at(-1)?.type === item.type && item.type !== "array") {
42
- current[current.length - 1] = mergeSchema(current.at(-1), item);
43
- return current;
45
+ }
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;
44
56
  }
45
- return current.concat(item);
46
- }, []);
47
- }
48
- return of.length > 1
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)
49
66
  ? {
50
- [kind]: of,
51
- ...(schema1.nullable || schema2.nullable ? { nullable: true } : {})
67
+ ...rest,
68
+ [kind]: of
52
69
  }
53
- : of[0];
70
+ : {
71
+ ...rest,
72
+ ...of[0]
73
+ });
54
74
  }
55
75
  export function mergeSchema(schema1, schema2) {
56
76
  if (schema1.$ref && schema2.$ref && schema1.$ref === schema2.$ref) {
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.1",
5
+ "version": "8.15.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.1",
37
+ "@tsed/openspec": "8.15.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.1",
47
- "@tsed/core": "8.15.1",
48
- "@tsed/hooks": "8.15.1",
49
- "@tsed/openspec": "8.15.1",
50
- "@tsed/typescript": "8.15.1",
46
+ "@tsed/barrels": "8.15.2",
47
+ "@tsed/core": "8.15.2",
48
+ "@tsed/hooks": "8.15.2",
49
+ "@tsed/openspec": "8.15.2",
50
+ "@tsed/typescript": "8.15.2",
51
51
  "@types/fs-extra": "11.0.4",
52
52
  "@types/json-schema": "7.0.15",
53
53
  "@types/picomatch": "3.0.2",