Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add condition to check type of property before parsing from json #10582

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading