Skip to content

Commit

Permalink
fix: Default structureDemiter & test in export (#1469)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanCizmar committed Dec 22, 2022
1 parent 0c2b304 commit 3ced45f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
Expand Up @@ -65,6 +65,7 @@ import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.context.request.WebRequest
import java.util.*
Expand Down Expand Up @@ -132,7 +133,8 @@ e.g. For key "home.header.title" would result in {"home": {"header": {"title": "
When null, resulting file will be a flat key-value object.
""",
)
structureDelimiter: Char? = '.',
@RequestParam(value = "structureDelimiter", defaultValue = ".", required = false)
structureDelimiter: Char?,
request: WebRequest
): ResponseEntity<Map<String, Any>>? {
val lastModified: Long = projectTranslationLastModifiedManager.getLastModified(projectHolder.project.id)
Expand All @@ -145,7 +147,7 @@ When null, resulting file will be a flat key-value object.
languageTags = languages,
namespace = ns,
projectId = projectHolder.project.id,
structureDelimiter = structureDelimiter
structureDelimiter = request.getStructureDelimiter()
)

return ResponseEntity.ok()
Expand All @@ -156,6 +158,17 @@ When null, resulting file will be a flat key-value object.
)
}

/**
* It has to be handled manually since spring returns default value even when empty value provided
*/
private fun WebRequest.getStructureDelimiter(): Char? {
val structureDelimiterParam = this.parameterMap["structureDelimiter"]?.first() ?: return '.'
if (structureDelimiterParam == "") {
return null
}
return structureDelimiterParam.toCharArray().first()
}

@PutMapping("")
@AccessWithApiKey(scopes = [ApiScope.TRANSLATIONS_EDIT])
@AccessWithProjectPermission(permission = Permission.ProjectPermissionType.TRANSLATE)
Expand Down
Expand Up @@ -95,6 +95,33 @@ class V2ExportControllerTest : ProjectAuthControllerTest("/v2/projects/") {
assertThat(time).isLessThan(2000)
}

@Test
@Transactional
@ProjectJWTAuthTestMethod
fun `the structureDelimiter works`() {
testData = TranslationsTestData()
testData.generateScopedData()
testDataService.saveTestData(testData.root)
prepareUserAndProject(testData)
commitTransaction()

performExport("structureDelimiter=").let { parsed ->
assertThatJson(parsed["en.json"]!!) {
node("hello\\.i\\.am\\.scoped").isEqualTo("yupee!")
}
}
performExport("structureDelimiter=+").let { parsed ->
assertThatJson(parsed["en.json"]!!) {
node("hello.i.am.plus.scoped").isEqualTo("yupee!")
}
}
performExport("").let { parsed ->
assertThatJson(parsed["en.json"]!!) {
node("hello.i.am.scoped").isEqualTo("yupee!")
}
}
}

private fun performExport(query: String = ""): Map<String, String> {
val mvcResult = performProjectAuthGet("export?$query")
.andIsOk
Expand Down
Expand Up @@ -69,7 +69,7 @@ class V2TranslationsControllerCachingTest : ProjectAuthControllerTest("/v2/proje
val lastModified = performAndGetLastModified()
performWithIsModifiedSince(lastModified).andIsNotModified

val newNow = Date()
val newNow = Date(Date().time + 50000)
whenever(currentDateProvider.date).then { newNow }
translationService.setTranslation(testData.aKey, testData.englishLanguage, "This was changed!")

Expand Down
Expand Up @@ -261,6 +261,9 @@ class V2TranslationsControllerViewTest : ProjectAuthControllerTest("/v2/projects
performProjectAuthGet("/translations/en,de?structureDelimiter=+").andIsOk.andAssertThatJson {
node("en.hello.i.am.plus.scoped").isEqualTo("yupee!")
}
performProjectAuthGet("/translations/en,de").andIsOk.andAssertThatJson {
node("en.hello.i.am.scoped").isEqualTo("yupee!")
}
}

@ProjectApiKeyAuthTestMethod(scopes = [ApiScope.TRANSLATIONS_VIEW])
Expand Down

0 comments on commit 3ced45f

Please sign in to comment.