-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove empty ids from source when importing resources (#4931)
Co-authored-by: Simon Dumas <[email protected]> Co-authored-by: dantb <[email protected]>
- Loading branch information
1 parent
5e21304
commit 55bcdfe
Showing
4 changed files
with
61 additions
and
8 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
30 changes: 30 additions & 0 deletions
30
ship/src/main/scala/ch/epfl/bluebrain/nexus/ship/resources/SourcePatcher.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,30 @@ | ||
package ch.epfl.bluebrain.nexus.ship.resources | ||
|
||
import cats.effect.IO | ||
import ch.epfl.bluebrain.nexus.delta.plugins.storage.FileSelf | ||
import ch.epfl.bluebrain.nexus.delta.sdk.model.BaseUri | ||
import ch.epfl.bluebrain.nexus.ship.ProjectMapper | ||
import ch.epfl.bluebrain.nexus.ship.resources.SourcePatcher.removeEmptyIds | ||
import io.circe.Json | ||
import io.circe.optics.JsonPath.root | ||
|
||
final class SourcePatcher(distributionPatcher: DistributionPatcher) { | ||
|
||
def apply(json: Json): IO[Json] = distributionPatcher.singleOrArray(json).map(removeEmptyIds) | ||
|
||
} | ||
|
||
object SourcePatcher { | ||
|
||
private val emptyString = Json.fromString("") | ||
|
||
// Remove empty ids from sources as they are not accepted anymore in payloads | ||
// Resources will be keep their ids as we explicitly pass them along the source payload | ||
val removeEmptyIds: Json => Json = root.obj.modify { | ||
case obj if obj("@id").contains(emptyString) => obj.remove("@id") | ||
case obj => obj | ||
} | ||
|
||
def apply(fileSelfParser: FileSelf, projectMapper: ProjectMapper, targetBase: BaseUri): SourcePatcher = | ||
new SourcePatcher(new DistributionPatcher(fileSelfParser, projectMapper, targetBase)) | ||
} |
23 changes: 23 additions & 0 deletions
23
ship/src/test/scala/ch/epfl/bluebrain/nexus/ship/resources/SourcePatcherSuite.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,23 @@ | ||
package ch.epfl.bluebrain.nexus.ship.resources | ||
|
||
import ch.epfl.bluebrain.nexus.testkit.mu.NexusSuite | ||
|
||
class SourcePatcherSuite extends NexusSuite { | ||
|
||
test("Removing empty ids does not modify the source when the @id is non empty") { | ||
val source = json"""{ "@id": "bob-id", "name": "Bob" }""" | ||
assertEquals(SourcePatcher.removeEmptyIds(source), source) | ||
} | ||
|
||
test("Removing empty ids does not modify the source when there is no @id") { | ||
val source = json"""{ "name": "Bob" }""" | ||
assertEquals(SourcePatcher.removeEmptyIds(source), source) | ||
} | ||
|
||
test("Removing empty ids removes the @id field when it is empty") { | ||
val source = json"""{ "@id": "", "name": "Bob" }""" | ||
val expected = json"""{ "name": "Bob" }""" | ||
assertEquals(SourcePatcher.removeEmptyIds(source), expected) | ||
} | ||
|
||
} |