From 477227f3012c0613bf932904e349b73d2b9be0be Mon Sep 17 00:00:00 2001 From: devhl Date: Sat, 26 Jul 2025 16:05:56 -0400 Subject: [PATCH 1/2] handle nested maps recurssively --- .../languages/AbstractCSharpCodegen.java | 42 +++++++++++++------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java index 84c47521f1d0..19bc7206894f 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java @@ -770,24 +770,42 @@ protected void patchProperty(Map enumRefs, CodegenModel mo property.name = patchPropertyName(model, property.name, null); + patchNestedMaps(property); + + // HOTFIX: https://github.com/OpenAPITools/openapi-generator/issues/14944 + if (property.datatypeWithEnum.equals("decimal")) { + property.isDecimal = true; + } + } + + private void patchNestedMaps(CodegenProperty property) { + // Process nested types before making any replacements to ensure we have the correct inner type + if (property.items != null) { + patchNestedMaps(property.items); + } + String[] nestedTypes = {"List", "Collection", "ICollection", "Dictionary"}; + + if (property.datatypeWithEnum != null) { + String originalType = property.datatypeWithEnum; + + for (String nestedType : nestedTypes) { + // fix incorrect data types for maps of maps + if (property.items != null) { + if (property.datatypeWithEnum.contains(", " + nestedType + ">")) { + property.datatypeWithEnum = property.datatypeWithEnum.replace(", " + nestedType + ">", ", " + property.items.datatypeWithEnum + ">"); + } - Arrays.stream(nestedTypes).forEach(nestedType -> { - // fix incorrect data types for maps of maps - if (property.datatypeWithEnum.contains(", " + nestedType + ">") && property.items != null) { - property.datatypeWithEnum = property.datatypeWithEnum.replace(", " + nestedType + ">", ", " + property.items.datatypeWithEnum + ">"); - property.dataType = property.datatypeWithEnum; + if (property.datatypeWithEnum.contains("<" + nestedType + ">")) { + property.datatypeWithEnum = property.datatypeWithEnum.replace("<" + nestedType + ">", "<" + property.items.datatypeWithEnum + ">"); + } + } } - if (property.datatypeWithEnum.contains("<" + nestedType + ">") && property.items != null) { - property.datatypeWithEnum = property.datatypeWithEnum.replace("<" + nestedType + ">", "<" + property.items.datatypeWithEnum + ">"); + // Only update dataType if we actually made changes + if (!originalType.equals(property.datatypeWithEnum)) { property.dataType = property.datatypeWithEnum; } - }); - - // HOTFIX: https://github.com/OpenAPITools/openapi-generator/issues/14944 - if (property.datatypeWithEnum.equals("decimal")) { - property.isDecimal = true; } } From c50238b0b17c62a632735fe40a55a166b4e4f736 Mon Sep 17 00:00:00 2001 From: devhl Date: Sat, 26 Jul 2025 16:11:12 -0400 Subject: [PATCH 2/2] improve git diff readability --- .../languages/AbstractCSharpCodegen.java | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java index 19bc7206894f..fbfe2cf39a77 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java @@ -751,33 +751,6 @@ private void patchPropertyVendorExtensions(CodegenProperty property) { protected void patchPropertyIsInherited(CodegenModel model, CodegenProperty property) { } - protected void patchProperty(Map enumRefs, CodegenModel model, CodegenProperty property) { - if (enumRefs.containsKey(property.dataType)) { - // Handle any enum properties referred to by $ref. - // This is different in C# than most other generators, because enums in C# are compiled to integral types, - // while enums in many other languages are true objects. - CodegenModel refModel = enumRefs.get(property.dataType); - property.allowableValues = refModel.allowableValues; - property.isEnum = true; - - // We do these after updateCodegenPropertyEnum to avoid generalities that don't mesh with C#. - property.isPrimitiveType = true; - } - - this.patchPropertyIsInherited(model, property); - - patchPropertyVendorExtensions(property); - - property.name = patchPropertyName(model, property.name, null); - - patchNestedMaps(property); - - // HOTFIX: https://github.com/OpenAPITools/openapi-generator/issues/14944 - if (property.datatypeWithEnum.equals("decimal")) { - property.isDecimal = true; - } - } - private void patchNestedMaps(CodegenProperty property) { // Process nested types before making any replacements to ensure we have the correct inner type if (property.items != null) { @@ -809,6 +782,33 @@ private void patchNestedMaps(CodegenProperty property) { } } + protected void patchProperty(Map enumRefs, CodegenModel model, CodegenProperty property) { + if (enumRefs.containsKey(property.dataType)) { + // Handle any enum properties referred to by $ref. + // This is different in C# than most other generators, because enums in C# are compiled to integral types, + // while enums in many other languages are true objects. + CodegenModel refModel = enumRefs.get(property.dataType); + property.allowableValues = refModel.allowableValues; + property.isEnum = true; + + // We do these after updateCodegenPropertyEnum to avoid generalities that don't mesh with C#. + property.isPrimitiveType = true; + } + + this.patchPropertyIsInherited(model, property); + + patchPropertyVendorExtensions(property); + + property.name = patchPropertyName(model, property.name, null); + + patchNestedMaps(property); + + // HOTFIX: https://github.com/OpenAPITools/openapi-generator/issues/14944 + if (property.datatypeWithEnum.equals("decimal")) { + property.isDecimal = true; + } + } + @Override protected List> buildEnumVars(List values, String dataType) { List> enumVars = super.buildEnumVars(values, dataType);