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

[tcgc] add UsageFlags.Json #1129

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions .chronus/changes/json_usage-2024-6-8-15-14-50.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 `UsageFlags.Json`. Will be set if a model is used with a JSON content type
Original file line number Diff line number Diff line change
Expand Up @@ -600,4 +600,5 @@ export enum UsageFlags {
MultipartFormData = 1 << 5,
// Used in spread.
Spread = 1 << 6,
Json = 1 << 7,
}
Original file line number Diff line number Diff line change
Expand Up @@ -529,3 +529,8 @@ export function getHttpOperationResponseHeaders(
}
return headers;
}

export function isJsonContentType(contentType: string): boolean {
const regex = new RegExp(/^(application|text)\/(.+\+)?json$/);
return regex.test(contentType);
}
10 changes: 9 additions & 1 deletion packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import {
getTypeDecorators,
intOrFloat,
isAzureCoreModel,
isJsonContentType,
isMultipartFormData,
isMultipartOperation,
isNeverOrVoidType,
Expand Down Expand Up @@ -1331,7 +1332,11 @@ function updateTypesFromOperation(
} else {
updateUsageOfModel(context, UsageFlags.Input, sdkType);
}
if (httpBody.contentTypes.some((x) => isJsonContentType(x))) {
updateUsageOfModel(context, UsageFlags.Json, sdkType);
}
if (httpBody.contentTypes.includes("application/merge-patch+json")) {
// will also have Json type
updateUsageOfModel(context, UsageFlags.JsonMergePatch, sdkType);
}
}
Expand All @@ -1352,6 +1357,9 @@ function updateTypesFromOperation(
if (generateConvenient) {
updateUsageOfModel(context, UsageFlags.Output, sdkType);
}
if (innerResponse.body.contentTypes.some((x) => isJsonContentType(x))) {
updateUsageOfModel(context, UsageFlags.Json, sdkType);
}
}
const headers = getHttpOperationResponseHeaders(innerResponse);
if (headers) {
Expand Down Expand Up @@ -1430,7 +1438,7 @@ function updateSpreadModelUsageAndAccess(context: TCGCContext): void {
}
for (const sdkType of context.modelsMap?.values() ?? []) {
// if a type only has spread usage, then it could be internal
if (sdkType.usage === UsageFlags.Spread) {
if ((sdkType.usage & UsageFlags.Spread) > 0) {
sdkType.access = "internal";
}
}
Expand Down
40 changes: 26 additions & 14 deletions packages/typespec-client-generator-core/test/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2203,9 +2203,12 @@ describe("typespec-client-generator-core: decorators", () => {
}
`)) as { Model1: Model; Model2: Model; Model3: Model; Model4: Model };

strictEqual(getUsage(runner.context, Model1), UsageFlags.Input);
strictEqual(getUsage(runner.context, Model2), UsageFlags.Output);
strictEqual(getUsage(runner.context, Model3), UsageFlags.Input | UsageFlags.Output);
strictEqual(getUsage(runner.context, Model1), UsageFlags.Input | UsageFlags.Json);
strictEqual(getUsage(runner.context, Model2), UsageFlags.Output | UsageFlags.Json);
strictEqual(
getUsage(runner.context, Model3),
UsageFlags.Input | UsageFlags.Output | UsageFlags.Json
);
strictEqual(getUsage(runner.context, Model4), UsageFlags.None);
});

Expand Down Expand Up @@ -2263,8 +2266,14 @@ describe("typespec-client-generator-core: decorators", () => {
};

strictEqual(getUsage(runner.context, Model1), UsageFlags.Input | UsageFlags.Output);
strictEqual(getUsage(runner.context, Model2), UsageFlags.Input | UsageFlags.Output);
strictEqual(getUsage(runner.context, Model3), UsageFlags.Input | UsageFlags.Output);
strictEqual(
getUsage(runner.context, Model2),
UsageFlags.Input | UsageFlags.Output | UsageFlags.Json
);
strictEqual(
getUsage(runner.context, Model3),
UsageFlags.Input | UsageFlags.Output | UsageFlags.Json
);
strictEqual(getUsage(runner.context, Model4), UsageFlags.None);
strictEqual(getUsage(runner.context, Enum1), UsageFlags.Input | UsageFlags.Output);
strictEqual(getUsage(runner.context, Enum2), UsageFlags.None);
Expand Down Expand Up @@ -2322,11 +2331,14 @@ describe("typespec-client-generator-core: decorators", () => {
}
`)) as { Fish: Model; Shark: Model; Salmon: Model; SawShark: Model; Origin: Model };

strictEqual(getUsage(runner.context, Fish), UsageFlags.Output);
strictEqual(getUsage(runner.context, Shark), UsageFlags.Input | UsageFlags.Output);
strictEqual(getUsage(runner.context, Salmon), UsageFlags.Output);
strictEqual(getUsage(runner.context, SawShark), UsageFlags.Output);
strictEqual(getUsage(runner.context, Origin), UsageFlags.Output);
strictEqual(getUsage(runner.context, Fish), UsageFlags.Output | UsageFlags.Json);
strictEqual(
getUsage(runner.context, Shark),
UsageFlags.Input | UsageFlags.Output | UsageFlags.Json
);
strictEqual(getUsage(runner.context, Salmon), UsageFlags.Output | UsageFlags.Json);
strictEqual(getUsage(runner.context, SawShark), UsageFlags.Output | UsageFlags.Json);
strictEqual(getUsage(runner.context, Origin), UsageFlags.Output | UsageFlags.Json);
});

