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

Parsing Dictionary objects incorrectly #113

Closed RyanRamchandar closed 10 months ago

RyanRamchandar commented 10 months ago

Given the Schema:

{
    "openapi": "3.0.1",
    "info": {
      "title": "Health",
      "version": "1.0"
    },
    "paths": {
      "/api/Health/Subscribe": {
        "get": {
          "tags": [
            "Health"
          ],
          "summary": "Retrieves the status of the services and their dependencies",
          "responses": {
            "200": {
              "description": "Successfully determined the status of the services and their dependencies",
              "content": {
                "text/event-stream": {
                    "schema": {
                        "type": "string"
                    }
                }
              }
            }
          }
        }
      }
    },
    "components": {
      "schemas": {
        "ServiceStatus": {
            "enum": [
              0,
              1,
              2
            ],
            "type": "integer",
            "format": "int32",
            "description": "The status of the service (0 - Stopped, 1 - Issue, 2 - Running)"
          },
          "ServiceStatusChain": {
            "type": "object",
            "description": "The return object for a service status request",
            "properties": {
              "status": {
                "$ref": "#/components/schemas/ServiceStatus"
              },
              "dependencies": {
                "type": "object",
                "additionalProperties": {
                  "$ref": "#/components/schemas/ServiceStatusChain",
                  "description": "The list of service dependencies (other services, servers, databases, etc.)"
                },
                "nullable": true
              }
            },
            "additionalProperties": false
          }
      }
    }
}

It generates like this:

service_status_chain.dart

import 'package:freezed_annotation/freezed_annotation.dart';

import 'dependencies.dart';
import 'service_status.dart';

part 'service_status_chain.freezed.dart';
part 'service_status_chain.g.dart';

/// The return object for a service status request
@Freezed()
class ServiceStatusChain with _$ServiceStatusChain {
  const factory ServiceStatusChain({
    ServiceStatus? status,
    Dependencies? dependencies,
  }) = _ServiceStatusChain;

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

I was expecting it to be Dictionary/Map return type: Map<String, ServiceStatusChain>? dependencies:

import 'package:freezed_annotation/freezed_annotation.dart';

import 'dependencies.dart';
import 'service_status.dart';

part 'service_status_chain.freezed.dart';
part 'service_status_chain.g.dart';

/// The return object for a service status request
@Freezed()
class ServiceStatusChain with _$ServiceStatusChain {
  const factory ServiceStatusChain({
    ServiceStatus? status,
    Map<String, ServiceStatusChain>? dependencies,
  }) = _ServiceStatusChain;

  factory ServiceStatusChain.fromJson(Map<String, Object?> json) => _$ServiceStatusChainFromJson(json);
}
RyanRamchandar commented 10 months ago

Official docs on Dictionaries: https://swagger.io/docs/specification/data-models/dictionaries/

Carapacik commented 10 months ago

Is the key always of type string?

RyanRamchandar commented 10 months ago

@Carapacik yes.