Skip to content

Commit

Permalink
fix-special-union-within-anonymous-model-issue (#2767)
Browse files Browse the repository at this point in the history
* fix-special-union-within-anonymous-model-issue

* fix enum key

* revert tmp changes

* fix ci
  • Loading branch information
qiaozha authored Aug 29, 2024
1 parent 8f46df8 commit 9a9fcca
Show file tree
Hide file tree
Showing 9 changed files with 296 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@ export enum KnownDefaultAction {

// @public
export enum KnownManagedServiceIdentityType {
"SystemAssigned, UserAssigned" = "SystemAssigned, UserAssigned",
None = "None",
SystemAndUserAssigned = "SystemAssigned, UserAssigned",
SystemAssigned = "SystemAssigned",
UserAssigned = "UserAssigned"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,8 @@ export enum KnownManagedServiceIdentityType {
SystemAssigned = "SystemAssigned",
/** UserAssigned */
UserAssigned = "UserAssigned",
/** SystemAssigned, UserAssigned */
"SystemAssigned, UserAssigned" = "SystemAssigned, UserAssigned",
/** SystemAndUserAssigned */
SystemAndUserAssigned = "SystemAssigned, UserAssigned",
}

/**
Expand Down
7 changes: 2 additions & 5 deletions packages/typespec-ts/src/modular/buildCodeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ import { Project } from "ts-morph";
import { SdkContext } from "../utils/interfaces.js";
import { getAddedOnVersions } from "@typespec/versioning";
import { getModelNamespaceName } from "../utils/namespaceUtils.js";
import { getSupportedHttpAuth } from "../utils/credentialUtils.js";
import { getType as getTypeName } from "./helpers/typeHelpers.js";
import { isModelWithAdditionalProperties } from "./emitModels.js";
import { reportDiagnostic } from "../lib.js";
import { useContext } from "../contextManager.js";
import { getSupportedHttpAuth } from "../utils/credentialUtils.js";

interface HttpServerParameter {
type: "endpointPath";
Expand Down Expand Up @@ -1073,10 +1073,7 @@ function intOrFloat(value: number): string {
}

function enumName(name: string): string {
if (name.toUpperCase() === name) {
return name;
}
return applyCasing(name, { casing: CASING }).toUpperCase();
return name;
}

function emitEnum(context: SdkContext, type: Enum): Record<string, any> {
Expand Down
22 changes: 12 additions & 10 deletions packages/typespec-ts/src/modular/emitModels.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { addImportsToFiles, getImportSpecifier } from "@azure-tools/rlc-common";
import * as path from "path";

import {
Client,
ModularCodeModel,
Type as ModularType
} from "./modularCodeModel.js";
import {
InterfaceDeclarationStructure,
OptionalKind,
SourceFile,
TypeAliasDeclarationStructure
} from "ts-morph";
import { addImportsToFiles, getImportSpecifier } from "@azure-tools/rlc-common";

import { addImportBySymbol } from "../utils/importHelper.js";
import { buildModelSerializer } from "./serialization/buildSerializerFunction.js";
import { buildOperationOptions } from "./buildOperations.js";
import { getDocsFromDescription } from "./helpers/docsHelpers.js";
import { getModularModelFilePath } from "./helpers/namingHelpers.js";
import { getType } from "./helpers/typeHelpers.js";
import {
Client,
ModularCodeModel,
Type as ModularType
} from "./modularCodeModel.js";
import { buildModelSerializer } from "./serialization/buildSerializerFunction.js";
import { toCamelCase } from "../utils/casingUtils.js";
import { addImportBySymbol } from "../utils/importHelper.js";

// ====== UTILITIES ======

Expand Down Expand Up @@ -199,9 +201,9 @@ export function buildModels(
isExported: true,
members:
model.values?.map((v) => ({
name: v.value,
name: v.name,
value: v.value,
docs: [v.value]
docs: v.description ? [v.description] : [v.name]
})) ?? [],
docs: [
`Known values of {@link ${model.name}} that the service accepts.`
Expand Down
10 changes: 7 additions & 3 deletions packages/typespec-ts/src/modular/helpers/operationHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -827,13 +827,17 @@ function isOptional(param: Parameter | Property): param is OptionalType {

function getOptional(param: OptionalType, runtimeImports: RuntimeImports) {
if (param.type.type === "model") {
const { propertiesStr } = getRequestModelMapping(
const { propertiesStr, directAssignment } = getRequestModelMapping(
param.type,
"options?." + param.clientName + "?",
runtimeImports,
[param.type]
);
return `"${param.restApiName}": { ${propertiesStr.join(", ")} }`;
const serializeContent =
directAssignment === true
? propertiesStr.join(",")
: `{${propertiesStr.join(",")}}`;
return `"${param.restApiName}": ${serializeContent}`;
}
if (
param.restApiName === "api-version" &&
Expand Down Expand Up @@ -942,7 +946,7 @@ export function getRequestModelMapping(
serializerName =
serializerName ??
getDeserializeFunctionName(modelPropertyType, "serialize");
const definition = `${serializerName}(${propertyPath})`;
const definition = `${serializerName}(${propertyPath.replace(/\?$/, "")})`;
props.push(definition);
return { propertiesStr: props, directAssignment: true };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export declare enum KnownManagedServiceIdentityType {
None = "None",
SystemAssigned = "SystemAssigned",
UserAssigned = "UserAssigned",
"SystemAssigned,UserAssigned" = "SystemAssigned,UserAssigned"
SystemAndUserAssignedV3 = "SystemAssigned,UserAssigned"
}

export declare class ManagedIdentityClient {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export declare interface IntsOnlySendOptionalParams extends OperationOptions {
}

export declare enum KnownStringExtensibleNamedUnion {
b = "b",
OptionB = "b",
c = "c"
}

Expand Down
79 changes: 78 additions & 1 deletion packages/typespec-ts/test/modularUnit/enumUnion.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import {
emitModularModelsFromTypeSpec,
emitModularOperationsFromTypeSpec
} from "../util/emitUtil.js";
import { assertEqualContent } from "../util/testUtil.js";

import { assert } from "chai";
import { assertEqualContent } from "../util/testUtil.js";

describe("header parameters", () => {
describe("named union", async () => {
Expand Down Expand Up @@ -1021,6 +1022,82 @@ describe("model type", () => {
);
});

it("union of enum with experimental extensible enum flags", async () => {
const modelFile = await emitModularModelsFromTypeSpec(`
union ImageSize {
string,
@doc("""
Very small image size of 256x256 pixels.
Only supported with dall-e-2 models.
""")
size256x256: "256x256",
@doc("""
A smaller image size of 512x512 pixels.
Only supported with dall-e-2 models.
""")
size512x512: "512x512",
@doc("""
A standard, square image size of 1024x1024 pixels.
Supported by both dall-e-2 and dall-e-3 models.
""")
size1024x1024: "1024x1024",
@doc("""
A wider image size of 1024x1792 pixels.
Only supported with dall-e-3 models.
""")
size1792x1024: "1792x1024",
@doc("""
A taller image size of 1792x1024 pixels.
Only supported with dall-e-3 models.
""")
size1024x1792: "1024x1792",
}
model Test {
color: ImageSize;
}
op read(@body body: Test): void;
`,
false,
false,
false,
false,
true,
true
);
assert.ok(modelFile);
await assertEqualContent(
modelFile!.getInterface("Test")!.getFullText()!,
`
export interface Test {
color: ImageSize;
}
`
);
await assertEqualContent(
modelFile!.getEnum("KnownImageSize")!.getFullText()!,
`
/** Known values of {@link ImageSize} that the service accepts. */
export enum KnownImageSize {
/** size256x256 */
size256x256 = "256x256",
/** size512x512 */
size512x512 = "512x512",
/** size1024x1024 */
size1024x1024 = "1024x1024",
/** size1792x1024 */
size1792x1024 = "1792x1024",
/** size1024x1792 */
size1024x1792 = "1024x1792",
}
`
);
});

it("union of enum", async () => {
const modelFile = await emitModularModelsFromTypeSpec(`
enum LR {
Expand Down
Loading

0 comments on commit 9a9fcca

Please sign in to comment.