Skip to content

Commit

Permalink
Merge branch 'main' into AvoidAdditionalProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
markcowl authored Feb 28, 2024
2 parents fe00c65 + 7a4d23f commit 0fa510f
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 43 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/fix_client_name-2024-1-26-23-27-21.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

support @clientName for extensible enum variant
7 changes: 7 additions & 0 deletions .chronus/changes/prevent_breaking-2024-1-28-17-11-9.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

rollback some of the breaking changes for common model types method
2 changes: 1 addition & 1 deletion core
Submodule core updated 37 files
+8 −0 .chronus/changes/docs-operation-level-2024-1-27-19-50-39.md
+8 −0 .chronus/changes/fix-dont-run-decorator-missing-args-2024-1-27-18-6-41.md
+8 −0 .chronus/changes/flexible-auth-2024-1-9-0-8-11.md
+8 −0 .chronus/changes/flexible-auth-2024-1-9-0-8-12.md
+7 −0 .chronus/changes/remove-prettier2.0-support-2024-1-26-14-18-55.md
+7 −0 .chronus/changes/remove-prettier2.0-support-2024-1-26-14-19-25.md
+1 −0 cspell.yaml
+95 −23 docs/libraries/http/authentication.md
+14 −4 docs/libraries/http/reference/data-types.md
+2 −2 docs/libraries/http/reference/decorators.md
+1 −0 docs/libraries/http/reference/index.mdx
+6 −0 packages/compiler/src/core/checker.ts
+1 −2 packages/compiler/src/formatter/print/needs-parens.ts
+51 −53 packages/compiler/src/formatter/print/printer.ts
+17 −5 packages/compiler/test/checker/decorators.test.ts
+2 −2 packages/http/README.md
+21 −5 packages/http/lib/auth.tsp
+2 −2 packages/http/lib/http-decorators.tsp
+185 −0 packages/http/src/auth.ts
+20 −15 packages/http/src/decorators.ts
+1 −0 packages/http/src/index.ts
+7 −1 packages/http/src/operations.ts
+21 −2 packages/http/src/types.ts
+98 −14 packages/http/test/http-decorators.test.ts
+75 −53 packages/openapi3/src/openapi.ts
+1 −0 packages/openapi3/src/types.ts
+140 −0 packages/openapi3/test/security.test.ts
+0 −1 packages/prettier-plugin-typespec/package.json
+0 −9 packages/prettier-plugin-typespec/test/smoke.test.ts
+20 −0 packages/samples/specs/authentication/interface-auth.tsp
+3 −24 packages/samples/specs/authentication/main.tsp
+34 −0 packages/samples/specs/authentication/operation-auth.tsp
+24 −0 packages/samples/specs/authentication/service-auth.tsp
+32 −0 packages/samples/test/output/authentication/@typespec/openapi3/openapi.TypeSpec.InterfaceAuth.yaml
+57 −0 packages/samples/test/output/authentication/@typespec/openapi3/openapi.TypeSpec.OperationAuth.yaml
+0 −0 packages/samples/test/output/authentication/@typespec/openapi3/openapi.TypeSpec.ServiceAuth.yaml
+0 −9 pnpm-lock.yaml
4 changes: 2 additions & 2 deletions packages/typespec-autorest/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ import {
stringTemplateToString,
} from "@typespec/compiler";
import {
Authentication,
HttpAuth,
HttpOperation,
HttpOperationParameters,
Expand All @@ -98,7 +99,6 @@ import {
HttpStatusCodesEntry,
MetadataInfo,
OAuth2FlowType,
ServiceAuthentication,
Visibility,
createMetadataInfo,
getAllHttpServices,
Expand Down Expand Up @@ -2189,7 +2189,7 @@ function createOAPIEmitter(
}

function processServiceAuthentication(
authentication: ServiceAuthentication,
authentication: Authentication,
serviceNamespace: Namespace
): {
securitySchemes: Record<string, OpenAPI2SecurityScheme>;
Expand Down
8 changes: 4 additions & 4 deletions packages/typespec-client-generator-core/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
UsageFlags,
getNamespaceFullName,
getProjectedName,
ignoreDiagnostics,
isService,
isTemplateDeclaration,
isTemplateDeclarationOrInstance,
Expand All @@ -35,6 +34,7 @@ import {
} from "./interfaces.js";
import { TCGCContext, createTCGCContext, parseEmitterName } from "./internal-utils.js";
import { createStateSymbol, reportDiagnostic } from "./lib.js";
import { getLibraryName } from "./public-utils.js";
import { getAllModels, getSdkEnum, getSdkModel } from "./types.js";

export const namespace = "Azure.ClientGenerator.Core";
Expand Down Expand Up @@ -310,7 +310,7 @@ function buildOperationGroupPath(context: TCGCContext, type: Namespace | Interfa
break;
}
if (isOperationGroup(context, type)) {
path.push(type.name);
path.push(getLibraryName(context, type));
}
if (type.namespace) {
type = type.namespace;
Expand Down Expand Up @@ -739,7 +739,7 @@ export function getUsage(context: TCGCContext, entity: Model | Enum): UsageFlags
getAllModels(context); // this will populate modelsMap
}
return entity.kind === "Model"
? ignoreDiagnostics(getSdkModel(context, entity)).usage
? getSdkModel(context, entity).usage
: getSdkEnum(context, entity).usage;
}

Expand Down Expand Up @@ -782,7 +782,7 @@ export function getAccess(
}

return entity.kind === "Model"
? ignoreDiagnostics(getSdkModel(context, entity)).access
? getSdkModel(context, entity).access
: getSdkEnum(context, entity).access;
}

Expand Down
7 changes: 5 additions & 2 deletions packages/typespec-client-generator-core/src/public-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,10 @@ export function getPropertyNames(context: TCGCContext, property: ModelProperty):
* @param type
* @returns the library name for a typespec type
*/
export function getLibraryName(context: TCGCContext, type: Type & { name?: string }): string {
export function getLibraryName(
context: TCGCContext,
type: Type & { name?: string | symbol }
): string {
// 1. check if there's a client name
let emitterSpecificName = getClientNameOverride(context, type);
if (emitterSpecificName) return emitterSpecificName;
Expand All @@ -190,7 +193,7 @@ export function getLibraryName(context: TCGCContext, type: Type & { name?: strin
if (clientSpecificName) return clientSpecificName;

// 4. check if there's a friendly name, if so return friendly name, otherwise return undefined
return getFriendlyName(context.program, type) ?? type.name;
return getFriendlyName(context.program, type) ?? (typeof type.name === "string" ? type.name : "");
}

export function capitalize(name: string): string {
Expand Down
51 changes: 41 additions & 10 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@ export function getSdkArrayOrDict(
context: TCGCContext,
type: Model,
operation?: Operation
): (SdkDictionaryType | SdkArrayType) | undefined {
return ignoreDiagnostics(getSdkArrayOrDictWithDiagnostics(context, type, operation));
}

export function getSdkArrayOrDictWithDiagnostics(
context: TCGCContext,
type: Model,
operation?: Operation
): [(SdkDictionaryType | SdkArrayType) | undefined, readonly Diagnostic[]] {
const diagnostics = createDiagnosticCollector();
if (type.indexer !== undefined) {
Expand Down Expand Up @@ -306,6 +314,14 @@ export function getSdkTuple(
context: TCGCContext,
type: Tuple,
operation?: Operation
): SdkTupleType {
return ignoreDiagnostics(getSdkTupleWithDiagnostics(context, type, operation));
}

export function getSdkTupleWithDiagnostics(
context: TCGCContext,
type: Tuple,
operation?: Operation
): [SdkTupleType, readonly Diagnostic[]] {
const diagnostics = createDiagnosticCollector();
return diagnostics.wrap({
Expand All @@ -320,7 +336,11 @@ function getNonNullOptions(context: TCGCContext, type: Union): Type[] {
return [...type.variants.values()].map((x) => x.type).filter((t) => !isNullType(t));
}

export function getSdkUnion(
export function getSdkUnion(context: TCGCContext, type: Union, operation?: Operation): SdkType {
return ignoreDiagnostics(getSdkUnionWithDiagnostics(context, type, operation));
}

export function getSdkUnionWithDiagnostics(
context: TCGCContext,
type: Union,
operation?: Operation
Expand All @@ -342,7 +362,7 @@ export function getSdkUnion(
}
return diagnostics.wrap({
...getSdkTypeBaseHelper(context, type, "union"),
name: type.name,
name: getLibraryName(context, type),
generatedName: type.name ? undefined : getGeneratedName(context, type),
values: nonNullOptions.map((x) =>
diagnostics.pipe(getClientTypeWithDiagnostics(context, x, operation))
Expand Down Expand Up @@ -379,7 +399,9 @@ function addDiscriminatorToModelType(
if (discriminator) {
let discriminatorProperty;
for (const childModel of type.derivedModels) {
const childModelSdkType = diagnostics.pipe(getSdkModel(context, childModel, operation));
const childModelSdkType = diagnostics.pipe(
getSdkModelWithDiagnostics(context, childModel, operation)
);
updateModelsMap(context, childModel, childModelSdkType, operation);
for (const property of childModelSdkType.properties) {
if (property.kind === "property") {
Expand Down Expand Up @@ -460,6 +482,14 @@ export function getSdkModel(
context: TCGCContext,
type: Model,
operation?: Operation
): SdkModelType {
return ignoreDiagnostics(getSdkModelWithDiagnostics(context, type, operation));
}

export function getSdkModelWithDiagnostics(
context: TCGCContext,
type: Model,
operation?: Operation
): [SdkModelType, readonly Diagnostic[]] {
const diagnostics = createDiagnosticCollector();
type = getEffectivePayloadType(context, type);
Expand Down Expand Up @@ -595,11 +625,12 @@ function getSdkUnionEnumValues(
enumType: SdkEnumType
): SdkEnumValueType[] {
const values: SdkEnumValueType[] = [];
for (const [name, member] of type.flattenedMembers.entries()) {
for (const member of type.flattenedMembers.values()) {
const docWrapper = getDocHelper(context, member.type);
const name = getLibraryName(context, member.type);
values.push({
kind: "enumvalue",
name: typeof name === "string" ? name : `${member.value}`,
name: name ? name : `${member.value}`,
description: docWrapper.description,
details: docWrapper.details,
value: member.value,
Expand Down Expand Up @@ -654,7 +685,7 @@ function getKnownValuesEnum(
const docWrapper = getDocHelper(context, type);
sdkType = {
...getSdkTypeBaseHelper(context, type, "enum"),
name: type.name,
name: getLibraryName(context, type),
description: docWrapper.description,
details: docWrapper.details,
valueType: getSdkEnumValueType(context, knownValues.members.values().next().value),
Expand Down Expand Up @@ -688,12 +719,12 @@ export function getClientTypeWithDiagnostics(
retval = getSdkConstant(context, type);
break;
case "Tuple":
retval = diagnostics.pipe(getSdkTuple(context, type, operation));
retval = diagnostics.pipe(getSdkTupleWithDiagnostics(context, type, operation));
break;
case "Model":
retval = diagnostics.pipe(getSdkArrayOrDict(context, type, operation));
retval = diagnostics.pipe(getSdkArrayOrDictWithDiagnostics(context, type, operation));
if (retval === undefined) {
retval = diagnostics.pipe(getSdkModel(context, type, operation));
retval = diagnostics.pipe(getSdkModelWithDiagnostics(context, type, operation));
}
break;
case "Intrinsic":
Expand Down Expand Up @@ -735,7 +766,7 @@ export function getClientTypeWithDiagnostics(
if (unionAsEnum && type.name) {
retval = getSdkUnionEnum(context, unionAsEnum, operation);
} else {
retval = diagnostics.pipe(getSdkUnion(context, type, operation));
retval = diagnostics.pipe(getSdkUnionWithDiagnostics(context, type, operation));
}
break;
case "ModelProperty":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -826,11 +826,12 @@ describe("typespec-client-generator-core: decorators", () => {
);
});

it("nested namespace and interface", async () => {
it("nested namespace and interface with naming change", async () => {
await runner.compile(`
@service({})
namespace Test1Client {
@route("/b")
@clientName("BRename")
namespace B {
op x(): void;
Expand All @@ -855,7 +856,7 @@ describe("typespec-client-generator-core: decorators", () => {
ok(b);
strictEqual(b.subOperationGroups?.length, 1);
strictEqual(listOperationGroups(runner.context, b).length, 1);
strictEqual(b.groupPath, "Test1Client.B");
strictEqual(b.groupPath, "Test1Client.BRename");
deepStrictEqual(
listOperationsInOperationGroup(runner.context, b).map((x) => x.name),
["x"]
Expand All @@ -865,7 +866,7 @@ describe("typespec-client-generator-core: decorators", () => {
ok(c);
strictEqual(c.subOperationGroups, undefined);
strictEqual(listOperationGroups(runner.context, c).length, 0);
strictEqual(c.groupPath, "Test1Client.B.C");
strictEqual(c.groupPath, "Test1Client.BRename.C");
deepStrictEqual(
listOperationsInOperationGroup(runner.context, c).map((x) => x.name),
["y"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1325,9 +1325,7 @@ describe("typespec-client-generator-core: public-utils", () => {
}
`)) as { repeatabilityResult: ModelProperty };

const union = ignoreDiagnostics(
getSdkUnion(runner.context, repeatabilityResult.type as Union)
);
const union = getSdkUnion(runner.context, repeatabilityResult.type as Union);
strictEqual(
(union as SdkUnionType).generatedName,
"ResponseWithAnonymousUnionRepeatabilityResult"
Expand All @@ -1350,9 +1348,7 @@ describe("typespec-client-generator-core: public-utils", () => {
}
`)) as { repeatabilityResult: ModelProperty };

const union = ignoreDiagnostics(
getSdkUnion(runner.context, repeatabilityResult.type as Union)
);
const union = getSdkUnion(runner.context, repeatabilityResult.type as Union);
strictEqual(
(union as SdkUnionType).generatedName,
"RequestParameterWithAnonymousUnionRepeatabilityResult"
Expand All @@ -1375,9 +1371,7 @@ describe("typespec-client-generator-core: public-utils", () => {
}
`)) as { repeatabilityResult: ModelProperty };

const stringType = ignoreDiagnostics(
getSdkUnion(runner.context, repeatabilityResult.type as Union)
)!;
const stringType = getSdkUnion(runner.context, repeatabilityResult.type as Union)!;
strictEqual(stringType.kind, "union");
strictEqual(stringType.values.length, 3);
strictEqual(stringType.values[0].kind, "constant");
Expand Down
42 changes: 39 additions & 3 deletions packages/typespec-client-generator-core/test/types.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AzureCoreTestLibrary } from "@azure-tools/typespec-azure-core/testing";
import { Enum, UsageFlags } from "@typespec/compiler";
import { Enum, Union, UsageFlags } from "@typespec/compiler";
import { expectDiagnostics } from "@typespec/compiler/testing";
import { deepEqual, deepStrictEqual, strictEqual } from "assert";
import { beforeEach, describe, it } from "vitest";
Expand All @@ -13,7 +13,13 @@ import {
SdkUnionType,
} from "../src/interfaces.js";
import { isErrorOrChildOfError } from "../src/public-utils.js";
import { getAllModels, getAllModelsWithDiagnostics, getSdkEnum, isReadOnly } from "../src/types.js";
import {
getAllModels,
getAllModelsWithDiagnostics,
getClientType,
getSdkEnum,
isReadOnly,
} from "../src/types.js";
import { SdkTestRunner, createSdkTestRunner, createTcgcTestRunnerForEmitter } from "./test-host.js";

describe("typespec-client-generator-core: types", () => {
Expand Down Expand Up @@ -444,7 +450,7 @@ describe("typespec-client-generator-core: types", () => {
);
const sdkType = getSdkTypeHelper(runner);
strictEqual(sdkType.kind, "union");
strictEqual(sdkType.name, undefined);
strictEqual(sdkType.name, "");
const values = sdkType.values;
strictEqual(values.length, 2);
strictEqual(values[0].kind, "string");
Expand Down Expand Up @@ -931,7 +937,37 @@ describe("typespec-client-generator-core: types", () => {
await helper("@azure-tools/typespec-csharp", "Enum1", "One");
await helper("@azure-tools/typespec-java", "JavaEnum1", "JavaOne");
});

it("union as enum rename", async () => {
const { TestUnion } = (await runner.compileWithCustomization(
`
@service({})
namespace N {
@test
union TestUnion{
@clientName("ARename")
"A",
"B": "B_v",
string
}
op x(body: TestUnion): void;
}
`,
`
namespace Customizations;
@@clientName(N.TestUnion, "TestUnionRename");
@@clientName(N.TestUnion.B, "BRename");
`
)) as { TestUnion: Union };

const enumType = getClientType(runner.context, TestUnion) as SdkEnumType;
strictEqual(enumType.name, "TestUnionRename");
strictEqual(enumType.values[0].name, "ARename");
strictEqual(enumType.values[1].name, "BRename");
});
});

describe("SdkBodyModelPropertyType", () => {
it("required", async function () {
await runner.compileWithBuiltInService(`
Expand Down
9 changes: 0 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0fa510f

Please sign in to comment.