Skip to content

Commit 45de0fa

Browse files
authored
Merge pull request #1713 from chappelo/fix/cast-renamespace-return
fix: error with type casted object with namespace
2 parents c50fc6d + 33e951a commit 45de0fa

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

packages/cli/src/metadataGeneration/exceptions.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ export class GenerateMetadataError extends Error {
1111
}
1212

1313
export class GenerateMetaDataWarning {
14-
constructor(private message: string, private node: Node | TypeNode, private onlyCurrent = false) { }
14+
constructor(
15+
private message: string,
16+
private node: Node | TypeNode,
17+
private onlyCurrent = false,
18+
) {}
1519

1620
toString() {
1721
return `Warning: ${this.message}\n${prettyLocationOfNode(this.node)}\n${prettyTroubleCause(this.node, this.onlyCurrent)}`;
@@ -34,9 +38,9 @@ export function prettyLocationOfNode(node: Node | TypeNode) {
3438
export function prettyTroubleCause(node: Node | TypeNode, onlyCurrent = false) {
3539
let name: string;
3640
if (onlyCurrent || !node.parent) {
37-
name = node.pos !== -1 ? node.getText() : ((node as any).name?.text || '<unknown name>');
41+
name = node.pos !== -1 && node.parent ? node.getText() : (node as any).name?.text || '<unknown name>';
3842
} else {
39-
name = node.parent.pos !== -1 ? node.parent.getText() : ((node as any).parent.name?.text || '<unknown name>');
43+
name = node.parent.pos !== -1 ? node.parent.getText() : (node as any).parent.name?.text || '<unknown name>';
4044
}
4145
return `This was caused by '${name}'`;
4246
}

tests/fixtures/controllers/getController.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ export class GetTestController extends Controller {
112112
return {} as TsoaTest.TestModel73;
113113
}
114114

115+
@Get('NamespaceWithTypeCastedObject')
116+
public async getNamespaceWithTypeCastedObject() {
117+
const test = { value: 'test' };
118+
return {
119+
value: test as TsoaTest.TestModel73,
120+
};
121+
}
122+
115123
@Get('Multi')
116124
public async getMultipleModels(): Promise<TestModel[]> {
117125
return [new ModelService().getModel(), new ModelService().getModel(), new ModelService().getModel()];

tests/unit/swagger/schemaDetails3.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4669,6 +4669,12 @@ describe('Definition generation for OpenAPI 3.0.0', () => {
46694669
type: 'object',
46704670
});
46714671
});
4672+
4673+
it('should generate schema with namespace type casted object', () => {
4674+
const response = specDefault.spec.paths['/GetTest/NamespaceWithTypeCastedObject']?.get?.responses;
4675+
4676+
expect(response).to.have.all.keys('200');
4677+
});
46724678
});
46734679

46744680
describe('@Res responses', () => {

0 commit comments

Comments
 (0)