gibahjoe / openapi-generator-dart

Openapi Generator for Dart/Flutter
BSD 3-Clause "New" or "Revised" License
112 stars 29 forks source link

Invalid model generated via List in additional properties #137

Open ricardoboss opened 4 months ago

ricardoboss commented 4 months ago

Description of the bug

When using a spec that defines a schema of an object containing string keys and string array values the generator produces invalid Dart code.

Steps to reproduce

To reproduce, create a new flutter project and set up openapi_generator with the following spec:

{
  "openapi": "3.0.1",
  "info": {
    "title": "Test API",
    "version": "1.0"
  },
  "paths": {
    "/Test": {
      "get": {
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/TestDto"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "TestDto": {
        "required": [
          "values"
        ],
        "type": "object",
        "properties": {
          "values": {
            "type": "object",
            "additionalProperties": {
              "type": "array",
              "items": {
                "type": "string"
              }
            }
          }
        },
        "additionalProperties": false
      }
    }
  }
}

Expected behavior

The generated code should be able to compile.

The code should have the following differences:

        return TestDto(
          values: json[r'values'] == null
              ? const {}
-             : mapCastOfType<String, List>(json, r'values'),
+             : mapCastOfType<String, List<String>>(json, r'values') ?? {},
        );

Logs

No response

Screenshots

image

Platform

macOS

Library version

5.0.2

Flutter version

3.16.9

Flutter channel

stable

Additional context

No response