Skip to content

Commit e921c4d

Browse files
CopilotCopilot
andcommitted
Use adapted type when adapting a literal enum value
Port of Azure/autorest.go#1997 (commit 11f88629). The adapted type contains the proper name and correctly handles internal types, fixing adapting Access.internal operations that return a polymorphic type. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 9511cbe commit e921c4d

3 files changed

Lines changed: 52 additions & 3 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
changeKind: fix
3+
packages:
4+
- "@azure-tools/typespec-go"
5+
---
6+
7+
Fixed adapting `Access.internal` operations that return a polymorphic type.

packages/typespec-go/src/tcgcadapter/types.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -913,13 +913,28 @@ export class TypeAdapter {
913913

914914
private getLiteralValue(constType: tcgc.SdkConstantType | tcgc.SdkEnumValueType): go.Literal {
915915
if (constType.kind === "enumvalue") {
916-
const valueName = `${helpers.getEffectiveName(constType.enumType)}${helpers.getEffectiveName(constType)}`;
917-
const keyName = `literal-${valueName}`;
916+
const goConstType = this.getConstantType(constType.enumType);
917+
// find the matching enum value
918+
let goConstValue: go.ConstantValue | undefined;
919+
for (const value of goConstType.values) {
920+
if (value.value === constType.value) {
921+
goConstValue = value;
922+
break;
923+
}
924+
}
925+
if (!goConstValue) {
926+
throw new AdapterError(
927+
"InternalError",
928+
`failed to find const value for ${constType.value} in const ${goConstType.name}`,
929+
constType.__raw?.node,
930+
);
931+
}
932+
const keyName = `literal-${goConstValue.name}`;
918933
let literalConst = this.types.get(keyName);
919934
if (literalConst) {
920935
return <go.Literal>literalConst;
921936
}
922-
const constValue = this.constValues.get(valueName);
937+
const constValue = this.constValues.get(goConstValue.name);
923938
if (!constValue) {
924939
throw new AdapterError(
925940
"InternalError",

packages/typespec-go/test/tsp/Regressions/main.tsp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,30 @@ op payloadWithExplicitContentType(
247247
@body
248248
thing: SomeModel,
249249
): void;
250+
251+
union InboundCallerIdentityType {
252+
SystemAssigned: "SystemAssigned",
253+
UserAssigned: "UserAssigned",
254+
string,
255+
}
256+
257+
@discriminator("type")
258+
model InboundCallerIdentity {
259+
type: InboundCallerIdentityType;
260+
}
261+
262+
model SystemAssignedInboundCallerIdentity extends InboundCallerIdentity {
263+
type: InboundCallerIdentityType.SystemAssigned;
264+
}
265+
266+
model UserAssignedInboundCallerIdentity extends InboundCallerIdentity {
267+
type: InboundCallerIdentityType.UserAssigned;
268+
userAssignedIdentity: string;
269+
}
270+
271+
/**
272+
* gets a polymorphic type when the operation is internal (makes the dependent type graph internal)
273+
*/
274+
@access(Access.internal)
275+
@route("/get-caller-identity")
276+
op getCallerIdentity(): InboundCallerIdentity;

0 commit comments

Comments
 (0)