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'; import 'package:json_annotation/json_annotation.dart';
part 'schema.g.dart'; part 'schema.g.dart';
@ -19,6 +20,7 @@ class Schema {
this.required, this.required,
this.items, this.items,
this.additionalProperties, this.additionalProperties,
this.content,
}); });
factory Schema.fromJson(final Map<String, dynamic> json) => _$SchemaFromJson(json); factory Schema.fromJson(final Map<String, dynamic> json) => _$SchemaFromJson(json);
@ -56,4 +58,8 @@ class Schema {
final Schema? items; final Schema? items;
final bool? additionalProperties; 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', 'properties',
'required', 'required',
'items', 'items',
'additionalProperties' 'additionalProperties',
'content'
], ],
); );
return Schema( return Schema(
@ -43,6 +44,9 @@ Schema _$SchemaFromJson(Map<String, dynamic> json) {
required: (json['required'] as List<dynamic>?)?.map((e) => e as String).toList(), 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>), items: json['items'] == null ? null : Schema.fromJson(json['items'] as Map<String, dynamic>),
additionalProperties: json['additionalProperties'] as bool?, 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('required', instance.required);
writeNotNull('items', instance.items?.toJson()); writeNotNull('items', instance.items?.toJson());
writeNotNull('additionalProperties', instance.additionalProperties); writeNotNull('additionalProperties', instance.additionalProperties);
writeNotNull('content', instance.content?.map((k, e) => MapEntry(k, e.toJson())));
return val; return val;
} }

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

@ -1060,9 +1060,23 @@ TypeResult resolveObject(
) )
..body = Code('_\$${identifier}FromJson(json)'), ..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( Method(
(final b) => b (final b) => b
..name = 'toJson' ..name = 'toJson'
@ -1070,21 +1084,35 @@ TypeResult resolveObject(
..lambda = true ..lambda = true
..body = Code('_\$${identifier}ToJson(this)'), ..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([ ..fields.addAll([
for (final propertyName in schema.properties!.keys) ...[ for (final propertyName in schema.properties!.keys) ...[
Field( Field(
(final b) { (final b) {
final propertySchema = schema.properties![propertyName]!;
final result = resolveType( final result = resolveType(
spec, spec,
state, state,
'${identifier}_${_toDartName(propertyName, uppercaseFirstCharacter: true)}', '${identifier}_${_toDartName(propertyName, uppercaseFirstCharacter: true)}',
schema.properties![propertyName]!, propertySchema,
extraJsonSerializableValues: extraJsonSerializableValues, extraJsonSerializableValues: extraJsonSerializableValues,
); );
final propertySchema = schema.properties![propertyName]!;
b b
..name = _toDartName(propertyName) ..name = _toDartName(propertyName)
..type = refer( ..type = refer(
@ -1099,19 +1127,23 @@ TypeResult resolveObject(
'/// ${propertySchema.description!}', '/// ${propertySchema.description!}',
], ],
]); ]);
if (_toDartName(propertyName) != propertyName) { if (_toDartName(propertyName) != propertyName ||
propertySchema.isJsonString ||
extraJsonKeyValues != null) {
b.annotations.add( b.annotations.add(
refer('JsonKey').call( refer('JsonKey').call(
[], [],
{ {
'name': refer("'$propertyName'"), if (_toDartName(propertyName) != propertyName) ...{
if (extraJsonKeyValues != null) ...{ 'name': refer("'$propertyName'"),
for (final p in extraJsonKeyValues.keys) ...{ },
if (p == propertyName) ...{ if (propertySchema.isJsonString) ...{
for (final key in extraJsonKeyValues[p]!.keys) ...{ 'fromJson': refer('${result.name}.fromJsonString'),
key: refer(extraJsonKeyValues[p]![key]!), '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': case 'binary':
result = TypeResultBase('Uint8List'); result = TypeResultBase('Uint8List');
break; 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; break;
case 'array': case 'array':
if (schema.items != null) { 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