openapi-generator-dart icon indicating copy to clipboard operation
openapi-generator-dart copied to clipboard

Error in generated code: The getter 'field' isn't defined for the type 'List<MultipartFile>'

Open Kahoulam opened this issue 2 years ago • 2 comments

Generated code error in file

Error message:

The getter 'field' isn't defined for the type 'List<MultipartFile>'.
Try importing the library that defines 'field', correcting the name to the name of an existing getter, or defining a getter or field named 'field'.

Error file Path: /lib/api/uploader_api.dart > UploaderApi > uploaderUploadWithHttpInfo

  Future<Response> uploaderUploadWithHttpInfo(List<MultipartFile> images,) async {
    // ignore: prefer_const_declarations
    final path = r'/upload';

    // ignore: prefer_final_locals
    Object? postBody;

    final queryParams = <QueryParam>[];
    final headerParams = <String, String>{};
    final formParams = <String, String>{};

    const contentTypes = <String>['multipart/form-data'];

    bool hasFields = false;
    final mp = MultipartRequest('POST', Uri.parse(path));
    if (images != null) {
      hasFields = true;
      
      // Error:
      // The getter 'field' isn't defined for the type 'List<MultipartFile>'.
      // Try importing the library that defines 'field', correcting the name to the name of an existing getter, or defining a getter or field named 'field'.
      mp.fields[r'images'] = images.field;
      
      // Error:
      // The argument type 'List<MultipartFile>' can't be assigned to the parameter type 'MultipartFile'
      mp.files.add(images);
    }
    if (hasFields) {
      postBody = mp;
    }

    return apiClient.invokeAPI(
      path,
      'POST',
      queryParams,
      postBody,
      headerParams,
      formParams,
      contentTypes.isEmpty ? null : contentTypes.first,
    );
  }

Annotation

@Openapi(
    alwaysRun: true,
    additionalProperties: AdditionalProperties(pubName: 'my_api'),
    inputSpecFile: 'openapi3.yaml',
    generatorName: Generator.dart,
    outputDirectory: 'api/')
class Example {}

Input spec file

openapi: 3.0.3
info:
    title: API Test
    version: 1.0.0
servers:
    - url: http://localhost:80
paths:
    /upload:
        post:
            tags:
                - Uploader
            summary: Upload Uploader
            operationId: Uploader#Upload
            requestBody:
                required: true
                content:
                    multipart/form-data:
                        schema:
                            $ref: '#/components/schemas/UploadRequestBody'
            responses:
                "200":
                    description: OK
components:
    schemas:
        UploadRequestBody:
            type: object
            properties:
                images:
                    type: array
                    items:
                        type: string
                        format: binary
                    minItems: 1
            required:
                - images
tags:
    - name: Uploader

Kahoulam avatar Feb 03 '23 12:02 Kahoulam

@Kahoulam is there any solution? im facing the same problem

zirg41 avatar Aug 15 '23 07:08 zirg41

Hi, unfortunately, file upload is something that breaks from time to time.

To work around this, I suggest you fix the bad code and then add the file to .openapi-generator to prevent it from being overwritten the next time the geenrator runs.

Let me know if you need further assistance

gibahjoe avatar Aug 17 '23 14:08 gibahjoe