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

fix: error with type casted object with namespace #1713

Merged
merged 2 commits into from
Nov 11, 2024
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
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
Loading