From bc441ad7f38abbbdb489b7bf2ff5dc164c26450b Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Sun, 5 Dec 2021 21:07:27 -0500 Subject: [PATCH] Don't mangle property names that contain a dollar sign (#1446) Without this fix the new test fails like this: value of: toJson(...) expected: {"$a":"apple","$b":"banana"} but was : {"${'$'}a":"apple","$b":"banana"} --- .../moshi/kotlin/codegen/apt/metadata.kt | 7 +------ .../moshi/kotlin/codegen/ksp/TargetTypes.kt | 7 +------ .../com/squareup/moshi/kotlin/DualKotlinTest.kt | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/apt/metadata.kt b/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/apt/metadata.kt index 5666041..dc58972 100644 --- a/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/apt/metadata.kt +++ b/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/apt/metadata.kt @@ -390,8 +390,7 @@ private fun declaredProperties( propertySpec = property, parameter = parameter, visibility = property.modifiers.visibility(), - jsonName = parameter?.jsonName ?: property.annotations.jsonName() - ?: name.escapeDollarSigns(), + jsonName = parameter?.jsonName ?: property.annotations.jsonName() ?: name, jsonIgnore = isIgnored ) } @@ -517,10 +516,6 @@ private fun AnnotationSpec.elementValue(name: String): T? { }?.value?.value as? T } -private fun String.escapeDollarSigns(): String { - return replace("\$", "\${\'\$\'}") -} - internal val TypeElement.metadata: Metadata get() { return getAnnotation(Metadata::class.java) diff --git a/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/ksp/TargetTypes.kt b/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/ksp/TargetTypes.kt index a6561fe..c4be956 100644 --- a/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/ksp/TargetTypes.kt +++ b/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/ksp/TargetTypes.kt @@ -243,8 +243,7 @@ private fun declaredProperties( propertySpec = propertySpec, parameter = parameter, visibility = property.getVisibility().toKModifier() ?: KModifier.PUBLIC, - jsonName = parameter?.jsonName ?: property.jsonName() - ?: name.escapeDollarSigns(), + jsonName = parameter?.jsonName ?: property.jsonName() ?: name, jsonIgnore = isTransient || parameter?.jsonIgnore == true || property.jsonIgnore() ) } @@ -279,7 +278,3 @@ private fun KSPropertyDeclaration.toPropertySpec( } .build() } - -private fun String.escapeDollarSigns(): String { - return replace("\$", "\${\'\$\'}") -} diff --git a/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/DualKotlinTest.kt b/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/DualKotlinTest.kt index a615000..de6e517 100644 --- a/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/DualKotlinTest.kt +++ b/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/DualKotlinTest.kt @@ -743,6 +743,22 @@ class DualKotlinTest { this.b = b } } + + @Test fun propertyNameHasDollarSign() { + val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build() + val jsonAdapter = moshi.adapter() + + val value = PropertyWithDollarSign("apple", "banana") + val json = """{"${'$'}a":"apple","${'$'}b":"banana"}""" + assertThat(jsonAdapter.toJson(value)).isEqualTo(json) + assertThat(jsonAdapter.fromJson(json)).isEqualTo(value) + } + + @JsonClass(generateAdapter = true) + data class PropertyWithDollarSign( + val `$a`: String, + @Json(name = "\$b") val b: String + ) } typealias TypeAlias = Int