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 issue with @removed decorator if model was not added from beginning #3409

Merged
merged 5 commits into from
May 21, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/versioning"
---

Fix issue with `@removed` decorator if model was not added from beginning.
tjprescott marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions packages/versioning/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,7 @@ function validateAvailabilityForContains(
for (const key of keySet) {
const sourceVal = sourceAvail.get(key)!;
const targetVal = targetAvail.get(key)!;
if (sourceVal === targetVal) continue;
if (
[Availability.Added].includes(targetVal) &&
[Availability.Removed, Availability.Unavailable].includes(sourceVal)
Expand Down
17 changes: 16 additions & 1 deletion packages/versioning/src/versioning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -806,10 +806,25 @@ export function getAvailabilityMap(
)
return undefined;

let parentMap: Map<string, Availability> | undefined = undefined;
if (type.kind === "ModelProperty" && type.model !== undefined) {
parentMap = getAvailabilityMap(program, type.model);
} else if (type.kind === "Operation" && type.interface !== undefined) {
parentMap = getAvailabilityMap(program, type.interface);
}

// implicitly, all versioned things are assumed to have been added at
// v1 if not specified
if (!added.length) {
added.push(allVersions[0]);
if (parentMap !== undefined) {
parentMap.forEach((key, value) => {
if (value === Availability.Added) {
added.push(allVersions.find((x) => x.name === key)!);
}
});
} else {
added.push(allVersions[0]);
}
}

// something isn't available by default
Expand Down
53 changes: 53 additions & 0 deletions packages/versioning/test/versioning.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,34 @@ describe("versioning: logic", () => {
);
});

it("can be removed respecting model versioning", async () => {
const {
source,
projections: [v2, v3, v4],
} = await versionedModel(
["v2", "v3", "v4"],
`@added(Versions.v2)
model Test {
a: int32;
@removed(Versions.v3) b: int32;
}
`
);

assertHasProperties(v2, ["a", "b"]);
assertHasProperties(v3, ["a"]);
assertHasProperties(v4, ["a"]);

assertModelProjectsTo(
[
[v2, "v2"],
[v3, "v3"],
[v3, "v4"],
],
source
);
});

it("can be renamed", async () => {
const {
source,
Expand Down Expand Up @@ -1365,6 +1393,31 @@ describe("versioning: logic", () => {
);
});

it("can be removed respecting interface versioning", async () => {
const {
source,
projections: [v2, v3, v4],
} = await versionedInterface(
["v2", "v3", "v4"],
`@added(Versions.v2)
interface Test {
allVersions(): void;
@removed(Versions.v3) version2Only(): void;
}
`
);
assertHasOperations(v2, ["allVersions", "version2Only"]);
assertHasOperations(v3, ["allVersions"]);
assertInterfaceProjectsTo(
[
[v2, "v2"],
[v3, "v3"],
[v4, "v4"],
],
source
);
});

it("can be renamed", async () => {
const {
source,
Expand Down
Loading