Skip to content

Commit

Permalink
Merge branch 'main' into FixUrlForExtensionRsource
Browse files Browse the repository at this point in the history
  • Loading branch information
v-hongli1 authored Mar 7, 2025
2 parents dbd2350 + 8d15659 commit c15edce
Show file tree
Hide file tree
Showing 36 changed files with 699 additions and 194 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/add_page_items_segments-2025-2-4-15-27-53.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-client-generator-core"
---

Add `pageItemsSegments` for `SdkPagingServiceMetadata` to indicate how to get page items from response.
7 changes: 7 additions & 0 deletions .chronus/changes/add_page_items_segments-2025-2-4-15-30-24.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

Keep empty for `serializedName` if the body parmeter is not explicitly defined.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@azure-tools/typespec-autorest"
- "@azure-tools/typespec-azure-resource-manager"
---

Fix using `@identifiers` on array of model with id not respected
7 changes: 7 additions & 0 deletions .chronus/changes/fix_bugs-2025-1-26-14-49-56.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

Consider extensible enum when doing example value mapping.
7 changes: 7 additions & 0 deletions .chronus/changes/fix_bugs-2025-1-26-15-49-39.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

Do not consider template type when calculating discriminator and orphan types.
7 changes: 7 additions & 0 deletions .chronus/changes/refine_content_type-2025-1-28-15-46-10.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: internal
packages:
- "@azure-tools/typespec-client-generator-core"
---

refine content type related logic
7 changes: 7 additions & 0 deletions .chronus/changes/stream_mapping-2025-1-24-18-38-23.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-client-generator-core"
---

Map all streaming request and response type to bytes.
7 changes: 7 additions & 0 deletions .chronus/changes/tcgc-migrateToMutator-2025-2-4-12-45-51.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-client-generator-core"
---

Move from projections to mutators
12 changes: 9 additions & 3 deletions packages/typespec-autorest/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1851,13 +1851,17 @@ export async function getOpenAPIForService(
}
}

function ifArrayItemContainsIdentifier(program: Program, array: ArrayModelType) {
function ifArrayItemContainsIdentifier(
program: Program,
array: ArrayModelType,
armIdentifiers: string[],
) {
if (array.indexer.value?.kind !== "Model") {
return true;
}
return (
getExtensions(program, array).has("x-ms-identifiers") ||
getProperty(array.indexer.value, "id")
(getProperty(array.indexer.value, "id") && armIdentifiers.includes("id"))
);
}

Expand Down Expand Up @@ -2418,7 +2422,9 @@ export async function getOpenAPIForService(
const armIdentifiers = getArmIdentifiers(program, typespecType);
if (isArmProviderNamespace(program, namespace) && hasValidArmIdentifiers(armIdentifiers)) {
array["x-ms-identifiers"] = armIdentifiers;
} else if (!ifArrayItemContainsIdentifier(program, typespecType as any)) {
} else if (
!ifArrayItemContainsIdentifier(program, typespecType as any, armIdentifiers ?? [])
) {
array["x-ms-identifiers"] = [];
}

Expand Down
60 changes: 33 additions & 27 deletions packages/typespec-autorest/test/openapi-output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ describe("typespec-autorest: extension decorator", () => {
});
});

