Skip to content

Commit

Permalink
fix: use correct imports for optional fields (#904)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
andrehp authored Aug 16, 2023
1 parent e213d26 commit fa13ec7
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
Binary file modified integration/avoid-import-conflicts/simple.bin
Binary file not shown.
5 changes: 5 additions & 0 deletions integration/avoid-import-conflicts/simple.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
83 changes: 83 additions & 0 deletions integration/avoid-import-conflicts/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 extends Exact<DeepPartial<DifferentSimple>, I>>(base?: I): DifferentSimple {
return DifferentSimple.fromPartial(base ?? ({} as any));
},
fromPartial<I extends Exact<DeepPartial<DifferentSimple>, 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 };
}
Expand Down
3 changes: 0 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}, `);
});

Expand Down

0 comments on commit fa13ec7

Please sign in to comment.