From 93318f24984a53afa24e5a1ce7f1e32e4ea1b070 Mon Sep 17 00:00:00 2001 From: Ofer <12687466+CptSchnitz@users.noreply.github.com> Date: Thu, 25 Jul 2024 16:42:47 +0300 Subject: [PATCH] fix: support ref in object root and not return 500 (#41) * chore: wip * test: added integration test --- src/configs/models/configManager.ts | 5 +++++ tests/integration/configs/configs.spec.ts | 17 +++++++++++++++++ tests/unit/config/configManager.spec.ts | 14 ++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/src/configs/models/configManager.ts b/src/configs/models/configManager.ts index 436a370..a7e3aaa 100644 --- a/src/configs/models/configManager.ts +++ b/src/configs/models/configManager.ts @@ -173,6 +173,11 @@ export class ConfigManager { replacementValue = { ...prevValue, ...config.config }; } + if (path === '') { + Object.assign(obj, replacementValue); + return; + } + pointer.set(obj, path, replacementValue); } } diff --git a/tests/integration/configs/configs.spec.ts b/tests/integration/configs/configs.spec.ts index e5a16e3..7d06f1d 100644 --- a/tests/integration/configs/configs.spec.ts +++ b/tests/integration/configs/configs.spec.ts @@ -389,6 +389,23 @@ describe('config', function () { expect(response.status).toBe(httpStatusCodes.CREATED); expect(response).toSatisfyApiSpec(); }); + + it('should return 201 and create a config that is a root ref to another config', async function () { + const response = await requestSender.postConfig({ + configName: 'config-root-ref', + schemaId: 'https://mapcolonies.com/simpleSchema/v1', + version: 1, + config: { + $ref: { + configName: 'config1', + version: 'latest', + }, + }, + }); + + expect(response.status).toBe(httpStatusCodes.CREATED); + expect(response).toSatisfyApiSpec(); + }); }); describe('Bad Path', function () { diff --git a/tests/unit/config/configManager.spec.ts b/tests/unit/config/configManager.spec.ts index cae1842..fe1fc34 100644 --- a/tests/unit/config/configManager.spec.ts +++ b/tests/unit/config/configManager.spec.ts @@ -52,6 +52,20 @@ describe('ConfigManager', () => { expect(result).toStrictEqual({ ...config, config: { avi: { test: 'test' } } }); }); + it('should return the config with it being dereferenced when the ref is in the root of the object', async () => { + // @ts-expect-error ts wants this to be a predicate + configValidator.validateRef = jest.fn().mockResolvedValue(true); + const config = { + config: { $ref: { configName: 'refName', version: 1 } }, + }; + const refs: ConfigRefResponse[] = [{ config: { test: 'test' }, configName: 'refName', version: 1, isMaxVersion: false }]; + configRepository.getConfigRecursive = jest.fn().mockResolvedValue([config, refs]); + + const result = await configManager.getConfig('configName', 1, true); + + expect(result).toStrictEqual({ ...config, config: { test: 'test' } }); + }); + it('should throw ConfigNotFoundError when the config does not exist', async () => { configRepository.getConfig = jest.fn().mockResolvedValue(null);