From f1fa0f20b82edaff8f67eaeccdb0885d20dabd86 Mon Sep 17 00:00:00 2001 From: Ievgen Kyrpychenko Date: Fri, 28 Jan 2022 17:18:25 +0100 Subject: [PATCH] feat: TECH can not read property encode of undefined (#53) --- internal/generator/runner_generate.go | 25 ++++++++---------- internal/generator/runner_generate_class.go | 8 +++++- tests/__tests__/generated/Flavors.ts | 18 +++---------- tests/__tests__/generated/Regressions.ts | 22 ++++------------ tests/__tests__/generated/Test.ts | 26 +++++-------------- tests/__tests__/generated/common/Common.ts | 16 ++---------- tests/__tests__/generated/common/Extra.ts | 16 ++---------- .../generated/google/protobuf/Empty.ts | 21 +++++++-------- .../generated/google/protobuf/Timestamp.ts | 21 +++++++-------- tests/__tests__/generatedRedundant/Flavors.ts | 18 +++---------- .../generatedRedundant/Regressions.ts | 22 ++++------------ tests/__tests__/generatedRedundant/Test.ts | 26 +++++-------------- .../generatedRedundant/common/Common.ts | 16 ++---------- .../generatedRedundant/common/Extra.ts | 16 ++---------- .../google/protobuf/Empty.ts | 21 +++++++-------- .../google/protobuf/Timestamp.ts | 21 +++++++-------- tests/__tests__/regressions03.test.ts | 9 +++++++ 17 files changed, 101 insertions(+), 221 deletions(-) diff --git a/internal/generator/runner_generate.go b/internal/generator/runner_generate.go index d821a41..07d250b 100644 --- a/internal/generator/runner_generate.go +++ b/internal/generator/runner_generate.go @@ -123,7 +123,9 @@ func (r *Runner) generateTypescriptNamespace(generatedFileStream *protogen.Gener ) r.indentLevel += 2 - r.generateTypescriptClassDecoratorDefinition(generatedFileStream, protoFile) + if r.currentPackage == "google.protobuf" { + r.generateTypescriptCommonPackageDecoratorDefinition(generatedFileStream, protoFile) + } // This interface is namespace-private, as it's being replicated for every generated file r.P( @@ -146,22 +148,17 @@ func (r *Runner) generateTypescriptNamespace(generatedFileStream *protogen.Gener r.P(generatedFileStream, "\n}") } -func (r *Runner) generateTypescriptClassDecoratorDefinition(generatedFileStream *protogen.GeneratedFile, protoFile *protogen.File) { +func (r *Runner) generateTypescriptCommonPackageDecoratorDefinition(generatedFileStream *protogen.GeneratedFile, protoFile *protogen.File) { r.P( generatedFileStream, - "const registerGrpcClass = >(", - " typeName: string", - "): protobufjs.TypeDecorator => {", - " if (protobufjs.util.decorateRoot.get(typeName) != null) {", - " // eslint-disable-next-line @typescript-eslint/ban-types", - " return (", - " // eslint-disable-next-line @typescript-eslint/no-unused-vars", - " _: protobufjs.Constructor", - " ): void => {", - " // Do nothing", - " }", + "const registerCommonClass = >(typeName: string): protobufjs.TypeDecorator => {", + " const registeredType = protobufjs.util.decorateRoot.get(typeName)", + " if (registeredType == null) {", + " return protobufjs.Type.d(typeName)", + " }", + " return (ctor: protobufjs.Constructor): void => {", + " Object.defineProperty(ctor, '$type', { value: registeredType, enumerable: false })", " }", - " return protobufjs.Type.d(typeName)", "}", ) } diff --git a/internal/generator/runner_generate_class.go b/internal/generator/runner_generate_class.go index ccf2111..c0c0021 100644 --- a/internal/generator/runner_generate_class.go +++ b/internal/generator/runner_generate_class.go @@ -130,9 +130,15 @@ func (r *Runner) generateTypescriptMessageClass(generatedFileStream *protogen.Ge if !hasEnums { implementedInterfaces += ", I" + className } + + classDecoratorName := "@protobufjs.Type.d" + if r.currentPackage == "google.protobuf" { + classDecoratorName = "@registerCommonClass" + } + r.P( generatedFileStream, - "@registerGrpcClass('"+strings.Replace(r.currentPackage, ".", "_", -1)+"_"+className+"')", + classDecoratorName+"('"+strings.Replace(r.currentPackage, ".", "_", -1)+"_"+className+"')", "export class "+className+" extends protobufjs.Message<"+className+"> implements "+implementedInterfaces+" {\n", ) r.indentLevel += 2 diff --git a/tests/__tests__/generated/Flavors.ts b/tests/__tests__/generated/Flavors.ts index f6f4f53..b98df00 100644 --- a/tests/__tests__/generated/Flavors.ts +++ b/tests/__tests__/generated/Flavors.ts @@ -1,5 +1,5 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468 +// GENERATOR VERSION: 2.1.0.d41be8f.1643383265 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as joinGRPC from '@join-com/grpc' @@ -11,18 +11,6 @@ import { grpc } from '@join-com/grpc' // eslint-disable-next-line @typescript-eslint/no-namespace export namespace Flavors { - const registerGrpcClass = >(typeName: string): protobufjs.TypeDecorator => { - if (protobufjs.util.decorateRoot.get(typeName) != null) { - // eslint-disable-next-line @typescript-eslint/ban-types - return ( - // eslint-disable-next-line @typescript-eslint/no-unused-vars - _: protobufjs.Constructor, - ): void => { - // Do nothing - } - } - return protobufjs.Type.d(typeName) - } interface ConvertibleTo { asInterface(): T } @@ -40,7 +28,7 @@ export namespace Flavors { emails: Email[] } - @registerGrpcClass('flavors_UserProfile') + @protobufjs.Type.d('flavors_UserProfile') export class UserProfile extends protobufjs.Message implements ConvertibleTo, IUserProfile @@ -94,7 +82,7 @@ export namespace Flavors { } } - @registerGrpcClass('flavors_UserRequest') + @protobufjs.Type.d('flavors_UserRequest') export class UserRequest extends protobufjs.Message implements ConvertibleTo, IUserRequest diff --git a/tests/__tests__/generated/Regressions.ts b/tests/__tests__/generated/Regressions.ts index 63f45ee..6b561c3 100644 --- a/tests/__tests__/generated/Regressions.ts +++ b/tests/__tests__/generated/Regressions.ts @@ -1,23 +1,11 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468 +// GENERATOR VERSION: 2.1.0.d41be8f.1643383265 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as protobufjs from 'protobufjs/light' // eslint-disable-next-line @typescript-eslint/no-namespace export namespace Regressions { - const registerGrpcClass = >(typeName: string): protobufjs.TypeDecorator => { - if (protobufjs.util.decorateRoot.get(typeName) != null) { - // eslint-disable-next-line @typescript-eslint/ban-types - return ( - // eslint-disable-next-line @typescript-eslint/no-unused-vars - _: protobufjs.Constructor, - ): void => { - // Do nothing - } - } - return protobufjs.Type.d(typeName) - } interface ConvertibleTo { asInterface(): T } @@ -52,7 +40,7 @@ export namespace Regressions { /** * @deprecated */ - @registerGrpcClass('regressions_DeprecatedMessageWithDeprecatedField') + @protobufjs.Type.d('regressions_DeprecatedMessageWithDeprecatedField') export class DeprecatedMessageWithDeprecatedField extends protobufjs.Message implements ConvertibleTo, IDeprecatedMessageWithDeprecatedField @@ -102,7 +90,7 @@ export namespace Regressions { } } - @registerGrpcClass('regressions_MessageWithDeprecatedField') + @protobufjs.Type.d('regressions_MessageWithDeprecatedField') export class MessageWithDeprecatedField extends protobufjs.Message implements ConvertibleTo, IMessageWithDeprecatedField @@ -146,7 +134,7 @@ export namespace Regressions { } } - @registerGrpcClass('regressions_Reg01Inner') + @protobufjs.Type.d('regressions_Reg01Inner') export class Reg01Inner extends protobufjs.Message implements ConvertibleTo, IReg01Inner { @protobufjs.Field.d(1, 'string', 'optional') public value?: string @@ -177,7 +165,7 @@ export namespace Regressions { } } - @registerGrpcClass('regressions_Reg01Outer') + @protobufjs.Type.d('regressions_Reg01Outer') export class Reg01Outer extends protobufjs.Message implements ConvertibleTo, IReg01Outer { @protobufjs.Field.d(1, Reg01Inner, 'optional') public inner?: Reg01Inner diff --git a/tests/__tests__/generated/Test.ts b/tests/__tests__/generated/Test.ts index 7cb3a90..f15e393 100644 --- a/tests/__tests__/generated/Test.ts +++ b/tests/__tests__/generated/Test.ts @@ -1,5 +1,5 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468 +// GENERATOR VERSION: 2.1.0.d41be8f.1643383265 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as joinGRPC from '@join-com/grpc' @@ -14,18 +14,6 @@ import { grpc } from '@join-com/grpc' // eslint-disable-next-line @typescript-eslint/no-namespace export namespace Foo { - const registerGrpcClass = >(typeName: string): protobufjs.TypeDecorator => { - if (protobufjs.util.decorateRoot.get(typeName) != null) { - // eslint-disable-next-line @typescript-eslint/ban-types - return ( - // eslint-disable-next-line @typescript-eslint/no-unused-vars - _: protobufjs.Constructor, - ): void => { - // Do nothing - } - } - return protobufjs.Type.d(typeName) - } interface ConvertibleTo { asInterface(): T } @@ -114,7 +102,7 @@ export namespace Foo { optionalField?: number } - @registerGrpcClass('foo_CustomOptionsTest') + @protobufjs.Type.d('foo_CustomOptionsTest') export class CustomOptionsTest extends protobufjs.Message implements ConvertibleTo @@ -179,7 +167,7 @@ export namespace Foo { /** * @deprecated */ - @registerGrpcClass('foo_Nested') + @protobufjs.Type.d('foo_Nested') export class Nested extends protobufjs.Message implements ConvertibleTo, INested { @protobufjs.Field.d(1, 'string', 'optional') public title?: string @@ -210,7 +198,7 @@ export namespace Foo { } } - @registerGrpcClass('foo_Request') + @protobufjs.Type.d('foo_Request') export class Request extends protobufjs.Message implements ConvertibleTo, IRequest { @protobufjs.Field.d(1, 'int32', 'optional') public id?: number @@ -241,7 +229,7 @@ export namespace Foo { } } - @registerGrpcClass('foo_RequiredPropertiesTest') + @protobufjs.Type.d('foo_RequiredPropertiesTest') export class RequiredPropertiesTest extends protobufjs.Message implements ConvertibleTo, IRequiredPropertiesTest @@ -296,7 +284,7 @@ export namespace Foo { } } - @registerGrpcClass('foo_Test') + @protobufjs.Type.d('foo_Test') export class Test extends protobufjs.Message implements ConvertibleTo { @protobufjs.Field.d(1, 'int32', 'optional') public fieldInt32?: number @@ -475,7 +463,7 @@ export namespace Foo { } } - @registerGrpcClass('foo_BigWrapper') + @protobufjs.Type.d('foo_BigWrapper') export class BigWrapper extends protobufjs.Message implements ConvertibleTo { @protobufjs.Field.d(1, Test, 'optional') public nestedTest?: Test diff --git a/tests/__tests__/generated/common/Common.ts b/tests/__tests__/generated/common/Common.ts index 787b23a..92983a0 100644 --- a/tests/__tests__/generated/common/Common.ts +++ b/tests/__tests__/generated/common/Common.ts @@ -1,23 +1,11 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468 +// GENERATOR VERSION: 2.1.0.d41be8f.1643383265 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as protobufjs from 'protobufjs/light' // eslint-disable-next-line @typescript-eslint/no-namespace export namespace Common { - const registerGrpcClass = >(typeName: string): protobufjs.TypeDecorator => { - if (protobufjs.util.decorateRoot.get(typeName) != null) { - // eslint-disable-next-line @typescript-eslint/ban-types - return ( - // eslint-disable-next-line @typescript-eslint/no-unused-vars - _: protobufjs.Constructor, - ): void => { - // Do nothing - } - } - return protobufjs.Type.d(typeName) - } interface ConvertibleTo { asInterface(): T } @@ -30,7 +18,7 @@ export namespace Common { latsName?: string } - @registerGrpcClass('common_OtherPkgMessage') + @protobufjs.Type.d('common_OtherPkgMessage') export class OtherPkgMessage extends protobufjs.Message implements ConvertibleTo, IOtherPkgMessage diff --git a/tests/__tests__/generated/common/Extra.ts b/tests/__tests__/generated/common/Extra.ts index 83e9fba..0d0b675 100644 --- a/tests/__tests__/generated/common/Extra.ts +++ b/tests/__tests__/generated/common/Extra.ts @@ -1,5 +1,5 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468 +// GENERATOR VERSION: 2.1.0.d41be8f.1643383265 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as protobufjs from 'protobufjs/light' @@ -8,18 +8,6 @@ import { GoogleProtobuf } from '../google/protobuf/Timestamp' // eslint-disable-next-line @typescript-eslint/no-namespace export namespace Common { - const registerGrpcClass = >(typeName: string): protobufjs.TypeDecorator => { - if (protobufjs.util.decorateRoot.get(typeName) != null) { - // eslint-disable-next-line @typescript-eslint/ban-types - return ( - // eslint-disable-next-line @typescript-eslint/no-unused-vars - _: protobufjs.Constructor, - ): void => { - // Do nothing - } - } - return protobufjs.Type.d(typeName) - } interface ConvertibleTo { asInterface(): T } @@ -33,7 +21,7 @@ export namespace Common { birthDate?: Date } - @registerGrpcClass('common_ExtraPkgMessage') + @protobufjs.Type.d('common_ExtraPkgMessage') export class ExtraPkgMessage extends protobufjs.Message implements ConvertibleTo { /** * @deprecated diff --git a/tests/__tests__/generated/google/protobuf/Empty.ts b/tests/__tests__/generated/google/protobuf/Empty.ts index 71df30f..13c8af2 100644 --- a/tests/__tests__/generated/google/protobuf/Empty.ts +++ b/tests/__tests__/generated/google/protobuf/Empty.ts @@ -1,22 +1,19 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468 +// GENERATOR VERSION: 2.1.0.d41be8f.1643383265 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as protobufjs from 'protobufjs/light' // eslint-disable-next-line @typescript-eslint/no-namespace export namespace GoogleProtobuf { - const registerGrpcClass = >(typeName: string): protobufjs.TypeDecorator => { - if (protobufjs.util.decorateRoot.get(typeName) != null) { - // eslint-disable-next-line @typescript-eslint/ban-types - return ( - // eslint-disable-next-line @typescript-eslint/no-unused-vars - _: protobufjs.Constructor, - ): void => { - // Do nothing - } + const registerCommonClass = >(typeName: string): protobufjs.TypeDecorator => { + const registeredType = protobufjs.util.decorateRoot.get(typeName) + if (registeredType == null) { + return protobufjs.Type.d(typeName) + } + return (ctor: protobufjs.Constructor): void => { + Object.defineProperty(ctor, '$type', { value: registeredType, enumerable: false }) } - return protobufjs.Type.d(typeName) } interface ConvertibleTo { asInterface(): T @@ -24,7 +21,7 @@ export namespace GoogleProtobuf { export interface IEmpty {} - @registerGrpcClass('google_protobuf_Empty') + @registerCommonClass('google_protobuf_Empty') export class Empty extends protobufjs.Message implements ConvertibleTo, IEmpty { public asInterface(): IEmpty { const message = { diff --git a/tests/__tests__/generated/google/protobuf/Timestamp.ts b/tests/__tests__/generated/google/protobuf/Timestamp.ts index 106ed08..85fd505 100644 --- a/tests/__tests__/generated/google/protobuf/Timestamp.ts +++ b/tests/__tests__/generated/google/protobuf/Timestamp.ts @@ -1,22 +1,19 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468 +// GENERATOR VERSION: 2.1.0.d41be8f.1643383265 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as protobufjs from 'protobufjs/light' // eslint-disable-next-line @typescript-eslint/no-namespace export namespace GoogleProtobuf { - const registerGrpcClass = >(typeName: string): protobufjs.TypeDecorator => { - if (protobufjs.util.decorateRoot.get(typeName) != null) { - // eslint-disable-next-line @typescript-eslint/ban-types - return ( - // eslint-disable-next-line @typescript-eslint/no-unused-vars - _: protobufjs.Constructor, - ): void => { - // Do nothing - } + const registerCommonClass = >(typeName: string): protobufjs.TypeDecorator => { + const registeredType = protobufjs.util.decorateRoot.get(typeName) + if (registeredType == null) { + return protobufjs.Type.d(typeName) + } + return (ctor: protobufjs.Constructor): void => { + Object.defineProperty(ctor, '$type', { value: registeredType, enumerable: false }) } - return protobufjs.Type.d(typeName) } interface ConvertibleTo { asInterface(): T @@ -27,7 +24,7 @@ export namespace GoogleProtobuf { nanos?: number } - @registerGrpcClass('google_protobuf_Timestamp') + @registerCommonClass('google_protobuf_Timestamp') export class Timestamp extends protobufjs.Message implements ConvertibleTo, ITimestamp { @protobufjs.Field.d(1, 'int64', 'optional') public seconds?: number diff --git a/tests/__tests__/generatedRedundant/Flavors.ts b/tests/__tests__/generatedRedundant/Flavors.ts index f6f4f53..b98df00 100644 --- a/tests/__tests__/generatedRedundant/Flavors.ts +++ b/tests/__tests__/generatedRedundant/Flavors.ts @@ -1,5 +1,5 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468 +// GENERATOR VERSION: 2.1.0.d41be8f.1643383265 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as joinGRPC from '@join-com/grpc' @@ -11,18 +11,6 @@ import { grpc } from '@join-com/grpc' // eslint-disable-next-line @typescript-eslint/no-namespace export namespace Flavors { - const registerGrpcClass = >(typeName: string): protobufjs.TypeDecorator => { - if (protobufjs.util.decorateRoot.get(typeName) != null) { - // eslint-disable-next-line @typescript-eslint/ban-types - return ( - // eslint-disable-next-line @typescript-eslint/no-unused-vars - _: protobufjs.Constructor, - ): void => { - // Do nothing - } - } - return protobufjs.Type.d(typeName) - } interface ConvertibleTo { asInterface(): T } @@ -40,7 +28,7 @@ export namespace Flavors { emails: Email[] } - @registerGrpcClass('flavors_UserProfile') + @protobufjs.Type.d('flavors_UserProfile') export class UserProfile extends protobufjs.Message implements ConvertibleTo, IUserProfile @@ -94,7 +82,7 @@ export namespace Flavors { } } - @registerGrpcClass('flavors_UserRequest') + @protobufjs.Type.d('flavors_UserRequest') export class UserRequest extends protobufjs.Message implements ConvertibleTo, IUserRequest diff --git a/tests/__tests__/generatedRedundant/Regressions.ts b/tests/__tests__/generatedRedundant/Regressions.ts index 63f45ee..6b561c3 100644 --- a/tests/__tests__/generatedRedundant/Regressions.ts +++ b/tests/__tests__/generatedRedundant/Regressions.ts @@ -1,23 +1,11 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468 +// GENERATOR VERSION: 2.1.0.d41be8f.1643383265 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as protobufjs from 'protobufjs/light' // eslint-disable-next-line @typescript-eslint/no-namespace export namespace Regressions { - const registerGrpcClass = >(typeName: string): protobufjs.TypeDecorator => { - if (protobufjs.util.decorateRoot.get(typeName) != null) { - // eslint-disable-next-line @typescript-eslint/ban-types - return ( - // eslint-disable-next-line @typescript-eslint/no-unused-vars - _: protobufjs.Constructor, - ): void => { - // Do nothing - } - } - return protobufjs.Type.d(typeName) - } interface ConvertibleTo { asInterface(): T } @@ -52,7 +40,7 @@ export namespace Regressions { /** * @deprecated */ - @registerGrpcClass('regressions_DeprecatedMessageWithDeprecatedField') + @protobufjs.Type.d('regressions_DeprecatedMessageWithDeprecatedField') export class DeprecatedMessageWithDeprecatedField extends protobufjs.Message implements ConvertibleTo, IDeprecatedMessageWithDeprecatedField @@ -102,7 +90,7 @@ export namespace Regressions { } } - @registerGrpcClass('regressions_MessageWithDeprecatedField') + @protobufjs.Type.d('regressions_MessageWithDeprecatedField') export class MessageWithDeprecatedField extends protobufjs.Message implements ConvertibleTo, IMessageWithDeprecatedField @@ -146,7 +134,7 @@ export namespace Regressions { } } - @registerGrpcClass('regressions_Reg01Inner') + @protobufjs.Type.d('regressions_Reg01Inner') export class Reg01Inner extends protobufjs.Message implements ConvertibleTo, IReg01Inner { @protobufjs.Field.d(1, 'string', 'optional') public value?: string @@ -177,7 +165,7 @@ export namespace Regressions { } } - @registerGrpcClass('regressions_Reg01Outer') + @protobufjs.Type.d('regressions_Reg01Outer') export class Reg01Outer extends protobufjs.Message implements ConvertibleTo, IReg01Outer { @protobufjs.Field.d(1, Reg01Inner, 'optional') public inner?: Reg01Inner diff --git a/tests/__tests__/generatedRedundant/Test.ts b/tests/__tests__/generatedRedundant/Test.ts index 7cb3a90..f15e393 100644 --- a/tests/__tests__/generatedRedundant/Test.ts +++ b/tests/__tests__/generatedRedundant/Test.ts @@ -1,5 +1,5 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468 +// GENERATOR VERSION: 2.1.0.d41be8f.1643383265 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as joinGRPC from '@join-com/grpc' @@ -14,18 +14,6 @@ import { grpc } from '@join-com/grpc' // eslint-disable-next-line @typescript-eslint/no-namespace export namespace Foo { - const registerGrpcClass = >(typeName: string): protobufjs.TypeDecorator => { - if (protobufjs.util.decorateRoot.get(typeName) != null) { - // eslint-disable-next-line @typescript-eslint/ban-types - return ( - // eslint-disable-next-line @typescript-eslint/no-unused-vars - _: protobufjs.Constructor, - ): void => { - // Do nothing - } - } - return protobufjs.Type.d(typeName) - } interface ConvertibleTo { asInterface(): T } @@ -114,7 +102,7 @@ export namespace Foo { optionalField?: number } - @registerGrpcClass('foo_CustomOptionsTest') + @protobufjs.Type.d('foo_CustomOptionsTest') export class CustomOptionsTest extends protobufjs.Message implements ConvertibleTo @@ -179,7 +167,7 @@ export namespace Foo { /** * @deprecated */ - @registerGrpcClass('foo_Nested') + @protobufjs.Type.d('foo_Nested') export class Nested extends protobufjs.Message implements ConvertibleTo, INested { @protobufjs.Field.d(1, 'string', 'optional') public title?: string @@ -210,7 +198,7 @@ export namespace Foo { } } - @registerGrpcClass('foo_Request') + @protobufjs.Type.d('foo_Request') export class Request extends protobufjs.Message implements ConvertibleTo, IRequest { @protobufjs.Field.d(1, 'int32', 'optional') public id?: number @@ -241,7 +229,7 @@ export namespace Foo { } } - @registerGrpcClass('foo_RequiredPropertiesTest') + @protobufjs.Type.d('foo_RequiredPropertiesTest') export class RequiredPropertiesTest extends protobufjs.Message implements ConvertibleTo, IRequiredPropertiesTest @@ -296,7 +284,7 @@ export namespace Foo { } } - @registerGrpcClass('foo_Test') + @protobufjs.Type.d('foo_Test') export class Test extends protobufjs.Message implements ConvertibleTo { @protobufjs.Field.d(1, 'int32', 'optional') public fieldInt32?: number @@ -475,7 +463,7 @@ export namespace Foo { } } - @registerGrpcClass('foo_BigWrapper') + @protobufjs.Type.d('foo_BigWrapper') export class BigWrapper extends protobufjs.Message implements ConvertibleTo { @protobufjs.Field.d(1, Test, 'optional') public nestedTest?: Test diff --git a/tests/__tests__/generatedRedundant/common/Common.ts b/tests/__tests__/generatedRedundant/common/Common.ts index 787b23a..92983a0 100644 --- a/tests/__tests__/generatedRedundant/common/Common.ts +++ b/tests/__tests__/generatedRedundant/common/Common.ts @@ -1,23 +1,11 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468 +// GENERATOR VERSION: 2.1.0.d41be8f.1643383265 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as protobufjs from 'protobufjs/light' // eslint-disable-next-line @typescript-eslint/no-namespace export namespace Common { - const registerGrpcClass = >(typeName: string): protobufjs.TypeDecorator => { - if (protobufjs.util.decorateRoot.get(typeName) != null) { - // eslint-disable-next-line @typescript-eslint/ban-types - return ( - // eslint-disable-next-line @typescript-eslint/no-unused-vars - _: protobufjs.Constructor, - ): void => { - // Do nothing - } - } - return protobufjs.Type.d(typeName) - } interface ConvertibleTo { asInterface(): T } @@ -30,7 +18,7 @@ export namespace Common { latsName?: string } - @registerGrpcClass('common_OtherPkgMessage') + @protobufjs.Type.d('common_OtherPkgMessage') export class OtherPkgMessage extends protobufjs.Message implements ConvertibleTo, IOtherPkgMessage diff --git a/tests/__tests__/generatedRedundant/common/Extra.ts b/tests/__tests__/generatedRedundant/common/Extra.ts index 83e9fba..0d0b675 100644 --- a/tests/__tests__/generatedRedundant/common/Extra.ts +++ b/tests/__tests__/generatedRedundant/common/Extra.ts @@ -1,5 +1,5 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468 +// GENERATOR VERSION: 2.1.0.d41be8f.1643383265 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as protobufjs from 'protobufjs/light' @@ -8,18 +8,6 @@ import { GoogleProtobuf } from '../google/protobuf/Timestamp' // eslint-disable-next-line @typescript-eslint/no-namespace export namespace Common { - const registerGrpcClass = >(typeName: string): protobufjs.TypeDecorator => { - if (protobufjs.util.decorateRoot.get(typeName) != null) { - // eslint-disable-next-line @typescript-eslint/ban-types - return ( - // eslint-disable-next-line @typescript-eslint/no-unused-vars - _: protobufjs.Constructor, - ): void => { - // Do nothing - } - } - return protobufjs.Type.d(typeName) - } interface ConvertibleTo { asInterface(): T } @@ -33,7 +21,7 @@ export namespace Common { birthDate?: Date } - @registerGrpcClass('common_ExtraPkgMessage') + @protobufjs.Type.d('common_ExtraPkgMessage') export class ExtraPkgMessage extends protobufjs.Message implements ConvertibleTo { /** * @deprecated diff --git a/tests/__tests__/generatedRedundant/google/protobuf/Empty.ts b/tests/__tests__/generatedRedundant/google/protobuf/Empty.ts index 71df30f..13c8af2 100644 --- a/tests/__tests__/generatedRedundant/google/protobuf/Empty.ts +++ b/tests/__tests__/generatedRedundant/google/protobuf/Empty.ts @@ -1,22 +1,19 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468 +// GENERATOR VERSION: 2.1.0.d41be8f.1643383265 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as protobufjs from 'protobufjs/light' // eslint-disable-next-line @typescript-eslint/no-namespace export namespace GoogleProtobuf { - const registerGrpcClass = >(typeName: string): protobufjs.TypeDecorator => { - if (protobufjs.util.decorateRoot.get(typeName) != null) { - // eslint-disable-next-line @typescript-eslint/ban-types - return ( - // eslint-disable-next-line @typescript-eslint/no-unused-vars - _: protobufjs.Constructor, - ): void => { - // Do nothing - } + const registerCommonClass = >(typeName: string): protobufjs.TypeDecorator => { + const registeredType = protobufjs.util.decorateRoot.get(typeName) + if (registeredType == null) { + return protobufjs.Type.d(typeName) + } + return (ctor: protobufjs.Constructor): void => { + Object.defineProperty(ctor, '$type', { value: registeredType, enumerable: false }) } - return protobufjs.Type.d(typeName) } interface ConvertibleTo { asInterface(): T @@ -24,7 +21,7 @@ export namespace GoogleProtobuf { export interface IEmpty {} - @registerGrpcClass('google_protobuf_Empty') + @registerCommonClass('google_protobuf_Empty') export class Empty extends protobufjs.Message implements ConvertibleTo, IEmpty { public asInterface(): IEmpty { const message = { diff --git a/tests/__tests__/generatedRedundant/google/protobuf/Timestamp.ts b/tests/__tests__/generatedRedundant/google/protobuf/Timestamp.ts index 106ed08..85fd505 100644 --- a/tests/__tests__/generatedRedundant/google/protobuf/Timestamp.ts +++ b/tests/__tests__/generatedRedundant/google/protobuf/Timestamp.ts @@ -1,22 +1,19 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468 +// GENERATOR VERSION: 2.1.0.d41be8f.1643383265 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as protobufjs from 'protobufjs/light' // eslint-disable-next-line @typescript-eslint/no-namespace export namespace GoogleProtobuf { - const registerGrpcClass = >(typeName: string): protobufjs.TypeDecorator => { - if (protobufjs.util.decorateRoot.get(typeName) != null) { - // eslint-disable-next-line @typescript-eslint/ban-types - return ( - // eslint-disable-next-line @typescript-eslint/no-unused-vars - _: protobufjs.Constructor, - ): void => { - // Do nothing - } + const registerCommonClass = >(typeName: string): protobufjs.TypeDecorator => { + const registeredType = protobufjs.util.decorateRoot.get(typeName) + if (registeredType == null) { + return protobufjs.Type.d(typeName) + } + return (ctor: protobufjs.Constructor): void => { + Object.defineProperty(ctor, '$type', { value: registeredType, enumerable: false }) } - return protobufjs.Type.d(typeName) } interface ConvertibleTo { asInterface(): T @@ -27,7 +24,7 @@ export namespace GoogleProtobuf { nanos?: number } - @registerGrpcClass('google_protobuf_Timestamp') + @registerCommonClass('google_protobuf_Timestamp') export class Timestamp extends protobufjs.Message implements ConvertibleTo, ITimestamp { @protobufjs.Field.d(1, 'int64', 'optional') public seconds?: number diff --git a/tests/__tests__/regressions03.test.ts b/tests/__tests__/regressions03.test.ts index caaf707..70d266c 100644 --- a/tests/__tests__/regressions03.test.ts +++ b/tests/__tests__/regressions03.test.ts @@ -2,6 +2,9 @@ import { GoogleProtobuf as gp1 } from './generated/google/protobuf/Timestamp' import { GoogleProtobuf as gp2 } from './generatedRedundant/google/protobuf/Timestamp' +import { GoogleProtobuf as gp3 } from './generated/google/protobuf/Empty' +import { GoogleProtobuf as gp4 } from './generatedRedundant/google/protobuf/Empty' + describe('regressions 03', () => { it('dummy test', () => { // This test has code only to ensure that the linters don't complain because of unused imports. @@ -10,5 +13,11 @@ describe('regressions 03', () => { expect(_ts1.asInterface().seconds).toEqual(_ts2.asInterface().seconds) expect(_ts1.asInterface().nanos).toEqual(_ts2.asInterface().nanos) + + const _ts3 = gp3.Empty.fromInterface({}) + const _ts4 = gp4.Empty.fromInterface({ seconds: 0, nanos: 0 }) + + expect(_ts3.asInterface()).toEqual({}) + expect(_ts4.asInterface()).toEqual({}) }) })