@tsed/json-mapper 8.13.5 → 8.14.0

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,5 +1,5 @@
1
- import { classOf, isArray, isBoolean, isClass, isEmpty, isNil, nameOf, objectKeys } from "@tsed/core";
2
- import { getPropertiesStores, JsonEntityStore, JsonParameterStore } from "@tsed/schema";
1
+ import { classOf, isArray, isBoolean, isEmpty, isNil, nameOf, objectKeys } from "@tsed/core";
2
+ import { getPropertiesStores, JsonEntityStore, JsonParameterStore, JsonSchema, VendorKeys } from "@tsed/schema";
3
3
  import { alterAfterDeserialize } from "../hooks/alterAfterDeserialize.js";
4
4
  import { alterBeforeDeserialize } from "../hooks/alterBeforeDeserialize.js";
5
5
  import { alterOnDeserialize } from "../hooks/alterOnDeserialize.js";
@@ -22,14 +22,10 @@ function mapParamStoreOptions(store, options) {
22
22
  store: undefined,
23
23
  type: store.getBestType(),
24
24
  collectionType: store.collectionType,
25
- groups: store.parameter.groups,
26
- // genericTypes: store.nestedGenerics[0],
27
- generics: store.nestedGenerics
25
+ groups: store.parameter.schema().getGroups(),
26
+ generics: store.parameter.schema().getGenericOf()
28
27
  };
29
28
  }
30
- function getGenericIndex(propertyStore) {
31
- return propertyStore.parent.schema.genericLabels.indexOf(propertyStore.itemSchema.genericType);
32
- }
33
29
  export class JsonDeserializer extends JsonMapperCompiler {
34
30
  constructor() {
35
31
  super();
@@ -41,6 +37,7 @@ export class JsonDeserializer extends JsonMapperCompiler {
41
37
  this.addGlobal("newInstanceOf", this.newInstanceOf.bind(this));
42
38
  this.addGlobal("alterBeforeDeserialize", this.alterBeforeDeserialize.bind(this));
43
39
  this.addGlobal("alterAfterDeserialize", this.alterAfterDeserialize.bind(this));
40
+ this.addGlobal("mapGenericsOptions", this.mapGenericsOptions.bind(this));
44
41
  this.addTypeMapper(Object, this.mapObject.bind(this));
45
42
  this.addTypeMapper(Array, this.mapArray.bind(this));
46
43
  this.addTypeMapper(Map, this.mapMap.bind(this));
@@ -95,14 +92,7 @@ export class JsonDeserializer extends JsonMapperCompiler {
95
92
  this.schemes[id] = entity.schema;
96
93
  writer.set("input", `alterBeforeDeserialize('${id}', input, options)`);
97
94
  }
98
- // generics and options
99
- writer.const("generics", "options.generics[0]");
100
- if (entity.schema.genericLabels?.length) {
101
- writer.set("options", "{...options, self: input, generics: [...options.generics].slice(1)}");
102
- }
103
- else {
104
- writer.set("options", "{...options, self: input}");
105
- }
95
+ writer.set("options", "{...options, self: input}");
106
96
  writer.const("obj", `newInstanceOf('${id}', input, options)`);
107
97
  // properties
108
98
  writer.add(...schemaProperties.flatMap((propertyStore) => {
@@ -176,21 +166,29 @@ export class JsonDeserializer extends JsonMapperCompiler {
176
166
  getPropertyFiller(propertyStore, id, groups, formatOpts) {
177
167
  const key = String(propertyStore.propertyKey);
178
168
  const schemaId = this.getSchemaId(id, key);
179
- const generics = propertyStore.itemSchema.nestedGenerics;
180
- const isGeneric = propertyStore.itemSchema.isGeneric && !generics?.length;
181
- if (isGeneric) {
182
- const index = getGenericIndex(propertyStore);
183
- const opts = Writer.options(formatOpts, `type: generics[${index}]`);
184
- return (writer) => writer.set(varKey(key), `compileAndMap(${varKey(key)}, ${opts})`);
169
+ if (propertyStore.itemSchema.isGeneric) {
170
+ const label = propertyStore.itemSchema.get(VendorKeys.GENERIC_LABEL);
171
+ return (writer) => {
172
+ const opts = Writer.options(formatOpts, `...mapGenericsOptions('${label}', options.generics)`);
173
+ return writer.set(varKey(key), `compileAndMap(${varKey(key)}, ${opts})`);
174
+ };
185
175
  }
186
176
  const type = propertyStore.itemSchema.hasDiscriminator ? propertyStore.itemSchema.discriminator().base : propertyStore.getBestType();
187
177
  const nestedMapper = this.compile(type, groups);
188
178
  if (propertyStore.isCollection) {
179
+ if (propertyStore.itemSchema.getGenericOf()) {
180
+ this.schemes[schemaId] = propertyStore.itemSchema;
181
+ return (writer) => {
182
+ return writer.callMapper(nameOf(propertyStore.collectionType), varKey(key), `id: '${nestedMapper.id}'`, `generics: schemes['${schemaId}'].getGenericOf()`, formatOpts);
183
+ };
184
+ }
189
185
  return (writer) => writer.callMapper(nameOf(propertyStore.collectionType), varKey(key), `id: '${nestedMapper.id}'`, formatOpts);
190
186
  }
191
- if (generics?.length) {
187
+ if (propertyStore.schema.getGenericOf()) {
192
188
  this.schemes[schemaId] = propertyStore.schema;
193
- return (writer) => writer.callMapper(nestedMapper.id, varKey(key), formatOpts, `generics: schemes['${schemaId}'].nestedGenerics`);
189
+ return (writer) => {
190
+ return writer.callMapper(nestedMapper.id, varKey(key), formatOpts, `generics: schemes['${schemaId}'].getGenericOf()`);
191
+ };
194
192
  }
195
193
  return (writer) => writer.callMapper(nestedMapper.id, varKey(key), formatOpts);
196
194
  }
@@ -222,7 +220,7 @@ export class JsonDeserializer extends JsonMapperCompiler {
222
220
  groups: groups === undefined ? (strictGroups ? [] : false) : groups || false,
223
221
  useAlias,
224
222
  customMappers,
225
- generics: options.generics || []
223
+ generics: options.generics || {}
226
224
  };
227
225
  }
228
226
  mapAdditionalProperties(entity, properties, groups) {
@@ -234,8 +232,8 @@ export class JsonDeserializer extends JsonMapperCompiler {
234
232
  if (exclude.length) {
235
233
  each = each.if(`![${exclude}].includes(key)`);
236
234
  }
237
- if (isClass(additionalProperties)) {
238
- const nestedMapper = this.compile(additionalProperties.getComputedType(), groups);
235
+ if (additionalProperties && additionalProperties instanceof JsonSchema) {
236
+ const nestedMapper = this.compile(additionalProperties.class, groups);
239
237
  each.set("obj[key]", Writer.mapper(nestedMapper.id, "input[key]", "options"));
240
238
  return writer;
241
239
  }
@@ -287,4 +285,15 @@ export class JsonDeserializer extends JsonMapperCompiler {
287
285
  alterAfterDeserialize(schemaId, value, options) {
288
286
  return alterAfterDeserialize(value, this.schemes[schemaId], options);
289
287
  }
288
+ mapGenericsOptions(label, inputGenerics) {
289
+ const generics = inputGenerics?.[label];
290
+ if (!generics) {
291
+ return {};
292
+ }
293
+ const type = generics[0] instanceof JsonSchema ? generics[0].getTarget() : generics[0];
294
+ return {
295
+ type,
296
+ generics: generics[1]
297
+ };
298
+ }
290
299
  }
@@ -27,7 +27,7 @@ export class Writer {
27
27
  return this;
28
28
  }
29
29
  arrow(...args) {
30
- this.add(`(${args}) => {`);
30
+ this.add(`(${args.join(", ")}) => {`);
31
31
  const writer = this.new();
32
32
  this.add("}");
33
33
  return writer;
@@ -30,4 +30,5 @@ export declare class JsonDeserializer extends JsonMapperCompiler<JsonDeserialize
30
30
  private mapItem;
31
31
  private alterBeforeDeserialize;
32
32
  private alterAfterDeserialize;
33
+ private mapGenericsOptions;
33
34
  }
@@ -1,5 +1,5 @@
1
1
  import { MetadataTypes, Type } from "@tsed/core";
2
- import { JsonEntityStore } from "@tsed/schema";
2
+ import { type GenericsMap, JsonEntityStore } from "@tsed/schema";
3
3
  import { JsonMapperMethods } from "../interfaces/JsonMapperMethods.js";
4
4
  import { JsonMapperGlobalOptions } from "./JsonMapperGlobalOptions.js";
5
5
  export interface JsonDeserializerOptions<T = any, C = any> extends MetadataTypes<T, C>, JsonMapperGlobalOptions {
@@ -21,8 +21,8 @@ export interface JsonDeserializerOptions<T = any, C = any> extends MetadataTypes
21
21
  */
22
22
  groups?: string[] | false;
23
23
  /**
24
- * Add types to deserialize a module using generics feature.
24
+ * Add types to deserialize a module using the generics feature.
25
25
  */
26
- generics?: Type[][];
26
+ generics?: GenericsMap;
27
27
  [key: string]: any;
28
28
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@tsed/json-mapper",
3
3
  "description": "Json mapper module for Ts.ED Framework",
4
4
  "type": "module",
5
- "version": "8.13.5",
5
+ "version": "8.14.0",
6
6
  "source": "./src/index.ts",
7
7
  "main": "./lib/esm/index.js",
8
8
  "module": "./lib/esm/index.js",
@@ -41,10 +41,10 @@
41
41
  "tslib": "2.7.0"
42
42
  },
43
43
  "devDependencies": {
44
- "@tsed/barrels": "8.13.5",
45
- "@tsed/core": "8.13.5",
46
- "@tsed/schema": "8.13.5",
47
- "@tsed/typescript": "8.13.5",
44
+ "@tsed/barrels": "8.14.0",
45
+ "@tsed/core": "8.14.0",
46
+ "@tsed/schema": "8.14.0",
47
+ "@tsed/typescript": "8.14.0",
48
48
  "eslint": "9.12.0",
49
49
  "typescript": "5.4.5",
50
50
  "vitest": "3.2.4"