describe("typespec-azure: identifiers decorator", () => {
describe("identifiers decorator", () => {
it("ignores name/id keys for x-ms-identifiers", async () => {
const oapi = await openApiFor(
`
Expand All @@ -818,11 +818,23 @@ describe("typespec-azure: identifiers decorator", () => {
model PetList {
value: Pet[]
}
@route("/Pets")
@get op list(): PetList;
`,
);
ok(oapi.paths["/Pets"].get);
deepStrictEqual(oapi.definitions.PetList.properties.value["x-ms-identifiers"], undefined);
});

it("ignores id property for x-ms-identifiers", async () => {
const oapi = await openApiFor(
`
model Pet {
name: string;
id: int32;
}
model PetList {
value: Pet[]
}
`,
);
deepStrictEqual(oapi.definitions.PetList.properties.value["x-ms-identifiers"], undefined);
});
it("uses identifiers decorator for properties", async () => {
Expand All @@ -840,11 +852,8 @@ describe("typespec-azure: identifiers decorator", () => {
@identifiers(#["age"])
value: Pet[]
}
@route("/Pets")
@get op list(): PetList;
`,
);
ok(oapi.paths["/Pets"].get);
deepStrictEqual(oapi.definitions.PetList.properties.value["x-ms-identifiers"], ["age"]);
});
it("identifies keys correctly as x-ms-identifiers", async () => {
Expand All @@ -862,11 +871,8 @@ describe("typespec-azure: identifiers decorator", () => {
model PetList {
value: Pet[]
}
@route("/Pets")
@get op list(): PetList;
`,
);
ok(oapi.paths["/Pets"].get);
deepStrictEqual(oapi.definitions.PetList.properties.value["x-ms-identifiers"], ["age"]);
});
it("x-ms-identifiers ignores keys for non armProviderNamespace", async () => {
Expand All @@ -880,11 +886,8 @@ describe("typespec-azure: identifiers decorator", () => {
model PetList {
value: Pet[]
}
@route("/Pets")
@get op list(): PetList;
`,
);
ok(oapi.paths["/Pets"].get);
deepStrictEqual(oapi.definitions.PetList.properties.value["x-ms-identifiers"], []);
});

Expand All @@ -900,13 +903,27 @@ describe("typespec-azure: identifiers decorator", () => {
@identifiers(#[])
value: Pet[]
}
@route("/Pets")
@get op list(): PetList;
`,
);
ok(oapi.paths["/Pets"].get);
deepStrictEqual(oapi.definitions.PetList.properties.value["x-ms-identifiers"], []);
});

it("prioritizes identifiers decorator over id prop", async () => {
const oapi = await openApiFor(
`
model Pet {
name: string;
id: string;
}
model PetList {
@identifiers(#[])
value: Pet[]
}
`,
);
deepStrictEqual(oapi.definitions.PetList.properties.value["x-ms-identifiers"], []);
});

it("supports multiple identifiers", async () => {
const oapi = await openApiFor(
`
Expand All @@ -922,11 +939,8 @@ describe("typespec-azure: identifiers decorator", () => {
@identifiers(#["name", "age"])
value: Pet[]
}
@route("/Pets")
@get op list(): PetList;
`,
);
ok(oapi.paths["/Pets"].get);
deepStrictEqual(oapi.definitions.PetList.properties.value["x-ms-identifiers"], ["name", "age"]);
});
it("supports inner properties in identifiers decorator", async () => {
Expand All @@ -948,11 +962,8 @@ describe("typespec-azure: identifiers decorator", () => {
@identifiers(#["dogs/breed"])
pets: Pet[]
}
@route("/Pets")
@get op list(): PetList;
`,
);
ok(oapi.paths["/Pets"].get);
deepStrictEqual(oapi.definitions.PetList.properties.pets["x-ms-identifiers"], ["dogs/breed"]);
});
it("support inner models in different namespace but route models should be on armProviderNamespace", async () => {
Expand Down Expand Up @@ -980,7 +991,6 @@ describe("typespec-azure: identifiers decorator", () => {
}
`,
);
ok(oapi.paths["/Pets"].get);
deepStrictEqual(oapi.definitions.PetList.properties.pets["x-ms-identifiers"], ["age"]);
});
it("supports inner properties for keys", async () => {
Expand Down Expand Up @@ -1014,12 +1024,8 @@ describe("typespec-azure: identifiers decorator", () => {
model PetList {
pets: Pet[]
}
@route("/Pets")
@get op list(): PetList;
`,
);
ok(oapi.paths["/Pets"].get);
deepStrictEqual(oapi.definitions.PetList.properties.pets["x-ms-identifiers"], [
"dogs/breed",
"cats/features/color",
Expand Down
5 changes: 5 additions & 0 deletions packages/typespec-azure-resource-manager/src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getAllProperties } from "@azure-tools/typespec-azure-core";
import {
$tag,
ArrayModelType,
getProperty as compilerGetProperty,
DecoratorContext,
getKeyName,
getTags,
Expand Down Expand Up @@ -427,6 +428,10 @@ export function getArmIdentifiers(program: Program, entity: ArrayModelType): str
result.push(property.name);
}
}

if (!result.includes("id") && compilerGetProperty(value, "id") !== undefined) {
result.push("id");
}
}

return result.length > 0 ? result : undefined;
Expand Down
6 changes: 6 additions & 0 deletions packages/typespec-client-generator-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@
"peerDependencies": {
"@azure-tools/typespec-azure-core": "workspace:~",
"@typespec/compiler": "workspace:~",
"@typespec/events": "workspace:~",
"@typespec/http": "workspace:~",
"@typespec/openapi": "workspace:~",
"@typespec/rest": "workspace:~",
"@typespec/sse": "workspace:~",
"@typespec/streams": "workspace:~",
"@typespec/versioning": "workspace:~",
"@typespec/xml": "workspace:~"
},
Expand All @@ -74,11 +77,14 @@
"@types/node": "~22.13.9",
"@types/pluralize": "^0.0.33",
"@typespec/compiler": "workspace:~",
"@typespec/events": "workspace:~",
"@typespec/http": "workspace:~",
"@typespec/library-linter": "workspace:~",
"@typespec/openapi": "workspace:~",
"@typespec/prettier-plugin-typespec": "workspace:~",
"@typespec/rest": "workspace:~",
"@typespec/sse": "workspace:~",
"@typespec/streams": "workspace:~",
"@typespec/tspd": "workspace:~",
"@typespec/xml": "workspace:~",
"@vitest/coverage-v8": "^3.0.7",
Expand Down
11 changes: 10 additions & 1 deletion packages/typespec-client-generator-core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
EmitContext,
emitFile,
ModelProperty,
Namespace,
Operation,
Program,
resolvePath,
Expand All @@ -24,7 +25,7 @@ import {
SdkUnionType,
TCGCContext,
} from "./interfaces.js";
import { parseEmitterName } from "./internal-utils.js";
import { handleVersioningMutationForGlobalNamespace, parseEmitterName } from "./internal-utils.js";
import { getSdkPackage } from "./package.js";

export function createTCGCContext(program: Program, emitterName?: string): TCGCContext {
Expand All @@ -34,6 +35,14 @@ export function createTCGCContext(program: Program, emitterName?: string): TCGCC
emitterName: diagnostics.pipe(
parseEmitterName(program, emitterName ?? program.emitters[0]?.metadata?.name),
),
getMutatedGlobalNamespace(): Namespace {
let globalNamespace = this.__mutatedGlobalNamespace;
if (!globalNamespace) {
globalNamespace = handleVersioningMutationForGlobalNamespace(this);
this.__mutatedGlobalNamespace = globalNamespace;
}
return globalNamespace;
},
diagnostics: diagnostics.diagnostics,
__originalProgram: program,
__clientToParameters: new Map(),
Expand Down
Loading

0 comments on commit c15edce

Please sign in to comment.