diff --git a/examples/__tests__/test-status-deserialize-class.ava.js b/examples/__tests__/test-status-deserialize-class.ava.js index f52d92d24..43bb69748 100644 --- a/examples/__tests__/test-status-deserialize-class.ava.js +++ b/examples/__tests__/test-status-deserialize-class.ava.js @@ -149,21 +149,3 @@ test("Ali set_extra_record without schema defined then gets", async (t) => { const recordWithoutSchemaDefined = await statusMessage.view("get_extra_record", { account_id: ali.accountId }); t.is(recordWithoutSchemaDefined, "Hello world!"); }); - -test("View get_subtype_of_efficient_recordes", async (t) => { - const { statusMessage } = t.context.accounts; - - t.is( - await statusMessage.view("get_subtype_of_efficient_recordes", { }), - 'string' - ); -}); - -test("View get_subtype_of_nested_efficient_recordes", async (t) => { - const { statusMessage } = t.context.accounts; - - t.is( - JSON.stringify(await statusMessage.view("get_subtype_of_nested_efficient_recordes", { })), - '{"collection":{"value":"string"}}' - ); -}); \ No newline at end of file diff --git a/packages/near-sdk-js/lib/collections/subtype.js b/packages/near-sdk-js/lib/collections/subtype.js index 0b4ee206f..24d31f65b 100644 --- a/packages/near-sdk-js/lib/collections/subtype.js +++ b/packages/near-sdk-js/lib/collections/subtype.js @@ -7,19 +7,17 @@ export class SubType { options = {}; } const subtype = this.subtype(); - if (options.reconstructor == undefined && - subtype != undefined) { + if (options.reconstructor == undefined && subtype != undefined) { if ( // eslint-disable-next-line no-prototype-builtins subtype.hasOwnProperty("class") && - typeof subtype.class.reconstructor === "function") { - // { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}} + typeof subtype.class.reconstruct === "function") { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - options.reconstructor = subtype.class.reconstructor; + options.reconstructor = subtype.class.reconstruct; } - else if (subtype.reconstructor === "function") { - options.reconstructor = subtype.reconstructor; + else if (typeof subtype.reconstruct === "function") { + options.reconstructor = subtype.reconstruct; } } return options; diff --git a/packages/near-sdk-js/lib/utils.js b/packages/near-sdk-js/lib/utils.js index b0cfe8b00..c6da00bc4 100644 --- a/packages/near-sdk-js/lib/utils.js +++ b/packages/near-sdk-js/lib/utils.js @@ -52,14 +52,14 @@ export function getValueWithOptions(subDatatype, value, options = { const collection = options.reconstructor(deserialized); if (subDatatype !== undefined && // eslint-disable-next-line no-prototype-builtins - subDatatype.hasOwnProperty("collection") && + subDatatype.hasOwnProperty("class") && // eslint-disable-next-line no-prototype-builtins - subDatatype["collection"].hasOwnProperty("value")) { + subDatatype["class"].hasOwnProperty("value")) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore collection.subtype = function () { - // example: { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}} - return subDatatype["collection"]["value"]; + // example: {class: UnorderedMap, value: UnorderedMap} + return subDatatype["class"]["value"]; }; } return collection; @@ -143,7 +143,9 @@ export function deserialize(valueToDeserialize) { }); } export function decodeObj2class(class_instance, obj) { - if (typeof obj != "object" || typeof obj === "bigint" || obj instanceof Date || + if (typeof obj != "object" || + typeof obj === "bigint" || + obj instanceof Date || class_instance.constructor.schema === undefined) { return obj; } @@ -180,15 +182,17 @@ export function decodeObj2class(class_instance, obj) { // eslint-disable-next-line no-prototype-builtins } else if (ty !== undefined && ty.hasOwnProperty("class")) { - // nested_lookup_recordes: {collection: {reconstructor: UnorderedMap.reconstruct, value: { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}}}}, - // {collection: {reconstructor: + // => nested_lookup_recordes: {class: UnorderedMap, value: {class: LookupMap }}, class_instance[key] = ty["class"].reconstruct(obj[key]); - const subtype_value = ty["value"]; - class_instance[key].subtype = function () { - // example: { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}} - // example: UnorderedMap - return subtype_value; - }; + // eslint-disable-next-line no-prototype-builtins + if (ty.hasOwnProperty("value")) { + const subtype_value = ty["value"]; + class_instance[key].subtype = function () { + // example: { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}} + // example: UnorderedMap or {class: UnorderedMap, value: 'string'} + return subtype_value; + }; + } } else if (ty !== undefined && typeof ty.reconstruct === "function") { class_instance[key] = ty.reconstruct(obj[key]); diff --git a/packages/near-sdk-js/src/collections/subtype.ts b/packages/near-sdk-js/src/collections/subtype.ts index 07cd5944b..86f281bbc 100644 --- a/packages/near-sdk-js/src/collections/subtype.ts +++ b/packages/near-sdk-js/src/collections/subtype.ts @@ -12,20 +12,17 @@ export abstract class SubType { options = {}; } const subtype = this.subtype(); - if ( - options.reconstructor == undefined && - subtype != undefined - ) { + if (options.reconstructor == undefined && subtype != undefined) { if ( - // eslint-disable-next-line no-prototype-builtins - subtype.hasOwnProperty("class") && - typeof subtype.class.reconstructor === "function") { - // { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}} + // eslint-disable-next-line no-prototype-builtins + subtype.hasOwnProperty("class") && + typeof subtype.class.reconstruct === "function" + ) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - options.reconstructor = subtype.class.reconstructor; - } else if (subtype.reconstructor === "function") { - options.reconstructor = subtype.reconstructor; + options.reconstructor = subtype.class.reconstruct; + } else if (typeof subtype.reconstruct === "function") { + options.reconstructor = subtype.reconstruct; } } return options; diff --git a/packages/near-sdk-js/src/utils.ts b/packages/near-sdk-js/src/utils.ts index 2e3519fcf..b9abfa314 100644 --- a/packages/near-sdk-js/src/utils.ts +++ b/packages/near-sdk-js/src/utils.ts @@ -94,15 +94,15 @@ export function getValueWithOptions( if ( subDatatype !== undefined && // eslint-disable-next-line no-prototype-builtins - subDatatype.hasOwnProperty("collection") && + subDatatype.hasOwnProperty("class") && // eslint-disable-next-line no-prototype-builtins - subDatatype["collection"].hasOwnProperty("value") + subDatatype["class"].hasOwnProperty("value") ) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore collection.subtype = function () { - // example: { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}} - return subDatatype["collection"]["value"]; + // example: {class: UnorderedMap, value: UnorderedMap} + return subDatatype["class"]["value"]; }; } return collection; @@ -208,7 +208,9 @@ export function deserialize(valueToDeserialize: Uint8Array): unknown { export function decodeObj2class(class_instance, obj) { if ( - typeof obj != "object" || typeof obj === "bigint" || obj instanceof Date || + typeof obj != "object" || + typeof obj === "bigint" || + obj instanceof Date || class_instance.constructor.schema === undefined ) { return obj; @@ -247,15 +249,17 @@ export function decodeObj2class(class_instance, obj) { } // eslint-disable-next-line no-prototype-builtins } else if (ty !== undefined && ty.hasOwnProperty("class")) { - // nested_lookup_recordes: {collection: {reconstructor: UnorderedMap.reconstruct, value: { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}}}}, - // {collection: {reconstructor: + // => nested_lookup_recordes: {class: UnorderedMap, value: {class: LookupMap }}, class_instance[key] = ty["class"].reconstruct(obj[key]); - const subtype_value = ty["value"]; - class_instance[key].subtype = function () { - // example: { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}} - // example: UnorderedMap - return subtype_value; - }; + // eslint-disable-next-line no-prototype-builtins + if (ty.hasOwnProperty("value")) { + const subtype_value = ty["value"]; + class_instance[key].subtype = function () { + // example: { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}} + // example: UnorderedMap or {class: UnorderedMap, value: 'string'} + return subtype_value; + }; + } } else if (ty !== undefined && typeof ty.reconstruct === "function") { class_instance[key] = ty.reconstruct(obj[key]); } else {