Skip to content

Commit

Permalink
Merge pull request #1713 from chappelo/fix/cast-renamespace-return
Browse files Browse the repository at this point in the history
fix: error with type casted object with namespace
  • Loading branch information
WoH authored Nov 11, 2024
2 parents c50fc6d + 33e951a commit 45de0fa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/cli/src/metadataGeneration/exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ export class GenerateMetadataError extends Error {
}

export class GenerateMetaDataWarning {
constructor(private message: string, private node: Node | TypeNode, private onlyCurrent = false) { }
constructor(
private message: string,
private node: Node | TypeNode,
private onlyCurrent = false,
) {}

toString() {
return `Warning: ${this.message}\n${prettyLocationOfNode(this.node)}\n${prettyTroubleCause(this.node, this.onlyCurrent)}`;
Expand All @@ -34,9 +38,9 @@ export function prettyLocationOfNode(node: Node | TypeNode) {
export function prettyTroubleCause(node: Node | TypeNode, onlyCurrent = false) {
let name: string;
if (onlyCurrent || !node.parent) {
name = node.pos !== -1 ? node.getText() : ((node as any).name?.text || '<unknown name>');
name = node.pos !== -1 && node.parent ? node.getText() : (node as any).name?.text || '<unknown name>';
} else {
name = node.parent.pos !== -1 ? node.parent.getText() : ((node as any).parent.name?.text || '<unknown name>');
name = node.parent.pos !== -1 ? node.parent.getText() : (node as any).parent.name?.text || '<unknown name>';
}
return `This was caused by '${name}'`;
}
8 changes: 8 additions & 0 deletions tests/fixtures/controllers/getController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ export class GetTestController extends Controller {
return {} as TsoaTest.TestModel73;
}

@Get('NamespaceWithTypeCastedObject')
public async getNamespaceWithTypeCastedObject() {
const test = { value: 'test' };
return {
value: test as TsoaTest.TestModel73,
};
}

@Get('Multi')
public async getMultipleModels(): Promise<TestModel[]> {
return [new ModelService().getModel(), new ModelService().getModel(), new ModelService().getModel()];
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/swagger/schemaDetails3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4669,6 +4669,12 @@ describe('Definition generation for OpenAPI 3.0.0', () => {
type: 'object',
});
});

it('should generate schema with namespace type casted object', () => {
const response = specDefault.spec.paths['/GetTest/NamespaceWithTypeCastedObject']?.get?.responses;

expect(response).to.have.all.keys('200');
});
});

describe('@Res responses', () => {
Expand Down

0 comments on commit 45de0fa

Please sign in to comment.