Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[protobuf] Handle cross-package references correctly in some buggy cases. #3561

Merged
merged 4 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/protobuf/src/transform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ function tspToProto(program: Program, emitterOptions: ProtobufEmitterOptions): P
program,
addInputParams(operation.parameters, operation),
operation,
operation.parameters
getEffectiveModelType(program, operation.parameters)
);

return {
Expand Down Expand Up @@ -557,7 +557,12 @@ function tspToProto(program: Program, emitterOptions: ProtobufEmitterOptions): P
const keyProto = addType(keyType, relativeSource);
const valueProto = addType(valueType, relativeSource) as ProtoRef | ProtoScalar;

return map(keyProto[1] as "string" | ScalarIntegralName, valueProto);
return map(
keyProto[1] as "string" | ScalarIntegralName,
valueType.kind === "Model"
? addImportSourceForProtoIfNeeded(program, valueProto, relativeSource, valueType)
: valueProto
);
}

function arrayToProto(t: Model, relativeSource: Model | Operation): ProtoType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ namespace A {
@field(1) testOutputField: int32;
@field(2) secondField: string;
}

model ReferentInput {
@field(1) referentField: C.Input;
}
}

@package({
Expand All @@ -19,8 +23,20 @@ namespace B {
@Protobuf.service
interface Service {
foo(...Input): A.Output;
bar(...A.ReferentInput): A.Output;
}

model Input {
@field(1) testInputField: string;
@field(2) mapField: Map<string, A.ReferentInput>;
@field(3) repeatedField: A.ReferentInput[];
}
}

@package({
name: "C",
})
namespace C {
model Input {
@field(1) testInputField: string;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ syntax = "proto3";

package A;

import "C.proto";

message Output {
int32 testOutputField = 1;
string secondField = 2;
}

message ReferentInput {
C.Input referentField = 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import "A.proto";

message Input {
string testInputField = 1;
map<string, A.ReferentInput> mapField = 2;
repeated A.ReferentInput repeatedField = 3;
}

service Service {
rpc Foo(Input) returns (A.Output);
rpc Bar(A.ReferentInput) returns (A.Output);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Generated by Microsoft TypeSpec

syntax = "proto3";

package C;

message Input {
string testInputField = 1;
}
Loading