diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellExperimentalClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellExperimentalClientCodegen.java index b2ba94fb27f9..20a61fd4366c 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellExperimentalClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellExperimentalClientCodegen.java @@ -594,6 +594,7 @@ public String toParamName(String name) { public Map postProcessOperationsWithModels(Map objs, List allModels) { Map operations = (Map) objs.get("operations"); HashMap modelMaps = new HashMap(); + HashMap processedModelMaps = new HashMap(); for (Object o : allModels) { HashMap h = (HashMap) o; @@ -606,7 +607,7 @@ public Map postProcessOperationsWithModels(Map o int index = 0; for (CodegenParameter p : op.allParams) { p.vendorExtensions.put("x-powershell-data-type", getPSDataType(p)); - p.vendorExtensions.put("x-powershell-example", constructExampleCode(p, modelMaps)); + p.vendorExtensions.put("x-powershell-example", constructExampleCode(p, modelMaps, processedModelMaps)); p.vendorExtensions.put("x-index", index); index++; } @@ -620,9 +621,10 @@ public Map postProcessOperationsWithModels(Map o } } + processedModelMaps.clear(); for (CodegenOperation operation : operationList) { for (CodegenParameter cp : operation.allParams) { - cp.vendorExtensions.put("x-powershell-example", constructExampleCode(cp, modelMaps)); + cp.vendorExtensions.put("x-powershell-example", constructExampleCode(cp, modelMaps, processedModelMaps)); } } @@ -670,9 +672,9 @@ public String toVarName(String name) { return name; } - private String constructExampleCode(CodegenParameter codegenParameter, HashMap modelMaps) { + private String constructExampleCode(CodegenParameter codegenParameter, HashMap modelMaps, HashMap processedModelMap) { if (codegenParameter.isListContainer) { // array - return "@(" + constructExampleCode(codegenParameter.items, modelMaps) + ")"; + return "@(" + constructExampleCode(codegenParameter.items, modelMaps, processedModelMap) + ")"; } else if (codegenParameter.isMapContainer) { // TODO: map, file type return "\"TODO\""; } else if (languageSpecificPrimitives.contains(codegenParameter.dataType) || @@ -704,7 +706,7 @@ private String constructExampleCode(CodegenParameter codegenParameter, HashMap modelMaps) { + private String constructExampleCode(CodegenProperty codegenProperty, HashMap modelMaps, HashMap processedModelMap) { if (codegenProperty.isListContainer) { // array - return "@(" + constructExampleCode(codegenProperty.items, modelMaps) + ")"; + return "@(" + constructExampleCode(codegenProperty.items, modelMaps, processedModelMap) + ")"; } else if (codegenProperty.isMapContainer) { // map return "\"TODO\""; } else if (languageSpecificPrimitives.contains(codegenProperty.dataType) || // primitive type @@ -746,7 +748,7 @@ private String constructExampleCode(CodegenProperty codegenProperty, HashMap modelMaps) { + private String constructExampleCode(CodegenModel codegenModel, HashMap modelMaps, HashMap processedModelMap) { String example; + + // break infinite recursion. Return, in case a model is already processed in the current context. + String model = codegenModel.name; + if (processedModelMap.containsKey(model)) { + return ""; + } + processedModelMap.put(model, true); + example = "(New-" + codegenModel.name; List propertyExamples = new ArrayList<>(); for (CodegenProperty codegenProperty : codegenModel.vars) { - propertyExamples.add(" -" + codegenProperty.name + " " + constructExampleCode(codegenProperty, modelMaps)); + propertyExamples.add(" -" + codegenProperty.name + " " + constructExampleCode(codegenProperty, modelMaps, processedModelMap)); } example += StringUtils.join(propertyExamples, " "); example += ")";