Browse Source

Merge pull request #140 from provokateurin/feature/discriminator

OpenAPI discriminator
pull/141/head
Kate 2 years ago committed by GitHub
parent
commit
caf92797bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      packages/dynamite/lib/src/models/discriminator.dart
  2. 35
      packages/dynamite/lib/src/models/discriminator.g.dart
  3. 4
      packages/dynamite/lib/src/models/schema.dart
  4. 6
      packages/dynamite/lib/src/models/schema.g.dart
  5. 55
      packages/dynamite/lib/src/openapi_builder.dart
  6. 2
      packages/dynamite/lib/src/type_result/base.dart
  7. 2
      packages/dynamite/lib/src/type_result/enum.dart
  8. 2
      packages/dynamite/lib/src/type_result/list.dart
  9. 2
      packages/dynamite/lib/src/type_result/map.dart
  10. 2
      packages/dynamite/lib/src/type_result/object.dart
  11. 53
      packages/nextcloud/lib/src/nextcloud.openapi.dart

18
packages/dynamite/lib/src/models/discriminator.dart

@ -0,0 +1,18 @@
import 'package:json_annotation/json_annotation.dart';
part 'discriminator.g.dart';
@JsonSerializable()
class Discriminator {
Discriminator({
required this.propertyName,
this.mapping,
});
factory Discriminator.fromJson(final Map<String, dynamic> json) => _$DiscriminatorFromJson(json);
Map<String, dynamic> toJson() => _$DiscriminatorToJson(this);
final String propertyName;
final Map<String, String>? mapping;
}

35
packages/dynamite/lib/src/models/discriminator.g.dart

@ -0,0 +1,35 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'discriminator.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
Discriminator _$DiscriminatorFromJson(Map<String, dynamic> json) {
$checkKeys(
json,
allowedKeys: const ['propertyName', 'mapping'],
);
return Discriminator(
propertyName: json['propertyName'] as String,
mapping: (json['mapping'] as Map<String, dynamic>?)?.map(
(k, e) => MapEntry(k, e as String),
),
);
}
Map<String, dynamic> _$DiscriminatorToJson(Discriminator instance) {
final val = <String, dynamic>{
'propertyName': instance.propertyName,
};
void writeNotNull(String key, dynamic value) {
if (value != null) {
val[key] = value;
}
}
writeNotNull('mapping', instance.mapping);
return val;
}

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

@ -1,3 +1,4 @@
import 'package:dynamite/src/models/discriminator.dart';
import 'package:json_annotation/json_annotation.dart'; import 'package:json_annotation/json_annotation.dart';
part 'schema.g.dart'; part 'schema.g.dart';
@ -21,6 +22,7 @@ class Schema {
this.additionalProperties, this.additionalProperties,
this.contentMediaType, this.contentMediaType,
this.contentSchema, this.contentSchema,
this.discriminator,
}); });
factory Schema.fromJson(final Map<String, dynamic> json) => _$SchemaFromJson(json); factory Schema.fromJson(final Map<String, dynamic> json) => _$SchemaFromJson(json);
@ -63,5 +65,7 @@ class Schema {
final Schema? contentSchema; final Schema? contentSchema;
final Discriminator? discriminator;
bool get isJsonString => type == 'string' && contentMediaType == 'application/json' && contentSchema != null; bool get isJsonString => type == 'string' && contentMediaType == 'application/json' && contentSchema != null;
} }

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

