From c3a51627c8a9616aaec9e86a72a72fc48c4188e6 Mon Sep 17 00:00:00 2001 From: Moshe Dicker Date: Tue, 12 Mar 2024 14:23:54 -0400 Subject: [PATCH] fix fromJson function types --- lib/src/open_api/callback.dart | 43 +++++------ lib/src/open_api/example.dart | 8 +- lib/src/open_api/index.freezed.dart | 43 +---------- lib/src/open_api/index.g.dart | 82 -------------------- lib/src/open_api/link.dart | 8 +- lib/src/open_api/parameter.dart | 8 +- lib/src/open_api/path_item.dart | 8 +- lib/src/open_api/request_body.dart | 4 +- lib/src/open_api/response.dart | 8 +- lib/src/open_api/schema.dart | 8 +- lib/src/open_api/security.dart | 26 +++---- lib/src/open_api/spec.dart | 113 ++++++++++++++-------------- 12 files changed, 127 insertions(+), 232 deletions(-) diff --git a/lib/src/open_api/callback.dart b/lib/src/open_api/callback.dart index 0d75da7..14929d8 100644 --- a/lib/src/open_api/callback.dart +++ b/lib/src/open_api/callback.dart @@ -26,28 +26,29 @@ class _ApiCallbackMapConverter const _ApiCallbackMapConverter(); @override - Map fromJson(Map json) => - fromJsonWithLogging(json, (Map json) { - Map out = {}; - - for (final key in json.keys) { - final name = key; - final expression = json[key]; - if (expression is! Map) { - continue; - } - if (expression.isEmpty) { - continue; - } - out[key] = ApiCallback( - name: name, - expression: { - expression.keys.first: PathItem.fromJson(expression.values.first), - }, - ); + Map fromJson(Map json) { + return fromJsonWithLogging(json, (Map json) { + Map out = {}; + + for (final key in json.keys) { + final name = key; + final expression = json[key]; + if (expression is! Map) { + continue; } - return out; - }); + if (expression.isEmpty) { + continue; + } + out[key] = ApiCallback( + name: name, + expression: { + expression.keys.first: PathItem.fromJson(expression.values.first), + }, + ); + } + return out; + }); + } @override Map toJson(Map data) { diff --git a/lib/src/open_api/example.dart b/lib/src/open_api/example.dart index e02fe4e..03ba3e5 100644 --- a/lib/src/open_api/example.dart +++ b/lib/src/open_api/example.dart @@ -74,7 +74,9 @@ class _ExampleRefConverter implements JsonConverter { } @override - String? fromJson(String? ref) => fromJsonWithLogging(ref, (ref) { - return ref == null ? ref : ref.split('/').last; - }); + String? fromJson(String? ref) { + return fromJsonWithLogging(ref, (ref) { + return ref == null ? ref : ref.split('/').last; + }); + } } diff --git a/lib/src/open_api/index.freezed.dart b/lib/src/open_api/index.freezed.dart index cd725c3..bb118a2 100644 --- a/lib/src/open_api/index.freezed.dart +++ b/lib/src/open_api/index.freezed.dart @@ -11297,10 +11297,6 @@ abstract class _SchemaMap extends Schema { throw _privateConstructorUsedError; } -Security _$SecurityFromJson(Map json) { - return _Security.fromJson(json); -} - /// @nodoc mixin _$Security { /// Each name must correspond to a security scheme which is declared @@ -11326,7 +11322,7 @@ mixin _$Security { required TResult orElse(), }) => throw _privateConstructorUsedError; - Map toJson() => throw _privateConstructorUsedError; + @JsonKey(ignore: true) $SecurityCopyWith get copyWith => throw _privateConstructorUsedError; @@ -11408,15 +11404,12 @@ class __$$SecurityImplCopyWithImpl<$Res> } /// @nodoc -@JsonSerializable() + class _$SecurityImpl extends _Security { const _$SecurityImpl({this.name, final List scopes = const []}) : _scopes = scopes, super._(); - factory _$SecurityImpl.fromJson(Map json) => - _$$SecurityImplFromJson(json); - /// Each name must correspond to a security scheme which is declared /// in the [Components.securitySchemes] list @override @@ -11448,7 +11441,6 @@ class _$SecurityImpl extends _Security { const DeepCollectionEquality().equals(other._scopes, _scopes)); } - @JsonKey(ignore: true) @override int get hashCode => Object.hash( runtimeType, name, const DeepCollectionEquality().hash(_scopes)); @@ -11486,13 +11478,6 @@ class _$SecurityImpl extends _Security { } return orElse(); } - - @override - Map toJson() { - return _$$SecurityImplToJson( - this, - ); - } } abstract class _Security extends Security { @@ -11500,9 +11485,6 @@ abstract class _Security extends Security { _$SecurityImpl; const _Security._() : super._(); - factory _Security.fromJson(Map json) = - _$SecurityImpl.fromJson; - @override /// Each name must correspond to a security scheme which is declared @@ -12971,10 +12953,6 @@ abstract class _ServerVariable implements ServerVariable { throw _privateConstructorUsedError; } -OpenApi _$OpenApiFromJson(Map json) { - return _OpenApi.fromJson(json); -} - /// @nodoc mixin _$OpenApi { /// This string must be the version number of the @@ -13048,7 +13026,7 @@ mixin _$OpenApi { required TResult orElse(), }) => throw _privateConstructorUsedError; - Map toJson() => throw _privateConstructorUsedError; + @JsonKey(ignore: true) $OpenApiCopyWith get copyWith => throw _privateConstructorUsedError; } @@ -13283,7 +13261,7 @@ class __$$OpenApiImplCopyWithImpl<$Res> } /// @nodoc -@JsonSerializable() + class _$OpenApiImpl extends _OpenApi { const _$OpenApiImpl( {@JsonKey(name: 'openapi') this.version = '3.0.3', @@ -13305,9 +13283,6 @@ class _$OpenApiImpl extends _OpenApi { _extraSchemaMapping = extraSchemaMapping, super._(); - factory _$OpenApiImpl.fromJson(Map json) => - _$$OpenApiImplFromJson(json); - /// This string must be the version number of the /// OpenAPI Specification that the OpenAPI document uses. /// This is not related to the API [Info.version] string. @@ -13468,7 +13443,6 @@ class _$OpenApiImpl extends _OpenApi { .equals(other._extraSchemaMapping, _extraSchemaMapping)); } - @JsonKey(ignore: true) @override int get hashCode => Object.hash( runtimeType, @@ -13517,13 +13491,6 @@ class _$OpenApiImpl extends _OpenApi { } return orElse(); } - - @override - Map toJson() { - return _$$OpenApiImplToJson( - this, - ); - } } abstract class _OpenApi extends OpenApi { @@ -13541,8 +13508,6 @@ abstract class _OpenApi extends OpenApi { final Map> extraSchemaMapping}) = _$OpenApiImpl; const _OpenApi._() : super._(); - factory _OpenApi.fromJson(Map json) = _$OpenApiImpl.fromJson; - @override /// This string must be the version number of the diff --git a/lib/src/open_api/index.g.dart b/lib/src/open_api/index.g.dart index e09db84..67b9e5f 100644 --- a/lib/src/open_api/index.g.dart +++ b/lib/src/open_api/index.g.dart @@ -1275,29 +1275,6 @@ Map _$$SchemaMapImplToJson(_$SchemaMapImpl instance) { return val; } -_$SecurityImpl _$$SecurityImplFromJson(Map json) => - _$SecurityImpl( - name: json['name'] as String?, - scopes: (json['scopes'] as List?) - ?.map((e) => e as String) - .toList() ?? - const [], - ); - -Map _$$SecurityImplToJson(_$SecurityImpl instance) { - final val = {}; - - void writeNotNull(String key, dynamic value) { - if (value != null) { - val[key] = value; - } - } - - writeNotNull('name', instance.name); - val['scopes'] = instance.scopes; - return val; -} - _$SecuritySchemeApiKeyImpl _$$SecuritySchemeApiKeyImplFromJson( Map json) => _$SecuritySchemeApiKeyImpl( @@ -1482,65 +1459,6 @@ Map _$$ServerVariableImplToJson( return val; } -_$OpenApiImpl _$$OpenApiImplFromJson(Map json) => - _$OpenApiImpl( - version: json['openapi'] as String? ?? '3.0.3', - info: Info.fromJson(json['info'] as Map), - externalDocs: json['externalDocs'] == null - ? null - : ExternalDocs.fromJson(json['externalDocs'] as Map), - jsonSchemaDialect: json['jsonSchemaDialect'] as String?, - servers: (json['servers'] as List?) - ?.map((e) => Server.fromJson(e as Map)) - .toList(), - tags: (json['tags'] as List?) - ?.map((e) => Tag.fromJson(e as Map)) - .toList(), - paths: (json['paths'] as Map?)?.map( - (k, e) => MapEntry(k, PathItem.fromJson(e as Map)), - ), - webhooks: (json['webhooks'] as Map?)?.map( - (k, e) => MapEntry(k, PathItem.fromJson(e as Map)), - ), - components: json['components'] == null - ? null - : Components.fromJson(json['components'] as Map), - security: (json['security'] as List?) - ?.map((e) => Security.fromJson(e as Map)) - .toList(), - extraSchemaMapping: - (json['extraSchemaMapping'] as Map?)?.map( - (k, e) => MapEntry( - k, (e as List).map((e) => e as String).toList()), - ) ?? - const {}, - ); - -Map _$$OpenApiImplToJson(_$OpenApiImpl instance) { - final val = { - 'openapi': instance.version, - 'info': instance.info.toJson(), - }; - - void writeNotNull(String key, dynamic value) { - if (value != null) { - val[key] = value; - } - } - - writeNotNull('externalDocs', instance.externalDocs?.toJson()); - writeNotNull('jsonSchemaDialect', instance.jsonSchemaDialect); - writeNotNull('servers', instance.servers?.map((e) => e.toJson()).toList()); - writeNotNull('tags', instance.tags?.map((e) => e.toJson()).toList()); - writeNotNull('paths', instance.paths?.map((k, e) => MapEntry(k, e.toJson()))); - writeNotNull( - 'webhooks', instance.webhooks?.map((k, e) => MapEntry(k, e.toJson()))); - writeNotNull('components', instance.components?.toJson()); - writeNotNull('security', instance.security?.map((e) => e.toJson()).toList()); - val['extraSchemaMapping'] = instance.extraSchemaMapping; - return val; -} - _$TagImpl _$$TagImplFromJson(Map json) => _$TagImpl( name: json['name'] as String, description: json['description'] as String?, diff --git a/lib/src/open_api/link.dart b/lib/src/open_api/link.dart index 3af851a..e1e3b7a 100644 --- a/lib/src/open_api/link.dart +++ b/lib/src/open_api/link.dart @@ -38,7 +38,9 @@ class _LinkRefConverter implements JsonConverter { } @override - String? fromJson(String? ref) => fromJsonWithLogging(ref, (ref) { - return ref == null ? ref : ref.split('/').last; - }); + String? fromJson(String? ref) { + return fromJsonWithLogging(ref, (ref) { + return ref == null ? ref : ref.split('/').last; + }); + } } diff --git a/lib/src/open_api/parameter.dart b/lib/src/open_api/parameter.dart index 23a1e92..de3551d 100644 --- a/lib/src/open_api/parameter.dart +++ b/lib/src/open_api/parameter.dart @@ -134,9 +134,11 @@ class _ParamRefConverter implements JsonConverter { } @override - String? fromJson(String? ref) => fromJsonWithLogging(ref, (ref) { - return ref == null ? ref : ref.split('/').last; - }); + String? fromJson(String? ref) { + return fromJsonWithLogging(ref, (ref) { + return ref == null ? ref : ref.split('/').last; + }); + } } /// Ensure that name or ref is provided diff --git a/lib/src/open_api/path_item.dart b/lib/src/open_api/path_item.dart index 443748b..01ec6ec 100644 --- a/lib/src/open_api/path_item.dart +++ b/lib/src/open_api/path_item.dart @@ -101,7 +101,9 @@ class _PathRefConverter implements JsonConverter { } @override - String? fromJson(String? ref) => fromJsonWithLogging(ref, (ref) { - return ref == null ? ref : ref.split('/').last; - }); + String? fromJson(String? ref) { + return fromJsonWithLogging(ref, (ref) { + return ref == null ? ref : ref.split('/').last; + }); + } } diff --git a/lib/src/open_api/request_body.dart b/lib/src/open_api/request_body.dart index bff7952..2aa972a 100644 --- a/lib/src/open_api/request_body.dart +++ b/lib/src/open_api/request_body.dart @@ -72,7 +72,7 @@ class _RequestRefConverter implements JsonConverter { } @override - String? fromJson(String? ref) => fromJsonWithLogging(ref, (ref) { + String? fromJson(String? ref) {return fromJsonWithLogging(ref, (ref) { return ref == null ? ref : ref.split('/').last; - }); + });} } diff --git a/lib/src/open_api/response.dart b/lib/src/open_api/response.dart index 75482aa..2b0b352 100644 --- a/lib/src/open_api/response.dart +++ b/lib/src/open_api/response.dart @@ -81,7 +81,9 @@ class _ResponseRefConverter implements JsonConverter { } @override - String? fromJson(String? ref) => fromJsonWithLogging(ref, (ref) { - return ref == null ? ref : ref.split('/').last; - }); + String? fromJson(String? ref) { + return fromJsonWithLogging(ref, (ref) { + return ref == null ? ref : ref.split('/').last; + }); + } } diff --git a/lib/src/open_api/schema.dart b/lib/src/open_api/schema.dart index e74261b..90c403d 100644 --- a/lib/src/open_api/schema.dart +++ b/lib/src/open_api/schema.dart @@ -413,9 +413,11 @@ class _SchemaRefConverter implements JsonConverter { } @override - String? fromJson(String? ref) => fromJsonWithLogging(ref, (ref) { - return ref == null ? ref : ref.split('/').last; - }); + String? fromJson(String? ref) { + return fromJsonWithLogging(ref, (ref) { + return ref == null ? ref : ref.split('/').last; + }); + } } // ========================================== diff --git a/lib/src/open_api/security.dart b/lib/src/open_api/security.dart index 6f668e7..773e0f8 100644 --- a/lib/src/open_api/security.dart +++ b/lib/src/open_api/security.dart @@ -20,20 +20,20 @@ class Security with _$Security { @Default([]) List scopes, }) = _Security; - factory Security.fromJson(Map json) => - fromJsonWithLogging(json, (json) { - if (json.isEmpty) { - return const Security(); - } else { - final name = json.keys.first; - return Security( - name: name, - scopes: List.from(json[name] ?? []), - ); - } - }); + factory Security.fromJson(Map json) { + return fromJsonWithLogging(json, (json) { + if (json.isEmpty) { + return const Security(); + } else { + final name = json.keys.first; + return Security( + name: name, + scopes: List.from(json[name] ?? []), + ); + } + }); + } - @override Map toJson() { if (name == null) { return {}; diff --git a/lib/src/open_api/spec.dart b/lib/src/open_api/spec.dart index 6eadd3f..7ebf166 100644 --- a/lib/src/open_api/spec.dart +++ b/lib/src/open_api/spec.dart @@ -169,73 +169,72 @@ class OpenApi with _$OpenApi { // ------------------------------------------ /// Create an [OpenApi] object from a JSON representation of an OpenAPI - factory OpenApi.fromJson(Map json) => - fromJsonWithLogging(json, (json) { - // Initialize the schemas, will be formatted in place below - Map schemas = json['components']?['schemas'] ?? {}; - final d = _formatSpecFromJson( - json: json, - schemas: schemas, - ); + factory OpenApi.fromJson(Map json) { + return fromJsonWithLogging(json, (json) { + // Initialize the schemas, will be formatted in place below + Map schemas = json['components']?['schemas'] ?? {}; + final d = _formatSpecFromJson( + json: json, + schemas: schemas, + ); - // Search for any extra schemas created by this generator - // Used to improve the generated schema library - schemas = d['components']?['schemas'] ?? {}; - final Map schemaExtra = {}; - final Map> extraSchemaMapping = {}; - for (final s in schemas.keys) { - final (schemaOut, extraOut) = _extraComponentSchemas( - schemaKey: s, - schemaMap: schemas[s], - allSchemaNames: (schemas.keys.toList() + schemaExtra.keys.toList()), - ); - schemas[s] = schemaOut; - if (extraOut.isNotEmpty) { - schemaExtra.addAll(extraOut); - extraSchemaMapping[s] = extraOut.keys.toList(); - } - } - // Add any extra schemas to the spec - schemas.addAll(schemaExtra); - if (schemas.isNotEmpty) { - d['components']?['schemas'] = schemas; + // Search for any extra schemas created by this generator + // Used to improve the generated schema library + schemas = d['components']?['schemas'] ?? {}; + final Map schemaExtra = {}; + final Map> extraSchemaMapping = {}; + for (final s in schemas.keys) { + final (schemaOut, extraOut) = _extraComponentSchemas( + schemaKey: s, + schemaMap: schemas[s], + allSchemaNames: (schemas.keys.toList() + schemaExtra.keys.toList()), + ); + schemas[s] = schemaOut; + if (extraOut.isNotEmpty) { + schemaExtra.addAll(extraOut); + extraSchemaMapping[s] = extraOut.keys.toList(); } + } + // Add any extra schemas to the spec + schemas.addAll(schemaExtra); + if (schemas.isNotEmpty) { + d['components']?['schemas'] = schemas; + } - final out = OpenApi( - version: d.containsKey('openapi') ? d['openapi'] : null, - info: Info.fromJson(d['info']), - jsonSchemaDialect: d['jsonSchemaDialect'], - externalDocs: d.containsKey('externalDocs') - ? ExternalDocs.fromJson(d['externalDocs']) - : null, - servers: (d['servers'] as List?) - ?.map((e) => Server.fromJson(e)) - .toList(), - tags: (d['tags'] as List?) - ?.map((e) => Tag.fromJson(e)) - .toList(), - paths: (d['paths'] as Map?) - ?.map((k, e) => MapEntry(k, PathItem.fromJson(e))), - webhooks: (d['webhooks'] as Map?) - ?.map((k, e) => MapEntry(k, PathItem.fromJson(e))), - components: d.containsKey('components') - ? Components.fromJson(d['components']) - : null, - security: (d['security'] as List?) - ?.map((e) => Security.fromJson(e)) - .toList(), - extraSchemaMapping: extraSchemaMapping, - ); + final out = OpenApi( + version: d.containsKey('openapi') ? d['openapi'] : null, + info: Info.fromJson(d['info']), + jsonSchemaDialect: d['jsonSchemaDialect'], + externalDocs: d.containsKey('externalDocs') + ? ExternalDocs.fromJson(d['externalDocs']) + : null, + servers: (d['servers'] as List?) + ?.map((e) => Server.fromJson(e)) + .toList(), + tags: + (d['tags'] as List?)?.map((e) => Tag.fromJson(e)).toList(), + paths: (d['paths'] as Map?) + ?.map((k, e) => MapEntry(k, PathItem.fromJson(e))), + webhooks: (d['webhooks'] as Map?) + ?.map((k, e) => MapEntry(k, PathItem.fromJson(e))), + components: d.containsKey('components') + ? Components.fromJson(d['components']) + : null, + security: (d['security'] as List?) + ?.map((e) => Security.fromJson(e)) + .toList(), + extraSchemaMapping: extraSchemaMapping, + ); - return out; - }); + return out; + }); + } // ------------------------------------------ // METHOD: toJson // ------------------------------------------ /// Convert the [OpenApi] object to a JSON spec representation - @override Map toJson() { if (paths == null && components == null && webhooks == null) { throw Exception(