Skip to content

Commit 64e9b37

Browse files
authored
[csharp] Handle nested maps recursively (#21636)
* handle nested maps recurssively * improve git diff readability
1 parent dfc66e3 commit 64e9b37

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,37 @@ private void patchPropertyVendorExtensions(CodegenProperty property) {
751751
protected void patchPropertyIsInherited(CodegenModel model, CodegenProperty property) {
752752
}
753753

754+
private void patchNestedMaps(CodegenProperty property) {
755+
// Process nested types before making any replacements to ensure we have the correct inner type
756+
if (property.items != null) {
757+
patchNestedMaps(property.items);
758+
}
759+
760+
String[] nestedTypes = {"List", "Collection", "ICollection", "Dictionary"};
761+
762+
if (property.datatypeWithEnum != null) {
763+
String originalType = property.datatypeWithEnum;
764+
765+
for (String nestedType : nestedTypes) {
766+
// fix incorrect data types for maps of maps
767+
if (property.items != null) {
768+
if (property.datatypeWithEnum.contains(", " + nestedType + ">")) {
769+
property.datatypeWithEnum = property.datatypeWithEnum.replace(", " + nestedType + ">", ", " + property.items.datatypeWithEnum + ">");
770+
}
771+
772+
if (property.datatypeWithEnum.contains("<" + nestedType + ">")) {
773+
property.datatypeWithEnum = property.datatypeWithEnum.replace("<" + nestedType + ">", "<" + property.items.datatypeWithEnum + ">");
774+
}
775+
}
776+
}
777+
778+
// Only update dataType if we actually made changes
779+
if (!originalType.equals(property.datatypeWithEnum)) {
780+
property.dataType = property.datatypeWithEnum;
781+
}
782+
}
783+
}
784+
754785
protected void patchProperty(Map<String, CodegenModel> enumRefs, CodegenModel model, CodegenProperty property) {
755786
if (enumRefs.containsKey(property.dataType)) {
756787
// Handle any enum properties referred to by $ref.
@@ -770,20 +801,7 @@ protected void patchProperty(Map<String, CodegenModel> enumRefs, CodegenModel mo
770801

771802
property.name = patchPropertyName(model, property.name, null);
772803

773-
String[] nestedTypes = {"List", "Collection", "ICollection", "Dictionary"};
774-
775-
Arrays.stream(nestedTypes).forEach(nestedType -> {
776-
// fix incorrect data types for maps of maps
777-
if (property.datatypeWithEnum.contains(", " + nestedType + ">") && property.items != null) {
778-
property.datatypeWithEnum = property.datatypeWithEnum.replace(", " + nestedType + ">", ", " + property.items.datatypeWithEnum + ">");
779-
property.dataType = property.datatypeWithEnum;
780-
}
781-
782-
if (property.datatypeWithEnum.contains("<" + nestedType + ">") && property.items != null) {
783-
property.datatypeWithEnum = property.datatypeWithEnum.replace("<" + nestedType + ">", "<" + property.items.datatypeWithEnum + ">");
784-
property.dataType = property.datatypeWithEnum;
785-
}
786-
});
804+
patchNestedMaps(property);
787805

788806
// HOTFIX: https://github.com/OpenAPITools/openapi-generator/issues/14944
789807
if (property.datatypeWithEnum.equals("decimal")) {

0 commit comments

Comments
 (0)