Skip to content

Commit

Permalink
fix(core)!: update class transformer library (#547)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
TimoGlastra authored Nov 24, 2021
1 parent 5545a2a commit dee03e3
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 62 deletions.
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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',
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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}`
)
})
Expand All @@ -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)

Expand Down Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ClassConstructor } from 'class-transformer'

import { classToPlain, plainToClass } from 'class-transformer'
import { instanceToPlain, plainToInstance } from 'class-transformer'

import {
PublicKeyTransformer,
Expand Down Expand Up @@ -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)
Expand All @@ -67,7 +67,7 @@ describe('Did | PublicKey', () => {
const publicKey = new PublicKey({
...json,
})
const transformed = classToPlain(publicKey)
const transformed = instanceToPlain(publicKey)
expect(transformed).toEqual(json)
})

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { classToPlain, plainToClass } from 'class-transformer'
import { instanceToPlain, plainToInstance } from 'class-transformer'

import { Service, ServiceTransformer, serviceTypes, IndyAgentService, DidCommService } from '../service'

Expand All @@ -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)
Expand All @@ -27,7 +27,7 @@ describe('Did | Service', () => {
...json,
})

const transformed = classToPlain(service)
const transformed = instanceToPlain(service)

expect(transformed).toEqual(json)
})
Expand All @@ -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)
})
Expand All @@ -61,7 +61,7 @@ describe('Did | Service', () => {
...json,
})

const transformed = classToPlain(service)
const transformed = instanceToPlain(service)

expect(transformed).toEqual(json)
})
Expand All @@ -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)
})
})

Expand All @@ -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)
})
Expand All @@ -117,7 +117,7 @@ describe('Did | Service', () => {
...json,
})

const transformed = classToPlain(service)
const transformed = instanceToPlain(service)

expect(transformed).toEqual(json)
})
Expand All @@ -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)
})
})

Expand All @@ -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)

Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -50,17 +50,17 @@ export function AuthenticationTransformer() {

// Referenced keys use other types than embedded keys.
const publicKeyClass = (publicKeyTypes[publicKeyJson.type] ?? PublicKey) as ClassConstructor<PublicKey>
const publicKey = plainToClass<PublicKey, unknown>(publicKeyClass, publicKeyJson)
const publicKey = plainToInstance<PublicKey, unknown>(publicKeyClass, publicKeyJson)
return new ReferencedAuthentication(publicKey, auth.type)
} else {
// embedded
const publicKeyClass = (publicKeyTypes[auth.type] ?? PublicKey) as ClassConstructor<PublicKey>
const publicKey = plainToClass<PublicKey, unknown>(publicKeyClass, auth)
const publicKey = plainToInstance<PublicKey, unknown>(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))
}
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -27,7 +27,7 @@ export function PublicKeyTransformer() {
({ value }: { value: { type: string }[] }) => {
return value.map((publicKeyJson) => {
const publicKeyClass = (publicKeyTypes[publicKeyJson.type] ?? PublicKey) as ClassConstructor<PublicKey>
const publicKey = plainToClass<PublicKey, unknown>(publicKeyClass, publicKeyJson)
const publicKey = plainToInstance<PublicKey, unknown>(publicKeyClass, publicKeyJson)

return publicKey
})
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -25,7 +25,7 @@ export function ServiceTransformer() {
({ value }: { value: { type: string }[] }) => {
return value.map((serviceJson) => {
const serviceClass = (serviceTypes[serviceJson.type] ?? Service) as ClassConstructor<Service>
const service = plainToClass<Service, unknown>(serviceClass, serviceJson)
const service = plainToInstance<Service, unknown>(serviceClass, serviceJson)

return service
})
Expand Down
12 changes: 5 additions & 7 deletions packages/core/src/utils/JsonTransformer.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import { classToPlain, deserialize, plainToClass, serialize } from 'class-transformer'
import { instanceToPlain, plainToInstance } from 'class-transformer'

export class JsonTransformer {
public static toJSON<T>(classInstance: T) {
return classToPlain(classInstance, {
return instanceToPlain(classInstance, {
exposeDefaultValues: true,
})
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
public static fromJSON<T>(json: any, Class: { new (...args: any[]): T }): T {
return plainToClass(Class, json, { exposeDefaultValues: true })
return plainToInstance(Class, json, { exposeDefaultValues: true })
}

public static serialize<T>(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<T>(jsonString: string, Class: { new (...args: any[]): T }): T {
return deserialize(Class, jsonString, { exposeDefaultValues: true })
return this.fromJSON(JSON.parse(jsonString), Class)
}
}
4 changes: 2 additions & 2 deletions packages/core/src/utils/__tests__/transformers.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { plainToClass } from 'class-transformer'
import { plainToInstance } from 'class-transformer'

import { CredentialRecord, CredentialState } from '../../modules/credentials'

Expand All @@ -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': {
Expand Down
Loading

0 comments on commit dee03e3

Please sign in to comment.