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
90 stars 38 forks source link

Maps are not being converted properly #128

Closed JohnGalt1717 closed 6 months ago

JohnGalt1717 commented 10 months ago

Consider the following which follows the spec here: https://swagger.io/docs/specification/data-models/dictionaries/

      "ValidationProblemDetails": {
        "type": "object",
        "properties": {
          "type": {
            "type": "string",
            "nullable": true
          },
          "title": {
            "type": "string",
            "nullable": true
          },
          "status": {
            "type": "integer",
            "format": "int32",
            "nullable": true
          },
          "detail": {
            "type": "string",
            "nullable": true
          },
          "instance": {
            "type": "string",
            "nullable": true
          },
          "errors": {
            "type": "object",
            "additionalProperties": {
              "type": "array",
              "items": {
                "type": "string"
              }
            }
          }
        },
        "additionalProperties": { }
      }

Results in this:

import 'package:json_annotation/json_annotation.dart';

import 'errors.dart';

part 'validation_problem_details.g.dart';

@JsonSerializable()
class ValidationProblemDetails {
  const ValidationProblemDetails({
    this.type,
    this.title,
    this.status,
    this.detail,
    this.instance,
    this.errors,
  });

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

  final String? type;
  final String? title;
  final int? status;
  final String? detail;
  final String? instance;
  final Map<String, Object?>? errors;

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

This does 2 things incorrectly:

First is it imports a new file that doesn't get created (correctly not created)

import 'errors.dart';

And second it almost gets the definition for the field correct but not quite:

final Map<String, Object?>? errors;

should be:

final Map<String, List<String?>? errors;

Sancene commented 10 months ago

Thanks for the issue!

Imports are fixed now as per 1.11.1

But the nested Map\List definition problem will take major rewrite of UniversalType. We are definitely looking into fixing this issue but it will take some time.

JohnGalt1717 commented 10 months ago

Thanks! I would have done a pull request but I couldn't figure out where it's happening (which makes sense based on what you said)

Sancene commented 6 months ago

As of planned 1.16.0 all collections are now resolved the same way as lists before. On branch feature/wrapping-collections map resolving is now working correctly and generates result as desired in this issue

As of now we need more test cases to cover this refactor and confirming that it doesnt break previous map and list behaviour