Repository: microsoft/kiota (csharp)

Issue:
Dart serialization uses incorrect property names
### What are you generating using Kiota, clients or plugins?

API Client/SDK

### In what context or format are you using Kiota?

Linux executable

### Client library/SDK language

Dart

### Describe the bug

Since 1.25.1 code generation for Dart is using incorrect names for the serialization when a naming conflict occurs. This seems to be caused by https://github.com/microsoft/kiota/pull/6393 (which is included in 1.25.1 even thought it's not mentioned in the release notes). The attached OpenAPI spec results in the following code in the (de)serialization: 
```
writer.writeStringValue('CertificateGetResponseType', certificateGetResponseType);
writer.writeIntValue('type', type_);
```

### Expected behavior

The generated code should have looked like this (which it was in 1.24.3):
```
writer.writeStringValue('\$type', certificateGetResponseType);
writer.writeIntValue('type', type_);
``` 

### How to reproduce

Generate the Dart SDK for the openapi spec included below.

### Open API description file

[openapi.json](https://github.com/user-attachments/files/19701456/openapi.json)

### Kiota Version

1.25.1

### Latest Kiota version known to work for scenario above?(Not required)

1.24.3

### Known Workarounds

_No response_

### Configuration

_No response_

### Debug output

<details><summary>Click to expand log</summary>
```
dbug: Kiota.Builder.KiotaBuilder[0]
      kiota version 1.25.1
info: Kiota.Builder.KiotaBuilder[0]
      loaded description from local source
dbug: Kiota.Builder.KiotaBuilder[0]
      step 1 - reading the stream - took 00:00:00.0209366
dbug: Kiota.Builder.KiotaBuilder[0]
      step 2 - parsing the document - took 00:00:00.2969836
dbug: Kiota.Builder.KiotaBuilder[0]
      step 3 - updating generation configuration from kiota extension - took 00:00:00.0002843
dbug: Kiota.Builder.KiotaBuilder[0]
      step 4 - filtering API paths with patterns - took 00:00:00.0098076
info: Kiota.Builder.KiotaBuilder[0]
      Client root URL set to https://keyhub.example.com/keyhub/rest/v1
dbug: Kiota.Builder.KiotaBuilder[0]
      step 5 - checking whether the output should be updated - took 00:00:00.0408300
dbug: Kiota.Builder.KiotaBuilder[0]
      step 6 - create uri space - took 00:00:00.0065002
info: Kiota.Builder.KiotaBuilder[0]
      No changes detected, skipping generation
Update of KeyHubClient client for Dart at /home/papegaaij/work/dart-test/lib/. completed
Client base url set to https://keyhub.example.com/keyhub/rest/v1
Update of 1 clients completed successfully

Hint: use the info command to get the list of dependencies you need to add to your project.
Example: kiota info -d "/home/papegaaij/work/dart-test/openapi.json" -l Dart
```
</details>


### Other information

_No response_

Pull request description:
Fixes #6444.

This fixes an oversight I accidentially introduced in #6393.

I adjusted the serialization name so it is correctly preserved in its original casing, but I forgot to check if it was already explicitly set.

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a545ea63ce..98d7a23049 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - Fixed a bug where path items with a trailing slash would be missing query parameters. [#6569](https://github.com/microsoft/kiota/issues/6569)
 - Fixed an issue where migration from lock to workspace would fail because of stream management. [#6515](https://github.com/microsoft/kiota/issues/6515)
 - Fixed a bug where media types from error responses would be missing from the accept header. [#6572](https://github.com/microsoft/kiota/issues/6572)
+- Fixed a bug where serialization names for Dart were not correct [#6624](https://github.com/microsoft/kiota/issues/6624)
 
 ## [1.26.1] - 2025-05-15
 
diff --git a/src/Kiota.Builder/Refiners/DartRefiner.cs b/src/Kiota.Builder/Refiners/DartRefiner.cs
index 2cedd42719..3ab1ed8922 100644
--- a/src/Kiota.Builder/Refiners/DartRefiner.cs
+++ b/src/Kiota.Builder/Refiners/DartRefiner.cs
@@ -315,7 +315,9 @@ private static void CorrectPropertyType(CodeProperty currentProperty)
         }
         else
         {
-            currentProperty.SerializationName = currentProperty.Name;
+            if (!currentProperty.IsNameEscaped)
+                currentProperty.SerializationName = currentProperty.Name;
+
             currentProperty.Name = currentProperty.Name.ToFirstCharacterLowerCase();
         }
         currentProperty.Type.Name = currentProperty.Type.Name.ToFirstCharacterUpperCase();


Test patch:
diff --git a/tests/Kiota.Builder.Tests/Refiners/DartLanguageRefinerTests.cs b/tests/Kiota.Builder.Tests/Refiners/DartLanguageRefinerTests.cs
index 003f4b8a0d..84b407977c 100644
--- a/tests/Kiota.Builder.Tests/Refiners/DartLanguageRefinerTests.cs
+++ b/tests/Kiota.Builder.Tests/Refiners/DartLanguageRefinerTests.cs
@@ -451,5 +451,27 @@ public async Task PreservesPropertyNames()
         Assert.Equal("property", model.Properties.First().Name);
         Assert.Equal("Property", model.Properties.First().WireName);
     }
+
+    [Fact]
+    public async Task DoesntOverwriteSerializationNameIfAlreadySet()
+    {
+        var model = root.AddClass(new CodeClass
+        {
+            Name = "model",
+            Kind = CodeClassKind.Model,
+        }).First();
+        model.AddProperty(new CodeProperty
+        {
+            Name = "CustomType",
+            SerializationName = "$type",
+            Type = new CodeType
+            {
+                Name = "string",
+            },
+        });
+        await ILanguageRefiner.RefineAsync(new GenerationConfiguration { Language = GenerationLanguage.Dart }, root);
+        Assert.Equal("customType", model.Properties.First().Name);
+        Assert.Equal("\\$type", model.Properties.First().WireName);
+    }
     #endregion
 }