OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Dart] MultipartRequest array of int not generating correctly

Open
#16,415 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I am doing multipartRequest to dotnet backend.
The error i get is:
ApiException 400: {"errors":{"Interests":["The value '[6, 5]' is not valid."]}}

this is whats generated:

/// Parameters:
  ///
  /// * [String] name (required):
  ///
  /// * [String] introduction (required):
  ///
  /// * [String] description (required):
  ///
  /// * [List<int>] interests (required):
  ///
  /// * [MultipartFile] file:
  Future<Response> apiPageCreatePostWithHttpInfo(String name, String introduction, String description, List<int> interests, { MultipartFile? file, }) async {
    // ignore: prefer_const_declarations
    final path = r'/api/page/create';

    // 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 (name != null) {
      hasFields = true;
      mp.fields[r'Name'] = parameterToString(name);
    }
    if (introduction != null) {
      hasFields = true;
      mp.fields[r'Introduction'] = parameterToString(introduction);
    }
    if (description != null) {
      hasFields = true;
      mp.fields[r'Description'] = parameterToString(description);
    }
    if (interests != null) {
      hasFields = true;
      mp.fields[r'Interests'] = parameterToString(interests); //HERE IS THE PROBLEM
    }
    if (file != null) {
      hasFields = true;
      mp.fields[r'File'] = file.field;
      mp.files.add(file);
    }
    if (hasFields) {
      postBody = mp;
    }

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

6.6.0

OpenAPI declaration file content or url
"/api/page/create": {
      "post": {
        "tags": ["Page"],
        "requestBody": {
          "content": {
            "multipart/form-data": {
              "schema": {
                "required": [
                  "Description",
                  "Interests",
                  "Introduction",
                  "Name"
                ],
                "type": "object",
                "properties": {
                  "Name": {
                    "type": "string"
                  },
                  "Introduction": {
                    "type": "string"
                  },
                  "Description": {
                    "type": "string"
                  },
                  "Interests": {
                    "type": "array",
                    "items": {
                      "type": "integer",
                      "format": "int32"
                    }
                  },
                  "File": {
                    "type": "string",
                    "format": "binary"
                  }
                }
              },
              "encoding": {
                "Name": {
                  "style": "form"
                },
                "Introduction": {
                  "style": "form"
                },
                "Description": {
                  "style": "form"
                },
                "Interests": {
                  "style": "form"
                },
                "File": {
                  "style": "form"
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/PageData"
                }
              }
            }
          },
          "400": {
            "description": "Bad Request",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "application/json": {
                "schema": {
                  "type": "string"
                }
              }
            }
          }
        },
      }
    },
Generation Details

openapi-generator-cli generate -g dart -i swagger.json -o ./lib/api/rest --additional-properties=useEnumExtensions=true,ensureUniqueParams=false

Steps to reproduce
  1. build
  2. try to call it
Suggest a fix

this could probably work?

if (interests != null) {
  hasFields = true;
  final interestsString = interests.map((e) => e.toString()).join(',');
  mp.fields[r'Interests'] = parameterToString(interestsString);
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Regenerate the Dart client from the supplied multipart OpenAPI schema using the listed openapi-generator-cli command, then inspect the generated apiPageCreatePostWithHttpInfo method and its Interests field handling. Reproduce the request against the reported .NET backend and verify that a List such as [6, 5] is encoded in a form the backend accepts.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.