Schema.fromJson constructor
Implementation
factory Schema.fromJson(Map<String, dynamic> json) {
final rawResponse = json['response'];
bool isMultiResponse = false;
Map<String, dynamic>? singleResponse;
bool isList = false;
Map<String, (Map<String, dynamic>, bool)>? multiResponses;
if (rawResponse is List) {
// Existing array-wrapped single response.
singleResponse = rawResponse.isEmpty
? {}
: Map<String, dynamic>.from(rawResponse.first as Map);
isList = true;
} else if (rawResponse is Map) {
final responseMap = Map<String, dynamic>.from(rawResponse);
// Detect multi-response: ALL top-level values must be Maps (or Lists of Maps).
final allObjects = responseMap.values.every(
(v) =>
v is Map ||
(v is List && v.isNotEmpty && v.first is Map),
);
if (allObjects && responseMap.isNotEmpty) {
isMultiResponse = true;
multiResponses = responseMap.map((key, value) {
if (value is List) {
return MapEntry(key, (Map<String, dynamic>.from(value.first as Map), true));
}
return MapEntry(key, (Map<String, dynamic>.from(value as Map), false));
});
} else {
// Mixed / primitive values → existing single-response.
singleResponse = responseMap;
}
}
return Schema(
api: json['api'] == null
? null
: Api.fromJson(Map<String, dynamic>.from(json['api'] as Map)),
response: singleResponse,
responses: multiResponses,
config: json['config'] == null
? null
: Config.fromJson(Map<String, dynamic>.from(json['config'] as Map)),
isList: isList,
isMultiResponse: isMultiResponse,
);
}