geostyler / geostyler-openlayers-parser

GeoStyler Style Parser implementation for OpenLayers styles
BSD 2-Clause "Simplified" License
36 stars 26 forks source link

geoStylerFilterToOlParserFilter Filter or rule only breaking bug with suggested fix #795

Closed xevxx closed 5 months ago

xevxx commented 5 months ago

Bug

Describe the bug geoStylerFilterToOlParserFilter in the switch statement, intermediate and restFilter are declared in the && case but used in the || case, this breaks if you only have an or filter.

offending code:

try {
      if (isNestedFilter) {
        switch (filter[0]) {
          case '&&':
            let intermediate = true;
            let restFilter = filter.slice(1);
            restFilter.forEach((f: Filter) => {
              if (!this.geoStylerFilterToOlParserFilter(feature, f)) {
                intermediate = false;
              }
            });
            matchesFilter = intermediate;
            break;
          case '||':
            intermediate = false;
            restFilter = filter.slice(1);
            restFilter.forEach((f: Filter) => {
              if (this.geoStylerFilterToOlParserFilter(feature, f)) {
                intermediate = true;
              }
            });
            matchesFilter = intermediate;
            break;

the fix is to declare the two vars outside of the switch scope:

try {
      let intermediate: boolean;
      let restFilter: any;
      if (isNestedFilter) {
        switch (filter[0]) {
          case '&&':
            intermediate = true;
            restFilter = filter.slice(1);
            restFilter.forEach((f: Filter) => {
              if (!this.geoStylerFilterToOlParserFilter(feature, f)) {
                intermediate = false;
              }
            });
            matchesFilter = intermediate;
            break;
          case '||':
            intermediate = false;
            restFilter = filter.slice(1);
            restFilter.forEach((f: Filter) => {
              if (this.geoStylerFilterToOlParserFilter(feature, f)) {
                intermediate = true;
              }
            });
            matchesFilter = intermediate;
            break;

To Reproduce process a style with an or filter only, code will break, sample failing filter (test filter to catch all records and show issue(i know its nonsensical)

"filter": [
                "||",
                [
                    "==",
                    "name_",
                    ""
                ],
                [
                    "!=",
                    "name_",
                    ""
                ]
            ]

Expected behavior style to be processed without failure

I have created a PR at https://github.com/geostyler/geostyler-openlayers-parser/pull/796

thanks for the library

jansule commented 5 months ago

Fixed in https://github.com/geostyler/geostyler-openlayers-parser/pull/796