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

Fixes handling of numeric strings in span tag values #436

Merged
merged 3 commits into from
Aug 11, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ describe('LinkValue', () => {
describe('<KeyValuesTable>', () => {
let wrapper;

const data = [{ key: 'span.kind', value: 'client' }, { key: 'omg', value: 'mos-def' }];
const data = [
{ key: 'span.kind', value: 'client' },
{ key: 'omg', value: 'mos-def' },
{ key: 'numericString', value: '12345678901234567890' },
{ key: 'jsonkey', value: JSON.stringify({ hello: 'world' }) },
];

beforeEach(() => {
wrapper = shallow(<KeyValuesTable data={data} />);
Expand Down Expand Up @@ -127,4 +132,14 @@ describe('<KeyValuesTable>', () => {
expect(copyIcon.prop('tooltipTitle')).toBe('Copy JSON');
});
});

it('renders a span value containing numeric string correctly', () => {
const el = wrapper.find('.ub-inline-block');
expect(el.length).toBe(data.length);
el.forEach((valueDiv, i) => {
if (data[i].key !== 'jsonkey') {
expect(valueDiv.html()).toMatch(`"${data[i].value}"`);
}
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ import { KeyValuePair, Link } from '../../../../types/trace';

import './KeyValuesTable.css';

function parseIfJson(value: any) {
try {
return JSON.parse(value);
// eslint-disable-next-line no-empty
} catch (_) {}
const jsonObjectOrArrayStartRegex = /^(\[|\{)/;

function parseIfComplexJson(value: any) {
// if the value is a string representing actual json object or array, then use json-markup
if (typeof value === 'string' && jsonObjectOrArrayStartRegex.test(value)) {
// otherwise just return as is
try {
return JSON.parse(value);
// eslint-disable-next-line no-empty
} catch (_) {}
}
return value;
}

Expand Down Expand Up @@ -66,7 +72,7 @@ export default function KeyValuesTable(props: KeyValuesTableProps) {
<tbody className="KeyValueTable--body">
{data.map((row, i) => {
const markup = {
__html: jsonMarkup(parseIfJson(row.value)),
__html: jsonMarkup(parseIfComplexJson(row.value)),
};
// eslint-disable-next-line react/no-danger
const jsonTable = <div className="ub-inline-block" dangerouslySetInnerHTML={markup} />;
Expand Down