Carapacik / swagger_parser

Dart package that takes an OpenApi definition file and generates REST clients based on retrofit and data classes for your project.
https://pub.dev/packages/swagger_parser
MIT License
87 stars 33 forks source link

Optional field is still `required` in generated model #234

Closed zhuscat closed 1 month ago

zhuscat commented 1 month ago

Steps to reproduce

Here is my test swagger file:

{
    "info": {
        "title": "An API",
        "version": "v1"
    },
    "openapi": "3.1.0",
    "components": {
        "schemas": {
            "Foo": {
                "type": "object",
                "properties": {
                    "id": {
                        "type": "string"
                    },
                    "name": {
                        "type": "string"
                    }
                },
                "required": [
                    "id"
                ]
            }
        },
        "parameters": {}
    },
    "paths": {
        "/hello": {
            "get": {
                "responses": {
                    "200": {
                        "description": "Respond a message",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object",
                                    "properties": {
                                        "message": {
                                            "type": "string"
                                        },
                                        "activity": {
                                            "$ref": "#/components/schemas/Foo"
                                        }
                                    },
                                    "required": [
                                        "message",
                                        "activity"
                                    ]
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

The name property of Foo is not required.

Paste the file in Web Interface, and download the zip generated.

image

Expected results

name is not required

Actual results

name is required

Your OpenApi snippet

{
    "info": {
        "title": "An API",
        "version": "v1"
    },
    "openapi": "3.1.0",
    "components": {
        "schemas": {
            "Foo": {
                "type": "object",
                "properties": {
                    "id": {
                        "type": "string"
                    },
                    "name": {
                        "type": "string"
                    }
                },
                "required": [
                    "id"
                ]
            }
        },
        "parameters": {}
    },
    "paths": {
        "/hello": {
            "get": {
                "responses": {
                    "200": {
                        "description": "Respond a message",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object",
                                    "properties": {
                                        "message": {
                                            "type": "string"
                                        },
                                        "activity": {
                                            "$ref": "#/components/schemas/Foo"
                                        }
                                    },
                                    "required": [
                                        "message",
                                        "activity"
                                    ]
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Code sample

No response

Logs

No response

Dart version and used packages versions

I use Web Interface