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

Counter widget sends double requests, the first one has viewport as undefined #10674 #10683

Merged
merged 10 commits into from
Nov 27, 2024
3 changes: 2 additions & 1 deletion web/client/components/widgets/enhancers/wpsCounter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const sameOptions = (o1 = {}, o2 = {}) =>
&& o1.aggregationAttribute === o2.aggregationAttribute
&& o1.viewParams === o2.viewParams;
import { getWpsUrl } from '../../../utils/LayersUtils';
import { validXMLFilter } from '../../../utils/XMLUtils';

/**
* Stream of props -> props to retrieve data from WPS aggregate process on params changes.
Expand All @@ -30,7 +31,7 @@ import { getWpsUrl } from '../../../utils/LayersUtils';
*/
const dataStreamFactory = ($props) =>
$props
.filter(({layer = {}, options}) => layer.name && getWpsUrl(layer) && options && options.aggregateFunction && options.aggregationAttribute)
.filter(({layer = {}, options, filter}) => layer.name && getWpsUrl(layer) && options && options.aggregateFunction && options.aggregationAttribute && validXMLFilter(filter))
.distinctUntilChanged(
({layer = {}, options = {}, filter}, newProps) =>
(newProps.layer && layer.name === newProps.layer.name && layer.loadingError === newProps.layer.loadingError)
Expand Down
30 changes: 30 additions & 0 deletions web/client/utils/XMLUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,33 @@ export const objectToAttributes = (obj = {}, xmlns) => keys(obj).filter(key => o
}));

export const assignNamespace = (nodes, xmlns) => nodes.filter(node => !!node).map(node => ({...node, xmlns}));


/**
* Checks if any <ogc:And> element in the XML string contains the text "undefined".
*
* @param {string} xmlString - The XML string to check.
* @returns {boolean} - Returns `false` if any <ogc:And> contains "undefined", otherwise `true`.
*
* @example
* validFilter('<ogc:Filter><ogc:And>undefined</ogc:And></ogc:Filter>'); // false
* validFilter('<ogc:Filter><ogc:And>valid</ogc:And></ogc:Filter>'); // true
*/
export function validXMLFilter(xmlString) {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, "application/xml");

// Find all <ogc:And> elements (you can have multiple <ogc:And> elements)
const andElements = xmlDoc.getElementsByTagName("ogc:And");

// Iterate through the <ogc:And> elements and check for "undefined"
for (let i = 0; i < andElements.length; i++) {
const andElement = andElements[i];

// If we find an <ogc:And> element with text "undefined", return false
if (andElement.textContent === "undefined") {
return false;
}
}
return true;
}
10 changes: 9 additions & 1 deletion web/client/utils/__tests__/XMLUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
escapeText,
escapeAttributeValue,
removeEmptyNodes,
writeXML
writeXML,
validXMLFilter
} from '../XMLUtils';

const namespaces = {
Expand Down Expand Up @@ -155,4 +156,11 @@ describe('XMLUtils tests', () => {
</Root>`;
expect(writeXML(removeEmptyNodes(tree), values(namespaces))).toBe(xml);
});

it('Check valid XML filter', () => {
const xmlFilter = '<ogc:Filter><ogc:And>filters</ogc:And></ogc:Filter>';
expect(validXMLFilter(xmlFilter)).toBe(true);
const invalidXMLFilter = '<ogc:Filter><ogc:And>undefined</ogc:And></ogc:Filter>';
expect(validXMLFilter(invalidXMLFilter)).toBe(false);
});
});
Loading