From 6841a01c55be759050e1cd8b971115a8ef3e313e Mon Sep 17 00:00:00 2001 From: Andrea Rosci Date: Tue, 5 Mar 2024 16:38:57 +0100 Subject: [PATCH] Make OData sorting predictable and unique Change-type: patch --- src/AutoUI/Filters/Filters.tsx | 12 ++--- src/AutoUI/spec_tmp.tsx | 86 +++++++++++++++++----------------- src/oData/jsonToOData.ts | 6 +-- 3 files changed, 51 insertions(+), 53 deletions(-) diff --git a/src/AutoUI/Filters/Filters.tsx b/src/AutoUI/Filters/Filters.tsx index 50b1b572..16d0d0cd 100644 --- a/src/AutoUI/Filters/Filters.tsx +++ b/src/AutoUI/Filters/Filters.tsx @@ -49,11 +49,9 @@ const removeFieldsWithNoFilter = (schema: JSONSchema): JSONSchema => { continue; } // Extract x-no-filter if available - const xNoFilter = - sieve.parseDescriptionProperty( - value, - 'x-no-filter', - ); + const xNoFilter = sieve.parseDescriptionProperty< + string[] | boolean | undefined + >(value, 'x-no-filter'); if (xNoFilter === true) { // Exclude property entirely if xNoFilter is true @@ -61,7 +59,9 @@ const removeFieldsWithNoFilter = (schema: JSONSchema): JSONSchema => { } const newValue: JSONSchemaDefinition = { ...value }; - const xNoFilterSet = Array.isArray(xNoFilter) ? new Set(xNoFilter) : undefined; + const xNoFilterSet = Array.isArray(xNoFilter) + ? new Set(xNoFilter) + : undefined; if ('properties' in value) { newValue.properties = processProperties(value.properties, xNoFilterSet); diff --git a/src/AutoUI/spec_tmp.tsx b/src/AutoUI/spec_tmp.tsx index c83717e1..2678aa50 100644 --- a/src/AutoUI/spec_tmp.tsx +++ b/src/AutoUI/spec_tmp.tsx @@ -1,54 +1,54 @@ -import React from "react"; -import { mount } from "enzyme"; +import React from 'react'; +import { mount } from 'enzyme'; import { - autoUIRunTransformers, - autoUIGetModelForCollection, - AutoUIProps, - AutoUI, -} from "./index"; + autoUIRunTransformers, + autoUIGetModelForCollection, + AutoUIProps, + AutoUI, +} from './index'; import { - dataExample, - AugmentedSshKey, - transformers, - model as sshKeyModel, -} from "./models/example"; -import { Provider, Table } from "rendition"; + dataExample, + AugmentedSshKey, + transformers, + model as sshKeyModel, +} from './models/example'; +import { Provider, Table } from 'rendition'; const props = {} as AutoUIProps; const TestAutoUI = () => ; const Demo = ({ data, model, ...otherProps }: AutoUIProps) => { - const memoizedData = React.useMemo( - () => - autoUIRunTransformers(dataExample, transformers, { - accessRole: "administrator", - }), - [dataExample] - ) as AugmentedSshKey[]; + const memoizedData = React.useMemo( + () => + autoUIRunTransformers(dataExample, transformers, { + accessRole: 'administrator', + }), + [dataExample], + ) as AugmentedSshKey[]; - return ( - - data={data ?? memoizedData} - model={model ?? autoUIGetModelForCollection(sshKeyModel)} - actions={[]} - {...otherProps} - /> - ); + return ( + + data={data ?? memoizedData} + model={model ?? autoUIGetModelForCollection(sshKeyModel)} + actions={[]} + {...otherProps} + /> + ); }; -describe("AutoUI", () => { - describe("Collection component", () => { - it("should render the collection with N results", () => { - const component = mount( - - - - ); - const table = component.find(Table); - const tableRows = table.find( - '[data-display="table-body"] [data-display="table-row"]' - ); +describe('AutoUI', () => { + describe('Collection component', () => { + it('should render the collection with N results', () => { + const component = mount( + + + , + ); + const table = component.find(Table); + const tableRows = table.find( + '[data-display="table-body"] [data-display="table-row"]', + ); - expect(tableRows).toHaveLength(dataExample.length); - }); - }); + expect(tableRows).toHaveLength(dataExample.length); + }); + }); }); diff --git a/src/oData/jsonToOData.ts b/src/oData/jsonToOData.ts index 47b1109d..e21891a0 100644 --- a/src/oData/jsonToOData.ts +++ b/src/oData/jsonToOData.ts @@ -231,7 +231,7 @@ export const orderbyBuilder = ( const direction = !reverse ? 'asc' : 'desc'; const customOrderByKey = customSort?.[field]; if (typeof customOrderByKey === 'string') { - return `${customOrderByKey} ${direction}`; + return [`${customOrderByKey} ${direction}`, `id ${direction}`]; } else if (customOrderByKey != null && typeof customOrderByKey !== 'string') { throw new Error( `Field ${field} error: custom sort for this field must be of type string, ${typeof customOrderByKey} is not accepted.`, @@ -241,7 +241,5 @@ export const orderbyBuilder = ( if (refScheme) { fieldPath += `/${refScheme.replace(/\[(.*?)\]/g, '').replace(/\./g, '/')}`; } - return { - [fieldPath]: direction, - }; + return [`${fieldPath} ${direction}`, `id ${direction}`]; };