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

Allow spreading a model that has props added in previous version #3911

Merged
Show file tree
Hide file tree
Changes from all 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"
---

Allow spreading a model that has props added in previous version
33 changes: 31 additions & 2 deletions packages/versioning/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import {
isTemplateInstance,
isType,
navigateProgram,
type ModelProperty,
type Namespace,
type Program,
type Type,
type TypeNameOptions,
} from "@typespec/compiler";
import {
$added,
$removed,
findVersionedNamespace,
getMadeOptionalOn,
getMadeRequiredOn,
Expand Down Expand Up @@ -772,6 +775,30 @@ function validateAvailabilityForRef(
}
}

function canIgnoreDependentVersioning(type: Type, versioning: "added" | "removed") {
if (type.kind === "ModelProperty") {
return canIgnoreVersioningOnProperty(type, versioning);
}
return false;
}

function canIgnoreVersioningOnProperty(
prop: ModelProperty,
versioning: "added" | "removed"
): boolean {
if (prop.sourceProperty === undefined) {
return false;
}

const decoratorFn = versioning === "added" ? $added : $removed;
// Check if the decorator was defined on this property or a source property. If source property ignore.
const selfDecorators = prop.decorators.filter((x) => x.decorator === decoratorFn);
const sourceDecorators = prop.sourceProperty.decorators.filter(
(x) => x.decorator === decoratorFn
);
return !selfDecorators.some((x) => !sourceDecorators.some((y) => x.node === y.node));
}

function validateAvailabilityForContains(
program: Program,
sourceAvail: Map<string, Availability> | undefined,
Expand All @@ -791,7 +818,8 @@ function validateAvailabilityForContains(
if (sourceVal === targetVal) continue;
if (
[Availability.Added].includes(targetVal) &&
[Availability.Removed, Availability.Unavailable].includes(sourceVal)
[Availability.Removed, Availability.Unavailable].includes(sourceVal) &&
!canIgnoreDependentVersioning(target, "added")
) {
const sourceAddedOn = findAvailabilityOnOrBeforeVersion(key, Availability.Added, sourceAvail);
reportDiagnostic(program, {
Expand All @@ -808,7 +836,8 @@ function validateAvailabilityForContains(
}
if (
[Availability.Removed].includes(sourceVal) &&
[Availability.Added, Availability.Available].includes(targetVal)
[Availability.Added, Availability.Available].includes(targetVal) &&
!canIgnoreDependentVersioning(target, "removed")
) {
const targetRemovedOn = findAvailabilityAfterVersion(key, Availability.Removed, targetAvail);
reportDiagnostic(program, {
Expand Down
28 changes: 28 additions & 0 deletions packages/versioning/test/incompatible-versioning.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,34 @@ describe("versioning: validate incompatible references", () => {
expectDiagnosticEmpty(diagnostics);
});

it("succeed when spreading a model that might have add properties added in previous versions", async () => {
const diagnostics = await runner.diagnose(`
model Base {
@added(Versions.v1) name: string;
}

@added(Versions.v2)
model Child {
...Base;
}
`);
expectDiagnosticEmpty(diagnostics);
});

it("succeed when spreading a model that might have add properties removed after the model", async () => {
const diagnostics = await runner.diagnose(`
model Base {
@removed(Versions.v3) name: string;
}

@removed(Versions.v2)
model Child {
...Base;
}
`);
expectDiagnosticEmpty(diagnostics);
});

it("emit diagnostic when model property was added before model itself", async () => {
const diagnostics = await runner.diagnose(`
@added(Versions.v3)
Expand Down
Loading