Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't mangle property names that contain a dollar sign #1446

Merged
merged 1 commit into from Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -396,8 +396,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
)
}
Expand Down Expand Up @@ -523,10 +522,6 @@ private fun <T> 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)
Expand Down
Expand Up @@ -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()
)
}
Expand Down Expand Up @@ -279,7 +278,3 @@ private fun KSPropertyDeclaration.toPropertySpec(
}
.build()
}

private fun String.escapeDollarSigns(): String {
return replace("\$", "\${\'\$\'}")
}
Expand Up @@ -743,6 +743,22 @@ class DualKotlinTest {
this.b = b
}
}

@Test fun propertyNameHasDollarSign() {
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
val jsonAdapter = moshi.adapter<PropertyWithDollarSign>()

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
Expand Down