Browse Source

Merge pull request #138 from provokateurin/fix/json-encoded-string-fields

dynamite: Fix JSON encoded string fields
pull/139/head
Kate 2 years ago committed by GitHub
parent
commit
9af53ca9bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  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, {
required final Map<String, String>? extraJsonSerializableValues,
required final Map<String, Map<String, String>>? extraJsonKeyValues,
final bool fromJsonString = false,
}) {
if (!state.resolvedTypes.contains(identifier)) {
state.resolvedTypes.add(identifier);
@ -1162,7 +1163,10 @@ TypeResult resolveObject(
),
);
}
return TypeResultObject(identifier);
return TypeResultObject(
identifier,
fromJsonString: fromJsonString,
);
}
TypeResult resolveType(
@ -1173,6 +1177,7 @@ TypeResult resolveType(
final Map<String, String>? extraJsonSerializableValues,
final Map<String, Map<String, String>>? extraJsonKeyValues,
final bool ignoreEnum = false,
final bool fromJsonString = false,
}) {
TypeResult? result;
if (schema.ref != null) {
@ -1183,6 +1188,7 @@ TypeResult resolveType(
name,
spec.components!.schemas![name]!,
extraJsonSerializableValues: extraJsonSerializableValues,
fromJsonString: fromJsonString,
);
} else if (schema.ofs != null) {
if (!state.resolvedTypes.contains(identifier)) {
@ -1195,6 +1201,7 @@ TypeResult resolveType(
'$identifier${schema.ofs!.indexOf(s)}',
s,
extraJsonSerializableValues: extraJsonSerializableValues,
fromJsonString: fromJsonString,
),
)
.toList();
@ -1334,6 +1341,7 @@ TypeResult resolveType(
identifier,
schema.contentSchema!,
extraJsonSerializableValues: extraJsonSerializableValues,
fromJsonString: true,
);
break;
}
@ -1350,6 +1358,7 @@ TypeResult resolveType(
identifier,
schema.items!,
extraJsonSerializableValues: extraJsonSerializableValues,
fromJsonString: fromJsonString,
);
result = TypeResultList(
'List<${subResult.name}>',
@ -1382,6 +1391,7 @@ TypeResult resolveType(
schema,
extraJsonSerializableValues: extraJsonSerializableValues,
extraJsonKeyValues: extraJsonKeyValues,
fromJsonString: fromJsonString,
);
break;
}
@ -1427,6 +1437,7 @@ TypeResult resolveType(
schema,
ignoreEnum: true,
extraJsonSerializableValues: extraJsonSerializableValues,
fromJsonString: fromJsonString,
);
b
..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 {
TypeResultObject(
super.typeName,
);
super.typeName, {
this.fromJsonString = false,
});
final bool fromJsonString;
@override
String serialize(final String object) => '$object.toJson()';
String serialize(final String object) {
if (fromJsonString) {
return '$object.toJsonString()';
}
return '$object.toJson()';
}
@override
String encode(final String object) => 'json.encode($object)';
@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
String decode(final String object) => 'json.decode($object as String)';

Loading…
Cancel
Save