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 deleted file mode 100644 index 9a11db64..00000000 --- a/example/lib/swagger_generated_code/pet_service_swagger.swagger.g.dart +++ /dev/null @@ -1,159 +0,0 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND - -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 int?, - name: json['name'] as String? ?? '', - ); - -Map _$CategoryToJson(Category instance) { - final val = {}; - - void writeNotNull(String key, dynamic value) { - if (value != null) { - val[key] = value; - } - } - - writeNotNull('id', instance.id); - writeNotNull('name', instance.name); - return val; -} - -User _$UserFromJson(Map json) => User( - id: json['id'] as int?, - username: json['username'] as String? ?? '', - firstName: json['firstName'] as String? ?? '', - lastName: json['lastName'] as String? ?? '', - email: json['email'] as String? ?? '', - password: json['password'] as String? ?? '', - phone: json['phone'] as String? ?? '', - userStatus: json['userStatus'] as int?, - ); - -Map _$UserToJson(User instance) { - final val = {}; - - void writeNotNull(String key, dynamic value) { - if (value != null) { - val[key] = value; - } - } - - writeNotNull('id', instance.id); - writeNotNull('username', instance.username); - writeNotNull('firstName', instance.firstName); - writeNotNull('lastName', instance.lastName); - writeNotNull('email', instance.email); - writeNotNull('password', instance.password); - writeNotNull('phone', instance.phone); - writeNotNull('userStatus', instance.userStatus); - return val; -} - -Tag _$TagFromJson(Map json) => Tag( - id: json['id'] as int?, - name: json['name'] as String? ?? '', - ); - -Map _$TagToJson(Tag instance) { - final val = {}; - - void writeNotNull(String key, dynamic value) { - if (value != null) { - val[key] = value; - } - } - - writeNotNull('id', instance.id); - writeNotNull('name', instance.name); - return val; -} - -Pet _$PetFromJson(Map json) => Pet( - id: json['id'] as int?, - 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: petStatusNullableFromJson(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', petStatusNullableToJson(instance.status)); - return val; -} - -ApiResponse _$ApiResponseFromJson(Map json) => ApiResponse( - code: json['code'] as int?, - type: json['type'] as String? ?? '', - message: json['message'] as String? ?? '', - ); - -Map _$ApiResponseToJson(ApiResponse instance) { - final val = {}; - - void writeNotNull(String key, dynamic value) { - if (value != null) { - val[key] = value; - } - } - - writeNotNull('code', instance.code); - writeNotNull('type', instance.type); - writeNotNull('message', instance.message); - return val; -} 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 deleted file mode 100644 index d8252e6d..00000000 --- a/example/lib/swagger_generated_code/pet_service_yaml.swagger.g.dart +++ /dev/null @@ -1,159 +0,0 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND - -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 int?, - name: json['name'] as String? ?? '', - ); - -Map _$CategoryToJson(Category instance) { - final val = {}; - - void writeNotNull(String key, dynamic value) { - if (value != null) { - val[key] = value; - } - } - - writeNotNull('id', instance.id); - writeNotNull('name', instance.name); - return val; -} - -User _$UserFromJson(Map json) => User( - id: json['id'] as int?, - username: json['username'] as String? ?? '', - firstName: json['firstName'] as String? ?? '', - lastName: json['lastName'] as String? ?? '', - email: json['email'] as String? ?? '', - password: json['password'] as String? ?? '', - phone: json['phone'] as String? ?? '', - userStatus: json['userStatus'] as int?, - ); - -Map _$UserToJson(User instance) { - final val = {}; - - void writeNotNull(String key, dynamic value) { - if (value != null) { - val[key] = value; - } - } - - writeNotNull('id', instance.id); - writeNotNull('username', instance.username); - writeNotNull('firstName', instance.firstName); - writeNotNull('lastName', instance.lastName); - writeNotNull('email', instance.email); - writeNotNull('password', instance.password); - writeNotNull('phone', instance.phone); - writeNotNull('userStatus', instance.userStatus); - return val; -} - -Tag _$TagFromJson(Map json) => Tag( - id: json['id'] as int?, - name: json['name'] as String? ?? '', - ); - -Map _$TagToJson(Tag instance) { - final val = {}; - - void writeNotNull(String key, dynamic value) { - if (value != null) { - val[key] = value; - } - } - - writeNotNull('id', instance.id); - writeNotNull('name', instance.name); - return val; -} - -Pet _$PetFromJson(Map json) => Pet( - id: json['id'] as int?, - 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: petStatusNullableFromJson(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', petStatusNullableToJson(instance.status)); - return val; -} - -ApiResponse _$ApiResponseFromJson(Map json) => ApiResponse( - code: json['code'] as int?, - type: json['type'] as String? ?? '', - message: json['message'] as String? ?? '', - ); - -Map _$ApiResponseToJson(ApiResponse instance) { - final val = {}; - - void writeNotNull(String key, dynamic value) { - if (value != null) { - val[key] = value; - } - } - - writeNotNull('code', instance.code); - writeNotNull('type', instance.type); - writeNotNull('message', instance.message); - return val; -} diff --git a/example/lib/swagger_generated_code/some_file_name.swagger.g.dart b/example/lib/swagger_generated_code/some_file_name.swagger.g.dart deleted file mode 100644 index c40a379d..00000000 --- a/example/lib/swagger_generated_code/some_file_name.swagger.g.dart +++ /dev/null @@ -1,159 +0,0 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND - -part of 'some_file_name.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 int?, - name: json['name'] as String? ?? '', - ); - -Map _$CategoryToJson(Category instance) { - final val = {}; - - void writeNotNull(String key, dynamic value) { - if (value != null) { - val[key] = value; - } - } - - writeNotNull('id', instance.id); - writeNotNull('name', instance.name); - return val; -} - -User _$UserFromJson(Map json) => User( - id: json['id'] as int?, - username: json['username'] as String? ?? '', - firstName: json['firstName'] as String? ?? '', - lastName: json['lastName'] as String? ?? '', - email: json['email'] as String? ?? '', - password: json['password'] as String? ?? '', - phone: json['phone'] as String? ?? '', - userStatus: json['userStatus'] as int?, - ); - -Map _$UserToJson(User instance) { - final val = {}; - - void writeNotNull(String key, dynamic value) { - if (value != null) { - val[key] = value; - } - } - - writeNotNull('id', instance.id); - writeNotNull('username', instance.username); - writeNotNull('firstName', instance.firstName); - writeNotNull('lastName', instance.lastName); - writeNotNull('email', instance.email); - writeNotNull('password', instance.password); - writeNotNull('phone', instance.phone); - writeNotNull('userStatus', instance.userStatus); - return val; -} - -Tag _$TagFromJson(Map json) => Tag( - id: json['id'] as int?, - name: json['name'] as String? ?? '', - ); - -Map _$TagToJson(Tag instance) { - final val = {}; - - void writeNotNull(String key, dynamic value) { - if (value != null) { - val[key] = value; - } - } - - writeNotNull('id', instance.id); - writeNotNull('name', instance.name); - return val; -} - -Pet _$PetFromJson(Map json) => Pet( - id: json['id'] as int?, - 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: petStatusNullableFromJson(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', petStatusNullableToJson(instance.status)); - return val; -} - -ApiResponse _$ApiResponseFromJson(Map json) => ApiResponse( - code: json['code'] as int?, - type: json['type'] as String? ?? '', - message: json['message'] as String? ?? '', - ); - -Map _$ApiResponseToJson(ApiResponse instance) { - final val = {}; - - void writeNotNull(String key, dynamic value) { - if (value != null) { - val[key] = value; - } - } - - writeNotNull('code', instance.code); - writeNotNull('type', instance.type); - writeNotNull('message', instance.message); - return val; -} diff --git a/lib/src/code_generators/enum_model.dart b/lib/src/code_generators/enum_model.dart index 4ff61b92..be76317d 100644 --- a/lib/src/code_generators/enum_model.dart +++ b/lib/src/code_generators/enum_model.dart @@ -97,6 +97,10 @@ const $name(this.value); result = '\$$result'; } + if (result == 'value') { + result = 'enumValue'; + } + return '$result(${isInteger ? fieldValue : '\'$fieldValue\''})'; } diff --git a/lib/src/code_generators/swagger_generator_base.dart b/lib/src/code_generators/swagger_generator_base.dart index db820dce..459d31b0 100644 --- a/lib/src/code_generators/swagger_generator_base.dart +++ b/lib/src/code_generators/swagger_generator_base.dart @@ -61,7 +61,7 @@ abstract class SwaggerGeneratorBase { } String generateEnumName(String className, String enumName) { - return getValidatedClassName('${className.capitalize}_$enumName'); + return getValidatedClassName('${className.capitalize}_${enumName}_generated'); } String generateRequestEnumName( diff --git a/lib/src/code_generators/swagger_models_generator.dart b/lib/src/code_generators/swagger_models_generator.dart index 25be4f2b..50189bc2 100644 --- a/lib/src/code_generators/swagger_models_generator.dart +++ b/lib/src/code_generators/swagger_models_generator.dart @@ -490,7 +490,7 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase { "@JsonKey(name: '$propertyKey'$includeIfNullString$dateToJsonValue${unknownEnumValue.jsonKey})\n"; final deprecatedContent = isDeprecated ? '@deprecated\n' : ''; - return '\t$jsonKeyContent$deprecatedContent\tfinal $typeName ${generateFieldName(propertyName)};${unknownEnumValue.fromJson}'; + return '\t$jsonKeyContent$deprecatedContent\t$typeName ${generateFieldName(propertyName)};${unknownEnumValue.fromJson}'; } JsonEnumValue generateEnumValue({ @@ -746,7 +746,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr typeName = typeName.makeNullable(); } - return '\t$jsonKeyContent$deprecatedContent\tfinal $typeName $propertyName;${unknownEnumValue.fromJson}'; + return '\t$jsonKeyContent$deprecatedContent\t$typeName $propertyName;${unknownEnumValue.fromJson}'; } String generatePropertyContentByRef( @@ -835,7 +835,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr typeName += '?'; } - return '\t$jsonKeyContent$deprecatedContent\tfinal $typeName $propertyName;${unknownEnumValue.fromJson}'; + return '\t$jsonKeyContent$deprecatedContent\t$typeName $propertyName;${unknownEnumValue.fromJson}'; } String generateEnumPropertyContent({ @@ -874,7 +874,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr return ''' @JsonKey(${unknownEnumValue.jsonKey.substring(2)}$includeIfNullString) ${isDeprecated ? kDeprecatedAnnotation : ''} - final $enumPropertyName ${generateFieldName(key)}; + $enumPropertyName ${generateFieldName(key)}; ${unknownEnumValue.fromJson}'''; } @@ -1026,7 +1026,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr listPropertyName = listPropertyName.makeNullable(); } - return '$jsonKeyContent$deprecatedContent final $listPropertyName ${generateFieldName(propertyName)};${unknownEnumValue.fromJson}'; + return '$jsonKeyContent$deprecatedContent $listPropertyName ${generateFieldName(propertyName)};${unknownEnumValue.fromJson}'; } String generateGeneralPropertyContent({ @@ -1102,7 +1102,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr typeName = typeName.makeNullable(); } - return '\t$jsonKeyContent$isDeprecatedContent final $typeName $propertyName;${unknownEnumValue.fromJson}'; + return '\t$jsonKeyContent$isDeprecatedContent $typeName $propertyName;${unknownEnumValue.fromJson}'; } String generatePropertyContentByType( @@ -1442,6 +1442,8 @@ String toString() => jsonEncode(this); ''' : ''; + final hasMapping = schema.discriminator?.mapping.isNotEmpty ?? false; + final fromJson = generatedFromJson(schema, validatedClassName); final toJson = generateToJson(schema, validatedClassName); @@ -1451,9 +1453,9 @@ String toString() => jsonEncode(this); final generatedClass = ''' @JsonSerializable(explicitToJson: true $createToJson) class $validatedClassName{ -\tconst $validatedClassName($generatedConstructorProperties);\n -\t$fromJson\n -\t$toJson\n +\t $validatedClassName($generatedConstructorProperties);\n +\t$fromJson${hasMapping ? '' : ''}\n +\t$toJson${hasMapping ? '' : ''}\n $generatedProperties \tstatic const fromJsonFactory = _\$${validatedClassName}FromJson; @@ -1470,19 +1472,36 @@ $copyWithMethod } String generatedFromJson(SwaggerSchema schema, String validatedClassName) { + final hasMapping = schema.discriminator?.mapping.isNotEmpty ?? false; + if (hasMapping) { + final discriminator = schema.discriminator!; + final propertyName = discriminator.propertyName; + final responseVar = validatedClassName.camelCase; + + return 'static $validatedClassName _\$${validatedClassName}FromJsonFix(Map json) { return $validatedClassName.fromJson(json);}\n\n' + '${discriminator.mapping.entries.map((entry) => '${entry.value.getRef()}? ${entry.key == 'dynamic' ? 'dynamicField' : entry.key.camelCase};').join('\n')}' + '\n\n' + 'factory $validatedClassName.fromJson(Map json) {' + '\t\tvar $responseVar = $validatedClassName();' + '\t\tswitch (json[\'$propertyName\']) {' + '\t\t\t${discriminator.mapping.entries.map((entry) => 'case \'${entry.key}\': $responseVar.${entry.key == 'dynamic' ? 'dynamicField' : entry.key.camelCase} = _\$${entry.value.split('/').last.pascalCase}FromJson(json); break;').join('\n')}' + '\t\t}' + '\treturn $responseVar;' + '}'; + } return 'factory $validatedClassName.fromJson(Map json) => _\$${validatedClassName}FromJson(json);'; } String generateToJson(SwaggerSchema schema, String validatedClassName) { - if (options.generateToJsonFor.isEmpty || - options.generateToJsonFor.contains(validatedClassName)) { - return ''' -\tstatic const toJsonFactory = _\$${validatedClassName}ToJson; -\tMap toJson() => _\$${validatedClassName}ToJson(this); -'''; - } - - return ''; + final hasMapping = schema.discriminator?.mapping.isNotEmpty ?? false; + if (hasMapping) { + return 'static Map _\$${validatedClassName}ToJsonFix($validatedClassName instance) { return instance.toJson();}\n\n' + 'Map toJson() =>' + '_\$${validatedClassName}ToJson(this)' + '\t\t\t${schema.discriminator!.mapping.entries.map( + (entry) => '\n..addAll(${entry.key == 'dynamic' ? 'dynamicField' : entry.key.camelCase}?.toJson() ?? {})').join('\n')};'; + } + return 'Map toJson() => _\$${validatedClassName}ToJson(this);'; } String generateCreateToJson(SwaggerSchema schema, String validatedClassName) { diff --git a/lib/src/swagger_models/responses/swagger_schema.dart b/lib/src/swagger_models/responses/swagger_schema.dart index 09ecb64c..4c8a0cbb 100644 --- a/lib/src/swagger_models/responses/swagger_schema.dart +++ b/lib/src/swagger_models/responses/swagger_schema.dart @@ -24,10 +24,11 @@ class SwaggerSchema { this.isNullable, this.hasAdditionalProperties = false, this.msEnum, + this.discriminator, this.title = '', this.readOnly = false, this.writeOnly = false, - this.deprecated = false, + this.deprecated = false, }); @JsonKey(name: 'readOnly', defaultValue: false) @@ -70,6 +71,9 @@ class SwaggerSchema { @JsonKey(name: 'x-ms-enum', defaultValue: null) MsEnum? msEnum; + @JsonKey(name: 'discriminator', defaultValue: null) + Discriminator? discriminator; + List get enumValues { final values = (msEnum?.values.isNotEmpty == true ? msEnum?.values.map((e) => e.value) @@ -166,3 +170,18 @@ class MsEnumValue { factory MsEnumValue.fromJson(Map json) => _$MsEnumValueFromJson(json); } + +@JsonSerializable() +class Discriminator { + Discriminator({this.propertyName = '', this.mapping = const {}}); + + @JsonKey(name: 'propertyName', defaultValue: '') + String propertyName; + @JsonKey(name: 'mapping', defaultValue: {}) + Map mapping; + + factory Discriminator.fromJson(Map json) => + _$DiscriminatorFromJson(json); + + Map toJson() => _$DiscriminatorToJson(this); +} diff --git a/lib/src/swagger_models/responses/swagger_schema.g2.dart b/lib/src/swagger_models/responses/swagger_schema.g2.dart index 2d9c735b..c4e5bc32 100644 --- a/lib/src/swagger_models/responses/swagger_schema.g2.dart +++ b/lib/src/swagger_models/responses/swagger_schema.g2.dart @@ -55,6 +55,9 @@ SwaggerSchema _$SwaggerSchemaFromJson(Map json) => msEnum: json['x-ms-enum'] == null ? null : MsEnum.fromJson(json['x-ms-enum'] as Map), + discriminator: json['discriminator'] == null + ? null + : Discriminator.fromJson(json['discriminator'] as Map), ); Map _$SwaggerSchemaToJson(SwaggerSchema instance) => @@ -81,6 +84,7 @@ Map _$SwaggerSchemaToJson(SwaggerSchema instance) => 'deprecated': instance.deprecated, 'additionalProperties': instance.hasAdditionalProperties, 'enumNames': instance.enumNames, + 'discriminator': instance.discriminator, }; MsEnum _$MsEnumFromJson(Map json) => MsEnum( @@ -102,3 +106,18 @@ Map _$MsEnumValueToJson(MsEnumValue instance) => { 'value': instance.value, }; + +Discriminator _$DiscriminatorFromJson(Map json) => + Discriminator( + propertyName: json['propertyName'] as String? ?? '', + mapping: (json['mapping'] as Map?)?.map( + (k, e) => MapEntry(k, e as String), + ) ?? + {}, + ); + +Map _$DiscriminatorToJson(Discriminator instance) => + { + 'propertyName': instance.propertyName, + 'mapping': instance.mapping, + }; \ No newline at end of file