@ -25,7 +25,8 @@ Schema _$SchemaFromJson(Map<String, dynamic> json) {
'items', 'items',
'additionalProperties', 'additionalProperties',
'contentMediaType', 'contentMediaType',
'contentSchema' 'contentSchema',
'discriminator'
], ],
); );
return Schema( return Schema(
@ -48,6 +49,8 @@ Schema _$SchemaFromJson(Map<String, dynamic> json) {
contentMediaType: json['contentMediaType'] as String?, contentMediaType: json['contentMediaType'] as String?,
contentSchema: contentSchema:
json['contentSchema'] == null ? null : Schema.fromJson(json['contentSchema'] as Map<String, dynamic>), json['contentSchema'] == null ? null : Schema.fromJson(json['contentSchema'] as Map<String, dynamic>),
discriminator:
json['discriminator'] == null ? null : Discriminator.fromJson(json['discriminator'] as Map<String, dynamic>),
); );
} }
@ -76,5 +79,6 @@ Map<String, dynamic> _$SchemaToJson(Schema instance) {
writeNotNull('additionalProperties', instance.additionalProperties); writeNotNull('additionalProperties', instance.additionalProperties);
writeNotNull('contentMediaType', instance.contentMediaType); writeNotNull('contentMediaType', instance.contentMediaType);
writeNotNull('contentSchema', instance.contentSchema?.toJson()); writeNotNull('contentSchema', instance.contentSchema?.toJson());
writeNotNull('discriminator', instance.discriminator?.toJson());
return val; return val;
} }

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

@ -1278,10 +1278,30 @@ TypeResult resolveType(
<String>[ <String>[
for (final result in results) ...[ for (final result in results) ...[
'${result.name}? ${fields[result.name]!};', '${result.name}? ${fields[result.name]!};',
],
for (final result in results) ...[
if (schema.discriminator != null) ...[
"if (data['${schema.discriminator!.propertyName}'] == '${result.name}'",
if (schema.discriminator!.mapping != null &&
schema.discriminator!.mapping!.isNotEmpty) ...[
for (final key in schema.discriminator!.mapping!.entries
.where((final entry) => entry.value.endsWith('/${result.name}'))
.map((final entry) => entry.key)) ...[
" || data['${schema.discriminator!.propertyName}'] == '$key'",
],
],
') {',
],
'try {', 'try {',
'${fields[result.name]!} = ${result.deserialize('data')};', '${fields[result.name]!} = ${result.deserialize('data')};',
'} catch (_) {', '} catch (_) {',
if (schema.discriminator != null) ...[
'rethrow;',
],
'}', '}',
if (schema.discriminator != null) ...[
'}',
],
], ],
if (schema.oneOf != null) ...[ if (schema.oneOf != null) ...[
"assert([${fields.values.join(',')}].where((final x) => x != null).length == 1, 'Need oneOf for \$data');", "assert([${fields.values.join(',')}].where((final x) => x != null).length == 1, 'Need oneOf for \$data');",
@ -1299,8 +1319,24 @@ TypeResult resolveType(
); );
}, },
), ),
Constructor(
(final b) {
b
..factory = true
..lambda = true
..name = 'fromJsonString'
..requiredParameters.add(
Parameter(
(final b) => b
..name = 'data'
..type = refer('String'),
),
)
..body = Code('$identifier.fromJson(json.decode(data))');
},
),
]) ])
..methods.add( ..methods.addAll([
Method( Method(
(final b) => b (final b) => b
..name = 'toJson' ..name = 'toJson'
@ -1308,7 +1344,22 @@ TypeResult resolveType(
..lambda = true ..lambda = true
..body = const Code('_data'), ..body = const Code('_data'),
), ),
); Method(
(final b) => b
..name = 'toJsonString'
..returns = refer('String')
..lambda = true
..static = true
..requiredParameters.add(
Parameter(
(final b) => b
..name = 'data'
..type = refer('dynamic'),
),
)
..body = const Code('json.encode(data)'),
),
]);
}, },
), ),
); );

2
packages/dynamite/lib/src/type_result/base.dart

