Skip to content

Commit

Permalink
fix: add condition to check type of property before parsing from json…
Browse files Browse the repository at this point in the history
… object

Signed-off-by: Muhammad Aaqil <[email protected]>
  • Loading branch information
aaqilniz committed Aug 25, 2024
1 parent 7a1083e commit 4ec1b7e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/cli/generators/datasource/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ module.exports = class DataSourceGenerator extends ArtifactGenerator {
if (props[key] == null || props[key] === '') {
delete props[key];
} else {
props[key] = JSON.parse(props[key]);
if (typeof props[key] === 'string') {
props[key] = JSON.parse(props[key]);
}
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,65 @@ export class DsDataSource extends juggler.DataSource
`;

exports[`lb4 datasource integration correctly coerces setting input of type object and array with config 1`] = `
import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core';
import {juggler} from '@loopback/repository';
const config = {
name: 'ds',
connector: 'rest',
localConfigOnly: true,
sharedData: {},
forwardErrorToEnvironment: false,
skipInstall: true,
skipCache: true,
skipLocalCache: true,
force: true,
'skip-cache': true,
'skip-install': true,
resolved: '/home/aaqilniz/working-space/patrick/forked/loopback-next/packages/cli/generators/datasource/index.js',
namespace: 'home:aaqilniz:working-space:patrick:forked:loopback-next:packages:cli:datasource',
_: [],
config: '{"name":"ds","connector":"rest","options":"{\\"test\\": \\"value\\"}","operations":"[\\"get\\", \\"post\\"]"}',
yes: true,
'force-install': false,
'ask-answered': false,
c: '{"name":"ds","connector":"rest","options":"{\\"test\\": \\"value\\"}","operations":"[\\"get\\", \\"post\\"]"}',
y: [
'@@_YEOMAN_EMPTY_MARKER_@@',
true
],
options: {
test: 'value'
},
operations: [
'get',
'post'
],
baseURL: null,
crud: false
};
// Observe application's life cycle to disconnect the datasource when
// application is stopped. This allows the application to be shut down
// gracefully. The \`stop()\` method is inherited from \`juggler.DataSource\`.
// Learn more at https://loopback.io/doc/en/lb4/Life-cycle.html
@lifeCycleObserver('datasource')
export class DsDataSource extends juggler.DataSource
implements LifeCycleObserver {
static dataSourceName = 'ds';
static readonly defaultConfig = config;
constructor(
@inject('datasources.config.ds', {optional: true})
dsConfig: object = config,
) {
super(dsConfig);
}
}
`;


exports[`lb4 datasource integration scaffolds correct file with cloudant input 1`] = `
import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core';
Expand Down
13 changes: 13 additions & 0 deletions packages/cli/test/integration/generators/datasource.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ describe('lb4 datasource integration', () => {

checkDataSourceFilesAgainstSnapshot();
});

it('correctly coerces setting input of type object and array with config', async () => {
await testUtils
.executeGenerator(generator)
.inDir(sandbox.path, () => testUtils.givenLBProject(sandbox.path))
.withArguments([
'--config',
`${JSON.stringify(complexCLIInput)}`,
'--yes',
]);

checkDataSourceFilesAgainstSnapshot();
});
});

function checkDataSourceFilesAgainstSnapshot() {
Expand Down

0 comments on commit 4ec1b7e

Please sign in to comment.