Skip to content

Commit

Permalink
fix: auto-set apify run INPUT.json only with values that have def…
Browse files Browse the repository at this point in the history
…aults (#641)
  • Loading branch information
vladfrangu authored Sep 9, 2024
1 parent f845877 commit 759c0b0
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from '../lib/consts.js';
import { execWithLog } from '../lib/exec.js';
import { deleteFile } from '../lib/files.js';
import { getAjvValidator, getDefaultsAndPrefillsFromInputSchema, readInputSchema } from '../lib/input_schema.js';
import { getAjvValidator, getDefaultsFromInputSchema, readInputSchema } from '../lib/input_schema.js';
import { error, info, warning } from '../lib/outputs.js';
import { ProjectAnalyzer } from '../lib/project_analyzer.js';
import { ScrapyProjectAnalyzer } from '../lib/projects/scrapy/ScrapyProjectAnalyzer.js';
Expand Down Expand Up @@ -405,7 +405,7 @@ export class RunCommand extends ApifyCommand<typeof RunCommand> {
const validator = new Ajv({ strict: false, unicodeRegExp: false });
validateInputSchema(validator, inputSchema); // This one throws an error in a case of invalid schema.

const defaults = getDefaultsAndPrefillsFromInputSchema(inputSchema);
const defaults = getDefaultsFromInputSchema(inputSchema);
const compiledInputSchema = getAjvValidator(inputSchema, validator);

// Step 2: try to fetch the existing INPUT from the local storage
Expand Down
4 changes: 1 addition & 3 deletions src/lib/input_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,12 @@ export const createPrefilledInputFileFromInputSchema = async (actorFolderDir: st
}
};

export const getDefaultsAndPrefillsFromInputSchema = (inputSchema: any) => {
export const getDefaultsFromInputSchema = (inputSchema: any) => {
const defaults: Record<string, unknown> = {};

for (const [key, fieldSchema] of Object.entries<any>(inputSchema.properties)) {
if (fieldSchema.default !== undefined) {
defaults[key] = fieldSchema.default;
} else if (fieldSchema.prefill !== undefined) {
defaults[key] = fieldSchema.prefill;
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/commands/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,14 @@ describe('apify run', () => {
expect(output).toStrictEqual({ awesome: true, help: 'this_maze_is_not_meant_for_you' });
});

it('automatically inserts missing prefilled fields', async () => {
it('does not insert missing prefilled fields', async () => {
writeFileSync(inputPath, '{"awesome": true}', { flag: 'w' });
copyFileSync(pathToPrefillsInputSchema, inputSchemaPath);

await RunCommand.run([], import.meta.url);

const output = loadJsonFileSync(outputPath);
expect(output).toStrictEqual({ awesome: true, help: 'this_maze_is_not_meant_for_you' });
expect(output).toStrictEqual({ awesome: true });
});
});
});

0 comments on commit 759c0b0

Please sign in to comment.