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

Generation of File type in Dart Request Body #248

Closed ObserverMoment closed 6 days ago

ObserverMoment commented 6 days ago

Steps to reproduce

Hey @StarProxima , @SasLuca , I am having an issue similar to [https://github.com/Carapacik/swagger_parser/issues/154] with generating file upload endpoints. Specifically the below file upload POST endpoint.

Are you able to advise where / why the issue is arising please?

This might be a bug or just a documentation issue I am not sure.

Cheers,

Rich

(Thanks for your hard work on what looks like a great package!)

Expected results

To generate without errors.

Actual results

The build_runner script gives me (after the freezed files are generated)

[SEVERE] json_serializable on lib/protect_api/models/object0.dart:

Could not generate `fromJson` code for `file`.
To support the type `File` you can:

The code in the model in which I see errors is:

// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint, unused_import

import 'dart:io';

import 'package:freezed_annotation/freezed_annotation.dart';

part 'object0.freezed.dart';
part 'object0.g.dart';

@Freezed()
class Object0 with _$Object0 {
  const factory Object0({
    required File file,
  }) = _Object0;

  factory Object0.fromJson(Map<String, Object?> json) => _$Object0FromJson(json);
}

Your OpenApi snippet

"/api/v1/cases/{caseId}/images": {
            "get": {
                "tags": [
                    "Cases"
                ],
                "summary": "Return the case images",
                "operationId": "getCaseImages",
                "parameters": [
                    {
                        "name": "caseId",
                        "in": "path",
                        "description": "The case id",
                        "required": true,
                        "schema": {
                            "type": "integer",
                            "format": "int64"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "*/*": {
                                "schema": {
                                    "type": "array",
                                    "items": {
                                        "$ref": "#/components/schemas/CaseImageList"
                                    }
                                }
                            }
                        }
                    }
                }
            },
            "post": {
                "tags": [
                    "Cases"
                ],
                "summary": "Add a case image",
                "operationId": "uploadCaseImage",
                "parameters": [
                    {
                        "name": "caseId",
                        "in": "path",
                        "description": "The case id",
                        "required": true,
                        "schema": {
                            "type": "integer",
                            "format": "int64"
                        }
                    }
                ],
                "requestBody": {
                    "content": {
                        "application/json": {
                            "schema": {
                                "required": [
                                    "file"
                                ],
                                "type": "object",
                                "properties": {
                                    "file": {
                                        "type": "string",
                                        "format": "binary"
                                    }
                                }
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "OK"
                    }
                }
            }
        },

Code sample

And the code from the generated endpoint client:

/// Add a case image.
  ///
  /// [caseId] - The case id.
  ///
  /// [body] - Name not received and was auto-generated.
  @POST('/api/v1/cases/{caseId}/images')
  Future<void> uploadCaseImage({
    @Path('caseId') required int caseId,
    @Body() Object0? body,
  });

Why would name for the body not be received?

Logs

Logs ```console [Paste your logs here] ```

Dart version and used packages versions

Latest Flutter / Dart +

freezed

freezed: ^2.3.5 json_serializable: ^6.8.0

codegen

build_runner: ^2.4.11 flutter_gen_runner: ^5.6.0

API Client

retrofit_generator: ^8.1.0 swagger_parser: ^1.18.0

ObserverMoment commented 6 days ago

Looks like the issue was incorrectly generated schema here.