-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed: Introduce 'delete objectFromIndex X' to deprecate 'delete obje…
…ct X from index Y' As we recently discovered, the following request was able to delete an entire index: ``` delete from "index" `object` "" ``` To fix this issue, we deprecated this way of deleting the object with the following approach: ``` val op: Option[SafeDeleteObjectOperation] = SafeDeleteObjectOperation("index", "objectID") delete objectFromIndex op.get ``` which forces the user to pass non-empty index name and objectID, otherwise the returned `Option` is `None`. This is made possible by the use of private constructors on the `SafeDeleteObjectDefinition` and of a companion object with a public `apply()` method that only lets the user instantiate a `SafeDeleteObjectDefinition` with valid i.e. non-empty parameters.
- Loading branch information
Showing
8 changed files
with
160 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/scala/algolia/definitions/SafeDeleteObjectDefinition.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2016 Algolia | ||
* http://www.algolia.com/ | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package algolia.definitions | ||
|
||
import algolia._ | ||
import algolia.http.HttpPayload | ||
import algolia.inputs.SafeDeleteObjectOperation | ||
import algolia.objects.RequestOptions | ||
import org.json4s.Formats | ||
|
||
case class SafeDeleteObjectDefinition( | ||
op: SafeDeleteObjectOperation, | ||
requestOptions: Option[RequestOptions] = None)(implicit val formats: Formats) | ||
extends Definition { | ||
|
||
override type T = SafeDeleteObjectDefinition | ||
|
||
override def options(requestOptions: RequestOptions): SafeDeleteObjectDefinition = | ||
copy(requestOptions = Some(requestOptions)) | ||
|
||
override private[algolia] def build(): HttpPayload = | ||
HttpPayload( | ||
http.DELETE, | ||
Seq("1", "indexes", op.index, op.objectID), | ||
isSearch = false, | ||
requestOptions = requestOptions | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/scala/algolia/inputs/SafeDeleteObjectOperation.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2016 Algolia | ||
* http://www.algolia.com/ | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package algolia.inputs | ||
|
||
class SafeDeleteObjectOperation private (i: String, o: String) { | ||
def index: String = i | ||
def objectID: String = o | ||
} | ||
|
||
object SafeDeleteObjectOperation { | ||
def apply(index: String, objectID: String): Option[SafeDeleteObjectOperation] = { | ||
val trimmedIndex = index.trim | ||
val trimmedObjectID = objectID.trim | ||
|
||
if (trimmedIndex.isEmpty || trimmedObjectID.isEmpty) { | ||
None | ||
} else { | ||
Some(new SafeDeleteObjectOperation(trimmedIndex, trimmedObjectID)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/test/scala/algolia/inputs/SafeDeleteObjectOperationTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2016 Algolia | ||
* http://www.algolia.com/ | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package algolia.inputs | ||
import algolia.AlgoliaTest | ||
|
||
class SafeDeleteObjectOperationTest extends AlgoliaTest { | ||
|
||
it("should produce a valid SafeDeleteObjectOperation with non-empty parameters") { | ||
val op = SafeDeleteObjectOperation("index1", "objectID1") | ||
op should not be (None) | ||
op.get.index should be("index1") | ||
op.get.objectID should be("objectID1") | ||
} | ||
|
||
it("should not produce a valid SafeDeleteObjectOperation with empty parameters") { | ||
SafeDeleteObjectOperation("", "") should be(None) | ||
SafeDeleteObjectOperation("not empty", "") should be(None) | ||
SafeDeleteObjectOperation("", "not empty") should be(None) | ||
SafeDeleteObjectOperation(" ", "not empty") should be(None) | ||
SafeDeleteObjectOperation("not empty", " ") should be(None) | ||
} | ||
|
||
} |