diff --git a/packages/@ama-sdk/schematics/schematics/typescript/core/helpers/copy-referenced-files.spec.ts b/packages/@ama-sdk/schematics/schematics/typescript/core/helpers/copy-referenced-files.spec.ts index b9f91fe7a8..4aaf1a2bd2 100644 --- a/packages/@ama-sdk/schematics/schematics/typescript/core/helpers/copy-referenced-files.spec.ts +++ b/packages/@ama-sdk/schematics/schematics/typescript/core/helpers/copy-referenced-files.spec.ts @@ -2,11 +2,11 @@ import { cleanVirtualFileSystem, useVirtualFileSystem } from '@o3r/test-helpers' import { readFile } from 'node:fs/promises'; import { dirname, join } from 'node:path'; -describe('Copy Referenced Files', () => { +describe('Specs processing', () => { const virtualFileSystem = useVirtualFileSystem(); - const copyReferencedFiles = require('./copy-referenced-files').copyReferencedFiles; + const {copyReferencedFiles, updateLocalRelativeRefs} = require('./copy-referenced-files'); - const migrationScriptMocksPath = join(__dirname, '../../../../testing/mocks'); + const specsMocksPath = join(__dirname, '../../../../testing/mocks'); const specFilePath = '../models/split-spec/split-spec.yaml'; const outputDirectory = './local-references'; @@ -14,7 +14,7 @@ describe('Copy Referenced Files', () => { if (!virtualFileSystem.existsSync(dirname(virtualPath))) { await virtualFileSystem.promises.mkdir(dirname(virtualPath), {recursive: true}); } - await virtualFileSystem.promises.writeFile(virtualPath, await readFile(join(migrationScriptMocksPath, realPath), {encoding: 'utf8'})); + await virtualFileSystem.promises.writeFile(virtualPath, await readFile(join(specsMocksPath, realPath), {encoding: 'utf8'})); }; beforeAll(async () => { @@ -39,4 +39,15 @@ describe('Copy Referenced Files', () => { expect(virtualFileSystem.existsSync(join(outputDirectory, 'spec-chunk3/spec-chunk3.yaml'))).toBe(true); expect(virtualFileSystem.existsSync(join(outputDirectory, 'spec-chunk4/spec-chunk4.yaml'))).toBe(true); }); + + it('should update with new local basepath', async () => { + const specWitheRelativesFilePath = 'split-spec/split-spec.yaml'; + const expectedSpecWitheRelativesFilePath = 'split-spec/spec-with-updated-paths.yaml'; + const expectedContent = await readFile(join(specsMocksPath, expectedSpecWitheRelativesFilePath), {encoding: 'utf8'}); + const specContent = await readFile(join(specsMocksPath, specWitheRelativesFilePath), {encoding: 'utf8'}); + + const baseRelativePath = await copyReferencedFiles(specFilePath, './output-local-directory'); + const newSpecContent = await updateLocalRelativeRefs(specContent, baseRelativePath); + expect(newSpecContent).toBe(expectedContent); + }); }); diff --git a/packages/@ama-sdk/schematics/schematics/typescript/core/helpers/copy-referenced-files.ts b/packages/@ama-sdk/schematics/schematics/typescript/core/helpers/copy-referenced-files.ts index ac73243b3d..041c6771b4 100644 --- a/packages/@ama-sdk/schematics/schematics/typescript/core/helpers/copy-referenced-files.ts +++ b/packages/@ama-sdk/schematics/schematics/typescript/core/helpers/copy-referenced-files.ts @@ -1,6 +1,6 @@ import { existsSync } from 'node:fs'; import { copyFile, mkdir, readFile, rm } from 'node:fs/promises'; -import { dirname, join, normalize, posix, relative, resolve } from 'node:path'; +import { dirname, join, normalize, posix, relative, resolve, sep } from 'node:path'; const refMatcher = /\B['"]?[$]ref['"]?\s*:\s*([^#\n]+)/g; @@ -53,7 +53,7 @@ export function updateLocalRelativeRefs(specContent: string, newBaseRelativePath return specContent.replace(refMatcher, (match, ref: string) => { const refPath = ref.replace(/['"]/g, ''); return refPath.startsWith('.') ? - match.replace(refPath, formatPath(normalize(posix.join(newBaseRelativePath, refPath)))) + match.replace(refPath, formatPath(normalize(posix.join(newBaseRelativePath.replaceAll(sep, posix.sep), refPath)))) : match; }); } @@ -64,12 +64,9 @@ export function updateLocalRelativeRefs(specContent: string, newBaseRelativePath * @param outputDirectory */ export async function copyReferencedFiles(specFilePath: string, outputDirectory: string) { - const dedupe = (paths: string[]) => - paths.filter((refPath, index) => { - const actualPath = join(dirname(specFilePath), refPath); - return paths.findIndex((otherRefPath) => join(dirname(specFilePath), otherRefPath) === actualPath) === index; - }); - const refPaths = dedupe(await extractRefPathRecursive(specFilePath, specFilePath, new Set())); + const dedupe = (paths: string[]) => ([...new Set(paths)]); + const allRefPaths = await extractRefPathRecursive(specFilePath, specFilePath, new Set()); + const refPaths = dedupe(allRefPaths); if (refPaths.length) { if (existsSync(outputDirectory)) { await rm(outputDirectory, { recursive: true }); diff --git a/packages/@ama-sdk/schematics/schematics/typescript/core/index.ts b/packages/@ama-sdk/schematics/schematics/typescript/core/index.ts index da50794267..75b4c64604 100644 --- a/packages/@ama-sdk/schematics/schematics/typescript/core/index.ts +++ b/packages/@ama-sdk/schematics/schematics/typescript/core/index.ts @@ -162,6 +162,7 @@ function ngGenerateTypescriptSDKFn(options: NgGenerateTypescriptSDKCoreSchematic if (path.relative(process.cwd(), specPath).startsWith('..')) { // TODO would be better to create files on tree instead of FS + // https://github.com/AmadeusITGroup/otter/issues/2078 const newRelativePath = await copyReferencedFiles(specPath, './spec-local-references'); if (newRelativePath) { specContent = updateLocalRelativeRefs(specContent, newRelativePath); diff --git a/packages/@ama-sdk/schematics/testing/mocks/split-spec/spec-chunk1.yaml b/packages/@ama-sdk/schematics/testing/mocks/split-spec/spec-chunk1.yaml index 7273682c85..f05a4a1d9c 100644 --- a/packages/@ama-sdk/schematics/testing/mocks/split-spec/spec-chunk1.yaml +++ b/packages/@ama-sdk/schematics/testing/mocks/split-spec/spec-chunk1.yaml @@ -7,3 +7,5 @@ properties: example: 10 category: $ref: './split-spec.yaml#/components/schemas/Category' + category2: + $ref: '../spec-chunk4/spec-chunk4.yaml#/components/schemas/Category' diff --git a/packages/@ama-sdk/schematics/testing/mocks/split-spec/spec-with-updated-paths.yaml b/packages/@ama-sdk/schematics/testing/mocks/split-spec/spec-with-updated-paths.yaml new file mode 100644 index 0000000000..4f5fc58aed --- /dev/null +++ b/packages/@ama-sdk/schematics/testing/mocks/split-spec/spec-with-updated-paths.yaml @@ -0,0 +1,45 @@ +openapi: 3.0.2 +info: + description: test + title: test + version: 0.0.0 +paths: + /test: + get: + responses: + '200': + description: test + content: + application/json: + schema: + $ref: './output-local-directory/split-spec/spec-chunk1.yaml' + /test2: + get: + responses: + '200': + description: test + content: + application/json: + schema: + $ref: './output-local-directory/spec-chunk2.yaml' + /test3: + get: + responses: + '200': + description: test + content: + application/json: + schema: + $ref: './output-local-directory/spec-chunk3/spec-chunk3.yaml' +components: + schemas: + Category: + type: object + properties: + id: + type: integer + format: int64 + example: 10 + name: + type: string + example: "test"