Browse Source

Merge pull request #136 from provokateurin/feature/json-encoded-string-fields

dynamite,nextcloud: Support JSON encoded as string fields
pull/137/head
Kate 2 years ago committed by GitHub
parent
commit
5115415246
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      packages/dynamite/lib/src/models/schema.dart
  2. 7
      packages/dynamite/lib/src/models/schema.g.dart
  3. 80
      packages/dynamite/lib/src/openapi_builder.dart
  4. 548
      packages/nextcloud/lib/src/nextcloud.openapi.dart

6
packages/dynamite/lib/src/models/schema.dart

@ -1,3 +1,4 @@
import 'package:dynamite/src/models/media_type.dart';
import 'package:json_annotation/json_annotation.dart';
part 'schema.g.dart';
@ -19,6 +20,7 @@ class Schema {
this.required,
this.items,
this.additionalProperties,
this.content,
});
factory Schema.fromJson(final Map<String, dynamic> json) => _$SchemaFromJson(json);
@ -56,4 +58,8 @@ class Schema {
final Schema? items;
final bool? additionalProperties;
final Map<String, MediaType>? content;
bool get isJsonString => type == 'string' && content?['application/json']?.schema != null;
}

7
packages/dynamite/lib/src/models/schema.g.dart

@ -23,7 +23,8 @@ Schema _$SchemaFromJson(Map<String, dynamic> json) {
'properties',
'required',
'items',
'additionalProperties'
'additionalProperties',
'content'
],
);
return Schema(
@ -43,6 +44,9 @@ Schema _$SchemaFromJson(Map<String, dynamic> json) {
required: (json['required'] as List<dynamic>?)?.map((e) => e as String).toList(),
items: json['items'] == null ? null : Schema.fromJson(json['items'] as Map<String, dynamic>),
additionalProperties: json['additionalProperties'] as bool?,
content: (json['content'] as Map<String, dynamic>?)?.map(
(k, e) => MapEntry(k, MediaType.fromJson(e as Map<String, dynamic>)),
),
);
}
@ -69,5 +73,6 @@ Map<String, dynamic> _$SchemaToJson(Schema instance) {
writeNotNull('required', instance.required);
writeNotNull('items', instance.items?.toJson());
writeNotNull('additionalProperties', instance.additionalProperties);
writeNotNull('content', instance.content?.map((k, e) => MapEntry(k, e.toJson())));
return val;
}

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

@ -1060,9 +1060,23 @@ TypeResult resolveObject(
)
..body = Code('_\$${identifier}FromJson(json)'),
),
Constructor(
(final b) => b
..factory = true
..name = 'fromJsonString'
..lambda = true
..requiredParameters.add(
Parameter(
(final b) => b
..name = 'data'
..type = refer('String'),
),
)
..body = Code('$identifier.fromJson(json.decode(data) as Map<String, dynamic>)'),
),
],
)
..methods.add(
..methods.addAll([
Method(
(final b) => b
..name = 'toJson'
@ -1070,21 +1084,35 @@ TypeResult resolveObject(
..lambda = true
..body = Code('_\$${identifier}ToJson(this)'),
),
)
Method(
(final b) => b
..name = 'toJsonString'
..returns = refer('String')
..lambda = true
..static = true
..requiredParameters.add(
Parameter(
(final b) => b
..name = 'data'
..type = refer(identifier),
),
)
..body = const Code('json.encode(data.toJson())'),
),
])
..fields.addAll([
for (final propertyName in schema.properties!.keys) ...[
Field(
(final b) {
final propertySchema = schema.properties![propertyName]!;
final result = resolveType(
spec,
state,
'${identifier}_${_toDartName(propertyName, uppercaseFirstCharacter: true)}',
schema.properties![propertyName]!,
propertySchema,
extraJsonSerializableValues: extraJsonSerializableValues,
);
final propertySchema = schema.properties![propertyName]!;
b
..name = _toDartName(propertyName)
..type = refer(
@ -1099,19 +1127,23 @@ TypeResult resolveObject(
'/// ${propertySchema.description!}',
],
]);
if (_toDartName(propertyName) != propertyName) {
if (_toDartName(propertyName) != propertyName ||
propertySchema.isJsonString ||
extraJsonKeyValues != null) {
b.annotations.add(
refer('JsonKey').call(
[],
{
'name': refer("'$propertyName'"),
if (extraJsonKeyValues != null) ...{
for (final p in extraJsonKeyValues.keys) ...{
if (p == propertyName) ...{
for (final key in extraJsonKeyValues[p]!.keys) ...{
key: refer(extraJsonKeyValues[p]![key]!),
},
},
if (_toDartName(propertyName) != propertyName) ...{
'name': refer("'$propertyName'"),
},
if (propertySchema.isJsonString) ...{
'fromJson': refer('${result.name}.fromJsonString'),
'toJson': refer('${result.name}.toJsonString'),
},
if (extraJsonKeyValues != null && extraJsonKeyValues.containsKey(propertyName)) ...{
for (final key in extraJsonKeyValues[propertyName]!.keys) ...{
key: refer(extraJsonKeyValues[propertyName]![key]!),
},
},
},
@ -1289,12 +1321,22 @@ TypeResult resolveType(
case 'binary':
result = TypeResultBase('Uint8List');
break;
case null:
result = TypeResultBase(
'String',
);
break;
}
if (schema.isJsonString) {
result = resolveType(
spec,
state,
identifier,
schema.content!['application/json']!.schema!,
extraJsonSerializableValues: extraJsonSerializableValues,
);
break;
}
result = TypeResultBase(
'String',
);
break;
case 'array':
if (schema.items != null) {

548
packages/nextcloud/lib/src/nextcloud.openapi.dart

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save