Skip to content

Commit

Permalink
[backend] Improve CSV mapper to handle empty lines and boolean (#4707)
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-julien committed Oct 20, 2023
1 parent b7159c9 commit aac7362
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
4 changes: 3 additions & 1 deletion opencti-platform/opencti-graphql/src/parser/csv-bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { validateInputCreation } from '../schema/schema-validator';
import { parsingProcess } from './csv-parser';
import { isStixDomainObjectContainer } from '../schema/stixDomainObject';
import { objects } from '../schema/stixRefRelationship';
import { isEmptyField } from '../database/utils';

const validateInput = async (context: AuthContext, user: AuthUser, inputs: Record<string, InputType>[]) => {
await Promise.all(inputs.map(async (input) => {
Expand All @@ -35,10 +36,11 @@ export const bundleProcess = async (context: AuthContext, user: AuthUser, conten
const records = await parsingProcess(content, mapper.separator);
if (records) {
await Promise.all((records.map(async (record: string[]) => {
const isEmptyLine = record.length === 1 && isEmptyField(record[0]);
// Handle header
if (skipLine) {
skipLine = false;
} else {
} else if (!isEmptyLine) {
// Compute input by representation
const inputs = mappingProcess(sanitizedMapper, record);
// Remove inline elements
Expand Down
13 changes: 10 additions & 3 deletions opencti-platform/opencti-graphql/src/parser/csv-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { CsvMapperRepresentationType, Operator } from '../modules/internal/csvMa
import type { AttributeColumn } from '../generated/graphql';
import { isValidTargetType } from '../modules/internal/csvMapper/csvMapper-utils';

export type InputType = string | string[] | number | Record<string, any>;
export type InputType = string | string[] | boolean | number | Record<string, any>;

// -- HANDLE VALUE --

Expand All @@ -23,10 +23,12 @@ const formatValue = (value: string, type: AttrType, column: AttributeColumn) =>
const timezone = column.configuration?.timezone;
if (type === 'string') {
return value.trim();
} if (type === 'numeric') {
}
if (type === 'numeric') {
const formattedValue = Number(value);
return Number.isNaN(formattedValue) ? null : formattedValue;
} if (type === 'date') {
}
if (type === 'date') {
try {
moment.suppressDeprecationWarnings = true;
if (isNotEmptyField(pattern_date)) {
Expand All @@ -40,6 +42,11 @@ const formatValue = (value: string, type: AttrType, column: AttributeColumn) =>
return null;
}
}
if (type === 'boolean') {
const stringBoolean = value.toLowerCase().trim();
// TODO Matching value must be configurable in parser option

This comment has been minimized.

Copy link
@labo-flg

labo-flg Oct 23, 2023

Member

@jpkha If this is a necessary change, it might be addressed on the side while working on #4505. wdyt ?

This comment has been minimized.

Copy link
@jpkha

jpkha Oct 23, 2023

Member

@labo-flg Yes,we have another task on CSV that will cover this issue. In the future, we could choose a default value for an empty value. Or a value that is not provided by the CSV and add a specific value instead.

This comment has been minimized.

Copy link
@richard-julien

richard-julien Oct 23, 2023

Author Member

This option is a bit different from the default value. Here is more adding an option to explain what kind of value represent "true" in the CSV column, so really specific to boolean handling. Before my commit it was only text "true", and i add "yes" and "1". Could be an intereting extra option to let the user defined explicitly the true representation

return stringBoolean === 'true' || stringBoolean === 'yes' || stringBoolean === '1';
}
return value;
};

Expand Down

0 comments on commit aac7362

Please sign in to comment.