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

fix-special-union-within-anonymous-model-issue #2767

Merged
Merged
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
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",
qiaozha marked this conversation as resolved.
Show resolved Hide resolved
}

/**
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();
qiaozha marked this conversation as resolved.
Show resolved Hide resolved
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
Loading