@ -1,7 +1,7 @@
part of '../../dynamite.dart'; part of '../../dynamite.dart';
class TypeResultBase extends TypeResult { class TypeResultBase extends TypeResult {
TypeResultBase(super.typeName); TypeResultBase(super.name);
@override @override
String serialize(final String object) => object; String serialize(final String object) => object;

2
packages/dynamite/lib/src/type_result/enum.dart

@ -2,7 +2,7 @@ part of '../../dynamite.dart';
class TypeResultEnum extends TypeResult { class TypeResultEnum extends TypeResult {
TypeResultEnum( TypeResultEnum(
super.typeName, super.name,
this.subType, this.subType,
); );

2
packages/dynamite/lib/src/type_result/list.dart

@ -2,7 +2,7 @@ part of '../../dynamite.dart';
class TypeResultList extends TypeResult { class TypeResultList extends TypeResult {
TypeResultList( TypeResultList(
super.typeName, super.name,
this.subType, this.subType,
); );

2
packages/dynamite/lib/src/type_result/map.dart

@ -2,7 +2,7 @@ part of '../../dynamite.dart';
class TypeResultMap extends TypeResult { class TypeResultMap extends TypeResult {
TypeResultMap( TypeResultMap(
super.typeName, super.name,
this.subType, this.subType,
); );

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

@ -2,7 +2,7 @@ part of '../../dynamite.dart';
class TypeResultObject extends TypeResult { class TypeResultObject extends TypeResult {
TypeResultObject( TypeResultObject(
super.typeName, { super.name, {
this.fromJsonString = false, this.fromJsonString = false,
}); });

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

@ -2814,10 +2814,10 @@ class CoreNavigationApps_Ocs_Data_Order {
factory CoreNavigationApps_Ocs_Data_Order.fromJson(dynamic data) { factory CoreNavigationApps_Ocs_Data_Order.fromJson(dynamic data) {
int? $int; int? $int;
String? string;
try { try {
$int = (data as int); $int = (data as int);
} catch (_) {} } catch (_) {}
String? string;
try { try {
string = (data as String); string = (data as String);
} catch (_) {} } catch (_) {}
@ -2829,6 +2829,9 @@ class CoreNavigationApps_Ocs_Data_Order {
); );
} }
factory CoreNavigationApps_Ocs_Data_Order.fromJsonString(String data) =>
CoreNavigationApps_Ocs_Data_Order.fromJson(json.decode(data));
final dynamic _data; final dynamic _data;
final int? $int; final int? $int;
@ -2838,6 +2841,8 @@ class CoreNavigationApps_Ocs_Data_Order {
// coverage:ignore-start // coverage:ignore-start
dynamic toJson() => _data; dynamic toJson() => _data;
// coverage:ignore-end // coverage:ignore-end
static String toJsonString(dynamic data) => json.encode(data);
} }
@JsonSerializable() @JsonSerializable()
@ -3348,6 +3353,9 @@ class NotificationsNotification_SubjectRichParameters {
); );
} }
factory NotificationsNotification_SubjectRichParameters.fromJsonString(String data) =>
NotificationsNotification_SubjectRichParameters.fromJson(json.decode(data));
final dynamic _data; final dynamic _data;
final Map<String, dynamic>? mapStringDynamic; final Map<String, dynamic>? mapStringDynamic;
@ -3355,6 +3363,8 @@ class NotificationsNotification_SubjectRichParameters {
// coverage:ignore-start // coverage:ignore-start
dynamic toJson() => _data; dynamic toJson() => _data;
// coverage:ignore-end // coverage:ignore-end
static String toJsonString(dynamic data) => json.encode(data);
} }
class NotificationsNotification_MessageRichParameters { class NotificationsNotification_MessageRichParameters {
@ -3374,6 +3384,9 @@ class NotificationsNotification_MessageRichParameters {
); );
} }
factory NotificationsNotification_MessageRichParameters.fromJsonString(String data) =>
NotificationsNotification_MessageRichParameters.fromJson(json.decode(data));
final dynamic _data; final dynamic _data;
final Map<String, dynamic>? mapStringDynamic; final Map<String, dynamic>? mapStringDynamic;
@ -3381,6 +3394,8 @@ class NotificationsNotification_MessageRichParameters {
// coverage:ignore-start // coverage:ignore-start
dynamic toJson() => _data; dynamic toJson() => _data;
// coverage:ignore-end // coverage:ignore-end
static String toJsonString(dynamic data) => json.encode(data);
} }
@JsonSerializable() @JsonSerializable()
@ -3961,10 +3976,10 @@ class UserStatusClearAt_Time {
factory UserStatusClearAt_Time.fromJson(dynamic data) { factory UserStatusClearAt_Time.fromJson(dynamic data) {
UserStatusClearAt_Time0? userStatusClearAtTime0; UserStatusClearAt_Time0? userStatusClearAtTime0;
int? $int;
try { try {
userStatusClearAtTime0 = UserStatusClearAt_Time0.fromValue(data as String); userStatusClearAtTime0 = UserStatusClearAt_Time0.fromValue(data as String);
} catch (_) {} } catch (_) {}
int? $int;
try { try {
$int = (data as int); $int = (data as int);
} catch (_) {} } catch (_) {}
@ -3976,6 +3991,8 @@ class UserStatusClearAt_Time {
); );
} }
factory UserStatusClearAt_Time.fromJsonString(String data) => UserStatusClearAt_Time.fromJson(json.decode(data));
final dynamic _data; final dynamic _data;
final UserStatusClearAt_Time0? userStatusClearAtTime0; final UserStatusClearAt_Time0? userStatusClearAtTime0;
@ -3986,6 +4003,8 @@ class UserStatusClearAt_Time {
// coverage:ignore-start // coverage:ignore-start
dynamic toJson() => _data; dynamic toJson() => _data;
// coverage:ignore-end // coverage:ignore-end
static String toJsonString(dynamic data) => json.encode(data);
} }
@JsonSerializable() @JsonSerializable()
@ -4020,10 +4039,10 @@ class UserStatusPublicUserStatus_ClearAt {
factory UserStatusPublicUserStatus_ClearAt.fromJson(dynamic data) { factory UserStatusPublicUserStatus_ClearAt.fromJson(dynamic data) {
UserStatusClearAt? userStatusClearAt; UserStatusClearAt? userStatusClearAt;
int? $int;
try { try {
userStatusClearAt = UserStatusClearAt.fromJson(data as Map<String, dynamic>); userStatusClearAt = UserStatusClearAt.fromJson(data as Map<String, dynamic>);
} catch (_) {} } catch (_) {}
int? $int;
try { try {
$int = (data as int); $int = (data as int);
} catch (_) {} } catch (_) {}
@ -4034,6 +4053,9 @@ class UserStatusPublicUserStatus_ClearAt {
); );
} }
factory UserStatusPublicUserStatus_ClearAt.fromJsonString(String data) =>
UserStatusPublicUserStatus_ClearAt.fromJson(json.decode(data));
final dynamic _data; final dynamic _data;
final UserStatusClearAt? userStatusClearAt; final UserStatusClearAt? userStatusClearAt;
@ -4044,6 +4066,8 @@ class UserStatusPublicUserStatus_ClearAt {
// coverage:ignore-start // coverage:ignore-start
dynamic toJson() => _data; dynamic toJson() => _data;
// coverage:ignore-end // coverage:ignore-end
static String toJsonString(dynamic data) => json.encode(data);
} }
enum UserStatusType { enum UserStatusType {
@ -4166,6 +4190,9 @@ class UserStatusFindStatus_Ocs_Data {
); );
} }
factory UserStatusFindStatus_Ocs_Data.fromJsonString(String data) =>
UserStatusFindStatus_Ocs_Data.fromJson(json.decode(data));
final dynamic _data; final dynamic _data;
final UserStatusPublicUserStatus? userStatusPublicUserStatus; final UserStatusPublicUserStatus? userStatusPublicUserStatus;
@ -4173,6 +4200,8 @@ class UserStatusFindStatus_Ocs_Data {
// coverage:ignore-start // coverage:ignore-start
dynamic toJson() => _data; dynamic toJson() => _data;
// coverage:ignore-end // coverage:ignore-end
static String toJsonString(dynamic data) => json.encode(data);
} }
@JsonSerializable() @JsonSerializable()
@ -4225,10 +4254,10 @@ class UserStatus_ClearAt {
factory UserStatus_ClearAt.fromJson(dynamic data) { factory UserStatus_ClearAt.fromJson(dynamic data) {
UserStatusClearAt? userStatusClearAt; UserStatusClearAt? userStatusClearAt;
int? $int;
try { try {
userStatusClearAt = UserStatusClearAt.fromJson(data as Map<String, dynamic>); userStatusClearAt = UserStatusClearAt.fromJson(data as Map<String, dynamic>);
} catch (_) {} } catch (_) {}
int? $int;
try { try {
$int = (data as int); $int = (data as int);
} catch (_) {} } catch (_) {}
@ -4239,6 +4268,8 @@ class UserStatus_ClearAt {
); );
} }
factory UserStatus_ClearAt.fromJsonString(String data) => UserStatus_ClearAt.fromJson(json.decode(data));
final dynamic _data; final dynamic _data;
final UserStatusClearAt? userStatusClearAt; final UserStatusClearAt? userStatusClearAt;
@ -4249,6 +4280,8 @@ class UserStatus_ClearAt {
// coverage:ignore-start // coverage:ignore-start
dynamic toJson() => _data; dynamic toJson() => _data;
// coverage:ignore-end // coverage:ignore-end
static String toJsonString(dynamic data) => json.encode(data);
} }
@JsonSerializable() @JsonSerializable()
@ -4308,6 +4341,9 @@ class UserStatusGetUserStatus_Ocs_Data {
); );
} }
factory UserStatusGetUserStatus_Ocs_Data.fromJsonString(String data) =>
UserStatusGetUserStatus_Ocs_Data.fromJson(json.decode(data));
final dynamic _data; final dynamic _data;
final UserStatus? userStatus; final UserStatus? userStatus;
@ -4315,6 +4351,8 @@ class UserStatusGetUserStatus_Ocs_Data {
// coverage:ignore-start // coverage:ignore-start
dynamic toJson() => _data; dynamic toJson() => _data;
// coverage:ignore-end // coverage:ignore-end
static String toJsonString(dynamic data) => json.encode(data);
} }
@JsonSerializable() @JsonSerializable()
@ -4368,10 +4406,10 @@ class UserStatusPredefinedStatus_ClearAt {
factory UserStatusPredefinedStatus_ClearAt.fromJson(dynamic data) { factory UserStatusPredefinedStatus_ClearAt.fromJson(dynamic data) {
UserStatusClearAt? userStatusClearAt; UserStatusClearAt? userStatusClearAt;
int? $int;
try { try {
userStatusClearAt = UserStatusClearAt.fromJson(data as Map<String, dynamic>); userStatusClearAt = UserStatusClearAt.fromJson(data as Map<String, dynamic>);
} catch (_) {} } catch (_) {}
int? $int;
try { try {
$int = (data as int); $int = (data as int);
} catch (_) {} } catch (_) {}
@ -4382,6 +4420,9 @@ class UserStatusPredefinedStatus_ClearAt {
); );
} }
factory UserStatusPredefinedStatus_ClearAt.fromJsonString(String data) =>
UserStatusPredefinedStatus_ClearAt.fromJson(json.decode(data));
final dynamic _data; final dynamic _data;
final UserStatusClearAt? userStatusClearAt; final UserStatusClearAt? userStatusClearAt;
@ -4392,6 +4433,8 @@ class UserStatusPredefinedStatus_ClearAt {
// coverage:ignore-start // coverage:ignore-start
dynamic toJson() => _data; dynamic toJson() => _data;
// coverage:ignore-end // coverage:ignore-end
static String toJsonString(dynamic data) => json.encode(data);
} }
@JsonSerializable() @JsonSerializable()

Loading…
Cancel
Save