Skip to content
Closed
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
15 changes: 13 additions & 2 deletions client/src/containers/Schema/SchemaDetail/Schema.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import Header from '../../Header';
import SchemaVersions from './SchemaVersions';
import SchemaUpdate from './SchemaUpdate';
import SchemaFields from './SchemaFields';
import endpoints from '../../../utils/endpoints';
import { getSelectedTab } from '../../../utils/functions';
import { Link } from 'react-router-dom';
Expand All @@ -18,7 +19,7 @@ class Schema extends Root {
roles: JSON.parse(sessionStorage.getItem('roles'))
};

tabs = ['update', 'versions'];
tabs = ['update', 'versions', 'fields'];

componentDidMount() {
this.getSchemaVersions();
Expand Down Expand Up @@ -46,7 +47,7 @@ class Schema extends Root {
{
schemaVersions: schemas.data,
totalVersions: schemas.data.length,
selectedTab: tabSelected === 'versions' ? tabSelected : 'update'
selectedTab: tabSelected
},
() => {
this.props.router.navigate(
Expand Down Expand Up @@ -74,6 +75,8 @@ class Schema extends Root {
schemaId={schemaId}
/>
);
case 'fields':
return <SchemaFields schemas={schemaVersions} />;
case 'versions':
return (
<SchemaVersions schemaName={schemaId} clusterId={clusterId} schemas={schemaVersions} />
Expand Down Expand Up @@ -109,6 +112,14 @@ class Schema extends Root {
Versions <span className="badge bg-secondary">{totalVersions}</span>
</Link>
</li>
<li className="nav-item">
<Link
to={`/ui/${clusterId}/schema/details/${schemaId}/fields`}
className={this.tabClassName('fields')}
>
Fields
</Link>
</li>
</ul>

<div className="tab-content">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import React, { useMemo, useState } from 'react';
import PropTypes from 'prop-types';
import { extractAvroFields, MAX_FIELDS } from '../../../../utils/avroFields';

const latestVersion = schemas =>
schemas.reduce((latest, schema) => (schema.version > latest ? schema.version : latest), 0);

const recordDoc = definition => {
try {
const parsed = JSON.parse(definition);
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed.doc : undefined;
} catch (e) {
return undefined;
}
};

const SchemaFields = ({ schemas }) => {
const versions = schemas || [];
const [selectedVersion, setSelectedVersion] = useState(() => latestVersion(versions));
const [filter, setFilter] = useState('');

const schema =
versions.find(version => version.version === selectedVersion) ??
versions.find(version => version.version === latestVersion(versions));
// A version the server could not parse comes back without schema and schemaType.
const failed = schema && (schema.exception || !schema.schema);
const isAvro = schema && !failed && (!schema.schemaType || schema.schemaType === 'AVRO');
const fields = useMemo(() => (isAvro ? extractAvroFields(schema.schema) : []), [isAvro, schema]);

if (!schema) {
return null;
}

const needle = filter.trim().toLowerCase();
const visibleFields = needle
? fields.filter(
field =>
field.path.toLowerCase().includes(needle) || field.doc.toLowerCase().includes(needle)
)
: fields;
const documentedCount = fields.filter(field => field.doc).length;
const description = isAvro ? recordDoc(schema.schema) : undefined;

return (
<div className="schema-fields">
<div className="d-flex flex-wrap align-items-center gap-2 mb-3">
<label htmlFor="schema-fields-version" className="mb-0">
Version
</label>
<select
id="schema-fields-version"
className="form-select w-auto"
value={schema.version}
onChange={event => setSelectedVersion(Number(event.target.value))}
>
{[...versions]
.sort((a, b) => b.version - a.version)
.map(version => (
<option key={version.version} value={version.version}>
{version.version} (id {version.id})
</option>
))}
</select>
{isAvro && (
<>
<input
type="search"
className="form-control w-auto"
placeholder="Filter fields"
aria-label="Filter fields"
value={filter}
onChange={event => setFilter(event.target.value)}
/>
<span className="text-muted">
{documentedCount} / {fields.length} fields documented
</span>
</>
)}
</div>

{failed && (
<div className="alert alert-warning" role="alert">
This version could not be parsed{schema.exception ? `: ${schema.exception}` : '.'}
</div>
)}

{!failed && !isAvro && (
<div className="alert alert-info" role="alert">
Field descriptions are only available for Avro schemas.
</div>
)}

{description && <p className="schema-fields-description">{description}</p>}

{isAvro && fields.length >= MAX_FIELDS && (
<div className="alert alert-warning" role="alert">
Only the first {MAX_FIELDS} fields are listed.
</div>
)}

{isAvro && (
<div className="table-responsive">
<table className="table table-sm table-bordered">
<thead>
<tr>
<th>Field</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
{visibleFields.map((field, index) => (
<tr key={`${field.path}-${index}`}>
<td>
<code>{field.path}</code>
</td>
<td>
<code>{field.type}</code>
</td>
<td>{field.doc}</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
);
};

SchemaFields.propTypes = {
schemas: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.number,
version: PropTypes.number,
schemaType: PropTypes.string,
schema: PropTypes.string
})
)
};

export default SchemaFields;
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import React from 'react';
import { afterEach, describe, expect, it } from 'vitest';
import { cleanup, fireEvent, render, screen } from '@testing-library/react';
import SchemaFields from './SchemaFields';

const v1 = {
id: 10,
version: 1,
schemaType: 'AVRO',
schema: JSON.stringify({
type: 'record',
name: 'User',
fields: [{ name: 'id', type: 'long', doc: 'Old id' }]
})
};

const v2 = {
id: 11,
version: 2,
schemaType: 'AVRO',
schema: JSON.stringify({
type: 'record',
name: 'User',
doc: 'A registered user.',
fields: [
{ name: 'id', type: 'long', doc: 'Internal user id' },
{ name: 'nickname', type: ['null', 'string'], default: null }
]
})
};

describe('SchemaFields', () => {
afterEach(() => cleanup());

it('shows every field of the latest version by default', () => {
render(<SchemaFields schemas={[v1, v2]} />);

expect(screen.getByText('A registered user.')).toBeTruthy();
expect(screen.getByText('Internal user id')).toBeTruthy();
expect(screen.getByText('nickname')).toBeTruthy();
expect(screen.getByText('null | string')).toBeTruthy();
expect(screen.getByText('1 / 2 fields documented')).toBeTruthy();
expect(screen.queryByText('Old id')).toBeNull();
});

it('switches to another version', () => {
render(<SchemaFields schemas={[v1, v2]} />);

fireEvent.change(screen.getByLabelText('Version'), { target: { value: '1' } });

expect(screen.getByText('Old id')).toBeTruthy();
expect(screen.queryByText('nickname')).toBeNull();
});

it('filters by field path or description', () => {
render(<SchemaFields schemas={[v2]} />);

fireEvent.change(screen.getByLabelText('Filter fields'), { target: { value: 'internal' } });

expect(screen.getByText('Internal user id')).toBeTruthy();
expect(screen.queryByText('nickname')).toBeNull();
});

it('explains that only Avro schemas are supported', () => {
render(
<SchemaFields
schemas={[{ id: 3, version: 1, schemaType: 'PROTOBUF', schema: 'syntax = "proto3";' }]}
/>
);

expect(
screen.getByText('Field descriptions are only available for Avro schemas.')
).toBeTruthy();
});

it('reports a version the server could not parse', () => {
render(
<SchemaFields
schemas={[
{ id: 4, version: 1, schemaType: null, schema: null, exception: 'Invalid schema' }
]}
/>
);

expect(screen.getByText('This version could not be parsed: Invalid schema')).toBeTruthy();
expect(screen.queryByText('Field')).toBeNull();
});

it('renders nothing until the versions are loaded', () => {
const { container } = render(<SchemaFields schemas={[]} />);

expect(container.innerHTML).toBe('');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import SchemaFields from './SchemaFields';

export default SchemaFields;
Loading
Loading