From daed546fb240073a9791062329a262ce38b827e3 Mon Sep 17 00:00:00 2001 From: Maece Fischer Date: Sun, 21 Apr 2024 10:37:04 +0200 Subject: [PATCH] fix componentRef import statements --- src/oas3/codegen/ts/generate.petstore.test.ts | 18 +++++---- src/oas3/codegen/ts/generator.ts | 37 ++++++++++++++++--- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/oas3/codegen/ts/generate.petstore.test.ts b/src/oas3/codegen/ts/generate.petstore.test.ts index 6a047f24..b73d80fe 100644 --- a/src/oas3/codegen/ts/generate.petstore.test.ts +++ b/src/oas3/codegen/ts/generate.petstore.test.ts @@ -2,12 +2,14 @@ import {generateOas3ToTs} from './generate'; const petStoreSpecification = require('./generate.petstore.test.specs.json'); test('can generate files from petstore specification', () => { - generateOas3ToTs({ - getSpecification: () => { - return new Promise(resolve => { - resolve(petStoreSpecification); - }); - }, - outputFolderPath: './generated-files-petstore', - }); + expect(() => { + generateOas3ToTs({ + getSpecification: () => { + return new Promise(resolve => { + resolve(petStoreSpecification); + }); + }, + outputFolderPath: './generated-files-petstore', + }); + }).not.toThrow(); }); diff --git a/src/oas3/codegen/ts/generator.ts b/src/oas3/codegen/ts/generator.ts index 52a3938e..782174d4 100644 --- a/src/oas3/codegen/ts/generator.ts +++ b/src/oas3/codegen/ts/generator.ts @@ -512,13 +512,40 @@ export class DefaultCodeGenerator implements CodeGenerator { if (!requiredOutput) { return; } - if (this.isSameOutputFile(requiredOutput.path, output.path)) { + + if ( + output.type !== OutputType.COMPONENT_REF && + this.isSameOutputFile(requiredOutput.path, output.path) + ) { return; } - const importAsset: ImportAsset = { - name: requiredOutput.createName(requiredOutput.path), - importAsName: requiredOutput.createName(output.path), - }; + + if (output.type === OutputType.COMPONENT_REF) { + const componentOutputPath = this.createOutputPathByComponentRef( + output.componentRef + ); + requiredOutput = availableOutputs.find(o => + areOutputPathsEqual(o.path, componentOutputPath) + ); + if (!requiredOutput) { + return; + } + if (this.isSameOutputFile(requiredOutput.path, output.path)) { + return; + } + } + + const importAsset: ImportAsset = + output.type === OutputType.COMPONENT_REF + ? { + name: output.createName(requiredOutput.path), + importAsName: output.createName(output.path), + } + : { + name: requiredOutput.createName(requiredOutput.path), + importAsName: requiredOutput.createName(output.path), + }; + const importPath = this.createImportPath( requiredOutput.path, output.path,