manager-app/verify_dto.dart
2026-03-13 17:40:55 +01:00

85 lines
2.6 KiB
Dart

import 'dart:convert';
// Mocking the behavior of the DTOs
class TranslationDTO {
String? language;
String? value;
TranslationDTO({this.language, this.value});
Map<String, dynamic> toJson() => {'language': language, 'value': value};
static TranslationDTO fromJson(Map<String, dynamic> json) =>
TranslationDTO(language: json['language'], value: json['value']);
}
class EventAddressDTOGeometry {
String? type;
Object? coordinates;
EventAddressDTOGeometry({this.type, this.coordinates});
Map<String, dynamic> toJson() => {'type': type, 'coordinates': coordinates};
}
class GuidedStepDTO {
List<TranslationDTO>? title;
EventAddressDTOGeometry? geometry;
GuidedStepDTO({this.title, this.geometry});
Map<String, dynamic> toJson() {
final json = <String, dynamic>{};
if (this.title != null) {
json['title'] = this.title!.map((v) => v.toJson()).toList();
}
if (this.geometry != null) {
json['geometry'] = this.geometry; // The reported bug: no .toJson() call
}
return json;
}
static GuidedStepDTO fromJson(dynamic value) {
if (value is Map) {
final json = value.cast<String, dynamic>();
return GuidedStepDTO(
title: json['title'] != null
? (json['title'] as List)
.map((i) => TranslationDTO.fromJson(i))
.toList()
: null,
geometry: json['geometry'] is Map
? EventAddressDTOGeometry(
type: json['geometry']['type'],
coordinates: json['geometry']['coordinates'],
)
: (json['geometry'] is EventAddressDTOGeometry
? json['geometry']
: null),
);
}
return GuidedStepDTO();
}
}
void main() {
var geo = EventAddressDTOGeometry(type: 'Point', coordinates: [1.0, 2.0]);
var step = GuidedStepDTO(
title: [TranslationDTO(language: 'FR', value: 'Test')],
geometry: geo,
);
print('Original geometry: ${step.geometry.runtimeType}');
var jsonMap = step.toJson();
print('Value in JSON map for geometry: ${jsonMap['geometry'].runtimeType}');
// This is what I do in showNewOrUpdateGuidedStep.dart
var clonedStep = GuidedStepDTO.fromJson(jsonMap);
print('Cloned geometry: ${clonedStep.geometry.runtimeType}');
// What happens if we actually JSON encode/decode?
var encoded = jsonEncode(jsonMap);
var decoded = jsonDecode(encoded);
print('Decoded value for geometry: ${decoded['geometry'].runtimeType}');
var clonedStepFromJSON = GuidedStepDTO.fromJson(decoded);
print(
'Cloned from actual JSON geometry: ${clonedStepFromJSON.geometry.runtimeType}');
}