File tree Expand file tree Collapse file tree 2 files changed +12
-1
lines changed
json-tests/commonTest/src/kotlinx/serialization/features
json/commonMain/src/kotlinx/serialization/json/internal Expand file tree Collapse file tree 2 files changed +12
-1
lines changed Original file line number Diff line number Diff line change @@ -182,6 +182,11 @@ class JsonNamingStrategyTest : JsonTestBase() {
182182 val json = Json (jsonWithNaming) {
183183 ignoreUnknownKeys = true
184184 }
185+ parametrizedTest { mode ->
186+ assertFailsWithMessage<SerializationException >(" The transformed name 'test_case' for property test_case already exists" ) {
187+ json.encodeToString(CollisionCheckPrimary (" a" , " b" ))
188+ }
189+ }
185190 parametrizedTest { mode ->
186191 assertFailsWithMessage<SerializationException >(" The suggested name 'test_case' for property test_case is already one of the names for property testCase" ) {
187192 json.decodeFromString<CollisionCheckPrimary >(""" {"test_case":"a"}""" , mode)
Original file line number Diff line number Diff line change @@ -55,9 +55,15 @@ internal fun Json.deserializationNamesMap(descriptor: SerialDescriptor): Map<Str
5555
5656internal fun SerialDescriptor.serializationNamesIndices (json : Json , strategy : JsonNamingStrategy ): Array <String > =
5757 json.schemaCache.getOrPut(this , JsonSerializationNamesKey ) {
58+ val trackingSet = mutableSetOf<String >()
5859 Array (elementsCount) { i ->
5960 val baseName = getElementName(i)
60- strategy.serialNameForJson(this , i, baseName)
61+ val name = strategy.serialNameForJson(this , i, baseName)
62+ if (! trackingSet.add(name)) throw JsonEncodingException (
63+ " The transformed name '$name ' for property $baseName already exists " +
64+ " in ${this @serializationNamesIndices} "
65+ )
66+ name
6167 }
6268 }
6369
You can’t perform that action at this time.
0 commit comments