it("usage and convenience", async () => {
Expand All @@ -2348,7 +2360,7 @@ describe("typespec-client-generator-core: decorators", () => {
}
`)) as { Fish: Model };

strictEqual(getUsage(runner.context, Fish), UsageFlags.Input);
strictEqual(getUsage(runner.context, Fish), UsageFlags.Input | UsageFlags.Json);

const { Dog } = (await runner.compile(`
@service({})
Expand All @@ -2368,7 +2380,7 @@ describe("typespec-client-generator-core: decorators", () => {
}
`)) as { Dog: Model };

strictEqual(getUsage(runner.context, Dog), UsageFlags.Output);
strictEqual(getUsage(runner.context, Dog), UsageFlags.Output | UsageFlags.Json);
});

it("patch usage", async () => {
Expand All @@ -2395,10 +2407,10 @@ describe("typespec-client-generator-core: decorators", () => {
}
`)) as { PatchModel: Model; JsonMergePatchModel: Model };

strictEqual(getUsage(runner.context, PatchModel), UsageFlags.Input);
strictEqual(getUsage(runner.context, PatchModel), UsageFlags.Input | UsageFlags.Json);
strictEqual(
getUsage(runner.context, JsonMergePatchModel),
UsageFlags.JsonMergePatch | UsageFlags.Input
UsageFlags.JsonMergePatch | UsageFlags.Input | UsageFlags.Json
);
});
});
Expand Down
18 changes: 9 additions & 9 deletions packages/typespec-client-generator-core/test/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2745,7 +2745,7 @@ describe("typespec-client-generator-core: package", () => {
strictEqual(bodyParameter.onClient, false);
strictEqual(bodyParameter.optional, false);
strictEqual(bodyParameter.type, sdkPackage.models[0]);
strictEqual(bodyParameter.type.usage, UsageFlags.Spread);
strictEqual(bodyParameter.type.usage, UsageFlags.Spread | UsageFlags.Json);
strictEqual(bodyParameter.type.access, "internal");

const correspondingMethodParams = bodyParameter.correspondingMethodParams;
Expand Down Expand Up @@ -3074,7 +3074,7 @@ describe("typespec-client-generator-core: package", () => {
strictEqual(models.length, 1);
const model = models.find((x) => x.name === "TestRequest");
ok(model);
strictEqual(model.usage, UsageFlags.Input);
strictEqual(model.usage, UsageFlags.Input | UsageFlags.Json);

strictEqual(method.parameters.length, 2);
const param = method.parameters[0];
Expand Down Expand Up @@ -3119,7 +3119,7 @@ describe("typespec-client-generator-core: package", () => {
strictEqual(models.length, 1);
const model = models.find((x) => x.name === "TestRequest");
ok(model);
strictEqual(model.usage, UsageFlags.Input);
strictEqual(model.usage, UsageFlags.Input | UsageFlags.Json);

strictEqual(method.parameters.length, 2);
const param = method.parameters[0];
Expand Down Expand Up @@ -3190,7 +3190,7 @@ describe("typespec-client-generator-core: package", () => {
strictEqual(bodyParameter.onClient, false);
strictEqual(bodyParameter.optional, false);
strictEqual(bodyParameter.type, sdkPackage.models[0]);
strictEqual(bodyParameter.type.usage, UsageFlags.Spread);
strictEqual(bodyParameter.type.usage, UsageFlags.Spread | UsageFlags.Json);
strictEqual(bodyParameter.type.access, "internal");

strictEqual(bodyParameter.correspondingMethodParams.length, 2);
Expand Down Expand Up @@ -3246,7 +3246,7 @@ describe("typespec-client-generator-core: package", () => {
strictEqual(bodyParameter.onClient, false);
strictEqual(bodyParameter.optional, false);
strictEqual(bodyParameter.type, sdkPackage.models[0]);
strictEqual(bodyParameter.type.usage, UsageFlags.Spread);
strictEqual(bodyParameter.type.usage, UsageFlags.Spread | UsageFlags.Json);
strictEqual(bodyParameter.type.access, "internal");

strictEqual(bodyParameter.correspondingMethodParams.length, 1);
Expand Down Expand Up @@ -3295,7 +3295,7 @@ describe("typespec-client-generator-core: package", () => {
strictEqual(bodyParameter.onClient, false);
strictEqual(bodyParameter.optional, false);
strictEqual(bodyParameter.type, sdkPackage.models[0]);
strictEqual(bodyParameter.type.usage, UsageFlags.Spread);
strictEqual(bodyParameter.type.usage, UsageFlags.Spread | UsageFlags.Json);
strictEqual(bodyParameter.type.access, "internal");

strictEqual(bodyParameter.correspondingMethodParams.length, 2);
Expand Down Expand Up @@ -3356,7 +3356,7 @@ describe("typespec-client-generator-core: package", () => {
strictEqual(bodyParameter.onClient, false);
strictEqual(bodyParameter.optional, false);
strictEqual(bodyParameter.type, sdkPackage.models[0]);
strictEqual(bodyParameter.type.usage, UsageFlags.Spread);
strictEqual(bodyParameter.type.usage, UsageFlags.Spread | UsageFlags.Json);
strictEqual(bodyParameter.type.access, "internal");

strictEqual(bodyParameter.correspondingMethodParams.length, 1);
Expand Down Expand Up @@ -3409,7 +3409,7 @@ describe("typespec-client-generator-core: package", () => {
strictEqual(bodyParameter.onClient, false);
strictEqual(bodyParameter.optional, false);
strictEqual(bodyParameter.type, sdkPackage.models[0]);
strictEqual(bodyParameter.type.usage, UsageFlags.Spread);
strictEqual(bodyParameter.type.usage, UsageFlags.Spread | UsageFlags.Json);
strictEqual(bodyParameter.type.access, "internal");

strictEqual(bodyParameter.correspondingMethodParams.length, 2);
Expand Down Expand Up @@ -3472,7 +3472,7 @@ describe("typespec-client-generator-core: package", () => {
strictEqual(bodyParameter.onClient, false);
strictEqual(bodyParameter.optional, false);
strictEqual(bodyParameter.type, sdkPackage.models[0]);
strictEqual(bodyParameter.type.usage, UsageFlags.Spread);
strictEqual(bodyParameter.type.usage, UsageFlags.Spread | UsageFlags.Json);
strictEqual(bodyParameter.type.access, "internal");

strictEqual(bodyParameter.correspondingMethodParams.length, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1579,7 +1579,7 @@ describe("typespec-client-generator-core: public-utils", () => {
const models = runner.context.experimental_sdkPackage.models;
strictEqual(models.length, 1);
strictEqual(models[0].name, "TestRequest");
strictEqual(models[0].usage, UsageFlags.Spread);
strictEqual(models[0].usage, UsageFlags.Spread | UsageFlags.Json);
});

it("anonymous model for body parameter", async () => {
Expand All @@ -1591,7 +1591,7 @@ describe("typespec-client-generator-core: public-utils", () => {
const models = runner.context.experimental_sdkPackage.models;
strictEqual(models.length, 1);
strictEqual(models[0].name, "TestRequest");
strictEqual(models[0].usage, UsageFlags.Spread);
strictEqual(models[0].usage, UsageFlags.Spread | UsageFlags.Json);
});

it("anonymous union in response header", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -782,9 +782,9 @@ describe("typespec-client-generator-core: enum types", () => {
strictEqual(enums.length, 2);
strictEqual(enums[0].name, "LR");
strictEqual(enums[0].crossLanguageDefinitionId, "N.LR");
strictEqual(enums[0].usage, UsageFlags.Input);
strictEqual(enums[0].usage, UsageFlags.Input | UsageFlags.Json);
strictEqual(enums[1].name, "UD");
strictEqual(enums[1].crossLanguageDefinitionId, "N.UD");
strictEqual(enums[1].usage, UsageFlags.Input);
strictEqual(enums[1].usage, UsageFlags.Input | UsageFlags.Json);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ describe("typespec-client-generator-core: model types", () => {
strictEqual(resp.properties[0].type, prop);
const req = sdkPackage.models.find((x) => x.name === "SendRequest" && x.isGeneratedName);
ok(req);
strictEqual(req.usage, UsageFlags.Spread);
strictEqual(req.usage, UsageFlags.Spread | UsageFlags.Json);
});

it("property of anonymous union as enum", async () => {
Expand Down Expand Up @@ -828,9 +828,9 @@ describe("typespec-client-generator-core: model types", () => {
`);
const models = runner.context.experimental_sdkPackage.models;
strictEqual(models.length, 1);
strictEqual(models[0].usage, UsageFlags.Input);
strictEqual(models.filter((x) => x.usage === UsageFlags.Input).length, 1);
strictEqual(models.filter((x) => x.usage === UsageFlags.Output).length, 0);
strictEqual(models[0].usage, UsageFlags.Input | UsageFlags.Json);
strictEqual(models.filter((x) => (x.usage & UsageFlags.Input) > 0).length, 1);
strictEqual(models.filter((x) => (x.usage & UsageFlags.Output) > 0).length, 0);
});

it("output usage", async () => {
Expand All @@ -842,10 +842,10 @@ describe("typespec-client-generator-core: model types", () => {
`);
const models = runner.context.experimental_sdkPackage.models;
strictEqual(models.length, 1);
strictEqual(models[0].usage, UsageFlags.Output);
strictEqual(models[0].usage, UsageFlags.Output | UsageFlags.Json);

strictEqual(models.filter((x) => x.usage === UsageFlags.Output).length, 1);
strictEqual(models.filter((x) => x.usage === UsageFlags.Input).length, 0);
strictEqual(models.filter((x) => (x.usage & UsageFlags.Output) > 0).length, 1);
strictEqual(models.filter((x) => (x.usage & UsageFlags.Input) > 0).length, 0);
});

it("roundtrip usage", async () => {
Expand All @@ -857,7 +857,7 @@ describe("typespec-client-generator-core: model types", () => {
`);
const models = runner.context.experimental_sdkPackage.models;
strictEqual(models.length, 1);
strictEqual(models[0].usage, UsageFlags.Input | UsageFlags.Output);
strictEqual(models[0].usage, UsageFlags.Input | UsageFlags.Output | UsageFlags.Json);

strictEqual(models.filter((x) => (x.usage & UsageFlags.Output) > 0).length, 1);
strictEqual(models.filter((x) => (x.usage & UsageFlags.Input) > 0).length, 1);
Expand Down Expand Up @@ -885,9 +885,12 @@ describe("typespec-client-generator-core: model types", () => {
strictEqual(models.length, 2);
strictEqual(
models.find((x) => x.name === "RoundTripModel")?.usage,
UsageFlags.Input | UsageFlags.Output
UsageFlags.Input | UsageFlags.Output | UsageFlags.Json
);
strictEqual(
models.find((x) => x.name === "ResultModel")?.usage,
UsageFlags.Output | UsageFlags.Json
);
strictEqual(models.find((x) => x.name === "ResultModel")?.usage, UsageFlags.Output);
});

it("usage propagation", async () => {
Expand Down Expand Up @@ -920,7 +923,7 @@ describe("typespec-client-generator-core: model types", () => {
`);
const models = runner.context.experimental_sdkPackage.models;
strictEqual(models.length, 4);
strictEqual(models[0].usage, UsageFlags.Input | UsageFlags.Output);
strictEqual(models[0].usage, UsageFlags.Input | UsageFlags.Output | UsageFlags.Json);
});

it("usage propagation from subtype", async () => {
Expand Down Expand Up @@ -953,7 +956,7 @@ describe("typespec-client-generator-core: model types", () => {
`);
const models = runner.context.experimental_sdkPackage.models;
strictEqual(models.length, 2);
strictEqual(models[0].usage, UsageFlags.Input | UsageFlags.Output);
strictEqual(models[0].usage, UsageFlags.Input | UsageFlags.Output | UsageFlags.Json);
});

it("usage propagation from subtype of type with another discriminated property", async () => {
Expand Down Expand Up @@ -990,7 +993,7 @@ describe("typespec-client-generator-core: model types", () => {
`);
const models = runner.context.experimental_sdkPackage.models;
strictEqual(models.length, 5);
strictEqual(models[0].usage, UsageFlags.Input | UsageFlags.Output);
strictEqual(models[0].usage, UsageFlags.Input | UsageFlags.Output | UsageFlags.Json);
});

it("unnamed model", async () => {
Expand Down Expand Up @@ -1199,15 +1202,18 @@ describe("typespec-client-generator-core: model types", () => {

strictEqual(AdditionalPropertiesModel.additionalProperties?.kind, "model");
strictEqual(AdditionalPropertiesModel.baseModel, undefined);
strictEqual(AdditionalPropertiesModel.usage, UsageFlags.Input);
strictEqual(AdditionalPropertiesModel.usage, UsageFlags.Input | UsageFlags.Json);
strictEqual(AdditionalPropertiesModel2.additionalProperties?.kind, "model");
strictEqual(AdditionalPropertiesModel2.baseModel, undefined);
strictEqual(AdditionalPropertiesModel2.usage, UsageFlags.Output);
strictEqual(AdditionalPropertiesModel2.usage, UsageFlags.Output | UsageFlags.Json);
strictEqual(AdditionalPropertiesModel3.additionalProperties?.kind, "model");
strictEqual(AdditionalPropertiesModel3.baseModel, undefined);
strictEqual(AdditionalPropertiesModel3.usage, UsageFlags.Input | UsageFlags.Output);
strictEqual(Test.usage, UsageFlags.Input | UsageFlags.Output);
strictEqual(Test2.usage, UsageFlags.Input | UsageFlags.Output);
strictEqual(
AdditionalPropertiesModel3.usage,
UsageFlags.Input | UsageFlags.Output | UsageFlags.Json
);
strictEqual(Test.usage, UsageFlags.Input | UsageFlags.Output | UsageFlags.Json);
strictEqual(Test2.usage, UsageFlags.Input | UsageFlags.Output | UsageFlags.Json);
});

it("additionalProperties of different types", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe("typespec-client-generator-core: multipart types", () => {
ok(modelB);
strictEqual(modelB.kind, "model");
strictEqual(modelB.isFormDataType, false);
strictEqual(modelB.usage, UsageFlags.Spread);
strictEqual(modelB.usage, UsageFlags.Spread | UsageFlags.Json);
strictEqual(modelB.properties.length, 1);
strictEqual(modelB.properties[0].type.kind, "bytes");
});
Expand Down
Loading
Loading