Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
12 changes: 9 additions & 3 deletions packages/http-client-csharp/emitter/src/lib/type-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,18 @@ export function fromSdkType<T extends SdkType>(
typeof sdkProperty.serializedName === "string" &&
sdkProperty.serializedName.toLocaleLowerCase() === "content-type";

// Check if the original property type was an enum (e.g., when @alternateType collapses
// a single-member enum to a constant) - preserve the enum type in this case
const isOriginalTypeEnum =
sdkProperty !== undefined && sdkProperty.__raw?.type?.kind === "Enum";

if (
sdkProperty &&
!isContentTypeHeader &&
(sdkProperty.optional || sdkProperty?.type.kind === "nullable") &&
sdkProperty?.type.kind !== "boolean" &&
sdkType.valueType.kind !== "boolean"
(((sdkProperty.optional || sdkProperty?.type.kind === "nullable") &&
sdkProperty?.type.kind !== "boolean" &&
sdkType.valueType.kind !== "boolean") ||
isOriginalTypeEnum)
) {
// turn the constant into an extensible enum
retVar = diagnostics.pipe(createEnumType(sdkContext, sdkType, namespace!));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,34 @@ describe("Constant enum conversion", () => {
strictEqual(enumType.access, undefined);
strictEqual(enumType.usage, UsageFlags.Input | UsageFlags.Json);
});

it("single-member enum with @alternateType should preserve enum type for required property", async () => {
const program = await typeSpecCompile(
`
enum Colors {
green,
}

model TestModel {
@alternateType("green")
prop: Colors;
}

op test(@body input: TestModel): void;
`,
runner,
{ IsTCGCNeeded: true },
);
const context = createEmitterContext(program);
const sdkContext = await createCSharpSdkContext(context);
const [root] = createModel(sdkContext);
const testModel = root.models.find((m) => m.name === "TestModel");
ok(testModel);
const propertyType = testModel.properties[0].type;
strictEqual(propertyType.kind, "enum");
const enumType = propertyType as InputEnumType;
strictEqual(enumType.isFixed, false);
strictEqual(enumType.values.length, 1);
strictEqual(enumType.values[0].value, "green");
});
});
Loading