Skip to content

Commit

Permalink
Backmerge/release/october 2024 2024 10 10 (#1661)
Browse files Browse the repository at this point in the history
  • Loading branch information
tadelesh authored Oct 10, 2024
1 parent e20b38b commit 15fdc99
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 14 deletions.
7 changes: 7 additions & 0 deletions packages/typespec-client-generator-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change Log - @azure-tools/typespec-client-generator-core

## 0.47.1

### Bug Fixes

- [#1659](https://github.com/Azure/typespec-azure/pull/1659) remove projection for source model since typespec core has already fixed the issue


## 0.47.0

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion packages/typespec-client-generator-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@azure-tools/typespec-client-generator-core",
"version": "0.47.0",
"version": "0.47.1",
"author": "Microsoft Corporation",
"description": "TypeSpec Data Plane Generation library",
"homepage": "https://azure.github.io/typespec-azure",
Expand Down
2 changes: 1 addition & 1 deletion packages/typespec-client-generator-core/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ function getSdkHttpParameters(
type = diagnostics.pipe(
getClientTypeWithDiagnostics(
context,
getHttpBodySpreadModel(context, tspBody.type as Model),
getHttpBodySpreadModel(tspBody.type as Model),
httpOperation.operation,
),
);
Expand Down
8 changes: 2 additions & 6 deletions packages/typespec-client-generator-core/src/internal-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
NumericLiteral,
Operation,
Program,
ProjectedProgram,
StringLiteral,
Type,
Union,
Expand Down Expand Up @@ -526,10 +525,9 @@ export function isHttpBodySpread(httpBody: HttpOperationBody | HttpOperationMult
* @param type
* @returns
*/
export function getHttpBodySpreadModel(context: TCGCContext, type: Model): Model {
export function getHttpBodySpreadModel(type: Model): Model {
if (type.sourceModels.length === 1 && type.sourceModels[0].usage === "spread") {
const innerModel = type.sourceModels[0].model;
const projectedProgram = context.program as ProjectedProgram;
// for case: `op test(...Model):void;`
if (innerModel.name !== "" && innerModel.properties.size === type.properties.size) {
return innerModel;
Expand All @@ -541,9 +539,7 @@ export function getHttpBodySpreadModel(context: TCGCContext, type: Model): Model
innerModel.sourceModels[0].model.name !== "" &&
innerModel.sourceModels[0].model.properties.size === type.properties.size
) {
return projectedProgram.projector
? (projectedProgram.projector.projectedTypes.get(innerModel.sourceModels[0].model) as Model)
: innerModel.sourceModels[0].model;
return innerModel.sourceModels[0].model;
}
}
return type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ function getContextPath(
result = [{ name: root.name }];
let bodyType: Type;
if (isHttpBodySpread(httpOperation.parameters.body)) {
bodyType = getHttpBodySpreadModel(context, httpOperation.parameters.body.type as Model);
bodyType = getHttpBodySpreadModel(httpOperation.parameters.body.type as Model);
} else {
bodyType = httpOperation.parameters.body.type;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,7 @@ export function getSdkModelPropertyType(
const httpBody = httpOperation.parameters.body;
if (httpBody) {
const httpBodyType = isHttpBodySpread(httpBody)
? getHttpBodySpreadModel(context, httpBody.type as Model)
? getHttpBodySpreadModel(httpBody.type as Model)
: httpBody.type;
if (type.model === httpBodyType) {
// only try to add multipartOptions for property of body
Expand Down Expand Up @@ -1560,7 +1560,7 @@ function updateTypesFromOperation(
sdkType = diagnostics.pipe(
getClientTypeWithDiagnostics(
context,
getHttpBodySpreadModel(context, httpBody.type as Model),
getHttpBodySpreadModel(httpBody.type as Model),
operation,
),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { AzureCoreTestLibrary } from "@azure-tools/typespec-azure-core/testing";
import { deepStrictEqual, ok, strictEqual } from "assert";
import { beforeEach, describe, it } from "vitest";
import { SdkClientType, SdkHttpOperation, UsageFlags } from "../../src/interfaces.js";
import {
SdkClientType,
SdkHttpOperation,
SdkServiceMethod,
UsageFlags,
} from "../../src/interfaces.js";
import { getAllModels } from "../../src/types.js";
import { SdkTestRunner, createSdkTestRunner } from "../test-host.js";
import { getServiceMethodOfClient } from "./utils.js";
Expand Down Expand Up @@ -920,10 +925,14 @@ describe("typespec-client-generator-core: spread", () => {
@route("modelref1")
@post
op ref1(...Test): void;
@route("modelref2")
@post
op ref2(@body body: Ref): void;
op ref2(@header header: string, ...Test): void;
@route("modelref3")
@post
op ref3(@body body: Ref): void;
`);
const sdkPackage = runner.context.sdkPackage;
strictEqual(sdkPackage.models.length, 2);
Expand All @@ -936,5 +945,19 @@ describe("typespec-client-generator-core: spread", () => {
strictEqual(sdkPackage.models[1].name, "Ref");
strictEqual(sdkPackage.models[1].usage, UsageFlags.Input | UsageFlags.Json);
strictEqual(sdkPackage.models[1].access, "public");

const client = sdkPackage.clients[0];
deepStrictEqual(
(client.methods[0] as SdkServiceMethod<SdkHttpOperation>).operation.bodyParam?.type,
sdkPackage.models[0],
);
deepStrictEqual(
(client.methods[1] as SdkServiceMethod<SdkHttpOperation>).operation.bodyParam?.type,
sdkPackage.models[0],
);
deepStrictEqual(
(client.methods[2] as SdkServiceMethod<SdkHttpOperation>).operation.bodyParam?.type,
sdkPackage.models[1],
);
});
});

0 comments on commit 15fdc99

Please sign in to comment.