Skip to content

Commit

Permalink
fix: support ref in object root and not return 500 (#41)
Browse files Browse the repository at this point in the history
* chore: wip

* test: added integration test
  • Loading branch information
CptSchnitz authored Jul 25, 2024
1 parent 29eb92b commit 93318f2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/configs/models/configManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ export class ConfigManager {
replacementValue = { ...prevValue, ...config.config };
}

if (path === '') {
Object.assign(obj, replacementValue);
return;
}

pointer.set(obj, path, replacementValue);
}
}
Expand Down
17 changes: 17 additions & 0 deletions tests/integration/configs/configs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/config/configManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 93318f2

Please sign in to comment.