Skip to content

Commit

Permalink
Fix up union recursive type logic (#301)
Browse files Browse the repository at this point in the history
* Fix up union recursive type logic

* Add generated changelog entries

* Add a test

* Prettier

---------

Co-authored-by: svc-changelog <[email protected]>
  • Loading branch information
mfedderly and svc-changelog authored Dec 20, 2024
1 parent 8ccf9eb commit 1a2cc0d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-301.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 1,6 @@
type: fix
fix:
description: Referencing a type with the same name in another package will correctly
be resolved instead of referencing the self type.
links:
- https://github.com/palantir/conjure-typescript/pull/301
19 changes: 19 additions & 0 deletions src/commands/generate/__tests__/tsReturnTypeVisitorTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 185,25 @@ describe("TsTypeVisitor", () => {
};
expect(visitor.external(externalType)).toEqual("Array<IObject>");
});

it("handles references to a union in another package with the same name", () => {
// unionName shares a name with fakeTypeName, but crucially it is a different package
// when we generate a reference to the other type, it needs to be namespaced instead of trying to use the local version
const unionName = { name: fakeTypeName.name, package: "" };
const union = ITypeDefinition.union({
typeName: unionName,
union: [],
});

const unionVisitor = new TsReturnTypeVisitor(
new Map<string, ITypeDefinition>([[createHashableTypeName(unionName), union]]),
fakeTypeName,
false,
DEFAULT_TYPE_GENERATION_FLAGS,
);

expect(unionVisitor.reference(unionName)).toEqual(`I${unionName.name}.I${unionName.name}`);
});
});

describe("with flavored generation flags", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/generate/tsReturnTypeVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 105,7 @@ export class TsReturnTypeVisitor implements ITypeVisitor<string> {
return obj.name;
} else if (ITypeDefinition.isUnion(typeDefinition)) {
// If the type reference is recursive, use a direct reference rather than a namespaced one
if (obj.name === this.currType.name && obj.package === obj.package) {
if (obj.name === this.currType.name && obj.package === this.currType.package) {
return withIPrefix;
}
return `${withIPrefix}.${withIPrefix}`;
Expand Down

0 comments on commit 1a2cc0d

Please sign in to comment.