Skip to content

Commit e7ed32f

Browse files
authored
fix: numericAttributesToIndex deserialization (#637)
1 parent ab766b0 commit e7ed32f

File tree

4 files changed

+52
-62
lines changed

4 files changed

+52
-62
lines changed

src/main/scala/algolia/AlgoliaDsl.scala

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import algolia.dsl._
3030
import algolia.objects._
3131
import org.json4s.JsonAST._
3232
import org.json4s.JsonDSL._
33-
import org.json4s.{CustomSerializer, Formats}
33+
import org.json4s.{CustomSerializer, FieldSerializer, Formats, JField}
3434

3535
import java.time.format.{DateTimeFormatter, DateTimeFormatterBuilder}
3636
import java.time.{Instant, LocalDateTime, ZoneOffset, ZonedDateTime}
@@ -77,7 +77,6 @@ object AlgoliaDsl extends AlgoliaDsl {
7777
org.json4s.DefaultFormats +
7878
new SearchableAttributesSerializer +
7979
new AttributesToIndexSerializer +
80-
new NumericAttributesToIndexSerializer +
8180
new RankingSerializer +
8281
new CustomRankingSerializer +
8382
new QueryTypeSerializer +
@@ -90,7 +89,10 @@ object AlgoliaDsl extends AlgoliaDsl {
9089
new IgnorePluralsSerializer +
9190
new LocalDateTimeSerializer +
9291
new ZonedDateTimeSerializer +
93-
new AlternativesSerializer
92+
new AlternativesSerializer +
93+
new FieldSerializer[IndexSettings](
94+
deserializer = numericAttributesToIndexDeserializer
95+
)
9496

9597
val searchableAttributesUnordered: Regex = """^unordered\(([\w-\\.]+)\)$""".r
9698
val searchableAttributesAttributes: Regex =
@@ -148,18 +150,6 @@ object AlgoliaDsl extends AlgoliaDsl {
148150
})
149151
)
150152

151-
class NumericAttributesToIndexSerializer
152-
extends CustomSerializer[NumericAttributesToIndex](
153-
_ =>
154-
({
155-
case JString(numericAttributesToIndexEqualOnly(attr)) =>
156-
NumericAttributesToIndex.equalOnly(attr)
157-
}, {
158-
case NumericAttributesToIndex.equalOnly(attr) =>
159-
JString(s"equalOnly($attr)")
160-
})
161-
)
162-
163153
class RankingSerializer
164154
extends CustomSerializer[Ranking](
165155
_ =>
@@ -412,6 +402,18 @@ object AlgoliaDsl extends AlgoliaDsl {
412402
})
413403
)
414404

405+
def numericAttributesToIndexDeserializer: PartialFunction[JField, JField] = {
406+
case JField("numericAttributesToIndex", JArray(s: Seq[JValue])) =>
407+
JField(
408+
"numericAttributesForFiltering",
409+
s.map(e =>
410+
e.asInstanceOf[JString].s match {
411+
case numericAttributesToIndexEqualOnly(attr) => attr
412+
}
413+
)
414+
)
415+
}
416+
415417
case object forwardToSlaves extends ForwardToReplicas
416418

417419
case object forwardToReplicas extends ForwardToReplicas
@@ -425,5 +427,4 @@ object AlgoliaDsl extends AlgoliaDsl {
425427
case object in extends In
426428

427429
case object abTests extends ABTests
428-
429430
}

src/main/scala/algolia/objects/IndexSettings.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ case class IndexSettings(
6868
paginationLimitedTo: Option[Int] = None,
6969
/* Performance */
7070
numericAttributesForFiltering: Option[Seq[String]] = None,
71-
numericAttributesToIndex: Option[Seq[NumericAttributesToIndex]] = None,
7271
allowCompressionOfIntegerArray: Option[Boolean] = None,
7372
/* Query rules */
7473
enableRules: Option[Boolean] = None,

src/main/scala/algolia/objects/NumericAttributesToIndex.scala

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/test/scala/algolia/dsl/IndexSettingsTest.scala

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ class IndexSettingsTest extends AlgoliaTest {
9999
| "attr2"
100100
| ]
101101
| },
102-
| "numericAttributesToIndex":[
103-
| "equalOnly(att5)"
102+
| "numericAttributesForFiltering":[
103+
| "att5",
104+
| "att6"
104105
| ],
105106
| "removeStopWords":"fr,en",
106107
| "ranking":[
@@ -124,7 +125,8 @@ class IndexSettingsTest extends AlgoliaTest {
124125
|}""".stripMargin
125126

126127
it("should deserialize json") {
127-
inside(parse(json).extract[IndexSettings]) {
128+
val indexSettings = parse(json).extract[IndexSettings]
129+
inside(indexSettings) {
128130
case i: IndexSettings =>
129131
i.attributesToIndex should be(
130132
Some(
@@ -144,11 +146,9 @@ class IndexSettingsTest extends AlgoliaTest {
144146
)
145147
)
146148
)
147-
i.numericAttributesToIndex should be(
149+
i.numericAttributesForFiltering should be(
148150
Some(
149-
Seq(
150-
NumericAttributesToIndex.equalOnly("att5")
151-
)
151+
Seq("att5", "att6")
152152
)
153153
)
154154
i.ranking should be(
@@ -205,8 +205,7 @@ class IndexSettingsTest extends AlgoliaTest {
205205
SearchableAttributes.unordered("att4")
206206
)
207207
),
208-
numericAttributesToIndex =
209-
Some(Seq(NumericAttributesToIndex.equalOnly("att5"))),
208+
numericAttributesForFiltering = Some(Seq("att5", "att6")),
210209
ranking = Some(
211210
Seq(
212211
Ranking.typo,
@@ -238,6 +237,33 @@ class IndexSettingsTest extends AlgoliaTest {
238237
writePretty(i) should be(json)
239238
}
240239

240+
it("should deserialize legacy json") {
241+
val jsonDeserialize =
242+
"""{
243+
| "numericAttributesToIndex":[
244+
| "equalOnly(att5)",
245+
| "equalOnly(att6)"
246+
| ]
247+
|}""".stripMargin
248+
249+
val indexSettings = parse(jsonDeserialize).extract[IndexSettings]
250+
inside(indexSettings) {
251+
case i: IndexSettings =>
252+
i.numericAttributesForFiltering should be(
253+
Some(Seq("att5", "att6"))
254+
)
255+
}
256+
257+
val jsonSerialize =
258+
"""{
259+
| "numericAttributesForFiltering":[
260+
| "att5",
261+
| "att6"
262+
| ]
263+
|}""".stripMargin
264+
265+
writePretty(indexSettings) should be(jsonSerialize)
266+
}
241267
}
242268

243269
}

0 commit comments

Comments
 (0)