Skip to content

Commit

Permalink
Fix regression using dep (#4157)
Browse files Browse the repository at this point in the history
Previous PR https://github.com/microsoft/typespec/pull/4145/files fix an
issue with referencing items from another sub namespace and simplified
the logic but seems like this is making the
typespec-azure-resource-manager library error.

The way the library was written kinda makes it seems like there is
indeed an error but can't change that right now so just reverting as we
plan to get rid of this error anyway
  • Loading branch information
timotheeguerin authored Aug 13, 2024
1 parent 2c8d4e2 commit 6567ec2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
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: internal
packages:
- "@typespec/versioning"
---

Fix regression using dep
14 changes: 14 additions & 0 deletions packages/versioning/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ function validateVersionedNamespaceUsage(
const sourceVersionedNamespace = source && findVersionedNamespace(program, source);
if (
targetVersionedNamespace !== undefined &&
!(source && (isSubNamespace(target, source) || isSubNamespace(source, target))) &&
sourceVersionedNamespace !== targetVersionedNamespace &&
dependencies?.get(targetVersionedNamespace) === undefined
) {
Expand Down Expand Up @@ -440,6 +441,19 @@ function validateVersionedPropertyNames(program: Program, source: Type) {
}
}

function isSubNamespace(parent: Namespace, child: Namespace): boolean {
let current: Namespace | undefined = child;

while (current && current.name !== "") {
if (current === parent) {
return true;
}
current = current.namespace;
}

return false;
}

function validateMadeOptional(program: Program, target: Type) {
if (target.kind === "ModelProperty") {
const madeOptionalOn = getMadeOptionalOn(program, target);
Expand Down
18 changes: 18 additions & 0 deletions packages/versioning/test/versioned-dependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,24 @@ describe("versioning: reference versioned library", () => {
expectDiagnosticEmpty(diagnostics);
});

// LEGACY test due to arm depending on it. Should remove when we relax using-versioned-library rule
it("doesn't emit diagnostic when parent namespace reference sub namespace that is versioned differently", async () => {
const diagnostics = await runner.diagnose(`
@versioned(Versions)
namespace MyService {
enum Versions {m1}
model Foo is SubNamespace.Bar;
@versioned(SubNamespace.Versions)
namespace SubNamespace {
enum Versions { s1 }
model Bar {}
}
}
`);
expectDiagnosticEmpty(diagnostics);
});

it("succeed if sub namespace of versioned service reference versioned library", async () => {
const diagnostics = await runner.diagnose(`
@versioned(Versions)
Expand Down

0 comments on commit 6567ec2

Please sign in to comment.