Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]Multipart/form-data request is not working, with swagger: 2.0 #726

Open
linbintie opened this issue Feb 23, 2024 · 12 comments
Open

[BUG]Multipart/form-data request is not working, with swagger: 2.0 #726

linbintie opened this issue Feb 23, 2024 · 12 comments
Assignees
Labels
bug Something isn't working Triage needed

Comments

@linbintie
Copy link

linbintie commented Feb 23, 2024

Hi guys!

Library version used:
swagger_dart_code_generator: ^2.15.0

Describe the bug

We are generating our current specification and some of the services are not properly generated. All of the multipart/form-data services are being created as common https://github.com/post calls without any @partfile attributes, leading us on not being able to use the generator for those services. Our spec is attached for your references (using OpenAPI 2.0).

 /project:
    post:
      tags:
        - project
      operationId: addProject
      consumes:
        - multipart/form-data
      parameters:
        - name: session
          in: formData
          type: string
          required: true
        - name: name
          in: formData
          type: string
          required: true
        - name: projId
          in: formData
          type: string
          required: true
        - name: dbSet
          in: formData
          type: string
          required: true
        - name: zone
          in: formData
          type: string
          allowEmptyValue: true
        - name: iconFile
          in: formData
          type: file
      responses:
        '201':
          description: Created
          schema:
            $ref: '#/definitions/project'

gen code:

@override
  Future<Response<Project>> _projectPost({
    required String? session,
    required String? name,
    required String? projId,
    required String? dbSet,
    String? zone,
    List<int>? iconFile,
  }) {
    final Uri $url = Uri.parse('/project');
    final $body = <String, dynamic>{
      'session': session,
      'name': name,
      'projId': projId,
      'dbSet': dbSet,
      'zone': zone,
      'iconFile': iconFile,
    };

    final Request $request = Request(
      'POST',
      $url,
      client.baseUrl,
      body: $body,
    );
    return client.send<Project, Project>($request);
  }

The generated code is wrong
Are we missing something?

@linbintie linbintie added bug Something isn't working Triage needed labels Feb 23, 2024
@akshatshah
Copy link

@linbintie I faced the same issue. I think it's because the isMultipart boolean value is set based on the requestBody here:

I was able to fix it by changing to the OpenAPI 3.0 spec which would have requestBody instead of consumes.

@linbintie
Copy link
Author

sorry,
I can't changing to the OpenAPI 3.0 because the services depends on OpenAPI 2.0. We rely on the same spec file to generate the code. I can't make a unilateral change. Is there any other way to fix it?

@Vovanella95
Copy link
Collaborator

Hi @linbintie , we can create workaround for you. Just let us know, what is expected generated code looks like

@linbintie
Copy link
Author

linbintie commented Feb 27, 2024

Thanks, @Vovanella95
I want is to generate code like this:

@override
  Future<Response<Project>> _projectPost({
    required String? session,
    required String? name,
    required String? projId,
    required String? dbSet,
    String? zone,
    List<int>? iconFile,
  }) {
    final Uri $url = Uri.parse('/project');
    final List<PartValue> $parts = <PartValue>[
      PartValue<String?>(
        'session',
        session,
      ),
      PartValue<String?>(
        'name',
        name,
      ),
      PartValue<String?>(
        'projId',
        projId,
      ),
      PartValue<String?>(
        'dbSet',
        dbSet,
      ),
      PartValue<String?>(
        'zone',
        zone,
      ),
      PartValueFile<List<int>?>(
        'iconFile',
        iconFile,
      ),
    ];
    final Request $request = Request(
      'POST',
      $url,
      client.baseUrl,
      parts: $parts,
      multipart: true,
    );

    return client.send<Project, Project>($request);
  }

it can work very well.

@Vovanella95
Copy link
Collaborator

Hi @linbintie , It's Chopper generated code, We can not generate it. We generate only .swagger.dart files. Can we generate swagger.dart file somehow for you?

@linbintie
Copy link
Author

@Vovanella95
yes, you can.

@Vovanella95
Copy link
Collaborator

@linbintie could you provide expected generated .swagger.dart file?

Sorry I do not have experience with multipart :)

@linbintie
Copy link
Author

linbintie commented Mar 8, 2024

@Vovanella95
it's okay,thank you.

this is .swagger.dart file:

consumes:
  - application/json
info:
  title: test
  version: 0.1.0
produces:
  - application/json
schemes:
  - http
swagger: '2.0'
basePath: /ad_v1
paths:
  /project:
    post:
      tags:
        - project
      operationId: addProject
      consumes:
        - multipart/form-data
      parameters:
        - name: session
          in: formData
          type: string
          required: true
        - name: name
          in: formData
          type: string
          required: true
        - name: projId
          in: formData
          type: string
          required: true
        - name: dbSet
          in: formData
          type: string
          required: true
        - name: zone
          in: formData
          type: string
          allowEmptyValue: true
        - name: iconFile
          in: formData
          type: file
      responses:
        '201':
          description: Created
        default:
          description: error

@Vovanella95
Copy link
Collaborator

Hi @linbintie , you provided swagger file. I asked for expected generated .swagger.dart file)) Sorry for misunderstanding)))

@linbintie
Copy link
Author

linbintie commented Mar 13, 2024

@Vovanella95

like this code?

Future<chopper.Response<Project>> projectPost({
    required String? session,
    required String? name,
    required String? projId,
    required String? dbSet,
    String? zone,
    http.MultipartFile? iconFile,
  }) {
    generatedMapping.putIfAbsent(Project, () => Project.fromJsonFactory);

    final Uri $url = Uri.parse('/project');
    final List<PartValue> $parts = <PartValue>[
      PartValue<String?>(
        'session',
        session,
      ),
      PartValue<String?>(
        'name',
        name,
      ),
      PartValue<String?>(
        'projId',
        projId,
      ),
      PartValue<String?>(
        'dbSet',
        dbSet,
      ),
      PartValue<String?>(
        'zone',
        zone,
      ),
      if (iconFile != null)
        PartValueFile(
          'iconFile',
          iconFile,
        ),
    ];

    final Request $request = Request(
      'POST',
      $url,
      client.baseUrl,
      parts: $parts,
      multipart: true,
    );
    return client.send<Project, Project>($request);
  }

@Vovanella95
Copy link
Collaborator

Hi @linbintie , yes, it's good! Thanks a lot! Will try to handle it asap

@linbintie
Copy link
Author

@Vovanella95
thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Triage needed
Projects
None yet
Development

No branches or pull requests

4 participants