swagger-api/swagger-codegen-generators

Issue: circular reference may cause OOM issue

fantasydog opened this issue · 0 comments

Version: 1.0.38
Scenario: applied by swagger-codegen-3.0.41. If there is a circle dependency exist in the json/yaml, then it could cause OOM.
Details:
What really happens is in DefaultCodegenConfig.fixUpParentAndInterfaces, where there is a todo note in the comment:
// TODO Determine what to do if the parent discriminator name == the grandparent discriminator name

currently there is nothing being done against this condition. However, if it really happens (parent equals parent.parent), the loop will not exit and eventually an OOM would occur due to unlimited expansion of the ArrayList: parent.children.

It is highly recommended to add a check, and at least throw an exception with detailed infomation, in order to avoid OOM and notify the user what is really going wrong.

Full code segment:

        CodegenModel parent = codegenModel.parentModel;
        // if a discriminator exists on the parent, don't add this child to the inheritance hierarchy
        // TODO Determine what to do if the parent discriminator name == the grandparent discriminator name
        while (parent != null) {
            if (parent.children == null) {
                parent.children = new ArrayList<CodegenModel>();
            }
            parent.children.add(codegenModel); // the list keeps growing
            if (parent.discriminator == null) {
                parent = allModels.get(parent.parent); // if parent equals parent.parent, then this is a self-reassignment
            } else {
                parent = null;
            }
        }