From ee16c3412347538692a47aa002ff028826c9e4a3 Mon Sep 17 00:00:00 2001 From: Masanori Doizaki Date: Fri, 29 Nov 2024 18:43:29 +0900 Subject: [PATCH] fix: Support response using inner interface --- .../src/metadataGeneration/typeResolver.ts | 3 +++ tests/fixtures/controllers/getController.ts | 8 +++++++ tests/unit/swagger/schemaDetails3.spec.ts | 22 +++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/packages/cli/src/metadataGeneration/typeResolver.ts b/packages/cli/src/metadataGeneration/typeResolver.ts index bbd40248..57c2ad5f 100644 --- a/packages/cli/src/metadataGeneration/typeResolver.ts +++ b/packages/cli/src/metadataGeneration/typeResolver.ts @@ -596,6 +596,9 @@ export class TypeResolver { const isGlobalDeclaration = (mod: ts.ModuleDeclaration) => mod.name.kind === ts.SyntaxKind.Identifier && mod.name.text === 'global'; while (!ts.isSourceFile(actNode)) { + if (ts.isBlock(actNode)) { + break; + } if (!(isFirst && ts.isEnumDeclaration(actNode)) && !ts.isModuleBlock(actNode)) { throwUnless(ts.isModuleDeclaration(actNode), new GenerateMetadataError(`This node kind is unknown: ${actNode.kind}`, type)); diff --git a/tests/fixtures/controllers/getController.ts b/tests/fixtures/controllers/getController.ts index 68d23ef1..3730ddf3 100644 --- a/tests/fixtures/controllers/getController.ts +++ b/tests/fixtures/controllers/getController.ts @@ -107,6 +107,14 @@ export class GetTestController extends Controller { return {} as GetterInterfaceHerited; } + @Get('InnerInterface') + public async getInnerInterface() { + interface InnerInterface { + value?: string; + } + return { value: 'test' } as InnerInterface; + } + @Get('ModuleRedeclarationAndNamespace') public async getModuleRedeclarationAndNamespace(): Promise { return {} as TsoaTest.TestModel73; diff --git a/tests/unit/swagger/schemaDetails3.spec.ts b/tests/unit/swagger/schemaDetails3.spec.ts index 20b92681..ff0c1e6f 100644 --- a/tests/unit/swagger/schemaDetails3.spec.ts +++ b/tests/unit/swagger/schemaDetails3.spec.ts @@ -4647,6 +4647,28 @@ describe('Definition generation for OpenAPI 3.0.0', () => { expect(extensionPath['x-attKey8']).to.deep.equal({ test: { testArray: ['testVal1', true, null, ['testVal2', 'testVal3', 123, true, null]] } }); }); + describe('Inner interface', () => { + it('should generate the proper schema', () => { + const ref = specDefault.spec.paths['/GetTest/InnerInterface'].get?.responses['200'].content?.['application/json']['schema']?.['$ref']; + expect(ref).to.equal('#/components/schemas/InnerInterface'); + expect(getComponentSchema('InnerInterface', specDefault)).to.deep.equal({ + additionalProperties: true, + description: undefined, + properties: { + value: { + default: undefined, + description: undefined, + example: undefined, + format: undefined, + type: 'string', + }, + }, + required: undefined, + type: 'object', + }); + }); + }); + describe('module declarations with namespaces', () => { it('should generate the proper schema for a model declared in a namespace in a module', () => { /* tslint:disable:no-string-literal */