Skip to content

Commit bc441ad

Browse files
authored
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"}
1 parent fb5dd08 commit bc441ad

3 files changed

Lines changed: 18 additions & 12 deletions

File tree

kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/apt/metadata.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,7 @@ private fun declaredProperties(
390390
propertySpec = property,
391391
parameter = parameter,
392392
visibility = property.modifiers.visibility(),
393-
jsonName = parameter?.jsonName ?: property.annotations.jsonName()
394-
?: name.escapeDollarSigns(),
393+
jsonName = parameter?.jsonName ?: property.annotations.jsonName() ?: name,
395394
jsonIgnore = isIgnored
396395
)
397396
}
@@ -517,10 +516,6 @@ private fun <T> AnnotationSpec.elementValue(name: String): T? {
517516
}?.value?.value as? T
518517
}
519518

520-
private fun String.escapeDollarSigns(): String {
521-
return replace("\$", "\${\'\$\'}")
522-
}
523-
524519
internal val TypeElement.metadata: Metadata
525520
get() {
526521
return getAnnotation(Metadata::class.java)

kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/ksp/TargetTypes.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,7 @@ private fun declaredProperties(
243243
propertySpec = propertySpec,
244244
parameter = parameter,
245245
visibility = property.getVisibility().toKModifier() ?: KModifier.PUBLIC,
246-
jsonName = parameter?.jsonName ?: property.jsonName()
247-
?: name.escapeDollarSigns(),
246+
jsonName = parameter?.jsonName ?: property.jsonName() ?: name,
248247
jsonIgnore = isTransient || parameter?.jsonIgnore == true || property.jsonIgnore()
249248
)
250249
}
@@ -279,7 +278,3 @@ private fun KSPropertyDeclaration.toPropertySpec(
279278
}
280279
.build()
281280
}
282-
283-
private fun String.escapeDollarSigns(): String {
284-
return replace("\$", "\${\'\$\'}")
285-
}

kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/DualKotlinTest.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,22 @@ class DualKotlinTest {
743743
this.b = b
744744
}
745745
}
746+
747+
@Test fun propertyNameHasDollarSign() {
748+
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
749+
val jsonAdapter = moshi.adapter<PropertyWithDollarSign>()
750+
751+
val value = PropertyWithDollarSign("apple", "banana")
752+
val json = """{"${'$'}a":"apple","${'$'}b":"banana"}"""
753+
assertThat(jsonAdapter.toJson(value)).isEqualTo(json)
754+
assertThat(jsonAdapter.fromJson(json)).isEqualTo(value)
755+
}
756+
757+
@JsonClass(generateAdapter = true)
758+
data class PropertyWithDollarSign(
759+
val `$a`: String,
760+
@Json(name = "\$b") val b: String
761+
)
746762
}
747763

748764
typealias TypeAlias = Int

0 commit comments

Comments
 (0)