Skip to content

Commit

Permalink
Merge pull request #320 from Ecwid/fix-product-filters-with-commas
Browse files Browse the repository at this point in the history
Fix wrong escaping of option/attribute names containing ',' symbol in /products/filters request
  • Loading branch information
Alexis2004 authored Sep 5, 2023
2 parents 3316dc4 + 114db0b commit 723b380
Showing 1 changed file with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,17 @@ data class GetProductFiltersRequest(
data class Option(
private val optionName: String = ""
) : FilterFieldType() {
override fun getFilterFieldName() = "option_" + escapeName(optionName)
override fun getFilterFieldName() = "option_$optionName"
}

data class Attribute(
private val attributeName: String = ""
) : FilterFieldType() {
override fun getFilterFieldName() = "attribute_" + escapeName(attributeName)
override fun getFilterFieldName() = "attribute_$attributeName"
}

abstract fun getFilterFieldName(): String

protected fun escapeName(name: String): String {
return name.replace(",", "\\,").replace("\\", "\\\\")
}

}

@Suppress("unused")
Expand Down Expand Up @@ -176,16 +172,26 @@ data class GetProductFiltersRequest(
}

private fun List<FilterFieldType>.toFilterFields(): String {
return joinToString(separator = ",", transform = FilterFieldType::getFilterFieldName)
return joinToString(separator = ",") { filterFieldType ->
escapeProductFilterName(filterFieldType.getFilterFieldName())
}
}

private fun Map<FilterFieldType, FilterFacetLimit>.toFilterFacetLimitValue(): String {
return toList()
.joinToString(separator = ",") { (filterFieldType, filterFacetLimit) ->
"${filterFieldType.getFilterFieldName()}:${filterFacetLimit.getFilterFacetLimitValue()}"
val filterName = escapeProductFilterName(filterFieldType.getFilterFieldName())
val filterFacetLimitValue = filterFacetLimit.getFilterFacetLimitValue()
"$filterName:$filterFacetLimitValue"
}
}

private fun escapeProductFilterName(name: String): String {
return name
.replace("\\", "\\\\")
.replace(",", "\\,")
}

private fun List<FilterCategoryId>.toCategoriesValue(): String {
val categoriesValue = joinToString(separator = ",") { categoryId ->
categoryId.getFilterParentCategoryIdValue()
Expand Down

0 comments on commit 723b380

Please sign in to comment.