Browse Source

dynamite: Fix JSON encoded string fields

pull/138/head
jld3103 2 years ago
parent
commit
36cac347cf
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 13
      packages/dynamite/lib/src/openapi_builder.dart
  2. 21
      packages/dynamite/lib/src/type_result/object.dart

13
packages/dynamite/lib/src/openapi_builder.dart

@ -991,6 +991,7 @@ TypeResult resolveObject(
final Schema schema, { final Schema schema, {
required final Map<String, String>? extraJsonSerializableValues, required final Map<String, String>? extraJsonSerializableValues,
required final Map<String, Map<String, String>>? extraJsonKeyValues, required final Map<String, Map<String, String>>? extraJsonKeyValues,
final bool fromJsonString = false,
}) { }) {
if (!state.resolvedTypes.contains(identifier)) { if (!state.resolvedTypes.contains(identifier)) {
state.resolvedTypes.add(identifier); state.resolvedTypes.add(identifier);
@ -1162,7 +1163,10 @@ TypeResult resolveObject(
), ),
); );
} }
return TypeResultObject(identifier); return TypeResultObject(
identifier,
fromJsonString: fromJsonString,
);
} }
TypeResult resolveType( TypeResult resolveType(
@ -1173,6 +1177,7 @@ TypeResult resolveType(
final Map<String, String>? extraJsonSerializableValues, final Map<String, String>? extraJsonSerializableValues,
final Map<String, Map<String, String>>? extraJsonKeyValues, final Map<String, Map<String, String>>? extraJsonKeyValues,
final bool ignoreEnum = false, final bool ignoreEnum = false,
final bool fromJsonString = false,
}) { }) {
TypeResult? result; TypeResult? result;
if (schema.ref != null) { if (schema.ref != null) {
@ -1183,6 +1188,7 @@ TypeResult resolveType(
name, name,
spec.components!.schemas![name]!, spec.components!.schemas![name]!,
extraJsonSerializableValues: extraJsonSerializableValues, extraJsonSerializableValues: extraJsonSerializableValues,
fromJsonString: fromJsonString,
); );
} else if (schema.ofs != null) { } else if (schema.ofs != null) {
if (!state.resolvedTypes.contains(identifier)) { if (!state.resolvedTypes.contains(identifier)) {
@ -1195,6 +1201,7 @@ TypeResult resolveType(
'$identifier${schema.ofs!.indexOf(s)}', '$identifier${schema.ofs!.indexOf(s)}',
s, s,
extraJsonSerializableValues: extraJsonSerializableValues, extraJsonSerializableValues: extraJsonSerializableValues,
fromJsonString: fromJsonString,
), ),
) )
.toList(); .toList();
@ -1334,6 +1341,7 @@ TypeResult resolveType(
identifier, identifier,
schema.contentSchema!, schema.contentSchema!,
extraJsonSerializableValues: extraJsonSerializableValues, extraJsonSerializableValues: extraJsonSerializableValues,
fromJsonString: true,
); );
break; break;
} }
@ -1350,6 +1358,7 @@ TypeResult resolveType(
identifier, identifier,
schema.items!, schema.items!,
extraJsonSerializableValues: extraJsonSerializableValues, extraJsonSerializableValues: extraJsonSerializableValues,
fromJsonString: fromJsonString,
); );
result = TypeResultList( result = TypeResultList(
'List<${subResult.name}>', 'List<${subResult.name}>',
@ -1382,6 +1391,7 @@ TypeResult resolveType(
schema, schema,
extraJsonSerializableValues: extraJsonSerializableValues, extraJsonSerializableValues: extraJsonSerializableValues,
extraJsonKeyValues: extraJsonKeyValues, extraJsonKeyValues: extraJsonKeyValues,
fromJsonString: fromJsonString,
); );
break; break;
} }
@ -1427,6 +1437,7 @@ TypeResult resolveType(
schema, schema,
ignoreEnum: true, ignoreEnum: true,
extraJsonSerializableValues: extraJsonSerializableValues, extraJsonSerializableValues: extraJsonSerializableValues,
fromJsonString: fromJsonString,
); );
b b
..name = _toDartName(value.toString()) ..name = _toDartName(value.toString())

21
packages/dynamite/lib/src/type_result/object.dart

@ -2,17 +2,30 @@ part of '../../dynamite.dart';
class TypeResultObject extends TypeResult { class TypeResultObject extends TypeResult {
TypeResultObject( TypeResultObject(
super.typeName, super.typeName, {
); this.fromJsonString = false,
});
final bool fromJsonString;
@override @override
String serialize(final String object) => '$object.toJson()'; String serialize(final String object) {
if (fromJsonString) {
return '$object.toJsonString()';
}
return '$object.toJson()';
}
@override @override
String encode(final String object) => 'json.encode($object)'; String encode(final String object) => 'json.encode($object)';
@override @override
String deserialize(final String object) => '$name.fromJson($object as Map<String, dynamic>)'; String deserialize(final String object) {
if (fromJsonString) {
return '$name.fromJsonString($object as String)';
}
return '$name.fromJson($object as Map<String, dynamic>)';
}
@override @override
String decode(final String object) => 'json.decode($object as String)'; String decode(final String object) => 'json.decode($object as String)';

Loading…
Cancel
Save