diff --git a/example/build.yaml b/example/build.yaml index 3c3a5d82..416a295f 100644 --- a/example/build.yaml +++ b/example/build.yaml @@ -27,7 +27,11 @@ targets: additional_headers: - "Cache-Control" overriden_models: - - "Order" + - file_name: "pet_service_json" + import_url: "../overriden_models.dart" + overriden_models: + - "Pet" + - "Order" separate_models: false enums_case_sensitive: false cut_from_model_names: "ModelRenameV3" diff --git a/example/lib/overriden_models.dart b/example/lib/overriden_models.dart new file mode 100644 index 00000000..79c24edb --- /dev/null +++ b/example/lib/overriden_models.dart @@ -0,0 +1,11 @@ +class Pet { + static Pet fromJsonFactory(Map json) { + throw UnimplementedError(); + } +} + +class Order { + static Order fromJsonFactory(Map json) { + throw UnimplementedError(); + } +} diff --git a/example/lib/swagger_generated_code/client_index.dart b/example/lib/swagger_generated_code/client_index.dart index 8a987c96..00b3fd27 100644 --- a/example/lib/swagger_generated_code/client_index.dart +++ b/example/lib/swagger_generated_code/client_index.dart @@ -1,3 +1,5 @@ export 'pet_service_json.swagger.dart' show PetServiceJson; +export 'pet_service_json.swagger.dart' show PetServiceJson; export 'pet_service_swagger.swagger.dart' show PetServiceSwagger; export 'pet_service_yaml.swagger.dart' show PetServiceYaml; +export 'pet_service_yaml.swagger.dart' show PetServiceYaml; diff --git a/example/lib/swagger_generated_code/overriden_models.dart b/example/lib/swagger_generated_code/overriden_models.dart deleted file mode 100644 index 81736b3d..00000000 --- a/example/lib/swagger_generated_code/overriden_models.dart +++ /dev/null @@ -1,9 +0,0 @@ -class Order { - final String id; - - const Order(this.id); - - static const fromJsonFactory = _orderFromJson; - - static Order _orderFromJson(Map json) => Order(''); -} diff --git a/example/lib/swagger_generated_code/pet_service_json.enums.swagger.dart b/example/lib/swagger_generated_code/pet_service_json.enums.swagger.dart index 6cd0427b..1ea689ca 100644 --- a/example/lib/swagger_generated_code/pet_service_json.enums.swagger.dart +++ b/example/lib/swagger_generated_code/pet_service_json.enums.swagger.dart @@ -1,4 +1,5 @@ import 'package:json_annotation/json_annotation.dart'; +import 'package:collection/collection.dart'; enum OrderStatus { @JsonValue(null) diff --git a/example/lib/swagger_generated_code/pet_service_json.swagger.chopper.dart b/example/lib/swagger_generated_code/pet_service_json.swagger.chopper.dart index 8a471d6b..04a3500d 100644 --- a/example/lib/swagger_generated_code/pet_service_json.swagger.chopper.dart +++ b/example/lib/swagger_generated_code/pet_service_json.swagger.chopper.dart @@ -58,7 +58,7 @@ class _$PetServiceJson extends PetServiceJson { @override Future>> _petFindByStatusGet({ - required String? status, + required List? status, String? cacheControl, }) { final Uri $url = Uri.parse('/pet/findByStatus'); diff --git a/example/lib/swagger_generated_code/pet_service_json.swagger.dart b/example/lib/swagger_generated_code/pet_service_json.swagger.dart index 8105d700..d48dd315 100644 --- a/example/lib/swagger_generated_code/pet_service_json.swagger.dart +++ b/example/lib/swagger_generated_code/pet_service_json.swagger.dart @@ -4,11 +4,13 @@ import 'package:json_annotation/json_annotation.dart'; import 'package:collection/collection.dart'; import 'dart:convert'; -import 'overriden_models.dart'; +import '../overriden_models.dart'; import 'package:chopper/chopper.dart'; import 'client_mapping.dart'; import 'dart:async'; +import 'package:http/http.dart' as http; +import 'package:http/http.dart' show MultipartFile; import 'package:chopper/chopper.dart' as chopper; import 'pet_service_json.enums.swagger.dart' as enums; export 'pet_service_json.enums.swagger.dart'; @@ -24,7 +26,9 @@ part 'pet_service_json.swagger.g.dart'; abstract class PetServiceJson extends ChopperService { static PetServiceJson create({ ChopperClient? client, + http.Client? httpClient, Authenticator? authenticator, + Converter? converter, Uri? baseUrl, Iterable? interceptors, }) { @@ -34,8 +38,9 @@ abstract class PetServiceJson extends ChopperService { final newClient = ChopperClient( services: [_$PetServiceJson()], - converter: $JsonSerializableConverter(), + converter: converter ?? $JsonSerializableConverter(), interceptors: interceptors ?? [], + client: httpClient, authenticator: authenticator, baseUrl: baseUrl ?? Uri.parse('http://petstore.swagger.io/v2')); return _$PetServiceJson(newClient); @@ -45,11 +50,11 @@ abstract class PetServiceJson extends ChopperService { ///@param body Pet object that needs to be added to the store Future petPost({ required Pet? body, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(Pet, () => Pet.fromJsonFactory); - return _petPost(body: body, cacheControl: cacheControl); + return _petPost(body: body, cacheControl: cacheControl?.toString()); } ///Add a new pet to the store @@ -64,11 +69,11 @@ abstract class PetServiceJson extends ChopperService { ///@param body Pet object that needs to be added to the store Future petPut({ required Pet? body, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(Pet, () => Pet.fromJsonFactory); - return _petPut(body: body, cacheControl: cacheControl); + return _petPut(body: body, cacheControl: cacheControl?.toString()); } ///Update an existing pet @@ -82,20 +87,21 @@ abstract class PetServiceJson extends ChopperService { ///Finds Pets by status ///@param status Status values that need to be considered for filter Future>> petFindByStatusGet({ - required enums.PetFindByStatusGetStatus? status, - String? cacheControl, + required List? status, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(Pet, () => Pet.fromJsonFactory); return _petFindByStatusGet( - status: status?.value?.toString(), cacheControl: cacheControl); + status: petFindByStatusGetStatusListToJson(status), + cacheControl: cacheControl?.toString()); } ///Finds Pets by status ///@param status Status values that need to be considered for filter @Get(path: '/pet/findByStatus') Future>> _petFindByStatusGet({ - @Query('status') required String? status, + @Query() required List? status, @Header('Cache-Control') String? cacheControl, }); @@ -103,18 +109,19 @@ abstract class PetServiceJson extends ChopperService { ///@param tags Tags to filter by Future>> petFindByTagsGet({ required List? tags, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(Pet, () => Pet.fromJsonFactory); - return _petFindByTagsGet(tags: tags, cacheControl: cacheControl); + return _petFindByTagsGet( + tags: tags, cacheControl: cacheControl?.toString()); } ///Finds Pets by tags ///@param tags Tags to filter by @Get(path: '/pet/findByTags') Future>> _petFindByTagsGet({ - @Query('tags') required List? tags, + @Query() required List? tags, @Header('Cache-Control') String? cacheControl, }); @@ -122,13 +129,15 @@ abstract class PetServiceJson extends ChopperService { ///@param petId ID of pet to return Future> petPetIdGet({ required int? petId, - String? apiKey, - String? cacheControl, + dynamic apiKey, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(Pet, () => Pet.fromJsonFactory); return _petPetIdGet( - petId: petId, apiKey: apiKey, cacheControl: cacheControl); + petId: petId, + apiKey: apiKey?.toString(), + cacheControl: cacheControl?.toString()); } ///Find pet by ID @@ -148,10 +157,13 @@ abstract class PetServiceJson extends ChopperService { required int? petId, String? name, String? status, - String? cacheControl, + dynamic cacheControl, }) { return _petPetIdPost( - petId: petId, name: name, status: status, cacheControl: cacheControl); + petId: petId, + name: name, + status: status, + cacheControl: cacheControl?.toString()); } ///Updates a pet in the store with form data @@ -175,10 +187,12 @@ abstract class PetServiceJson extends ChopperService { Future petPetIdDelete({ String? apiKey, required int? petId, - String? cacheControl, + dynamic cacheControl, }) { return _petPetIdDelete( - apiKey: apiKey, petId: petId, cacheControl: cacheControl); + apiKey: apiKey?.toString(), + petId: petId, + cacheControl: cacheControl?.toString()); } ///Deletes a pet @@ -199,7 +213,7 @@ abstract class PetServiceJson extends ChopperService { required int? petId, String? additionalMetadata, List? file, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent( ApiResponse, () => ApiResponse.fromJsonFactory); @@ -208,7 +222,7 @@ abstract class PetServiceJson extends ChopperService { petId: petId, additionalMetadata: additionalMetadata, file: file, - cacheControl: cacheControl); + cacheControl: cacheControl?.toString()); } ///uploads an image @@ -228,10 +242,11 @@ abstract class PetServiceJson extends ChopperService { ///Returns pet inventories by status Future> storeInventoryGet({ - String? apiKey, - String? cacheControl, + dynamic apiKey, + dynamic cacheControl, }) { - return _storeInventoryGet(apiKey: apiKey, cacheControl: cacheControl); + return _storeInventoryGet( + apiKey: apiKey?.toString(), cacheControl: cacheControl?.toString()); } ///Returns pet inventories by status @@ -245,11 +260,11 @@ abstract class PetServiceJson extends ChopperService { ///@param body order placed for purchasing the pet Future> storeOrderPost({ required Order? body, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(Order, () => Order.fromJsonFactory); - return _storeOrderPost(body: body, cacheControl: cacheControl); + return _storeOrderPost(body: body, cacheControl: cacheControl?.toString()); } ///Place an order for a pet @@ -264,11 +279,12 @@ abstract class PetServiceJson extends ChopperService { ///@param orderId ID of pet that needs to be fetched Future> storeOrderOrderIdGet({ required int? orderId, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(Order, () => Order.fromJsonFactory); - return _storeOrderOrderIdGet(orderId: orderId, cacheControl: cacheControl); + return _storeOrderOrderIdGet( + orderId: orderId, cacheControl: cacheControl?.toString()); } ///Find purchase order by ID @@ -283,10 +299,10 @@ abstract class PetServiceJson extends ChopperService { ///@param orderId ID of the order that needs to be deleted Future storeOrderOrderIdDelete({ required int? orderId, - String? cacheControl, + dynamic cacheControl, }) { return _storeOrderOrderIdDelete( - orderId: orderId, cacheControl: cacheControl); + orderId: orderId, cacheControl: cacheControl?.toString()); } ///Delete purchase order by ID @@ -301,11 +317,11 @@ abstract class PetServiceJson extends ChopperService { ///@param body Created user object Future userPost({ required User? body, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(User, () => User.fromJsonFactory); - return _userPost(body: body, cacheControl: cacheControl); + return _userPost(body: body, cacheControl: cacheControl?.toString()); } ///Create user @@ -320,9 +336,10 @@ abstract class PetServiceJson extends ChopperService { ///@param body List of user object Future userCreateWithArrayPost({ required List? body, - String? cacheControl, + dynamic cacheControl, }) { - return _userCreateWithArrayPost(body: body, cacheControl: cacheControl); + return _userCreateWithArrayPost( + body: body, cacheControl: cacheControl?.toString()); } ///Creates list of users with given input array @@ -337,9 +354,10 @@ abstract class PetServiceJson extends ChopperService { ///@param body List of user object Future userCreateWithListPost({ required List? body, - String? cacheControl, + dynamic cacheControl, }) { - return _userCreateWithListPost(body: body, cacheControl: cacheControl); + return _userCreateWithListPost( + body: body, cacheControl: cacheControl?.toString()); } ///Creates list of users with given input array @@ -356,10 +374,12 @@ abstract class PetServiceJson extends ChopperService { Future> userLoginGet({ required String? username, required String? password, - String? cacheControl, + dynamic cacheControl, }) { return _userLoginGet( - username: username, password: password, cacheControl: cacheControl); + username: username, + password: password, + cacheControl: cacheControl?.toString()); } ///Logs user into the system @@ -367,14 +387,14 @@ abstract class PetServiceJson extends ChopperService { ///@param password The password for login in clear text @Get(path: '/user/login') Future> _userLoginGet({ - @Query('username') required String? username, - @Query('password') required String? password, + @Query() required String? username, + @Query() required String? password, @Header('Cache-Control') String? cacheControl, }); ///Logs out current logged in user session - Future userLogoutGet({String? cacheControl}) { - return _userLogoutGet(cacheControl: cacheControl); + Future userLogoutGet({dynamic cacheControl}) { + return _userLogoutGet(cacheControl: cacheControl?.toString()); } ///Logs out current logged in user session @@ -386,11 +406,12 @@ abstract class PetServiceJson extends ChopperService { ///@param username The name that needs to be fetched. Use user1 for testing. Future> userUsernameGet({ required String? username, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(User, () => User.fromJsonFactory); - return _userUsernameGet(username: username, cacheControl: cacheControl); + return _userUsernameGet( + username: username, cacheControl: cacheControl?.toString()); } ///Get user by user name @@ -407,12 +428,12 @@ abstract class PetServiceJson extends ChopperService { Future userUsernamePut({ required String? username, required User? body, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(User, () => User.fromJsonFactory); return _userUsernamePut( - username: username, body: body, cacheControl: cacheControl); + username: username, body: body, cacheControl: cacheControl?.toString()); } ///Updated user @@ -429,9 +450,10 @@ abstract class PetServiceJson extends ChopperService { ///@param username The name that needs to be deleted Future userUsernameDelete({ required String? username, - String? cacheControl, + dynamic cacheControl, }) { - return _userUsernameDelete(username: username, cacheControl: cacheControl); + return _userUsernameDelete( + username: username, cacheControl: cacheControl?.toString()); } ///Delete user @@ -445,7 +467,7 @@ abstract class PetServiceJson extends ChopperService { @JsonSerializable(explicitToJson: true) class Category { - Category({ + const Category({ this.id, this.name, }); @@ -453,13 +475,14 @@ class Category { factory Category.fromJson(Map json) => _$CategoryFromJson(json); + static const toJsonFactory = _$CategoryToJson; + Map toJson() => _$CategoryToJson(this); + @JsonKey(name: 'id', includeIfNull: false) - final num? id; + final int? id; @JsonKey(name: 'name', includeIfNull: false, defaultValue: '') final String? name; static const fromJsonFactory = _$CategoryFromJson; - static const toJsonFactory = _$CategoryToJson; - Map toJson() => _$CategoryToJson(this); @override bool operator ==(dynamic other) { @@ -482,11 +505,11 @@ class Category { } extension $CategoryExtension on Category { - Category copyWith({num? id, String? name}) { + Category copyWith({int? id, String? name}) { return Category(id: id ?? this.id, name: name ?? this.name); } - Category copyWithWrapped({Wrapped? id, Wrapped? name}) { + Category copyWithWrapped({Wrapped? id, Wrapped? name}) { return Category( id: (id != null ? id.value : this.id), name: (name != null ? name.value : this.name)); @@ -495,7 +518,7 @@ extension $CategoryExtension on Category { @JsonSerializable(explicitToJson: true) class User { - User({ + const User({ this.id, this.username, this.firstName, @@ -508,8 +531,11 @@ class User { factory User.fromJson(Map json) => _$UserFromJson(json); + static const toJsonFactory = _$UserToJson; + Map toJson() => _$UserToJson(this); + @JsonKey(name: 'id', includeIfNull: false) - final num? id; + final int? id; @JsonKey(name: 'username', includeIfNull: false, defaultValue: '') final String? username; @JsonKey(name: 'firstName', includeIfNull: false, defaultValue: '') @@ -525,8 +551,6 @@ class User { @JsonKey(name: 'userStatus', includeIfNull: false) final int? userStatus; static const fromJsonFactory = _$UserFromJson; - static const toJsonFactory = _$UserToJson; - Map toJson() => _$UserToJson(this); @override bool operator ==(dynamic other) { @@ -573,7 +597,7 @@ class User { extension $UserExtension on User { User copyWith( - {num? id, + {int? id, String? username, String? firstName, String? lastName, @@ -593,7 +617,7 @@ extension $UserExtension on User { } User copyWithWrapped( - {Wrapped? id, + {Wrapped? id, Wrapped? username, Wrapped? firstName, Wrapped? lastName, @@ -615,20 +639,21 @@ extension $UserExtension on User { @JsonSerializable(explicitToJson: true) class Tag { - Tag({ + const Tag({ this.id, this.name, }); factory Tag.fromJson(Map json) => _$TagFromJson(json); + static const toJsonFactory = _$TagToJson; + Map toJson() => _$TagToJson(this); + @JsonKey(name: 'id', includeIfNull: false) - final num? id; + final int? id; @JsonKey(name: 'name', includeIfNull: false, defaultValue: '') final String? name; static const fromJsonFactory = _$TagFromJson; - static const toJsonFactory = _$TagToJson; - Map toJson() => _$TagToJson(this); @override bool operator ==(dynamic other) { @@ -651,122 +676,20 @@ class Tag { } extension $TagExtension on Tag { - Tag copyWith({num? id, String? name}) { + Tag copyWith({int? id, String? name}) { return Tag(id: id ?? this.id, name: name ?? this.name); } - Tag copyWithWrapped({Wrapped? id, Wrapped? name}) { + Tag copyWithWrapped({Wrapped? id, Wrapped? name}) { return Tag( id: (id != null ? id.value : this.id), name: (name != null ? name.value : this.name)); } } -@JsonSerializable(explicitToJson: true) -class Pet { - Pet({ - this.id, - this.category, - this.name, - this.photoUrls, - this.tags, - this.status, - }); - - factory Pet.fromJson(Map json) => _$PetFromJson(json); - - @JsonKey(name: 'id', includeIfNull: false) - final num? id; - @JsonKey(name: 'category', includeIfNull: false) - final Category? category; - @JsonKey(name: 'name', includeIfNull: false, defaultValue: '') - final String? name; - @JsonKey(name: 'photoUrls', includeIfNull: false) - final List? photoUrls; - @JsonKey(name: 'tags', includeIfNull: false) - final List? tags; - @JsonKey( - name: 'status', - includeIfNull: false, - toJson: petStatusToJson, - fromJson: petStatusFromJson, - ) - final enums.PetStatus? status; - static const fromJsonFactory = _$PetFromJson; - static const toJsonFactory = _$PetToJson; - Map toJson() => _$PetToJson(this); - - @override - bool operator ==(dynamic other) { - return identical(this, other) || - (other is Pet && - (identical(other.id, id) || - const DeepCollectionEquality().equals(other.id, id)) && - (identical(other.category, category) || - const DeepCollectionEquality() - .equals(other.category, category)) && - (identical(other.name, name) || - const DeepCollectionEquality().equals(other.name, name)) && - (identical(other.photoUrls, photoUrls) || - const DeepCollectionEquality() - .equals(other.photoUrls, photoUrls)) && - (identical(other.tags, tags) || - const DeepCollectionEquality().equals(other.tags, tags)) && - (identical(other.status, status) || - const DeepCollectionEquality().equals(other.status, status))); - } - - @override - String toString() => jsonEncode(this); - - @override - int get hashCode => - const DeepCollectionEquality().hash(id) ^ - const DeepCollectionEquality().hash(category) ^ - const DeepCollectionEquality().hash(name) ^ - const DeepCollectionEquality().hash(photoUrls) ^ - const DeepCollectionEquality().hash(tags) ^ - const DeepCollectionEquality().hash(status) ^ - runtimeType.hashCode; -} - -extension $PetExtension on Pet { - Pet copyWith( - {num? id, - Category? category, - String? name, - List? photoUrls, - List? tags, - enums.PetStatus? status}) { - return Pet( - id: id ?? this.id, - category: category ?? this.category, - name: name ?? this.name, - photoUrls: photoUrls ?? this.photoUrls, - tags: tags ?? this.tags, - status: status ?? this.status); - } - - Pet copyWithWrapped( - {Wrapped? id, - Wrapped? category, - Wrapped? name, - Wrapped?>? photoUrls, - Wrapped?>? tags, - Wrapped? status}) { - return Pet( - id: (id != null ? id.value : this.id), - category: (category != null ? category.value : this.category), - name: (name != null ? name.value : this.name), - photoUrls: (photoUrls != null ? photoUrls.value : this.photoUrls), - tags: (tags != null ? tags.value : this.tags), - status: (status != null ? status.value : this.status)); - } -} - @JsonSerializable(explicitToJson: true) class ApiResponse { - ApiResponse({ + const ApiResponse({ this.code, this.type, this.message, @@ -775,6 +698,9 @@ class ApiResponse { factory ApiResponse.fromJson(Map json) => _$ApiResponseFromJson(json); + static const toJsonFactory = _$ApiResponseToJson; + Map toJson() => _$ApiResponseToJson(this); + @JsonKey(name: 'code', includeIfNull: false) final int? code; @JsonKey(name: 'type', includeIfNull: false, defaultValue: '') @@ -782,8 +708,6 @@ class ApiResponse { @JsonKey(name: 'message', includeIfNull: false, defaultValue: '') final String? message; static const fromJsonFactory = _$ApiResponseFromJson; - static const toJsonFactory = _$ApiResponseToJson; - Map toJson() => _$ApiResponseToJson(this); @override bool operator ==(dynamic other) { @@ -827,20 +751,41 @@ extension $ApiResponseExtension on ApiResponse { } } -String? orderStatusToJson(enums.OrderStatus? orderStatus) { +String? orderStatusNullableToJson(enums.OrderStatus? orderStatus) { return orderStatus?.value; } +String? orderStatusToJson(enums.OrderStatus orderStatus) { + return orderStatus.value; +} + enums.OrderStatus orderStatusFromJson( Object? orderStatus, [ enums.OrderStatus? defaultValue, ]) { - return enums.OrderStatus.values - .firstWhereOrNull((e) => e.value == orderStatus) ?? + return enums.OrderStatus.values.firstWhereOrNull((e) => + e.value.toString().toLowerCase() == + orderStatus?.toString().toLowerCase()) ?? defaultValue ?? enums.OrderStatus.swaggerGeneratedUnknown; } +enums.OrderStatus? orderStatusNullableFromJson( + Object? orderStatus, [ + enums.OrderStatus? defaultValue, +]) { + if (orderStatus == null) { + return null; + } + return enums.OrderStatus.values + .firstWhereOrNull((e) => e.value == orderStatus) ?? + defaultValue; +} + +String orderStatusExplodedListToJson(List? orderStatus) { + return orderStatus?.map((e) => e.value!).join(',') ?? ''; +} + List orderStatusListToJson(List? orderStatus) { if (orderStatus == null) { return []; @@ -871,19 +816,40 @@ List? orderStatusNullableListFromJson( return orderStatus.map((e) => orderStatusFromJson(e.toString())).toList(); } -String? petStatusToJson(enums.PetStatus? petStatus) { +String? petStatusNullableToJson(enums.PetStatus? petStatus) { return petStatus?.value; } +String? petStatusToJson(enums.PetStatus petStatus) { + return petStatus.value; +} + enums.PetStatus petStatusFromJson( Object? petStatus, [ enums.PetStatus? defaultValue, ]) { - return enums.PetStatus.values.firstWhereOrNull((e) => e.value == petStatus) ?? + return enums.PetStatus.values.firstWhereOrNull((e) => + e.value.toString().toLowerCase() == + petStatus?.toString().toLowerCase()) ?? defaultValue ?? enums.PetStatus.swaggerGeneratedUnknown; } +enums.PetStatus? petStatusNullableFromJson( + Object? petStatus, [ + enums.PetStatus? defaultValue, +]) { + if (petStatus == null) { + return null; + } + return enums.PetStatus.values.firstWhereOrNull((e) => e.value == petStatus) ?? + defaultValue; +} + +String petStatusExplodedListToJson(List? petStatus) { + return petStatus?.map((e) => e.value!).join(',') ?? ''; +} + List petStatusListToJson(List? petStatus) { if (petStatus == null) { return []; @@ -914,21 +880,44 @@ List? petStatusNullableListFromJson( return petStatus.map((e) => petStatusFromJson(e.toString())).toList(); } -String? petFindByStatusGetStatusToJson( +String? petFindByStatusGetStatusNullableToJson( enums.PetFindByStatusGetStatus? petFindByStatusGetStatus) { return petFindByStatusGetStatus?.value; } +String? petFindByStatusGetStatusToJson( + enums.PetFindByStatusGetStatus petFindByStatusGetStatus) { + return petFindByStatusGetStatus.value; +} + enums.PetFindByStatusGetStatus petFindByStatusGetStatusFromJson( Object? petFindByStatusGetStatus, [ enums.PetFindByStatusGetStatus? defaultValue, ]) { - return enums.PetFindByStatusGetStatus.values - .firstWhereOrNull((e) => e.value == petFindByStatusGetStatus) ?? + return enums.PetFindByStatusGetStatus.values.firstWhereOrNull((e) => + e.value.toString().toLowerCase() == + petFindByStatusGetStatus?.toString().toLowerCase()) ?? defaultValue ?? enums.PetFindByStatusGetStatus.swaggerGeneratedUnknown; } +enums.PetFindByStatusGetStatus? petFindByStatusGetStatusNullableFromJson( + Object? petFindByStatusGetStatus, [ + enums.PetFindByStatusGetStatus? defaultValue, +]) { + if (petFindByStatusGetStatus == null) { + return null; + } + return enums.PetFindByStatusGetStatus.values + .firstWhereOrNull((e) => e.value == petFindByStatusGetStatus) ?? + defaultValue; +} + +String petFindByStatusGetStatusExplodedListToJson( + List? petFindByStatusGetStatus) { + return petFindByStatusGetStatus?.map((e) => e.value!).join(',') ?? ''; +} + List petFindByStatusGetStatusListToJson( List? petFindByStatusGetStatus) { if (petFindByStatusGetStatus == null) { @@ -1019,6 +1008,16 @@ class $JsonSerializableConverter extends chopper.JsonConverter { return chopper.Response(response.base, null, error: response.error); } + if (ResultType == String) { + return response.copyWith(); + } + + if (ResultType == DateTime) { + return response.copyWith( + body: DateTime.parse((response.body as String).replaceAll('"', '')) + as ResultType); + } + final jsonRes = await super.convertResponse(response); return jsonRes.copyWith( body: $jsonDecoder.decode(jsonRes.body) as ResultType); diff --git a/example/lib/swagger_generated_code/pet_service_json.swagger.g.dart b/example/lib/swagger_generated_code/pet_service_json.swagger.g.dart index 63efd647..f1bb7df4 100644 --- a/example/lib/swagger_generated_code/pet_service_json.swagger.g.dart +++ b/example/lib/swagger_generated_code/pet_service_json.swagger.g.dart @@ -7,7 +7,7 @@ part of 'pet_service_json.swagger.dart'; // ************************************************************************** Category _$CategoryFromJson(Map json) => Category( - id: json['id'] as num?, + id: json['id'] as int?, name: json['name'] as String? ?? '', ); @@ -26,7 +26,7 @@ Map _$CategoryToJson(Category instance) { } User _$UserFromJson(Map json) => User( - id: json['id'] as num?, + id: json['id'] as int?, username: json['username'] as String? ?? '', firstName: json['firstName'] as String? ?? '', lastName: json['lastName'] as String? ?? '', @@ -57,7 +57,7 @@ Map _$UserToJson(User instance) { } Tag _$TagFromJson(Map json) => Tag( - id: json['id'] as num?, + id: json['id'] as int?, name: json['name'] as String? ?? '', ); @@ -75,39 +75,6 @@ Map _$TagToJson(Tag instance) { return val; } -Pet _$PetFromJson(Map json) => Pet( - id: json['id'] as num?, - category: json['category'] == null - ? null - : Category.fromJson(json['category'] as Map), - name: json['name'] as String? ?? '', - photoUrls: (json['photoUrls'] as List?) - ?.map((e) => e as String) - .toList(), - tags: (json['tags'] as List?) - ?.map((e) => Tag.fromJson(e as Map)) - .toList(), - status: petStatusFromJson(json['status']), - ); - -Map _$PetToJson(Pet instance) { - final val = {}; - - void writeNotNull(String key, dynamic value) { - if (value != null) { - val[key] = value; - } - } - - writeNotNull('id', instance.id); - writeNotNull('category', instance.category?.toJson()); - writeNotNull('name', instance.name); - writeNotNull('photoUrls', instance.photoUrls); - writeNotNull('tags', instance.tags?.map((e) => e.toJson()).toList()); - writeNotNull('status', petStatusToJson(instance.status)); - return val; -} - ApiResponse _$ApiResponseFromJson(Map json) => ApiResponse( code: json['code'] as int?, type: json['type'] as String? ?? '', diff --git a/example/lib/swagger_generated_code/pet_service_swagger.enums.swagger.dart b/example/lib/swagger_generated_code/pet_service_swagger.enums.swagger.dart index 6cd0427b..1ea689ca 100644 --- a/example/lib/swagger_generated_code/pet_service_swagger.enums.swagger.dart +++ b/example/lib/swagger_generated_code/pet_service_swagger.enums.swagger.dart @@ -1,4 +1,5 @@ import 'package:json_annotation/json_annotation.dart'; +import 'package:collection/collection.dart'; enum OrderStatus { @JsonValue(null) diff --git a/example/lib/swagger_generated_code/pet_service_swagger.swagger.chopper.dart b/example/lib/swagger_generated_code/pet_service_swagger.swagger.chopper.dart index 15f92955..15ef88dc 100644 --- a/example/lib/swagger_generated_code/pet_service_swagger.swagger.chopper.dart +++ b/example/lib/swagger_generated_code/pet_service_swagger.swagger.chopper.dart @@ -58,7 +58,7 @@ class _$PetServiceSwagger extends PetServiceSwagger { @override Future>> _petFindByStatusGet({ - required String? status, + required List? status, String? cacheControl, }) { final Uri $url = Uri.parse('/pet/findByStatus'); diff --git a/example/lib/swagger_generated_code/pet_service_swagger.swagger.dart b/example/lib/swagger_generated_code/pet_service_swagger.swagger.dart index 25069bf0..de713a08 100644 --- a/example/lib/swagger_generated_code/pet_service_swagger.swagger.dart +++ b/example/lib/swagger_generated_code/pet_service_swagger.swagger.dart @@ -4,11 +4,12 @@ import 'package:json_annotation/json_annotation.dart'; import 'package:collection/collection.dart'; import 'dart:convert'; -import 'overriden_models.dart'; import 'package:chopper/chopper.dart'; import 'client_mapping.dart'; import 'dart:async'; +import 'package:http/http.dart' as http; +import 'package:http/http.dart' show MultipartFile; import 'package:chopper/chopper.dart' as chopper; import 'pet_service_swagger.enums.swagger.dart' as enums; export 'pet_service_swagger.enums.swagger.dart'; @@ -24,7 +25,9 @@ part 'pet_service_swagger.swagger.g.dart'; abstract class PetServiceSwagger extends ChopperService { static PetServiceSwagger create({ ChopperClient? client, + http.Client? httpClient, Authenticator? authenticator, + Converter? converter, Uri? baseUrl, Iterable? interceptors, }) { @@ -34,8 +37,9 @@ abstract class PetServiceSwagger extends ChopperService { final newClient = ChopperClient( services: [_$PetServiceSwagger()], - converter: $JsonSerializableConverter(), + converter: converter ?? $JsonSerializableConverter(), interceptors: interceptors ?? [], + client: httpClient, authenticator: authenticator, baseUrl: baseUrl ?? Uri.parse('http://petstore.swagger.io/v2')); return _$PetServiceSwagger(newClient); @@ -45,11 +49,11 @@ abstract class PetServiceSwagger extends ChopperService { ///@param body Pet object that needs to be added to the store Future petPost({ required Pet? body, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(Pet, () => Pet.fromJsonFactory); - return _petPost(body: body, cacheControl: cacheControl); + return _petPost(body: body, cacheControl: cacheControl?.toString()); } ///Add a new pet to the store @@ -64,11 +68,11 @@ abstract class PetServiceSwagger extends ChopperService { ///@param body Pet object that needs to be added to the store Future petPut({ required Pet? body, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(Pet, () => Pet.fromJsonFactory); - return _petPut(body: body, cacheControl: cacheControl); + return _petPut(body: body, cacheControl: cacheControl?.toString()); } ///Update an existing pet @@ -82,20 +86,21 @@ abstract class PetServiceSwagger extends ChopperService { ///Finds Pets by status ///@param status Status values that need to be considered for filter Future>> petFindByStatusGet({ - required enums.PetFindByStatusGetStatus? status, - String? cacheControl, + required List? status, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(Pet, () => Pet.fromJsonFactory); return _petFindByStatusGet( - status: status?.value?.toString(), cacheControl: cacheControl); + status: petFindByStatusGetStatusListToJson(status), + cacheControl: cacheControl?.toString()); } ///Finds Pets by status ///@param status Status values that need to be considered for filter @Get(path: '/pet/findByStatus') Future>> _petFindByStatusGet({ - @Query('status') required String? status, + @Query() required List? status, @Header('Cache-Control') String? cacheControl, }); @@ -103,18 +108,19 @@ abstract class PetServiceSwagger extends ChopperService { ///@param tags Tags to filter by Future>> petFindByTagsGet({ required List? tags, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(Pet, () => Pet.fromJsonFactory); - return _petFindByTagsGet(tags: tags, cacheControl: cacheControl); + return _petFindByTagsGet( + tags: tags, cacheControl: cacheControl?.toString()); } ///Finds Pets by tags ///@param tags Tags to filter by @Get(path: '/pet/findByTags') Future>> _petFindByTagsGet({ - @Query('tags') required List? tags, + @Query() required List? tags, @Header('Cache-Control') String? cacheControl, }); @@ -122,13 +128,15 @@ abstract class PetServiceSwagger extends ChopperService { ///@param petId ID of pet to return Future> petPetIdGet({ required int? petId, - String? apiKey, - String? cacheControl, + dynamic apiKey, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(Pet, () => Pet.fromJsonFactory); return _petPetIdGet( - petId: petId, apiKey: apiKey, cacheControl: cacheControl); + petId: petId, + apiKey: apiKey?.toString(), + cacheControl: cacheControl?.toString()); } ///Find pet by ID @@ -148,10 +156,13 @@ abstract class PetServiceSwagger extends ChopperService { required int? petId, String? name, String? status, - String? cacheControl, + dynamic cacheControl, }) { return _petPetIdPost( - petId: petId, name: name, status: status, cacheControl: cacheControl); + petId: petId, + name: name, + status: status, + cacheControl: cacheControl?.toString()); } ///Updates a pet in the store with form data @@ -175,10 +186,12 @@ abstract class PetServiceSwagger extends ChopperService { Future petPetIdDelete({ String? apiKey, required int? petId, - String? cacheControl, + dynamic cacheControl, }) { return _petPetIdDelete( - apiKey: apiKey, petId: petId, cacheControl: cacheControl); + apiKey: apiKey?.toString(), + petId: petId, + cacheControl: cacheControl?.toString()); } ///Deletes a pet @@ -199,7 +212,7 @@ abstract class PetServiceSwagger extends ChopperService { required int? petId, String? additionalMetadata, List? file, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent( ApiResponse, () => ApiResponse.fromJsonFactory); @@ -208,7 +221,7 @@ abstract class PetServiceSwagger extends ChopperService { petId: petId, additionalMetadata: additionalMetadata, file: file, - cacheControl: cacheControl); + cacheControl: cacheControl?.toString()); } ///uploads an image @@ -228,10 +241,11 @@ abstract class PetServiceSwagger extends ChopperService { ///Returns pet inventories by status Future> storeInventoryGet({ - String? apiKey, - String? cacheControl, + dynamic apiKey, + dynamic cacheControl, }) { - return _storeInventoryGet(apiKey: apiKey, cacheControl: cacheControl); + return _storeInventoryGet( + apiKey: apiKey?.toString(), cacheControl: cacheControl?.toString()); } ///Returns pet inventories by status @@ -245,11 +259,11 @@ abstract class PetServiceSwagger extends ChopperService { ///@param body order placed for purchasing the pet Future> storeOrderPost({ required Order? body, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(Order, () => Order.fromJsonFactory); - return _storeOrderPost(body: body, cacheControl: cacheControl); + return _storeOrderPost(body: body, cacheControl: cacheControl?.toString()); } ///Place an order for a pet @@ -264,11 +278,12 @@ abstract class PetServiceSwagger extends ChopperService { ///@param orderId ID of pet that needs to be fetched Future> storeOrderOrderIdGet({ required int? orderId, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(Order, () => Order.fromJsonFactory); - return _storeOrderOrderIdGet(orderId: orderId, cacheControl: cacheControl); + return _storeOrderOrderIdGet( + orderId: orderId, cacheControl: cacheControl?.toString()); } ///Find purchase order by ID @@ -283,10 +298,10 @@ abstract class PetServiceSwagger extends ChopperService { ///@param orderId ID of the order that needs to be deleted Future storeOrderOrderIdDelete({ required int? orderId, - String? cacheControl, + dynamic cacheControl, }) { return _storeOrderOrderIdDelete( - orderId: orderId, cacheControl: cacheControl); + orderId: orderId, cacheControl: cacheControl?.toString()); } ///Delete purchase order by ID @@ -301,11 +316,11 @@ abstract class PetServiceSwagger extends ChopperService { ///@param body Created user object Future userPost({ required User? body, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(User, () => User.fromJsonFactory); - return _userPost(body: body, cacheControl: cacheControl); + return _userPost(body: body, cacheControl: cacheControl?.toString()); } ///Create user @@ -320,9 +335,10 @@ abstract class PetServiceSwagger extends ChopperService { ///@param body List of user object Future userCreateWithArrayPost({ required List? body, - String? cacheControl, + dynamic cacheControl, }) { - return _userCreateWithArrayPost(body: body, cacheControl: cacheControl); + return _userCreateWithArrayPost( + body: body, cacheControl: cacheControl?.toString()); } ///Creates list of users with given input array @@ -337,9 +353,10 @@ abstract class PetServiceSwagger extends ChopperService { ///@param body List of user object Future userCreateWithListPost({ required List? body, - String? cacheControl, + dynamic cacheControl, }) { - return _userCreateWithListPost(body: body, cacheControl: cacheControl); + return _userCreateWithListPost( + body: body, cacheControl: cacheControl?.toString()); } ///Creates list of users with given input array @@ -356,10 +373,12 @@ abstract class PetServiceSwagger extends ChopperService { Future> userLoginGet({ required String? username, required String? password, - String? cacheControl, + dynamic cacheControl, }) { return _userLoginGet( - username: username, password: password, cacheControl: cacheControl); + username: username, + password: password, + cacheControl: cacheControl?.toString()); } ///Logs user into the system @@ -367,14 +386,14 @@ abstract class PetServiceSwagger extends ChopperService { ///@param password The password for login in clear text @Get(path: '/user/login') Future> _userLoginGet({ - @Query('username') required String? username, - @Query('password') required String? password, + @Query() required String? username, + @Query() required String? password, @Header('Cache-Control') String? cacheControl, }); ///Logs out current logged in user session - Future userLogoutGet({String? cacheControl}) { - return _userLogoutGet(cacheControl: cacheControl); + Future userLogoutGet({dynamic cacheControl}) { + return _userLogoutGet(cacheControl: cacheControl?.toString()); } ///Logs out current logged in user session @@ -386,11 +405,12 @@ abstract class PetServiceSwagger extends ChopperService { ///@param username The name that needs to be fetched. Use user1 for testing. Future> userUsernameGet({ required String? username, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(User, () => User.fromJsonFactory); - return _userUsernameGet(username: username, cacheControl: cacheControl); + return _userUsernameGet( + username: username, cacheControl: cacheControl?.toString()); } ///Get user by user name @@ -407,12 +427,12 @@ abstract class PetServiceSwagger extends ChopperService { Future userUsernamePut({ required String? username, required User? body, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(User, () => User.fromJsonFactory); return _userUsernamePut( - username: username, body: body, cacheControl: cacheControl); + username: username, body: body, cacheControl: cacheControl?.toString()); } ///Updated user @@ -429,9 +449,10 @@ abstract class PetServiceSwagger extends ChopperService { ///@param username The name that needs to be deleted Future userUsernameDelete({ required String? username, - String? cacheControl, + dynamic cacheControl, }) { - return _userUsernameDelete(username: username, cacheControl: cacheControl); + return _userUsernameDelete( + username: username, cacheControl: cacheControl?.toString()); } ///Delete user @@ -443,9 +464,113 @@ abstract class PetServiceSwagger extends ChopperService { }); } +@JsonSerializable(explicitToJson: true) +class Order { + const Order({ + this.id, + this.petId, + this.quantity, + this.shipDate, + this.status, + this.complete, + }); + + factory Order.fromJson(Map json) => _$OrderFromJson(json); + + static const toJsonFactory = _$OrderToJson; + Map toJson() => _$OrderToJson(this); + + @JsonKey(name: 'id', includeIfNull: false) + final int? id; + @JsonKey(name: 'petId', includeIfNull: false) + final int? petId; + @JsonKey(name: 'quantity', includeIfNull: false) + final int? quantity; + @JsonKey(name: 'shipDate', includeIfNull: false) + final DateTime? shipDate; + @JsonKey( + name: 'status', + includeIfNull: false, + toJson: orderStatusNullableToJson, + fromJson: orderStatusNullableFromJson, + ) + final enums.OrderStatus? status; + @JsonKey(name: 'complete', includeIfNull: false, defaultValue: false) + final bool? complete; + static const fromJsonFactory = _$OrderFromJson; + + @override + bool operator ==(dynamic other) { + return identical(this, other) || + (other is Order && + (identical(other.id, id) || + const DeepCollectionEquality().equals(other.id, id)) && + (identical(other.petId, petId) || + const DeepCollectionEquality().equals(other.petId, petId)) && + (identical(other.quantity, quantity) || + const DeepCollectionEquality() + .equals(other.quantity, quantity)) && + (identical(other.shipDate, shipDate) || + const DeepCollectionEquality() + .equals(other.shipDate, shipDate)) && + (identical(other.status, status) || + const DeepCollectionEquality().equals(other.status, status)) && + (identical(other.complete, complete) || + const DeepCollectionEquality() + .equals(other.complete, complete))); + } + + @override + String toString() => jsonEncode(this); + + @override + int get hashCode => + const DeepCollectionEquality().hash(id) ^ + const DeepCollectionEquality().hash(petId) ^ + const DeepCollectionEquality().hash(quantity) ^ + const DeepCollectionEquality().hash(shipDate) ^ + const DeepCollectionEquality().hash(status) ^ + const DeepCollectionEquality().hash(complete) ^ + runtimeType.hashCode; +} + +extension $OrderExtension on Order { + Order copyWith( + {int? id, + int? petId, + int? quantity, + DateTime? shipDate, + enums.OrderStatus? status, + bool? complete}) { + return Order( + id: id ?? this.id, + petId: petId ?? this.petId, + quantity: quantity ?? this.quantity, + shipDate: shipDate ?? this.shipDate, + status: status ?? this.status, + complete: complete ?? this.complete); + } + + Order copyWithWrapped( + {Wrapped? id, + Wrapped? petId, + Wrapped? quantity, + Wrapped? shipDate, + Wrapped? status, + Wrapped? complete}) { + return Order( + id: (id != null ? id.value : this.id), + petId: (petId != null ? petId.value : this.petId), + quantity: (quantity != null ? quantity.value : this.quantity), + shipDate: (shipDate != null ? shipDate.value : this.shipDate), + status: (status != null ? status.value : this.status), + complete: (complete != null ? complete.value : this.complete)); + } +} + @JsonSerializable(explicitToJson: true) class Category { - Category({ + const Category({ this.id, this.name, }); @@ -453,13 +578,14 @@ class Category { factory Category.fromJson(Map json) => _$CategoryFromJson(json); + static const toJsonFactory = _$CategoryToJson; + Map toJson() => _$CategoryToJson(this); + @JsonKey(name: 'id', includeIfNull: false) - final num? id; + final int? id; @JsonKey(name: 'name', includeIfNull: false, defaultValue: '') final String? name; static const fromJsonFactory = _$CategoryFromJson; - static const toJsonFactory = _$CategoryToJson; - Map toJson() => _$CategoryToJson(this); @override bool operator ==(dynamic other) { @@ -482,11 +608,11 @@ class Category { } extension $CategoryExtension on Category { - Category copyWith({num? id, String? name}) { + Category copyWith({int? id, String? name}) { return Category(id: id ?? this.id, name: name ?? this.name); } - Category copyWithWrapped({Wrapped? id, Wrapped? name}) { + Category copyWithWrapped({Wrapped? id, Wrapped? name}) { return Category( id: (id != null ? id.value : this.id), name: (name != null ? name.value : this.name)); @@ -495,7 +621,7 @@ extension $CategoryExtension on Category { @JsonSerializable(explicitToJson: true) class User { - User({ + const User({ this.id, this.username, this.firstName, @@ -508,8 +634,11 @@ class User { factory User.fromJson(Map json) => _$UserFromJson(json); + static const toJsonFactory = _$UserToJson; + Map toJson() => _$UserToJson(this); + @JsonKey(name: 'id', includeIfNull: false) - final num? id; + final int? id; @JsonKey(name: 'username', includeIfNull: false, defaultValue: '') final String? username; @JsonKey(name: 'firstName', includeIfNull: false, defaultValue: '') @@ -525,8 +654,6 @@ class User { @JsonKey(name: 'userStatus', includeIfNull: false) final int? userStatus; static const fromJsonFactory = _$UserFromJson; - static const toJsonFactory = _$UserToJson; - Map toJson() => _$UserToJson(this); @override bool operator ==(dynamic other) { @@ -573,7 +700,7 @@ class User { extension $UserExtension on User { User copyWith( - {num? id, + {int? id, String? username, String? firstName, String? lastName, @@ -593,7 +720,7 @@ extension $UserExtension on User { } User copyWithWrapped( - {Wrapped? id, + {Wrapped? id, Wrapped? username, Wrapped? firstName, Wrapped? lastName, @@ -615,20 +742,21 @@ extension $UserExtension on User { @JsonSerializable(explicitToJson: true) class Tag { - Tag({ + const Tag({ this.id, this.name, }); factory Tag.fromJson(Map json) => _$TagFromJson(json); + static const toJsonFactory = _$TagToJson; + Map toJson() => _$TagToJson(this); + @JsonKey(name: 'id', includeIfNull: false) - final num? id; + final int? id; @JsonKey(name: 'name', includeIfNull: false, defaultValue: '') final String? name; static const fromJsonFactory = _$TagFromJson; - static const toJsonFactory = _$TagToJson; - Map toJson() => _$TagToJson(this); @override bool operator ==(dynamic other) { @@ -651,11 +779,11 @@ class Tag { } extension $TagExtension on Tag { - Tag copyWith({num? id, String? name}) { + Tag copyWith({int? id, String? name}) { return Tag(id: id ?? this.id, name: name ?? this.name); } - Tag copyWithWrapped({Wrapped? id, Wrapped? name}) { + Tag copyWithWrapped({Wrapped? id, Wrapped? name}) { return Tag( id: (id != null ? id.value : this.id), name: (name != null ? name.value : this.name)); @@ -664,19 +792,22 @@ extension $TagExtension on Tag { @JsonSerializable(explicitToJson: true) class Pet { - Pet({ + const Pet({ this.id, this.category, - this.name, - this.photoUrls, + required this.name, + required this.photoUrls, this.tags, this.status, }); factory Pet.fromJson(Map json) => _$PetFromJson(json); + static const toJsonFactory = _$PetToJson; + Map toJson() => _$PetToJson(this); + @JsonKey(name: 'id', includeIfNull: false) - final num? id; + final int? id; @JsonKey(name: 'category', includeIfNull: false) final Category? category; @JsonKey(name: 'name', includeIfNull: false, defaultValue: '') @@ -688,13 +819,11 @@ class Pet { @JsonKey( name: 'status', includeIfNull: false, - toJson: petStatusToJson, - fromJson: petStatusFromJson, + toJson: petStatusNullableToJson, + fromJson: petStatusNullableFromJson, ) final enums.PetStatus? status; static const fromJsonFactory = _$PetFromJson; - static const toJsonFactory = _$PetToJson; - Map toJson() => _$PetToJson(this); @override bool operator ==(dynamic other) { @@ -732,7 +861,7 @@ class Pet { extension $PetExtension on Pet { Pet copyWith( - {num? id, + {int? id, Category? category, String? name, List? photoUrls, @@ -748,7 +877,7 @@ extension $PetExtension on Pet { } Pet copyWithWrapped( - {Wrapped? id, + {Wrapped? id, Wrapped? category, Wrapped? name, Wrapped?>? photoUrls, @@ -766,7 +895,7 @@ extension $PetExtension on Pet { @JsonSerializable(explicitToJson: true) class ApiResponse { - ApiResponse({ + const ApiResponse({ this.code, this.type, this.message, @@ -775,6 +904,9 @@ class ApiResponse { factory ApiResponse.fromJson(Map json) => _$ApiResponseFromJson(json); + static const toJsonFactory = _$ApiResponseToJson; + Map toJson() => _$ApiResponseToJson(this); + @JsonKey(name: 'code', includeIfNull: false) final int? code; @JsonKey(name: 'type', includeIfNull: false, defaultValue: '') @@ -782,8 +914,6 @@ class ApiResponse { @JsonKey(name: 'message', includeIfNull: false, defaultValue: '') final String? message; static const fromJsonFactory = _$ApiResponseFromJson; - static const toJsonFactory = _$ApiResponseToJson; - Map toJson() => _$ApiResponseToJson(this); @override bool operator ==(dynamic other) { @@ -827,20 +957,41 @@ extension $ApiResponseExtension on ApiResponse { } } -String? orderStatusToJson(enums.OrderStatus? orderStatus) { +String? orderStatusNullableToJson(enums.OrderStatus? orderStatus) { return orderStatus?.value; } +String? orderStatusToJson(enums.OrderStatus orderStatus) { + return orderStatus.value; +} + enums.OrderStatus orderStatusFromJson( Object? orderStatus, [ enums.OrderStatus? defaultValue, ]) { - return enums.OrderStatus.values - .firstWhereOrNull((e) => e.value == orderStatus) ?? + return enums.OrderStatus.values.firstWhereOrNull((e) => + e.value.toString().toLowerCase() == + orderStatus?.toString().toLowerCase()) ?? defaultValue ?? enums.OrderStatus.swaggerGeneratedUnknown; } +enums.OrderStatus? orderStatusNullableFromJson( + Object? orderStatus, [ + enums.OrderStatus? defaultValue, +]) { + if (orderStatus == null) { + return null; + } + return enums.OrderStatus.values + .firstWhereOrNull((e) => e.value == orderStatus) ?? + defaultValue; +} + +String orderStatusExplodedListToJson(List? orderStatus) { + return orderStatus?.map((e) => e.value!).join(',') ?? ''; +} + List orderStatusListToJson(List? orderStatus) { if (orderStatus == null) { return []; @@ -871,19 +1022,40 @@ List? orderStatusNullableListFromJson( return orderStatus.map((e) => orderStatusFromJson(e.toString())).toList(); } -String? petStatusToJson(enums.PetStatus? petStatus) { +String? petStatusNullableToJson(enums.PetStatus? petStatus) { return petStatus?.value; } +String? petStatusToJson(enums.PetStatus petStatus) { + return petStatus.value; +} + enums.PetStatus petStatusFromJson( Object? petStatus, [ enums.PetStatus? defaultValue, ]) { - return enums.PetStatus.values.firstWhereOrNull((e) => e.value == petStatus) ?? + return enums.PetStatus.values.firstWhereOrNull((e) => + e.value.toString().toLowerCase() == + petStatus?.toString().toLowerCase()) ?? defaultValue ?? enums.PetStatus.swaggerGeneratedUnknown; } +enums.PetStatus? petStatusNullableFromJson( + Object? petStatus, [ + enums.PetStatus? defaultValue, +]) { + if (petStatus == null) { + return null; + } + return enums.PetStatus.values.firstWhereOrNull((e) => e.value == petStatus) ?? + defaultValue; +} + +String petStatusExplodedListToJson(List? petStatus) { + return petStatus?.map((e) => e.value!).join(',') ?? ''; +} + List petStatusListToJson(List? petStatus) { if (petStatus == null) { return []; @@ -914,21 +1086,44 @@ List? petStatusNullableListFromJson( return petStatus.map((e) => petStatusFromJson(e.toString())).toList(); } -String? petFindByStatusGetStatusToJson( +String? petFindByStatusGetStatusNullableToJson( enums.PetFindByStatusGetStatus? petFindByStatusGetStatus) { return petFindByStatusGetStatus?.value; } +String? petFindByStatusGetStatusToJson( + enums.PetFindByStatusGetStatus petFindByStatusGetStatus) { + return petFindByStatusGetStatus.value; +} + enums.PetFindByStatusGetStatus petFindByStatusGetStatusFromJson( Object? petFindByStatusGetStatus, [ enums.PetFindByStatusGetStatus? defaultValue, ]) { - return enums.PetFindByStatusGetStatus.values - .firstWhereOrNull((e) => e.value == petFindByStatusGetStatus) ?? + return enums.PetFindByStatusGetStatus.values.firstWhereOrNull((e) => + e.value.toString().toLowerCase() == + petFindByStatusGetStatus?.toString().toLowerCase()) ?? defaultValue ?? enums.PetFindByStatusGetStatus.swaggerGeneratedUnknown; } +enums.PetFindByStatusGetStatus? petFindByStatusGetStatusNullableFromJson( + Object? petFindByStatusGetStatus, [ + enums.PetFindByStatusGetStatus? defaultValue, +]) { + if (petFindByStatusGetStatus == null) { + return null; + } + return enums.PetFindByStatusGetStatus.values + .firstWhereOrNull((e) => e.value == petFindByStatusGetStatus) ?? + defaultValue; +} + +String petFindByStatusGetStatusExplodedListToJson( + List? petFindByStatusGetStatus) { + return petFindByStatusGetStatus?.map((e) => e.value!).join(',') ?? ''; +} + List petFindByStatusGetStatusListToJson( List? petFindByStatusGetStatus) { if (petFindByStatusGetStatus == null) { @@ -1019,6 +1214,16 @@ class $JsonSerializableConverter extends chopper.JsonConverter { return chopper.Response(response.base, null, error: response.error); } + if (ResultType == String) { + return response.copyWith(); + } + + if (ResultType == DateTime) { + return response.copyWith( + body: DateTime.parse((response.body as String).replaceAll('"', '')) + as ResultType); + } + final jsonRes = await super.convertResponse(response); return jsonRes.copyWith( body: $jsonDecoder.decode(jsonRes.body) as ResultType); diff --git a/example/lib/swagger_generated_code/pet_service_swagger.swagger.g.dart b/example/lib/swagger_generated_code/pet_service_swagger.swagger.g.dart index c08df232..9a11db64 100644 --- a/example/lib/swagger_generated_code/pet_service_swagger.swagger.g.dart +++ b/example/lib/swagger_generated_code/pet_service_swagger.swagger.g.dart @@ -6,8 +6,37 @@ part of 'pet_service_swagger.swagger.dart'; // JsonSerializableGenerator // ************************************************************************** +Order _$OrderFromJson(Map json) => Order( + id: json['id'] as int?, + petId: json['petId'] as int?, + quantity: json['quantity'] as int?, + shipDate: json['shipDate'] == null + ? null + : DateTime.parse(json['shipDate'] as String), + status: orderStatusNullableFromJson(json['status']), + complete: json['complete'] as bool? ?? false, + ); + +Map _$OrderToJson(Order instance) { + final val = {}; + + void writeNotNull(String key, dynamic value) { + if (value != null) { + val[key] = value; + } + } + + writeNotNull('id', instance.id); + writeNotNull('petId', instance.petId); + writeNotNull('quantity', instance.quantity); + writeNotNull('shipDate', instance.shipDate?.toIso8601String()); + writeNotNull('status', orderStatusNullableToJson(instance.status)); + writeNotNull('complete', instance.complete); + return val; +} + Category _$CategoryFromJson(Map json) => Category( - id: json['id'] as num?, + id: json['id'] as int?, name: json['name'] as String? ?? '', ); @@ -26,7 +55,7 @@ Map _$CategoryToJson(Category instance) { } User _$UserFromJson(Map json) => User( - id: json['id'] as num?, + id: json['id'] as int?, username: json['username'] as String? ?? '', firstName: json['firstName'] as String? ?? '', lastName: json['lastName'] as String? ?? '', @@ -57,7 +86,7 @@ Map _$UserToJson(User instance) { } Tag _$TagFromJson(Map json) => Tag( - id: json['id'] as num?, + id: json['id'] as int?, name: json['name'] as String? ?? '', ); @@ -76,7 +105,7 @@ Map _$TagToJson(Tag instance) { } Pet _$PetFromJson(Map json) => Pet( - id: json['id'] as num?, + id: json['id'] as int?, category: json['category'] == null ? null : Category.fromJson(json['category'] as Map), @@ -87,7 +116,7 @@ Pet _$PetFromJson(Map json) => Pet( tags: (json['tags'] as List?) ?.map((e) => Tag.fromJson(e as Map)) .toList(), - status: petStatusFromJson(json['status']), + status: petStatusNullableFromJson(json['status']), ); Map _$PetToJson(Pet instance) { @@ -104,7 +133,7 @@ Map _$PetToJson(Pet instance) { writeNotNull('name', instance.name); writeNotNull('photoUrls', instance.photoUrls); writeNotNull('tags', instance.tags?.map((e) => e.toJson()).toList()); - writeNotNull('status', petStatusToJson(instance.status)); + writeNotNull('status', petStatusNullableToJson(instance.status)); return val; } diff --git a/example/lib/swagger_generated_code/pet_service_yaml.enums.swagger.dart b/example/lib/swagger_generated_code/pet_service_yaml.enums.swagger.dart index 6cd0427b..1ea689ca 100644 --- a/example/lib/swagger_generated_code/pet_service_yaml.enums.swagger.dart +++ b/example/lib/swagger_generated_code/pet_service_yaml.enums.swagger.dart @@ -1,4 +1,5 @@ import 'package:json_annotation/json_annotation.dart'; +import 'package:collection/collection.dart'; enum OrderStatus { @JsonValue(null) diff --git a/example/lib/swagger_generated_code/pet_service_yaml.swagger.chopper.dart b/example/lib/swagger_generated_code/pet_service_yaml.swagger.chopper.dart index 08063eba..053fcca7 100644 --- a/example/lib/swagger_generated_code/pet_service_yaml.swagger.chopper.dart +++ b/example/lib/swagger_generated_code/pet_service_yaml.swagger.chopper.dart @@ -58,7 +58,7 @@ class _$PetServiceYaml extends PetServiceYaml { @override Future>> _petFindByStatusGet({ - required String? status, + required List? status, String? cacheControl, }) { final Uri $url = Uri.parse('/pet/findByStatus'); diff --git a/example/lib/swagger_generated_code/pet_service_yaml.swagger.dart b/example/lib/swagger_generated_code/pet_service_yaml.swagger.dart index 51291308..a1c5f657 100644 --- a/example/lib/swagger_generated_code/pet_service_yaml.swagger.dart +++ b/example/lib/swagger_generated_code/pet_service_yaml.swagger.dart @@ -4,11 +4,12 @@ import 'package:json_annotation/json_annotation.dart'; import 'package:collection/collection.dart'; import 'dart:convert'; -import 'overriden_models.dart'; import 'package:chopper/chopper.dart'; import 'client_mapping.dart'; import 'dart:async'; +import 'package:http/http.dart' as http; +import 'package:http/http.dart' show MultipartFile; import 'package:chopper/chopper.dart' as chopper; import 'pet_service_yaml.enums.swagger.dart' as enums; export 'pet_service_yaml.enums.swagger.dart'; @@ -24,7 +25,9 @@ part 'pet_service_yaml.swagger.g.dart'; abstract class PetServiceYaml extends ChopperService { static PetServiceYaml create({ ChopperClient? client, + http.Client? httpClient, Authenticator? authenticator, + Converter? converter, Uri? baseUrl, Iterable? interceptors, }) { @@ -34,8 +37,9 @@ abstract class PetServiceYaml extends ChopperService { final newClient = ChopperClient( services: [_$PetServiceYaml()], - converter: $JsonSerializableConverter(), + converter: converter ?? $JsonSerializableConverter(), interceptors: interceptors ?? [], + client: httpClient, authenticator: authenticator, baseUrl: baseUrl ?? Uri.parse('http://petstore.swagger.io/v2')); return _$PetServiceYaml(newClient); @@ -45,11 +49,11 @@ abstract class PetServiceYaml extends ChopperService { ///@param body Pet object that needs to be added to the store Future petPost({ required Pet? body, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(Pet, () => Pet.fromJsonFactory); - return _petPost(body: body, cacheControl: cacheControl); + return _petPost(body: body, cacheControl: cacheControl?.toString()); } ///Add a new pet to the store @@ -64,11 +68,11 @@ abstract class PetServiceYaml extends ChopperService { ///@param body Pet object that needs to be added to the store Future petPut({ required Pet? body, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(Pet, () => Pet.fromJsonFactory); - return _petPut(body: body, cacheControl: cacheControl); + return _petPut(body: body, cacheControl: cacheControl?.toString()); } ///Update an existing pet @@ -82,20 +86,21 @@ abstract class PetServiceYaml extends ChopperService { ///Finds Pets by status ///@param status Status values that need to be considered for filter Future>> petFindByStatusGet({ - required enums.PetFindByStatusGetStatus? status, - String? cacheControl, + required List? status, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(Pet, () => Pet.fromJsonFactory); return _petFindByStatusGet( - status: status?.value?.toString(), cacheControl: cacheControl); + status: petFindByStatusGetStatusListToJson(status), + cacheControl: cacheControl?.toString()); } ///Finds Pets by status ///@param status Status values that need to be considered for filter @Get(path: '/pet/findByStatus') Future>> _petFindByStatusGet({ - @Query('status') required String? status, + @Query() required List? status, @Header('Cache-Control') String? cacheControl, }); @@ -103,18 +108,19 @@ abstract class PetServiceYaml extends ChopperService { ///@param tags Tags to filter by Future>> petFindByTagsGet({ required List? tags, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(Pet, () => Pet.fromJsonFactory); - return _petFindByTagsGet(tags: tags, cacheControl: cacheControl); + return _petFindByTagsGet( + tags: tags, cacheControl: cacheControl?.toString()); } ///Finds Pets by tags ///@param tags Tags to filter by @Get(path: '/pet/findByTags') Future>> _petFindByTagsGet({ - @Query('tags') required List? tags, + @Query() required List? tags, @Header('Cache-Control') String? cacheControl, }); @@ -122,13 +128,15 @@ abstract class PetServiceYaml extends ChopperService { ///@param petId ID of pet to return Future> petPetIdGet({ required int? petId, - String? apiKey, - String? cacheControl, + dynamic apiKey, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(Pet, () => Pet.fromJsonFactory); return _petPetIdGet( - petId: petId, apiKey: apiKey, cacheControl: cacheControl); + petId: petId, + apiKey: apiKey?.toString(), + cacheControl: cacheControl?.toString()); } ///Find pet by ID @@ -148,10 +156,13 @@ abstract class PetServiceYaml extends ChopperService { required int? petId, String? name, String? status, - String? cacheControl, + dynamic cacheControl, }) { return _petPetIdPost( - petId: petId, name: name, status: status, cacheControl: cacheControl); + petId: petId, + name: name, + status: status, + cacheControl: cacheControl?.toString()); } ///Updates a pet in the store with form data @@ -175,10 +186,12 @@ abstract class PetServiceYaml extends ChopperService { Future petPetIdDelete({ String? apiKey, required int? petId, - String? cacheControl, + dynamic cacheControl, }) { return _petPetIdDelete( - apiKey: apiKey, petId: petId, cacheControl: cacheControl); + apiKey: apiKey?.toString(), + petId: petId, + cacheControl: cacheControl?.toString()); } ///Deletes a pet @@ -199,7 +212,7 @@ abstract class PetServiceYaml extends ChopperService { required int? petId, String? additionalMetadata, List? file, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent( ApiResponse, () => ApiResponse.fromJsonFactory); @@ -208,7 +221,7 @@ abstract class PetServiceYaml extends ChopperService { petId: petId, additionalMetadata: additionalMetadata, file: file, - cacheControl: cacheControl); + cacheControl: cacheControl?.toString()); } ///uploads an image @@ -228,10 +241,11 @@ abstract class PetServiceYaml extends ChopperService { ///Returns pet inventories by status Future> storeInventoryGet({ - String? apiKey, - String? cacheControl, + dynamic apiKey, + dynamic cacheControl, }) { - return _storeInventoryGet(apiKey: apiKey, cacheControl: cacheControl); + return _storeInventoryGet( + apiKey: apiKey?.toString(), cacheControl: cacheControl?.toString()); } ///Returns pet inventories by status @@ -245,11 +259,11 @@ abstract class PetServiceYaml extends ChopperService { ///@param body order placed for purchasing the pet Future> storeOrderPost({ required Order? body, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(Order, () => Order.fromJsonFactory); - return _storeOrderPost(body: body, cacheControl: cacheControl); + return _storeOrderPost(body: body, cacheControl: cacheControl?.toString()); } ///Place an order for a pet @@ -264,11 +278,12 @@ abstract class PetServiceYaml extends ChopperService { ///@param orderId ID of pet that needs to be fetched Future> storeOrderOrderIdGet({ required int? orderId, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(Order, () => Order.fromJsonFactory); - return _storeOrderOrderIdGet(orderId: orderId, cacheControl: cacheControl); + return _storeOrderOrderIdGet( + orderId: orderId, cacheControl: cacheControl?.toString()); } ///Find purchase order by ID @@ -283,10 +298,10 @@ abstract class PetServiceYaml extends ChopperService { ///@param orderId ID of the order that needs to be deleted Future storeOrderOrderIdDelete({ required int? orderId, - String? cacheControl, + dynamic cacheControl, }) { return _storeOrderOrderIdDelete( - orderId: orderId, cacheControl: cacheControl); + orderId: orderId, cacheControl: cacheControl?.toString()); } ///Delete purchase order by ID @@ -301,11 +316,11 @@ abstract class PetServiceYaml extends ChopperService { ///@param body Created user object Future userPost({ required User? body, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(User, () => User.fromJsonFactory); - return _userPost(body: body, cacheControl: cacheControl); + return _userPost(body: body, cacheControl: cacheControl?.toString()); } ///Create user @@ -320,9 +335,10 @@ abstract class PetServiceYaml extends ChopperService { ///@param body List of user object Future userCreateWithArrayPost({ required List? body, - String? cacheControl, + dynamic cacheControl, }) { - return _userCreateWithArrayPost(body: body, cacheControl: cacheControl); + return _userCreateWithArrayPost( + body: body, cacheControl: cacheControl?.toString()); } ///Creates list of users with given input array @@ -337,9 +353,10 @@ abstract class PetServiceYaml extends ChopperService { ///@param body List of user object Future userCreateWithListPost({ required List? body, - String? cacheControl, + dynamic cacheControl, }) { - return _userCreateWithListPost(body: body, cacheControl: cacheControl); + return _userCreateWithListPost( + body: body, cacheControl: cacheControl?.toString()); } ///Creates list of users with given input array @@ -356,10 +373,12 @@ abstract class PetServiceYaml extends ChopperService { Future> userLoginGet({ required String? username, required String? password, - String? cacheControl, + dynamic cacheControl, }) { return _userLoginGet( - username: username, password: password, cacheControl: cacheControl); + username: username, + password: password, + cacheControl: cacheControl?.toString()); } ///Logs user into the system @@ -367,14 +386,14 @@ abstract class PetServiceYaml extends ChopperService { ///@param password The password for login in clear text @Get(path: '/user/login') Future> _userLoginGet({ - @Query('username') required String? username, - @Query('password') required String? password, + @Query() required String? username, + @Query() required String? password, @Header('Cache-Control') String? cacheControl, }); ///Logs out current logged in user session - Future userLogoutGet({String? cacheControl}) { - return _userLogoutGet(cacheControl: cacheControl); + Future userLogoutGet({dynamic cacheControl}) { + return _userLogoutGet(cacheControl: cacheControl?.toString()); } ///Logs out current logged in user session @@ -386,11 +405,12 @@ abstract class PetServiceYaml extends ChopperService { ///@param username The name that needs to be fetched. Use user1 for testing. Future> userUsernameGet({ required String? username, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(User, () => User.fromJsonFactory); - return _userUsernameGet(username: username, cacheControl: cacheControl); + return _userUsernameGet( + username: username, cacheControl: cacheControl?.toString()); } ///Get user by user name @@ -407,12 +427,12 @@ abstract class PetServiceYaml extends ChopperService { Future userUsernamePut({ required String? username, required User? body, - String? cacheControl, + dynamic cacheControl, }) { generatedMapping.putIfAbsent(User, () => User.fromJsonFactory); return _userUsernamePut( - username: username, body: body, cacheControl: cacheControl); + username: username, body: body, cacheControl: cacheControl?.toString()); } ///Updated user @@ -429,9 +449,10 @@ abstract class PetServiceYaml extends ChopperService { ///@param username The name that needs to be deleted Future userUsernameDelete({ required String? username, - String? cacheControl, + dynamic cacheControl, }) { - return _userUsernameDelete(username: username, cacheControl: cacheControl); + return _userUsernameDelete( + username: username, cacheControl: cacheControl?.toString()); } ///Delete user @@ -443,9 +464,113 @@ abstract class PetServiceYaml extends ChopperService { }); } +@JsonSerializable(explicitToJson: true) +class Order { + const Order({ + this.id, + this.petId, + this.quantity, + this.shipDate, + this.status, + this.complete, + }); + + factory Order.fromJson(Map json) => _$OrderFromJson(json); + + static const toJsonFactory = _$OrderToJson; + Map toJson() => _$OrderToJson(this); + + @JsonKey(name: 'id', includeIfNull: false) + final int? id; + @JsonKey(name: 'petId', includeIfNull: false) + final int? petId; + @JsonKey(name: 'quantity', includeIfNull: false) + final int? quantity; + @JsonKey(name: 'shipDate', includeIfNull: false) + final DateTime? shipDate; + @JsonKey( + name: 'status', + includeIfNull: false, + toJson: orderStatusNullableToJson, + fromJson: orderStatusNullableFromJson, + ) + final enums.OrderStatus? status; + @JsonKey(name: 'complete', includeIfNull: false, defaultValue: false) + final bool? complete; + static const fromJsonFactory = _$OrderFromJson; + + @override + bool operator ==(dynamic other) { + return identical(this, other) || + (other is Order && + (identical(other.id, id) || + const DeepCollectionEquality().equals(other.id, id)) && + (identical(other.petId, petId) || + const DeepCollectionEquality().equals(other.petId, petId)) && + (identical(other.quantity, quantity) || + const DeepCollectionEquality() + .equals(other.quantity, quantity)) && + (identical(other.shipDate, shipDate) || + const DeepCollectionEquality() + .equals(other.shipDate, shipDate)) && + (identical(other.status, status) || + const DeepCollectionEquality().equals(other.status, status)) && + (identical(other.complete, complete) || + const DeepCollectionEquality() + .equals(other.complete, complete))); + } + + @override + String toString() => jsonEncode(this); + + @override + int get hashCode => + const DeepCollectionEquality().hash(id) ^ + const DeepCollectionEquality().hash(petId) ^ + const DeepCollectionEquality().hash(quantity) ^ + const DeepCollectionEquality().hash(shipDate) ^ + const DeepCollectionEquality().hash(status) ^ + const DeepCollectionEquality().hash(complete) ^ + runtimeType.hashCode; +} + +extension $OrderExtension on Order { + Order copyWith( + {int? id, + int? petId, + int? quantity, + DateTime? shipDate, + enums.OrderStatus? status, + bool? complete}) { + return Order( + id: id ?? this.id, + petId: petId ?? this.petId, + quantity: quantity ?? this.quantity, + shipDate: shipDate ?? this.shipDate, + status: status ?? this.status, + complete: complete ?? this.complete); + } + + Order copyWithWrapped( + {Wrapped? id, + Wrapped? petId, + Wrapped? quantity, + Wrapped? shipDate, + Wrapped? status, + Wrapped? complete}) { + return Order( + id: (id != null ? id.value : this.id), + petId: (petId != null ? petId.value : this.petId), + quantity: (quantity != null ? quantity.value : this.quantity), + shipDate: (shipDate != null ? shipDate.value : this.shipDate), + status: (status != null ? status.value : this.status), + complete: (complete != null ? complete.value : this.complete)); + } +} + @JsonSerializable(explicitToJson: true) class Category { - Category({ + const Category({ this.id, this.name, }); @@ -453,13 +578,14 @@ class Category { factory Category.fromJson(Map json) => _$CategoryFromJson(json); + static const toJsonFactory = _$CategoryToJson; + Map toJson() => _$CategoryToJson(this); + @JsonKey(name: 'id', includeIfNull: false) - final num? id; + final int? id; @JsonKey(name: 'name', includeIfNull: false, defaultValue: '') final String? name; static const fromJsonFactory = _$CategoryFromJson; - static const toJsonFactory = _$CategoryToJson; - Map toJson() => _$CategoryToJson(this); @override bool operator ==(dynamic other) { @@ -482,11 +608,11 @@ class Category { } extension $CategoryExtension on Category { - Category copyWith({num? id, String? name}) { + Category copyWith({int? id, String? name}) { return Category(id: id ?? this.id, name: name ?? this.name); } - Category copyWithWrapped({Wrapped? id, Wrapped? name}) { + Category copyWithWrapped({Wrapped? id, Wrapped? name}) { return Category( id: (id != null ? id.value : this.id), name: (name != null ? name.value : this.name)); @@ -495,7 +621,7 @@ extension $CategoryExtension on Category { @JsonSerializable(explicitToJson: true) class User { - User({ + const User({ this.id, this.username, this.firstName, @@ -508,8 +634,11 @@ class User { factory User.fromJson(Map json) => _$UserFromJson(json); + static const toJsonFactory = _$UserToJson; + Map toJson() => _$UserToJson(this); + @JsonKey(name: 'id', includeIfNull: false) - final num? id; + final int? id; @JsonKey(name: 'username', includeIfNull: false, defaultValue: '') final String? username; @JsonKey(name: 'firstName', includeIfNull: false, defaultValue: '') @@ -525,8 +654,6 @@ class User { @JsonKey(name: 'userStatus', includeIfNull: false) final int? userStatus; static const fromJsonFactory = _$UserFromJson; - static const toJsonFactory = _$UserToJson; - Map toJson() => _$UserToJson(this); @override bool operator ==(dynamic other) { @@ -573,7 +700,7 @@ class User { extension $UserExtension on User { User copyWith( - {num? id, + {int? id, String? username, String? firstName, String? lastName, @@ -593,7 +720,7 @@ extension $UserExtension on User { } User copyWithWrapped( - {Wrapped? id, + {Wrapped? id, Wrapped? username, Wrapped? firstName, Wrapped? lastName, @@ -615,20 +742,21 @@ extension $UserExtension on User { @JsonSerializable(explicitToJson: true) class Tag { - Tag({ + const Tag({ this.id, this.name, }); factory Tag.fromJson(Map json) => _$TagFromJson(json); + static const toJsonFactory = _$TagToJson; + Map toJson() => _$TagToJson(this); + @JsonKey(name: 'id', includeIfNull: false) - final num? id; + final int? id; @JsonKey(name: 'name', includeIfNull: false, defaultValue: '') final String? name; static const fromJsonFactory = _$TagFromJson; - static const toJsonFactory = _$TagToJson; - Map toJson() => _$TagToJson(this); @override bool operator ==(dynamic other) { @@ -651,11 +779,11 @@ class Tag { } extension $TagExtension on Tag { - Tag copyWith({num? id, String? name}) { + Tag copyWith({int? id, String? name}) { return Tag(id: id ?? this.id, name: name ?? this.name); } - Tag copyWithWrapped({Wrapped? id, Wrapped? name}) { + Tag copyWithWrapped({Wrapped? id, Wrapped? name}) { return Tag( id: (id != null ? id.value : this.id), name: (name != null ? name.value : this.name)); @@ -664,19 +792,22 @@ extension $TagExtension on Tag { @JsonSerializable(explicitToJson: true) class Pet { - Pet({ + const Pet({ this.id, this.category, - this.name, - this.photoUrls, + required this.name, + required this.photoUrls, this.tags, this.status, }); factory Pet.fromJson(Map json) => _$PetFromJson(json); + static const toJsonFactory = _$PetToJson; + Map toJson() => _$PetToJson(this); + @JsonKey(name: 'id', includeIfNull: false) - final num? id; + final int? id; @JsonKey(name: 'category', includeIfNull: false) final Category? category; @JsonKey(name: 'name', includeIfNull: false, defaultValue: '') @@ -688,13 +819,11 @@ class Pet { @JsonKey( name: 'status', includeIfNull: false, - toJson: petStatusToJson, - fromJson: petStatusFromJson, + toJson: petStatusNullableToJson, + fromJson: petStatusNullableFromJson, ) final enums.PetStatus? status; static const fromJsonFactory = _$PetFromJson; - static const toJsonFactory = _$PetToJson; - Map toJson() => _$PetToJson(this); @override bool operator ==(dynamic other) { @@ -732,7 +861,7 @@ class Pet { extension $PetExtension on Pet { Pet copyWith( - {num? id, + {int? id, Category? category, String? name, List? photoUrls, @@ -748,7 +877,7 @@ extension $PetExtension on Pet { } Pet copyWithWrapped( - {Wrapped? id, + {Wrapped? id, Wrapped? category, Wrapped? name, Wrapped?>? photoUrls, @@ -766,7 +895,7 @@ extension $PetExtension on Pet { @JsonSerializable(explicitToJson: true) class ApiResponse { - ApiResponse({ + const ApiResponse({ this.code, this.type, this.message, @@ -775,6 +904,9 @@ class ApiResponse { factory ApiResponse.fromJson(Map json) => _$ApiResponseFromJson(json); + static const toJsonFactory = _$ApiResponseToJson; + Map toJson() => _$ApiResponseToJson(this); + @JsonKey(name: 'code', includeIfNull: false) final int? code; @JsonKey(name: 'type', includeIfNull: false, defaultValue: '') @@ -782,8 +914,6 @@ class ApiResponse { @JsonKey(name: 'message', includeIfNull: false, defaultValue: '') final String? message; static const fromJsonFactory = _$ApiResponseFromJson; - static const toJsonFactory = _$ApiResponseToJson; - Map toJson() => _$ApiResponseToJson(this); @override bool operator ==(dynamic other) { @@ -827,20 +957,41 @@ extension $ApiResponseExtension on ApiResponse { } } -String? orderStatusToJson(enums.OrderStatus? orderStatus) { +String? orderStatusNullableToJson(enums.OrderStatus? orderStatus) { return orderStatus?.value; } +String? orderStatusToJson(enums.OrderStatus orderStatus) { + return orderStatus.value; +} + enums.OrderStatus orderStatusFromJson( Object? orderStatus, [ enums.OrderStatus? defaultValue, ]) { - return enums.OrderStatus.values - .firstWhereOrNull((e) => e.value == orderStatus) ?? + return enums.OrderStatus.values.firstWhereOrNull((e) => + e.value.toString().toLowerCase() == + orderStatus?.toString().toLowerCase()) ?? defaultValue ?? enums.OrderStatus.swaggerGeneratedUnknown; } +enums.OrderStatus? orderStatusNullableFromJson( + Object? orderStatus, [ + enums.OrderStatus? defaultValue, +]) { + if (orderStatus == null) { + return null; + } + return enums.OrderStatus.values + .firstWhereOrNull((e) => e.value == orderStatus) ?? + defaultValue; +} + +String orderStatusExplodedListToJson(List? orderStatus) { + return orderStatus?.map((e) => e.value!).join(',') ?? ''; +} + List orderStatusListToJson(List? orderStatus) { if (orderStatus == null) { return []; @@ -871,19 +1022,40 @@ List? orderStatusNullableListFromJson( return orderStatus.map((e) => orderStatusFromJson(e.toString())).toList(); } -String? petStatusToJson(enums.PetStatus? petStatus) { +String? petStatusNullableToJson(enums.PetStatus? petStatus) { return petStatus?.value; } +String? petStatusToJson(enums.PetStatus petStatus) { + return petStatus.value; +} + enums.PetStatus petStatusFromJson( Object? petStatus, [ enums.PetStatus? defaultValue, ]) { - return enums.PetStatus.values.firstWhereOrNull((e) => e.value == petStatus) ?? + return enums.PetStatus.values.firstWhereOrNull((e) => + e.value.toString().toLowerCase() == + petStatus?.toString().toLowerCase()) ?? defaultValue ?? enums.PetStatus.swaggerGeneratedUnknown; } +enums.PetStatus? petStatusNullableFromJson( + Object? petStatus, [ + enums.PetStatus? defaultValue, +]) { + if (petStatus == null) { + return null; + } + return enums.PetStatus.values.firstWhereOrNull((e) => e.value == petStatus) ?? + defaultValue; +} + +String petStatusExplodedListToJson(List? petStatus) { + return petStatus?.map((e) => e.value!).join(',') ?? ''; +} + List petStatusListToJson(List? petStatus) { if (petStatus == null) { return []; @@ -914,21 +1086,44 @@ List? petStatusNullableListFromJson( return petStatus.map((e) => petStatusFromJson(e.toString())).toList(); } -String? petFindByStatusGetStatusToJson( +String? petFindByStatusGetStatusNullableToJson( enums.PetFindByStatusGetStatus? petFindByStatusGetStatus) { return petFindByStatusGetStatus?.value; } +String? petFindByStatusGetStatusToJson( + enums.PetFindByStatusGetStatus petFindByStatusGetStatus) { + return petFindByStatusGetStatus.value; +} + enums.PetFindByStatusGetStatus petFindByStatusGetStatusFromJson( Object? petFindByStatusGetStatus, [ enums.PetFindByStatusGetStatus? defaultValue, ]) { - return enums.PetFindByStatusGetStatus.values - .firstWhereOrNull((e) => e.value == petFindByStatusGetStatus) ?? + return enums.PetFindByStatusGetStatus.values.firstWhereOrNull((e) => + e.value.toString().toLowerCase() == + petFindByStatusGetStatus?.toString().toLowerCase()) ?? defaultValue ?? enums.PetFindByStatusGetStatus.swaggerGeneratedUnknown; } +enums.PetFindByStatusGetStatus? petFindByStatusGetStatusNullableFromJson( + Object? petFindByStatusGetStatus, [ + enums.PetFindByStatusGetStatus? defaultValue, +]) { + if (petFindByStatusGetStatus == null) { + return null; + } + return enums.PetFindByStatusGetStatus.values + .firstWhereOrNull((e) => e.value == petFindByStatusGetStatus) ?? + defaultValue; +} + +String petFindByStatusGetStatusExplodedListToJson( + List? petFindByStatusGetStatus) { + return petFindByStatusGetStatus?.map((e) => e.value!).join(',') ?? ''; +} + List petFindByStatusGetStatusListToJson( List? petFindByStatusGetStatus) { if (petFindByStatusGetStatus == null) { @@ -1019,6 +1214,16 @@ class $JsonSerializableConverter extends chopper.JsonConverter { return chopper.Response(response.base, null, error: response.error); } + if (ResultType == String) { + return response.copyWith(); + } + + if (ResultType == DateTime) { + return response.copyWith( + body: DateTime.parse((response.body as String).replaceAll('"', '')) + as ResultType); + } + final jsonRes = await super.convertResponse(response); return jsonRes.copyWith( body: $jsonDecoder.decode(jsonRes.body) as ResultType); diff --git a/example/lib/swagger_generated_code/pet_service_yaml.swagger.g.dart b/example/lib/swagger_generated_code/pet_service_yaml.swagger.g.dart index ce9984c2..d8252e6d 100644 --- a/example/lib/swagger_generated_code/pet_service_yaml.swagger.g.dart +++ b/example/lib/swagger_generated_code/pet_service_yaml.swagger.g.dart @@ -6,8 +6,37 @@ part of 'pet_service_yaml.swagger.dart'; // JsonSerializableGenerator // ************************************************************************** +Order _$OrderFromJson(Map json) => Order( + id: json['id'] as int?, + petId: json['petId'] as int?, + quantity: json['quantity'] as int?, + shipDate: json['shipDate'] == null + ? null + : DateTime.parse(json['shipDate'] as String), + status: orderStatusNullableFromJson(json['status']), + complete: json['complete'] as bool? ?? false, + ); + +Map _$OrderToJson(Order instance) { + final val = {}; + + void writeNotNull(String key, dynamic value) { + if (value != null) { + val[key] = value; + } + } + + writeNotNull('id', instance.id); + writeNotNull('petId', instance.petId); + writeNotNull('quantity', instance.quantity); + writeNotNull('shipDate', instance.shipDate?.toIso8601String()); + writeNotNull('status', orderStatusNullableToJson(instance.status)); + writeNotNull('complete', instance.complete); + return val; +} + Category _$CategoryFromJson(Map json) => Category( - id: json['id'] as num?, + id: json['id'] as int?, name: json['name'] as String? ?? '', ); @@ -26,7 +55,7 @@ Map _$CategoryToJson(Category instance) { } User _$UserFromJson(Map json) => User( - id: json['id'] as num?, + id: json['id'] as int?, username: json['username'] as String? ?? '', firstName: json['firstName'] as String? ?? '', lastName: json['lastName'] as String? ?? '', @@ -57,7 +86,7 @@ Map _$UserToJson(User instance) { } Tag _$TagFromJson(Map json) => Tag( - id: json['id'] as num?, + id: json['id'] as int?, name: json['name'] as String? ?? '', ); @@ -76,7 +105,7 @@ Map _$TagToJson(Tag instance) { } Pet _$PetFromJson(Map json) => Pet( - id: json['id'] as num?, + id: json['id'] as int?, category: json['category'] == null ? null : Category.fromJson(json['category'] as Map), @@ -87,7 +116,7 @@ Pet _$PetFromJson(Map json) => Pet( tags: (json['tags'] as List?) ?.map((e) => Tag.fromJson(e as Map)) .toList(), - status: petStatusFromJson(json['status']), + status: petStatusNullableFromJson(json['status']), ); Map _$PetToJson(Pet instance) { @@ -104,7 +133,7 @@ Map _$PetToJson(Pet instance) { writeNotNull('name', instance.name); writeNotNull('photoUrls', instance.photoUrls); writeNotNull('tags', instance.tags?.map((e) => e.toJson()).toList()); - writeNotNull('status', petStatusToJson(instance.status)); + writeNotNull('status', petStatusNullableToJson(instance.status)); return val; } diff --git a/lib/src/code_generators/swagger_additions_generator.dart b/lib/src/code_generators/swagger_additions_generator.dart index 6b3cab31..834a28a6 100644 --- a/lib/src/code_generators/swagger_additions_generator.dart +++ b/lib/src/code_generators/swagger_additions_generator.dart @@ -49,9 +49,10 @@ class SwaggerAdditionsGenerator extends SwaggerGeneratorBase { final chopperPartImport = buildOnlyModels ? '' : "part '$swaggerFileName.swagger.chopper.dart';"; - final overridenModels = options.overridenModels.isEmpty - ? '' - : 'import \'overriden_models.dart\';'; + final overridenModels = options.overridenModels + .any((e) => e.fileName == swaggerFileName) + ? 'import \'${options.overridenModels.firstWhere((e) => e.fileName == swaggerFileName).importUrl}\';' + : ''; final chopperImports = buildOnlyModels ? '' diff --git a/lib/src/code_generators/swagger_models_generator.dart b/lib/src/code_generators/swagger_models_generator.dart index 594327af..6795e3be 100644 --- a/lib/src/code_generators/swagger_models_generator.dart +++ b/lib/src/code_generators/swagger_models_generator.dart @@ -38,8 +38,13 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase { List allEnumNames, List allEnumListNames, Map allClasses, + String fileName, ) { - if (options.overridenModels.contains(getValidatedClassName(className))) { + if (options.overridenModels + .firstWhereOrNull((e) => e.fileName == fileName) + ?.overridenModels + .contains(getValidatedClassName(className)) == + true) { return ''; } @@ -102,7 +107,10 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase { final itemClassName = '$className\$Item'; if (options.overridenModels - .contains(getValidatedClassName(itemClassName))) { + .firstWhereOrNull((e) => e.fileName == fileName) + ?.overridenModels + .contains(getValidatedClassName(itemClassName)) == + true) { return ''; } @@ -301,6 +309,7 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase { allEnums.map((e) => e.name).toList(), allEnumListNames, classes, + fileName, ); }).join('\n'); diff --git a/lib/src/code_generators/swagger_requests_generator.dart b/lib/src/code_generators/swagger_requests_generator.dart index 7c962d07..b1e5b81f 100644 --- a/lib/src/code_generators/swagger_requests_generator.dart +++ b/lib/src/code_generators/swagger_requests_generator.dart @@ -918,6 +918,9 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase { typeName = _mapParameterName(schema.items!.type, schema.items!.format, options.modelPostfix) .asList(); + } else if (schema.allOf.length == 1 && + schema.allOf.first.ref.isNotEmpty) { + typeName = getValidatedClassName(schema.allOf.first.ref.getRef()); } else { typeName = _getRequestBodyTypeName( schema: schema, diff --git a/lib/src/models/generator_options.dart b/lib/src/models/generator_options.dart index 4be5e95f..f5529d1b 100644 --- a/lib/src/models/generator_options.dart +++ b/lib/src/models/generator_options.dart @@ -68,7 +68,7 @@ class GeneratorOptions { final bool withConverter; @JsonKey(defaultValue: []) - final List overridenModels; + final List overridenModels; @JsonKey(defaultValue: []) final List generateToJsonFor; @@ -195,3 +195,24 @@ class DefaultHeaderValueMap { factory DefaultHeaderValueMap.fromJson(Map json) => _$DefaultHeaderValueMapFromJson(json); } + +@JsonSerializable(fieldRename: FieldRename.snake) +class OverridenModelsItem { + @JsonKey() + final String fileName; + @JsonKey() + final List overridenModels; + @JsonKey() + final String importUrl; + + OverridenModelsItem({ + required this.fileName, + required this.overridenModels, + required this.importUrl, + }); + + Map toJson() => _$OverridenModelsItemToJson(this); + + factory OverridenModelsItem.fromJson(Map json) => + _$OverridenModelsItemFromJson(json); +} diff --git a/lib/src/models/generator_options.g2.dart b/lib/src/models/generator_options.g2.dart index 6050b4a8..477b1347 100644 --- a/lib/src/models/generator_options.g2.dart +++ b/lib/src/models/generator_options.g2.dart @@ -76,7 +76,8 @@ GeneratorOptions _$GeneratorOptionsFromJson(Map json) => GeneratorOptions( overrideToString: json['override_to_string'] as bool? ?? true, pageWidth: json['page_width'] as int?, overridenModels: (json['overriden_models'] as List?) - ?.map((e) => e as String) + ?.map((e) => OverridenModelsItem.fromJson( + Map.from(e as Map))) .toList() ?? [], generateToJsonFor: (json['generate_to_json_for'] as List?) @@ -163,3 +164,20 @@ Map _$DefaultHeaderValueMapToJson( 'header_name': instance.headerName, 'default_value': instance.defaultValue, }; + +OverridenModelsItem _$OverridenModelsItemFromJson(Map json) => + OverridenModelsItem( + fileName: json['file_name'] as String, + overridenModels: (json['overriden_models'] as List) + .map((e) => e as String) + .toList(), + importUrl: json['import_url'] as String, + ); + +Map _$OverridenModelsItemToJson( + OverridenModelsItem instance) => + { + 'file_name': instance.fileName, + 'overriden_models': instance.overridenModels, + 'import_url': instance.importUrl, + }; diff --git a/lib/swagger_dart_code_generator.dart b/lib/swagger_dart_code_generator.dart index ebd6cd1e..dba7e1ad 100644 --- a/lib/swagger_dart_code_generator.dart +++ b/lib/swagger_dart_code_generator.dart @@ -63,20 +63,6 @@ Map> _generateExtensions(GeneratorOptions options) { additionalResultPath = _getAdditionalResultPath(options).replaceAll('\\', '/'); - if (options.overridenModels.isNotEmpty) { - final path = normalize('${options.outputFolder}overriden_models.dart'); - - if (!Directory(options.outputFolder).existsSync()) { - Directory(options.outputFolder).createSync(); - } - - if (!File(path).existsSync()) { - File(path).createSync(); - File(path).writeAsString( - '//Put your overriden models here (${options.overridenModels.join(',')})'); - } - } - File(additionalResultPath).createSync(); var out = normalize(options.outputFolder); diff --git a/test/generator_tests/models_generator_test.dart b/test/generator_tests/models_generator_test.dart index 852c6d5e..c93d4190 100644 --- a/test/generator_tests/models_generator_test.dart +++ b/test/generator_tests/models_generator_test.dart @@ -221,6 +221,7 @@ void main() { [], [], {}, + 'fle_name', ); expect(result, contains(classExpectedResult)); @@ -243,6 +244,7 @@ void main() { [], [], {}, + 'fle_name', ); expect(result, contains(classExpectedResult));