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
94 stars 43 forks source link

Relationship DTO not imported #86

Closed Mezatsong closed 12 months ago

Mezatsong commented 12 months ago

Relationship DTO are not imported, I have to manually do it each time

"components": {
        "schemas": {
            "Channel": {
                "type": "object",
                "properties": {
                    "id": {
                        "type": "integer",
                        "description": "integer",
                        "nullable": false,
                        "example": 1566687616
                    },
                    "name": {
                        "type": "string",
                        "description": "string(255): Whatsapp, Telegram, etc",
                        "nullable": false,
                        "example": "string"
                    },
                    "slug": {
                        "type": "string",
                        "description": "string(255)",
                        "nullable": false,
                        "example": "string"
                    },
                    "description": {
                        "type": "string",
                        "description": "string(255)",
                        "nullable": true,
                        "example": "string"
                    }
                },
                "required": [
                    "id",
                    "name",
                    "slug"
                ]
            },
            "Church": {
                "type": "object",
                "properties": {
                    "id": {
                        "type": "integer",
                        "description": "integer",
                        "nullable": false,
                        "example": 1232951869
                    },
                    "name": {
                        "type": "string",
                        "description": "string(255)",
                        "nullable": false,
                        "example": "string"
                    },
                    "path": {
                        "type": "string",
                        "description": "string(255): parent-id path ex: '-1-2-6-' for a church of id 6, having church parent id = 2, and church-2 belong its self to 1",
                        "nullable": false,
                        "example": "string"
                    },
                    "address": {
                        "type": "string",
                        "description": "string(255): Describe church location/address",
                        "nullable": false,
                        "example": "string"
                    },
                    "reported_by_user_id": {
                        "type": "integer",
                        "format": "int64",
                        "description": "bigint",
                        "nullable": true,
                        "example": 1725148829
                    },
                    "main_channel_id": {
                        "type": "integer",
                        "description": "integer",
                        "nullable": false,
                        "example": 1641752488
                    },
                    "created_at": {
                        "type": "string",
                        "format": "date-time",
                        "description": "datetime",
                        "nullable": true,
                        "example": "string"
                    },
                    "updated_at": {
                        "type": "string",
                        "format": "date-time",
                        "description": "datetime",
                        "nullable": true,
                        "example": "string"
                    },
                    "deleted_at": {
                        "type": "string",
                        "format": "date-time",
                        "description": "datetime",
                        "nullable": true,
                        "example": "string"
                    },
                    "mainChannel": {
                        "type": "object",
                        "$ref": "#/components/schemas/Channel"
                    }
                },
                "required": [
                    "id",
                    "name",
                    "path",
                    "address",
                    "main_channel_id"
                ]
            },
           ....

Here is the generated Church model, see like Channel is used (last attribute) but not imported

 import 'package:json_annotation/json_annotation.dart';

part 'church.g.dart';

@JsonSerializable()
class Church {
  const Church({
    required this.id,
    required this.name,
    required this.path,
    required this.address,
    required this.mainChannelId,
    this.reportedByUserId,
    this.createdAt,
    this.updatedAt,
    this.deletedAt,
    this.mainChannel,
  });

  factory Church.fromJson(Map<String, Object?> json) => _$ChurchFromJson(json);

  /// integer
  final int id;
  /// string(255)
  final String name;
  /// string(255): parent-id path ex: '-1-2-6-' for a church of id 6, having church parent id = 2, and church-2 belong its self to 1
  final String path;
  /// string(255): Describe church location/address
  final String address;
  /// bigint
  @JsonKey(name: 'reported_by_user_id')
  final int? reportedByUserId;
  /// integer
  @JsonKey(name: 'main_channel_id')
  final int mainChannelId;
  /// datetime
  @JsonKey(name: 'created_at')
  final DateTime? createdAt;
  /// datetime
  @JsonKey(name: 'updated_at')
  final DateTime? updatedAt;
  /// datetime
  @JsonKey(name: 'deleted_at')
  final DateTime? deletedAt;
  final Channel? mainChannel;

  Map<String, Object?> toJson() => _$ChurchToJson(this);
}

You can test it with your own openapi json file and you get this error, I got this error each times and for now, I have to import every time myself.

swagger_parser: ^1.5.2

here is my swagger_parser.json

# dart run swagger_parser:generate
# dart run build_runner build -d

swagger_parser:
  schema_path: assets/openapi.json # Required. Sets the OpenApi schema path directory for api definition
  output_directory: lib/api # Required. Sets output directory for generated files (Clients and Dtos)
  language: dart # Optional. Sets the programming language. Current available languages are: dart, kotlin. Default: dart
  root_interface: true # Optional (dart only). Set 'true' to generate interface with all clients instances. Default: true
  squish_clients: false # Optional. Set 'true' to put all clients in one folder. Default: false
  client_postfix: ApiCall # Optional. Set postfix for Client class and file. Default: Client
  freezed: false # Optional (dart only). Set 'true' to generate data classes using freezed package. Default: false
  enums_to_json: true # Optional. Set 'true' to include toJson() in enums. If set to false, serialization will use .name instead. Default: false
  enums_prefix: false # Optional. Set 'true' to set enum prefix from parent component. Default: false
  replacement_rules: # Optional. Set regex replacement rules for the names of the generated classes/enums. All rules are applied in order.
    # Example of rule
    - pattern: "[0-9]+"
      replacement: ""
Carapacik commented 12 months ago

Fixed