From dee03e38d2732ba0bd38eeacca6ad58b191e87f8 Mon Sep 17 00:00:00 2001 From: Timo Glastra Date: Wed, 24 Nov 2021 11:25:58 +0100 Subject: [PATCH] fix(core)!: update class transformer library (#547) BREAKING CHANGE: class-transformer released a breaking change in a patch version, causing AFJ to break. I updated to the newer version and pinned the version exactly as this is the second time this has happened now. Signed-off-by: Timo Glastra --- packages/core/package.json | 4 +-- .../did/__tests__/Authentication.test.ts | 14 ++++----- .../models/did/__tests__/DidDoc.test.ts | 6 ++-- .../models/did/__tests__/PublicKey.test.ts | 14 ++++----- .../models/did/__tests__/Service.test.ts | 30 +++++++++---------- .../models/did/authentication/index.ts | 8 ++--- .../connections/models/did/publicKey/index.ts | 4 +-- .../connections/models/did/service/index.ts | 4 +-- packages/core/src/utils/JsonTransformer.ts | 12 ++++---- .../src/utils/__tests__/transformers.test.ts | 4 +-- yarn.lock | 22 +++++++------- 11 files changed, 60 insertions(+), 62 deletions(-) diff --git a/packages/core/package.json b/packages/core/package.json index d83644fcb2..cffa53569b 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -31,8 +31,8 @@ "bn.js": "^5.2.0", "borc": "^3.0.0", "buffer": "^6.0.3", - "class-transformer": "^0.4.0", - "class-validator": "^0.13.1", + "class-transformer": "0.5.1", + "class-validator": "0.13.1", "js-sha256": "^0.9.0", "lru_map": "^0.4.1", "luxon": "^1.27.0", diff --git a/packages/core/src/modules/connections/models/did/__tests__/Authentication.test.ts b/packages/core/src/modules/connections/models/did/__tests__/Authentication.test.ts index 730a2e603d..5cd81e82c9 100644 --- a/packages/core/src/modules/connections/models/did/__tests__/Authentication.test.ts +++ b/packages/core/src/modules/connections/models/did/__tests__/Authentication.test.ts @@ -1,6 +1,6 @@ import type { Authentication } from '../authentication' -import { classToPlain, plainToClass } from 'class-transformer' +import { instanceToPlain, plainToInstance } from 'class-transformer' import { AuthenticationTransformer, ReferencedAuthentication, EmbeddedAuthentication } from '../authentication' import { PublicKey, RsaSig2018 } from '../publicKey' @@ -15,7 +15,7 @@ describe('Did | Authentication', () => { }) const referencedAuthentication = new ReferencedAuthentication(publicKey, 'RsaSignatureAuthentication2018') - const transformed = classToPlain(referencedAuthentication) + const transformed = instanceToPlain(referencedAuthentication) expect(transformed).toMatchObject({ type: 'RsaSignatureAuthentication2018', @@ -49,7 +49,7 @@ describe('Did | Authentication', () => { publicKey: [embeddedAuthenticationJson], authentication: [referencedAuthenticationJson, embeddedAuthenticationJson], } - const authenticationWrapper = plainToClass(AuthenticationTransformerTest, authenticationWrapperJson) + const authenticationWrapper = plainToInstance(AuthenticationTransformerTest, authenticationWrapperJson) expect(authenticationWrapper.authentication.length).toBe(2) @@ -77,7 +77,7 @@ describe('Did | Authentication', () => { publicKey: [publicKeyJson], authentication: [referencedAuthenticationJson], } - const authenticationWrapper = plainToClass(AuthenticationTransformerTest, authenticationWrapperJson) + const authenticationWrapper = plainToInstance(AuthenticationTransformerTest, authenticationWrapperJson) expect(authenticationWrapper.authentication.length).toBe(1) @@ -98,7 +98,7 @@ describe('Did | Authentication', () => { authentication: [referencedAuthenticationJson], } - expect(() => plainToClass(AuthenticationTransformerTest, authenticationWrapperJson)).toThrowError( + expect(() => plainToInstance(AuthenticationTransformerTest, authenticationWrapperJson)).toThrowError( `Invalid public key referenced ${referencedAuthenticationJson.publicKey}` ) }) @@ -114,7 +114,7 @@ describe('Did | Authentication', () => { const authenticationWrapperJson = { authentication: [publicKeyJson], } - const authenticationWrapper = plainToClass(AuthenticationTransformerTest, authenticationWrapperJson) + const authenticationWrapper = plainToInstance(AuthenticationTransformerTest, authenticationWrapperJson) expect(authenticationWrapper.authentication.length).toBe(1) @@ -145,7 +145,7 @@ describe('Did | Authentication', () => { ] expect(authenticationWrapper.authentication.length).toBe(2) - const [embeddedJson, referencedJson] = classToPlain(authenticationWrapper).authentication + const [embeddedJson, referencedJson] = instanceToPlain(authenticationWrapper).authentication expect(embeddedJson).toMatchObject({ controller: 'test', diff --git a/packages/core/src/modules/connections/models/did/__tests__/DidDoc.test.ts b/packages/core/src/modules/connections/models/did/__tests__/DidDoc.test.ts index a1b7898a61..3272624749 100644 --- a/packages/core/src/modules/connections/models/did/__tests__/DidDoc.test.ts +++ b/packages/core/src/modules/connections/models/did/__tests__/DidDoc.test.ts @@ -1,4 +1,4 @@ -import { classToPlain, plainToClass } from 'class-transformer' +import { instanceToPlain, plainToInstance } from 'class-transformer' import { DidDoc } from '../DidDoc' import { ReferencedAuthentication, EmbeddedAuthentication } from '../authentication' @@ -70,7 +70,7 @@ const didDoc = new DidDoc({ // TODO: add more tests describe('Did | DidDoc', () => { it('should correctly transforms Json to DidDoc class', () => { - const didDoc = plainToClass(DidDoc, diddoc) + const didDoc = plainToInstance(DidDoc, diddoc) // Check array length of all items expect(didDoc.publicKey.length).toBe(diddoc.publicKey.length) @@ -97,7 +97,7 @@ describe('Did | DidDoc', () => { }) it('should correctly transforms DidDoc class to Json', () => { - const json = classToPlain(didDoc) + const json = instanceToPlain(didDoc) // Check array length of all items expect(json.publicKey.length).toBe(didDoc.publicKey.length) diff --git a/packages/core/src/modules/connections/models/did/__tests__/PublicKey.test.ts b/packages/core/src/modules/connections/models/did/__tests__/PublicKey.test.ts index 721a58d023..7d735337a4 100644 --- a/packages/core/src/modules/connections/models/did/__tests__/PublicKey.test.ts +++ b/packages/core/src/modules/connections/models/did/__tests__/PublicKey.test.ts @@ -1,6 +1,6 @@ import type { ClassConstructor } from 'class-transformer' -import { classToPlain, plainToClass } from 'class-transformer' +import { instanceToPlain, plainToInstance } from 'class-transformer' import { PublicKeyTransformer, @@ -52,7 +52,7 @@ describe('Did | PublicKey', () => { controller: 'did:sov:LjgpST2rjsoxYegQDRm7EL', } - const service = plainToClass(PublicKey, json) + const service = plainToInstance(PublicKey, json) expect(service.id).toBe(json.id) expect(service.type).toBe(json.type) expect(service.controller).toBe(json.controller) @@ -67,7 +67,7 @@ describe('Did | PublicKey', () => { const publicKey = new PublicKey({ ...json, }) - const transformed = classToPlain(publicKey) + const transformed = instanceToPlain(publicKey) expect(transformed).toEqual(json) }) @@ -76,7 +76,7 @@ describe('Did | PublicKey', () => { test.each(publicKeyJsonToClassTests)( 'should correctly transform Json to %s class', async (_, publicKeyClass, json, valueKey) => { - const publicKey = plainToClass(publicKeyClass, json) + const publicKey = plainToInstance(publicKeyClass, json) expect(publicKey.id).toBe(json.id) expect(publicKey.type).toBe(json.type) @@ -92,7 +92,7 @@ describe('Did | PublicKey', () => { test.each(publicKeyClassToJsonTests)( 'should correctly transform %s class to Json', async (_, publicKey, json, valueKey) => { - const publicKeyJson = classToPlain(publicKey) + const publicKeyJson = instanceToPlain(publicKey) expect(publicKey.value).toBe(json[valueKey]) expect(publicKeyJson).toMatchObject(json) @@ -116,7 +116,7 @@ describe('Did | PublicKey', () => { const publicKeyWrapperJson = { publicKey: [publicKeyJson], } - const publicKeyWrapper = plainToClass(PublicKeyTransformerTest, publicKeyWrapperJson) + const publicKeyWrapper = plainToInstance(PublicKeyTransformerTest, publicKeyWrapperJson) expect(publicKeyWrapper.publicKey.length).toBe(1) @@ -134,7 +134,7 @@ describe('Did | PublicKey', () => { const publicKeyWrapperJson = { publicKey: publicKeyArray, } - const publicKeyWrapper = plainToClass(PublicKeyTransformerTest, publicKeyWrapperJson) + const publicKeyWrapper = plainToInstance(PublicKeyTransformerTest, publicKeyWrapperJson) expect(publicKeyWrapper.publicKey.length).toBe(publicKeyArray.length) diff --git a/packages/core/src/modules/connections/models/did/__tests__/Service.test.ts b/packages/core/src/modules/connections/models/did/__tests__/Service.test.ts index f9bb29822c..c0aeeb8919 100644 --- a/packages/core/src/modules/connections/models/did/__tests__/Service.test.ts +++ b/packages/core/src/modules/connections/models/did/__tests__/Service.test.ts @@ -1,4 +1,4 @@ -import { classToPlain, plainToClass } from 'class-transformer' +import { instanceToPlain, plainToInstance } from 'class-transformer' import { Service, ServiceTransformer, serviceTypes, IndyAgentService, DidCommService } from '../service' @@ -9,7 +9,7 @@ describe('Did | Service', () => { type: 'Mediator', serviceEndpoint: 'https://example.com', } - const service = plainToClass(Service, json) + const service = plainToInstance(Service, json) expect(service.id).toBe(json.id) expect(service.type).toBe(json.type) @@ -27,7 +27,7 @@ describe('Did | Service', () => { ...json, }) - const transformed = classToPlain(service) + const transformed = instanceToPlain(service) expect(transformed).toEqual(json) }) @@ -42,7 +42,7 @@ describe('Did | Service', () => { priority: 10, serviceEndpoint: 'https://example.com', } - const service = plainToClass(IndyAgentService, json) + const service = plainToInstance(IndyAgentService, json) expect(service).toMatchObject(json) }) @@ -61,7 +61,7 @@ describe('Did | Service', () => { ...json, }) - const transformed = classToPlain(service) + const transformed = instanceToPlain(service) expect(transformed).toEqual(json) }) @@ -75,14 +75,14 @@ describe('Did | Service', () => { serviceEndpoint: 'https://example.com', } - const transformService = plainToClass(IndyAgentService, json) + const transformService = plainToInstance(IndyAgentService, json) const constructorService = new IndyAgentService({ ...json }) expect(transformService.priority).toBe(0) expect(constructorService.priority).toBe(0) - expect(classToPlain(transformService).priority).toBe(0) - expect(classToPlain(constructorService).priority).toBe(0) + expect(instanceToPlain(transformService).priority).toBe(0) + expect(instanceToPlain(constructorService).priority).toBe(0) }) }) @@ -97,7 +97,7 @@ describe('Did | Service', () => { priority: 10, serviceEndpoint: 'https://example.com', } - const service = plainToClass(DidCommService, json) + const service = plainToInstance(DidCommService, json) expect(service).toMatchObject(json) }) @@ -117,7 +117,7 @@ describe('Did | Service', () => { ...json, }) - const transformed = classToPlain(service) + const transformed = instanceToPlain(service) expect(transformed).toEqual(json) }) @@ -132,14 +132,14 @@ describe('Did | Service', () => { serviceEndpoint: 'https://example.com', } - const transformService = plainToClass(DidCommService, json) + const transformService = plainToInstance(DidCommService, json) const constructorService = new DidCommService({ ...json }) expect(transformService.priority).toBe(0) expect(constructorService.priority).toBe(0) - expect(classToPlain(transformService).priority).toBe(0) - expect(classToPlain(constructorService).priority).toBe(0) + expect(instanceToPlain(transformService).priority).toBe(0) + expect(instanceToPlain(constructorService).priority).toBe(0) }) }) @@ -159,7 +159,7 @@ describe('Did | Service', () => { const serviceWrapperJson = { service: [serviceJson], } - const serviceWrapper = plainToClass(ServiceTransformerTest, serviceWrapperJson) + const serviceWrapper = plainToInstance(ServiceTransformerTest, serviceWrapperJson) expect(serviceWrapper.service.length).toBe(1) @@ -185,7 +185,7 @@ describe('Did | Service', () => { const serviceWrapperJson = { service: serviceArray, } - const serviceWrapper = plainToClass(ServiceTransformerTest, serviceWrapperJson) + const serviceWrapper = plainToInstance(ServiceTransformerTest, serviceWrapperJson) expect(serviceWrapper.service.length).toBe(serviceArray.length) diff --git a/packages/core/src/modules/connections/models/did/authentication/index.ts b/packages/core/src/modules/connections/models/did/authentication/index.ts index 9631c3ae4a..ffa6db021c 100644 --- a/packages/core/src/modules/connections/models/did/authentication/index.ts +++ b/packages/core/src/modules/connections/models/did/authentication/index.ts @@ -1,6 +1,6 @@ import type { ClassConstructor } from 'class-transformer' -import { Transform, TransformationType, plainToClass, classToPlain } from 'class-transformer' +import { Transform, TransformationType, plainToInstance, instanceToPlain } from 'class-transformer' import { AriesFrameworkError } from '../../../../../error' import { PublicKey, publicKeyTypes } from '../publicKey' @@ -50,17 +50,17 @@ export function AuthenticationTransformer() { // Referenced keys use other types than embedded keys. const publicKeyClass = (publicKeyTypes[publicKeyJson.type] ?? PublicKey) as ClassConstructor - const publicKey = plainToClass(publicKeyClass, publicKeyJson) + const publicKey = plainToInstance(publicKeyClass, publicKeyJson) return new ReferencedAuthentication(publicKey, auth.type) } else { // embedded const publicKeyClass = (publicKeyTypes[auth.type] ?? PublicKey) as ClassConstructor - const publicKey = plainToClass(publicKeyClass, auth) + const publicKey = plainToInstance(publicKeyClass, auth) return new EmbeddedAuthentication(publicKey) } }) } else { - return value.map((auth) => (auth instanceof EmbeddedAuthentication ? classToPlain(auth.publicKey) : auth)) + return value.map((auth) => (auth instanceof EmbeddedAuthentication ? instanceToPlain(auth.publicKey) : auth)) } } ) diff --git a/packages/core/src/modules/connections/models/did/publicKey/index.ts b/packages/core/src/modules/connections/models/did/publicKey/index.ts index d2ecbf6128..72a2105e7e 100644 --- a/packages/core/src/modules/connections/models/did/publicKey/index.ts +++ b/packages/core/src/modules/connections/models/did/publicKey/index.ts @@ -1,6 +1,6 @@ import type { ClassConstructor } from 'class-transformer' -import { Transform, plainToClass } from 'class-transformer' +import { Transform, plainToInstance } from 'class-transformer' import { Ed25119Sig2018 } from './Ed25119Sig2018' import { EddsaSaSigSecp256k1 } from './EddsaSaSigSecp256k1' @@ -27,7 +27,7 @@ export function PublicKeyTransformer() { ({ value }: { value: { type: string }[] }) => { return value.map((publicKeyJson) => { const publicKeyClass = (publicKeyTypes[publicKeyJson.type] ?? PublicKey) as ClassConstructor - const publicKey = plainToClass(publicKeyClass, publicKeyJson) + const publicKey = plainToInstance(publicKeyClass, publicKeyJson) return publicKey }) diff --git a/packages/core/src/modules/connections/models/did/service/index.ts b/packages/core/src/modules/connections/models/did/service/index.ts index 81e059c194..cedbbe0170 100644 --- a/packages/core/src/modules/connections/models/did/service/index.ts +++ b/packages/core/src/modules/connections/models/did/service/index.ts @@ -1,6 +1,6 @@ import type { ClassConstructor } from 'class-transformer' -import { Transform, plainToClass } from 'class-transformer' +import { Transform, plainToInstance } from 'class-transformer' import { DidCommService } from './DidCommService' import { IndyAgentService } from './IndyAgentService' @@ -25,7 +25,7 @@ export function ServiceTransformer() { ({ value }: { value: { type: string }[] }) => { return value.map((serviceJson) => { const serviceClass = (serviceTypes[serviceJson.type] ?? Service) as ClassConstructor - const service = plainToClass(serviceClass, serviceJson) + const service = plainToInstance(serviceClass, serviceJson) return service }) diff --git a/packages/core/src/utils/JsonTransformer.ts b/packages/core/src/utils/JsonTransformer.ts index 6c51cdb925..bdf0d4f989 100644 --- a/packages/core/src/utils/JsonTransformer.ts +++ b/packages/core/src/utils/JsonTransformer.ts @@ -1,25 +1,23 @@ -import { classToPlain, deserialize, plainToClass, serialize } from 'class-transformer' +import { instanceToPlain, plainToInstance } from 'class-transformer' export class JsonTransformer { public static toJSON(classInstance: T) { - return classToPlain(classInstance, { + return instanceToPlain(classInstance, { exposeDefaultValues: true, }) } // eslint-disable-next-line @typescript-eslint/no-explicit-any public static fromJSON(json: any, Class: { new (...args: any[]): T }): T { - return plainToClass(Class, json, { exposeDefaultValues: true }) + return plainToInstance(Class, json, { exposeDefaultValues: true }) } public static serialize(classInstance: T): string { - return serialize(classInstance, { - exposeDefaultValues: true, - }) + return JSON.stringify(this.toJSON(classInstance)) } // eslint-disable-next-line @typescript-eslint/no-explicit-any public static deserialize(jsonString: string, Class: { new (...args: any[]): T }): T { - return deserialize(Class, jsonString, { exposeDefaultValues: true }) + return this.fromJSON(JSON.parse(jsonString), Class) } } diff --git a/packages/core/src/utils/__tests__/transformers.test.ts b/packages/core/src/utils/__tests__/transformers.test.ts index 67c7f3f653..6792f13a9c 100644 --- a/packages/core/src/utils/__tests__/transformers.test.ts +++ b/packages/core/src/utils/__tests__/transformers.test.ts @@ -1,4 +1,4 @@ -import { plainToClass } from 'class-transformer' +import { plainToInstance } from 'class-transformer' import { CredentialRecord, CredentialState } from '../../modules/credentials' @@ -14,7 +14,7 @@ describe('transformers', () => { metadata.credentialDefinitionId = 'abc:def:CL' // Converted old to new credentialRecord - const cr = plainToClass(CredentialRecord, jsonCredentialRecord) + const cr = plainToInstance(CredentialRecord, jsonCredentialRecord) expect(cr.metadata.data).toEqual({ '_internal/indyRequest': { diff --git a/yarn.lock b/yarn.lock index 8fe4e6ef1e..2fa13757c5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2291,9 +2291,9 @@ integrity sha512-Y2mHTRAbqfFkpjldbkHGY8JIzRN6XqYRliG8/24FcHm2D2PwW24fl5xMRTVGdrb7iMrwCaIEbLWerGIkXuFWVg== "@types/validator@^13.1.3": - version "13.6.3" - resolved "https://registry.yarnpkg.com/@types/validator/-/validator-13.6.3.tgz#31ca2e997bf13a0fffca30a25747d5b9f7dbb7de" - integrity sha512-fWG42pMJOL4jKsDDZZREnXLjc3UE0R8LOJfARWYg6U966rxDT7TYejYzLnUF5cvSObGg34nd0+H2wHHU5Omdfw== + version "13.7.0" + resolved "https://registry.yarnpkg.com/@types/validator/-/validator-13.7.0.tgz#fa25263656d234473025c2d48249a900053c355a" + integrity sha512-+jBxVvXVuggZOrm04NR8z+5+bgoW4VZyLzUO+hmPPW1mVFL/HaitLAkizfv4yg9TbG8lkfHWVMQ11yDqrVVCzA== "@types/ws@^7.4.4", "@types/ws@^7.4.6": version "7.4.7" @@ -3287,10 +3287,10 @@ cjs-module-lexer@^1.0.0: resolved "https://registry.yarnpkg.com/cjs-module-lexer/-/cjs-module-lexer-1.2.2.tgz#9f84ba3244a512f3a54e5277e8eef4c489864e40" integrity sha512-cOU9usZw8/dXIXKtwa8pM0OTJQuJkxMN6w30csNRUerHfeQ5R6U3kkU/FtJeIf3M202OHfY2U8ccInBG7/xogA== -class-transformer@^0.4.0: - version "0.4.0" - resolved "https://registry.yarnpkg.com/class-transformer/-/class-transformer-0.4.0.tgz#b52144117b423c516afb44cc1c76dbad31c2165b" - integrity sha512-ETWD/H2TbWbKEi7m9N4Km5+cw1hNcqJSxlSYhsLsNjQzWWiZIYA1zafxpK9PwVfaZ6AqR5rrjPVUBGESm5tQUA== +class-transformer@0.5.1: + version "0.5.1" + resolved "https://registry.yarnpkg.com/class-transformer/-/class-transformer-0.5.1.tgz#24147d5dffd2a6cea930a3250a677addf96ab336" + integrity sha512-SQa1Ws6hUbfC98vKGxZH3KFY0Y1lm5Zm0SY8XX9zbK7FJCyVEac3ATW0RIpwzW+oOfmHE5PMPufDG9hCfoEOMw== class-utils@^0.3.5: version "0.3.6" @@ -3302,7 +3302,7 @@ class-utils@^0.3.5: isobject "^3.0.0" static-extend "^0.1.1" -class-validator@^0.13.1: +class-validator@0.13.1: version "0.13.1" resolved "https://registry.yarnpkg.com/class-validator/-/class-validator-0.13.1.tgz#381b2001ee6b9e05afd133671fbdf760da7dec67" integrity sha512-zWIeYFhUitvAHBwNhDdCRK09hWx+P0HUwFE8US8/CxFpMVzkUK8RJl7yOIE+BVu2lxyPNgeOaFv78tLE47jBIg== @@ -6467,9 +6467,9 @@ libnpmpublish@^4.0.0: ssri "^8.0.1" libphonenumber-js@^1.9.7: - version "1.9.22" - resolved "https://registry.yarnpkg.com/libphonenumber-js/-/libphonenumber-js-1.9.22.tgz#b6b460603dedbd58f2d71f15500f216d70850fad" - integrity sha512-nE0aF0wrNq09ewF36s9FVqRW73hmpw6cobVDlbexmsu1432LEfuN24BCudNuRx4t2rElSeK/N0JbedzRW/TC4A== + version "1.9.43" + resolved "https://registry.yarnpkg.com/libphonenumber-js/-/libphonenumber-js-1.9.43.tgz#2371e4383e6780990381d5b900b8c22666221cbb" + integrity sha512-tNB87ZutAiAkl3DE/Bo0Mxqn/XZbNxhPg4v9bYBwQQW4dlhBGqXl1vtmPxeDWbrijzwOA9vRjOOFm5V9SK/W3w== lines-and-columns@^1.1.6: version "1.1.6"