Fix: Prevent automatic parsing of string data types to numbers (#46035)

* Fix: Render unitless strings as strings from displayProcessor

Co-authored-by: kay delaney <kay@grafana.com>
Co-authored-by: Alexandra Vargas <alexa1866@gmail.com>
Co-authored-by: joshhunt <josh@trtr.co>
Co-authored-by: Ashley Harrison <ashley.harrison@grafana.com>
Co-authored-by: Joao Silva <joao.silva@grafana.com>

* update tests

* Rename none unit

* rename none unit to Number

Co-authored-by: kay delaney <kay@grafana.com>
Co-authored-by: Alexandra Vargas <alexa1866@gmail.com>
Co-authored-by: Ashley Harrison <ashley.harrison@grafana.com>
Co-authored-by: Joao Silva <joao.silva@grafana.com>
This commit is contained in:
Josh Hunt 2022-04-28 16:07:22 +01:00 committed by GitHub
parent c3ba6f962b
commit eeaa160ae8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 20 deletions

View File

@ -6,11 +6,11 @@ import { MappingType, ValueMapping } from '../types/valueMapping';
import { getDisplayProcessor, getRawDisplayProcessor } from './displayProcessor';
function getDisplayProcessorFromConfig(config: FieldConfig) {
function getDisplayProcessorFromConfig(config: FieldConfig, fieldType: FieldType = FieldType.number) {
return getDisplayProcessor({
field: {
config,
type: FieldType.number,
type: fieldType,
},
theme: createTheme(),
});
@ -456,32 +456,32 @@ describe('Date display options', () => {
});
describe('number formatting for string values', () => {
it('should preserve string unchanged if unit is strings', () => {
const processor = getDisplayProcessor({
field: {
type: FieldType.string,
config: { unit: 'string' },
},
theme: createTheme(),
});
it('should preserve string unchanged if unit is string', () => {
const processor = getDisplayProcessorFromConfig({ unit: 'string' }, FieldType.string);
expect(processor('22.1122334455').text).toEqual('22.1122334455');
});
it('should format string as number if no unit', () => {
const processor = getDisplayProcessor({
field: {
type: FieldType.string,
config: { decimals: 2 },
},
theme: createTheme(),
});
expect(processor('22.1122334455').text).toEqual('22.11');
it('should preserve string unchanged if no unit is specified', () => {
const processor = getDisplayProcessorFromConfig({}, FieldType.string);
expect(processor('22.1122334455').text).toEqual('22.1122334455');
// Support empty/missing strings
expect(processor(undefined).text).toEqual('');
expect(processor(null).text).toEqual('');
expect(processor('').text).toEqual('');
});
it('should format string as number if unit is `none`', () => {
const processor = getDisplayProcessorFromConfig({ unit: 'none' }, FieldType.string);
expect(processor('0x10').text).toEqual('16');
});
it('should not parse a 64 bit number when the data type is string', () => {
const value = '2882377905688543293';
const instance = getDisplayProcessorFromConfig({}, FieldType.string);
const disp = instance(value);
expect(disp.text).toEqual(value);
});
});
});

View File

@ -68,6 +68,8 @@ export function getDisplayProcessor(options?: DisplayProcessorOptions): DisplayP
if (!isBooleanUnit(unit)) {
unit = 'bool';
}
} else if (!unit && field.type === FieldType.string) {
unit = 'string';
}
const formatFunc = getValueFormat(unit || 'none');

View File

@ -38,7 +38,7 @@ export const getCategories = (): ValueFormatCategory[] => [
{
name: 'Misc',
formats: [
{ name: 'none', id: 'none', fn: toFixedUnit('') },
{ name: 'Number', id: 'none', fn: toFixedUnit('') },
{ name: 'String', id: 'string', fn: stringFormater },
{
name: 'short',