Skip to content

[BUG] [DART] The constructors of generated nested class declarations do not instantiate fields. #4973

@ka-zo

Description

@ka-zo

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue? yes
  • Have you validated the input using an OpenAPI validator (example)? yes
  • What's the version of OpenAPI Generator used? v4.2.2
  • Have you search for related issues/PRs? yes
  • What's the actual output vs expected output? I expect classes in the generated model directory to have a non-empty constructor that also instantiates fields of the class. Some generated classes are embedded into other generated classes as fields. When such a class is instantiated, those fields of this class, that have a type of other generated classes do not get automatically instantiated. The consequence is that, when you want to use such a class, then you need to instantiate not just the class itself but also MANUALLY all its fields that do not get automatically instantiated. It's even worse, when such an object in a field has also fields,that need to be instantiated. I expect all generated classes to have a non-empty constructor, that automatically instantiates all fields that have a type of a generated class.
openapi-generator version

v4.2.2

OpenAPI declaration file content or url

My openapi.json specification:

{
    "openapi": "3.0.2",
    "info": {
        "title": "test",
        "version": "v1"
    },
    "paths": {
        "/api/v1/average/height/": {
            "post": {
                "tags": [
                    "height",
                    "average"
                ],
                "summary": "GetAverageHeight",
                "description": "Getaverageheightatthespecifiedage.",
                "operationId": "get_average_height_api_v1_average_height__post",
                "requestBody": {
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/Person"
                            }
                        }
                    },
                    "required": true
                },
                "responses": {
                    "200": {
                        "description": "SuccessfulResponse",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/PersonHeight"
                                }
                            }
                        }
                    },
                    "422": {
                        "description": "ValidationError",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/HTTPValidationError"
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "AgeValue": {
                "title": "AgeValue",
                "required": [
                    "unit",
                    "value"
                ],
                "type": "object",
                "properties": {
                    "unit": {
                        "title": "Unit",
                        "enum": [
                            "month"
                        ],
                        "type": "string"
                    },
                    "value": {
                        "title": "Value",
                        "maximum": 216.0,
                        "minimum": 0.0,
                        "type": "number",
                        "description": "Ageofperson."
                    }
                }
            },
            "HTTPValidationError": {
                "title": "HTTPValidationError",
                "type": "object",
                "properties": {
                    "detail": {
                        "title": "Detail",
                        "type": "array",
                        "items": {
                            "$ref": "#/components/schemas/ValidationError"
                        }
                    }
                }
            },
            "HeightValue": {
                "title": "HeightValue",
                "required": [
                    "unit",
                    "value"
                ],
                "type": "object",
                "properties": {
                    "unit": {
                        "title": "Unit",
                        "enum": [
                            "cm"
                        ],
                        "type": "string"
                    },
                    "value": {
                        "title": "Value",
                        "maximum": 300.0,
                        "minimum": 10.0,
                        "type": "number",
                        "description": "Valueoftheheight."
                    }
                }
            },
            "Person": {
                "title": "Person",
                "required": [
                    "age"
                ],
                "type": "object",
                "properties": {
                    "age": {
                        "$ref": "#/components/schemas/AgeValue"
                    }
                }
            },
            "PersonHeight": {
                "title": "PersonHeight",
                "required": [
                    "person",
                    "height"
                ],
                "type": "object",
                "properties": {
                    "person": {
                        "$ref": "#/components/schemas/Person"
                    },
                    "height": {
                        "$ref": "#/components/schemas/HeightValue"
                    }
                }
            },
            "ValidationError": {
                "title": "ValidationError",
                "required": [
                    "loc",
                    "msg",
                    "type"
                ],
                "type": "object",
                "properties": {
                    "loc": {
                        "title": "Location",
                        "type": "array",
                        "items": {
                            "type": "string"
                        }
                    },
                    "msg": {
                        "title": "Message",
                        "type": "string"
                    },
                    "type": {
                        "title": "ErrorType",
                        "type": "string"
                    }
                }
            }
        }
    },
    "servers": [
        {
            "url": "http://10.0.2.2:8000",
            "description": "Justatestserver."
        }
    ]
}

My flutterconfig-dart.json file:

{
    "browserClient": false,
    "useEnumExtension": true
}
Command line used for generation
java -jar .\openapi-generator-cli-4.2.2.jar generate -i .\openapi.json -g dart -o .\openapi-test -c flutterconfig-dart.json
Steps to reproduce

Just generate the code using the command line, and then open any model dart files, such as:
\openapi-test\lib\model\person.dart

There you can see that the constructor of the Person class is Person(); So no instantiation of the field age takes place.

So in order to use the Person class you would need to do:

Person person = new Person();
person.age = new AgeValue(); // This would be totally unnecessary, with the fix I suggest below.

Imagine how big this problem is, if you wanted to instantiate a class with deeply nested field of other classes.

Suggest a fix

Extend all generated constructors to instantiate all fields.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions