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
102 stars 47 forks source link

Handling binary #49

Closed JasCodes closed 1 year ago

JasCodes commented 1 year ago

Hey mate @Carapacik

Allot of great work, was checking out the fixes in commit history.

This is last issue pending with my project's openapi. Will be migrating to swagger_parser soon :)

 "CreateUploadDto": {
                "type": "object",
                "properties": {
                    "assets": {
                        "type": "string",
                        "format": "binary"
                    },
                    "type": {
                        "type": "string",
                        "enum": [
                            "RESOURCES",
                            "MESSAGES",
                            "COVER_IMAGE"
                        ]
                    }
                },
                "required": [
                    "assets",
                    "type"
                ]
            },

produces

import 'dart:io';

import 'package:json_annotation/json_annotation.dart';

import 'type.dart';

part 'create_upload_dto.g.dart';

@JsonSerializable()
class CreateUploadDto {
  const CreateUploadDto({
    required this.assets,
    required this.type,
  });

  factory CreateUploadDto.fromJson(Map<String, dynamic> json) => _$CreateUploadDtoFromJson(json);

  final File assets;
  final Type type;

  Map<String, dynamic> toJson() => _$CreateUploadDtoToJson(this);
}

And I can't serialize FIle by default.


  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
  ┃                               ┃
  ┃   Welcome to swagger_parser   ┃
  ┃                               ┃
  ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

Generate...
The generation was completed successfully. You can run the generation using build_runner.
[INFO] Generating build script...
[WARNING] Invalid builder key `freezed:freezed` found in global_options config of build.yaml. This configuration will have no effect.
[WARNING] Invalid builder key `retrofit_generator:retrofit_generator` found in global_options config of build.yaml. This configuration will have no effect.
[INFO] Generating build script completed, took 160ms

[WARNING] Configuring `freezed:freezed` in global options but this is not a known Builder
[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[INFO] Reading cached asset graph completed, took 70ms

[INFO] Checking for updates since last build...
[INFO] Checking for updates since last build completed, took 333ms

[INFO] Running build...
[INFO] 1.2s elapsed, 0/16 actions completed.
[INFO] 2.2s elapsed, 147/163 actions completed.
[INFO] Running build completed, took 2.5s

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 32ms

[SEVERE] json_serializable on lib/shared_models/create_upload_dto.dart (cached):

Could not generate `fromJson` code for `assets`.
To support the type `File` you can:
* Use `JsonConverter`
  https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonConverter-class.html
* Use `JsonKey` fields `fromJson` and `toJson`
  https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/fromJson.html
  https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/toJson.html
package:glu_client_flutter_repo/shared_models/create_upload_dto.dart:18:14
   ╷
18 │   final File assets;
   │              ^^^^^^
   ╵
[SEVERE] Failed after 2.6s
Carapacik commented 1 year ago

I don't even know how to make it sent from such a dto. Try to write some custom functions for file in jsonKey:

  @JsonKey(fromJson: _fileFromJson, toJson: _fileToJson)
  final File assets;
Carapacik commented 1 year ago

@jascodes If you would tell me what type of files are coming or being sent to you, I would help you write a converter.