From fa13ec752c6564af045081548f5fc5cabb687151 Mon Sep 17 00:00:00 2001 From: Andre Hahn Pereira Date: Wed, 16 Aug 2023 14:53:44 -0300 Subject: [PATCH] fix: use correct imports for optional fields (#904) It seems that the ts-poet.Code.toString() changes the struct and it was causing the code gen to fail to use the correct imports in some situations. Added a struct which was failing to compile before the fix. --- integration/avoid-import-conflicts/simple.bin | Bin 1947 -> 2234 bytes .../avoid-import-conflicts/simple.proto | 5 ++ integration/avoid-import-conflicts/simple.ts | 83 ++++++++++++++++++ src/main.ts | 3 - 4 files changed, 88 insertions(+), 3 deletions(-) diff --git a/integration/avoid-import-conflicts/simple.bin b/integration/avoid-import-conflicts/simple.bin index 45366f7ffdb28e59b47d1b793bcbada9f88fe05d..aa18fec38e2a257b9e600789a5e28a3262ed2fa6 100644 GIT binary patch delta 666 zcmZvY%SyvQ6ozNc978&7sTmVvQ=8sYTnS#5f>2ykTnP06q(X{?-Y5YVF1qbnm--Nb zJ6}K$pT!q&;v+Bu9EW)!M>#q%0oOeSR+^rjabj2Gld^yrl z$XMr@`yYRJ&X=JqN<Sr3!r3)ia|NFM7|)Z}Q|fM1PZyA9@{s&0bH&%304!4ez=g->O@^o4X^&tVwxuLYjp63~&hOTm?F>9zTBw1J$!mL?eI_HSVbE{4@-e$*yT3cTvAITYkhO_~| z7qSn8VhFKh@P)2VKI3_zmdVEktwp^`IwQK6bpYsM)`3ur;ql-D!WR!dt2}pVNIomI luJ+=yN_;)t%LxLAdYgVph(NRJl0nzs+&$Q*=k3dO^ADRM9BKdn diff --git a/integration/avoid-import-conflicts/simple.proto b/integration/avoid-import-conflicts/simple.proto index bd0886204..4cc37d287 100644 --- a/integration/avoid-import-conflicts/simple.proto +++ b/integration/avoid-import-conflicts/simple.proto @@ -13,6 +13,11 @@ message Simple { simple2.Simple otherSimple = 2; } +message DifferentSimple { + string name = 1; + optional simple2.Simple otherOptionalSimple2 = 2; +} + message SimpleEnums { SimpleEnum local_enum = 1; simple2.SimpleEnum import_enum = 2; diff --git a/integration/avoid-import-conflicts/simple.ts b/integration/avoid-import-conflicts/simple.ts index 53fa95643..0fd6d0e70 100644 --- a/integration/avoid-import-conflicts/simple.ts +++ b/integration/avoid-import-conflicts/simple.ts @@ -56,6 +56,11 @@ export interface Simple { otherSimple: Simple3 | undefined; } +export interface DifferentSimple { + name: string; + otherOptionalSimple2?: Simple3 | undefined; +} + export interface SimpleEnums { localEnum: SimpleEnum; importEnum: SimpleEnum1; @@ -145,6 +150,84 @@ export const Simple = { }, }; +function createBaseDifferentSimple(): DifferentSimple { + return { name: "", otherOptionalSimple2: undefined }; +} + +export const DifferentSimple = { + encode(message: DifferentSimple, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer { + if (message.name !== "") { + writer.uint32(10).string(message.name); + } + if (message.otherOptionalSimple2 !== undefined) { + Simple3.encode(message.otherOptionalSimple2, writer.uint32(18).fork()).ldelim(); + } + return writer; + }, + + decode(input: _m0.Reader | Uint8Array, length?: number): DifferentSimple { + const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = createBaseDifferentSimple(); + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + if (tag !== 10) { + break; + } + + message.name = reader.string(); + continue; + case 2: + if (tag !== 18) { + break; + } + + message.otherOptionalSimple2 = Simple3.decode(reader, reader.uint32()); + continue; + } + if ((tag & 7) === 4 || tag === 0) { + break; + } + reader.skipType(tag & 7); + } + return message; + }, + + fromJSON(object: any): DifferentSimple { + return { + name: isSet(object.name) ? String(object.name) : "", + otherOptionalSimple2: isSet(object.otherOptionalSimple2) + ? Simple3.fromJSON(object.otherOptionalSimple2) + : undefined, + }; + }, + + toJSON(message: DifferentSimple): unknown { + const obj: any = {}; + if (message.name !== "") { + obj.name = message.name; + } + if (message.otherOptionalSimple2 !== undefined) { + obj.otherOptionalSimple2 = Simple3.toJSON(message.otherOptionalSimple2); + } + return obj; + }, + + create, I>>(base?: I): DifferentSimple { + return DifferentSimple.fromPartial(base ?? ({} as any)); + }, + fromPartial, I>>(object: I): DifferentSimple { + const message = createBaseDifferentSimple(); + message.name = object.name ?? ""; + message.otherOptionalSimple2 = (object.otherOptionalSimple2 !== undefined && object.otherOptionalSimple2 !== null) + ? Simple3.fromPartial(object.otherOptionalSimple2) + : undefined; + return message; + }, +}; + function createBaseSimpleEnums(): SimpleEnums { return { localEnum: 0, importEnum: 0 }; } diff --git a/src/main.ts b/src/main.ts index 8efce8ac2..769354ee2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -873,9 +873,6 @@ function generateInterfaceDeclaration( const name = maybeSnakeToCamel(fieldDesc.name, options); const isOptional = isOptionalProperty(fieldDesc, messageDesc.options, options); const type = toTypeName(ctx, messageDesc, fieldDesc, isOptional); - if (isOptional && !type.toString().includes("undefined")) { - console.warn(name, type); - } chunks.push(code`${maybeReadonly(options)}${name}${isOptional ? "?" : ""}: ${type}, `); });