Shawson commented on code in PR #3247:
URL: https://github.com/apache/avro/pull/3247#discussion_r1984680678


##########
lang/csharp/src/apache/main/CodeGen/CodeGen.cs:
##########
@@ -1266,5 +1273,102 @@ private static string 
ReplaceMappedNamespacesInSchema(string input, IEnumerable<
                 return 
$@"""namespace""{m.Groups[1].Value}:{m.Groups[2].Value}""{ns}""";
             });
         }
+    /// <summary>
+    /// Replace namespaces in a parsed JSON schema object for all "type" 
fields.
+    /// </summary>
+    /// <param name="schemaJson">The JSON schema as a string.</param>
+    /// <param name="namespaceMapping">The mapping of old namespaces to new 
namespaces.</param>
+    /// <returns>The updated JSON schema as a string.</returns>
+    private static string ReplaceMappedNamespacesInSchemaTypes(string 
schemaJson, IEnumerable<KeyValuePair<string, string>> namespaceMapping)
+    {
+        if (string.IsNullOrWhiteSpace(schemaJson) || namespaceMapping == null)
+        {
+            return schemaJson;
+        }
+
+        var schemaToken = JToken.Parse(schemaJson);
+
+        UpdateNamespacesInJToken(schemaToken, namespaceMapping);
+
+        return schemaToken.ToString(Formatting.Indented);
     }
+
+    /// <summary>
+    /// Recursively navigates and updates "type" fields in a JToken.
+    /// </summary>
+    /// <param name="token">The current JToken to process.</param>
+    /// <param name="namespaceMapping">The mapping of old namespaces to new 
namespaces.</param>
+    private static void UpdateNamespacesInJToken(JToken token, 
IEnumerable<KeyValuePair<string, string>> namespaceMapping)
+    {
+        if (token is JObject obj)
+        {
+            if (obj.ContainsKey("type"))
+            {
+                var typeToken = obj["type"];
+                if (typeToken is JValue) // Single type
+                {
+                    string type = typeToken.ToString();
+                    obj["type"] = ReplaceNamespace(type, namespaceMapping);
+                }
+                else if (typeToken is JArray typeArray) // Array of types
+                {
+                    for (int i = 0; i < typeArray.Count; i++)
+                    {
+                        var arrayItem = typeArray[i];
+                        if (arrayItem is JValue) // Simple type
+                        {
+                            string type = arrayItem.ToString();
+                            typeArray[i] = ReplaceNamespace(type, 
namespaceMapping);
+                        }
+                        else if (arrayItem is JObject nestedObj) // Nested 
object
+                        {
+                            UpdateNamespacesInJToken(nestedObj, 
namespaceMapping);
+                        }
+                    }
+                }
+                else if (typeToken is JObject nestedTypeObj) // Complex type
+                {
+                    UpdateNamespacesInJToken(nestedTypeObj, namespaceMapping);
+                }
+            }
+
+            // Recurse into all properties of the object
+            foreach (var property in obj.Properties())
+            {
+                UpdateNamespacesInJToken(property.Value, namespaceMapping);
+            }

Review Comment:
   I've added a list of properties which shouldn't be considered for the 
namespace mapping such as default/ doc/ symbols and a few others- However I'd 
disagree on `type` - this is a property which could have a namespace and I've 
an example of this in the test I included as part of this PR



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to