diff --git a/CHANGELOG.md b/CHANGELOG.md index 3359467f..4617c87e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +# 2.12.1 +* Fixed return type nullability ([#670](https://github.com/epam-cross-platform-lab/swagger-dart-code-generator/issues/670)) +* Fixed generation of DateTime return types + +# 2.11.13 + +* Fixed different issues + # 2.11.12 * Added generation of const constructors diff --git a/lib/src/code_generators/swagger_additions_generator.dart b/lib/src/code_generators/swagger_additions_generator.dart index a14e25da..6b3cab31 100644 --- a/lib/src/code_generators/swagger_additions_generator.dart +++ b/lib/src/code_generators/swagger_additions_generator.dart @@ -198,10 +198,16 @@ class \$JsonSerializableConverter extends chopper.JsonConverter { return chopper.Response(response.base, null, error: response.error); } - if(ResultType == String) { + 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/lib/src/code_generators/swagger_models_generator.dart b/lib/src/code_generators/swagger_models_generator.dart index 431bdcac..9fedf0b3 100644 --- a/lib/src/code_generators/swagger_models_generator.dart +++ b/lib/src/code_generators/swagger_models_generator.dart @@ -443,7 +443,13 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase { final includeIfNullString = generateIncludeIfNullString(); - if (typeName != kDynamic && (prop.isNullable || options.nullableFields)) { + if (typeName != kDynamic && + (prop.isNullable || options.nullableModels.contains(typeName))) { + typeName = typeName.makeNullable(); + } + + if (requiredProperties.isNotEmpty && + !requiredProperties.contains(propertyKey)) { typeName = typeName.makeNullable(); } @@ -564,15 +570,11 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr String propertyKey, SwaggerSchema prop, ) { - if (options.nullableModels.contains(className) || prop.isNullable == true) { - return true; - } - if (requiredProperties.contains(propertyKey)) { return false; } - return options.nullableFields; + return options.nullableModels.contains(className) || prop.isNullable; } String nullable( @@ -639,11 +641,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr final jsonKeyContent = "@JsonKey(name: '${_validatePropertyKey(propertyKey)}'$includeIfNullString${unknownEnumValue.jsonKey}$dateToJsonValue)\n"; - if (prop.isNullable || options.nullableFields) { - typeName = typeName.makeNullable(); - } - - if (requiredProperties.isNotEmpty && + if ((prop.isNullable || options.nullableModels.contains(className)) && !requiredProperties.contains(propertyKey)) { typeName = typeName.makeNullable(); } @@ -708,11 +706,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr final jsonKeyContent = "@JsonKey(name: '${_validatePropertyKey(propertyKey)}'$includeIfNullString${unknownEnumValue.jsonKey})\n"; - if (prop.isNullable || options.nullableFields) { - typeName = typeName.makeNullable(); - } - - if (requiredProperties.isNotEmpty && + if ((prop.isNullable || options.nullableModels.contains(className)) && !requiredProperties.contains(propertyKey)) { typeName = typeName.makeNullable(); } @@ -758,7 +752,8 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr typeName += options.modelPostfix; } - final isPropertyNullable = refSchema?.isNullable == true || + final isPropertyNullable = options.nullableModels.contains(className) || + refSchema?.isNullable == true || isNullable(className, requiredProperties, propertyKey, prop); final unknownEnumValue = generateEnumValue( @@ -781,18 +776,16 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr final jsonKeyContent = "@JsonKey(name: '${_validatePropertyKey(propertyKey)}'$includeIfNullString${unknownEnumValue.jsonKey})\n"; - if (prop.isNullable || options.nullableFields) { - typeName = typeName.makeNullable(); - } - - if (requiredProperties.isNotEmpty && + if ((prop.isNullable || options.nullableModels.contains(className)) && !requiredProperties.contains(propertyKey)) { typeName = typeName.makeNullable(); } final propertySchema = allClasses[prop.ref.getUnformattedRef()]; - if (propertySchema?.isNullable == true || isPropertyNullable) { + if (propertySchema?.isNullable == true || + isPropertyNullable || + options.nullableModels.contains(className)) { typeName = typeName.makeNullable(); } @@ -832,11 +825,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr var enumPropertyName = className.capitalize + key.capitalize; - if (prop.isNullable || options.nullableFields) { - enumPropertyName = enumPropertyName.makeNullable(); - } - - if (requiredProperties.isNotEmpty && + if ((prop.isNullable || options.nullableModels.contains(className)) && !requiredProperties.contains(propertyKey)) { enumPropertyName = enumPropertyName.makeNullable(); } @@ -986,11 +975,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr var listPropertyName = 'List<$typeName>'; - if (prop.isNullable || options.nullableFields) { - listPropertyName = listPropertyName.makeNullable(); - } - - if (requiredProperties.isNotEmpty && + if ((prop.isNullable || options.nullableModels.contains(className)) && !requiredProperties.contains(propertyKey)) { listPropertyName = listPropertyName.makeNullable(); } @@ -1041,6 +1026,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr defaultValue: prop.defaultValue, isList: false, isNullable: isNullable(className, requiredProperties, propertyKey, prop), + className: className, ); final dateToJsonValue = generateToJsonForDate(prop); @@ -1061,11 +1047,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr jsonKeyContent += ')\n'; } - if (prop.isNullable || options.nullableFields) { - typeName = typeName.makeNullable(); - } - - if (requiredProperties.isNotEmpty && + if ((prop.isNullable || options.nullableModels.contains(className)) && !requiredProperties.contains(propertyKey)) { typeName = typeName.makeNullable(); } @@ -1325,9 +1307,8 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr propertyNames.add(fieldName); - final isNullableProperty = options.nullableModels.contains(className) || - options.nullableFields || - value.isNullable; + final isNullableProperty = + options.nullableModels.contains(className) || value.isNullable; final isRequiredProperty = requiredProperties.contains(key); diff --git a/lib/src/code_generators/swagger_requests_generator.dart b/lib/src/code_generators/swagger_requests_generator.dart index 5a4cc87f..2abab3eb 100644 --- a/lib/src/code_generators/swagger_requests_generator.dart +++ b/lib/src/code_generators/swagger_requests_generator.dart @@ -448,8 +448,9 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase { var allModelsString = ''; allModels.toSet().forEach((model) { + final validatedName = getValidatedClassName(model); allModelsString += - 'generatedMapping.putIfAbsent($model, () => $model.fromJsonFactory);\n'; + 'generatedMapping.putIfAbsent($validatedName, () => $validatedName.fromJsonFactory);\n'; }); return Code( diff --git a/lib/src/models/generator_options.dart b/lib/src/models/generator_options.dart index f4c3ca24..4be5e95f 100644 --- a/lib/src/models/generator_options.dart +++ b/lib/src/models/generator_options.dart @@ -37,7 +37,6 @@ class GeneratorOptions { this.overridenModels = const [], this.generateToJsonFor = const [], this.multipartFileType = 'List', - this.nullableFields = true, }); /// Build options from a JSON map. @@ -50,9 +49,6 @@ class GeneratorOptions { @JsonKey(defaultValue: true) final bool withBaseUrl; - @JsonKey(defaultValue: true) - final bool nullableFields; - @JsonKey(defaultValue: false) final bool addBasePathToRequests; diff --git a/lib/src/models/generator_options.g2.dart b/lib/src/models/generator_options.g2.dart index 97a95dca..6050b4a8 100644 --- a/lib/src/models/generator_options.g2.dart +++ b/lib/src/models/generator_options.g2.dart @@ -84,14 +84,12 @@ GeneratorOptions _$GeneratorOptionsFromJson(Map json) => GeneratorOptions( .toList() ?? [], multipartFileType: json['multipart_file_type'] as String? ?? 'List', - nullableFields: json['nullable_fields'] as bool? ?? true, ); Map _$GeneratorOptionsToJson(GeneratorOptions instance) => { 'use_path_for_request_names': instance.usePathForRequestNames, 'with_base_url': instance.withBaseUrl, - 'nullable_fields': instance.nullableFields, 'add_base_path_to_requests': instance.addBasePathToRequests, 'page_width': instance.pageWidth, 'override_to_string': instance.overrideToString, diff --git a/lib/src/swagger_models/responses/swagger_schema.dart b/lib/src/swagger_models/responses/swagger_schema.dart index 579b8ad8..a33d1121 100644 --- a/lib/src/swagger_models/responses/swagger_schema.dart +++ b/lib/src/swagger_models/responses/swagger_schema.dart @@ -21,7 +21,7 @@ class SwaggerSchema { this.required = const [], this.description = '', this.enumNames, - this.isNullable = false, + this.isNullable = true, this.hasAdditionalProperties = false, this.msEnum, }); @@ -76,7 +76,7 @@ class SwaggerSchema { @JsonKey(name: 'properties', defaultValue: {}) Map properties; - @JsonKey(name: 'nullable', defaultValue: false) + @JsonKey(name: 'nullable', defaultValue: true) bool isNullable; @JsonKey(name: 'schema') diff --git a/lib/src/swagger_models/responses/swagger_schema.g2.dart b/lib/src/swagger_models/responses/swagger_schema.g2.dart index 73e1998d..af44df10 100644 --- a/lib/src/swagger_models/responses/swagger_schema.g2.dart +++ b/lib/src/swagger_models/responses/swagger_schema.g2.dart @@ -44,7 +44,7 @@ SwaggerSchema _$SwaggerSchemaFromJson(Map json) => enumNames: (json['enumNames'] as List?) ?.map((e) => e as String) .toList(), - isNullable: json['nullable'] as bool? ?? false, + isNullable: json['nullable'] as bool? ?? true, hasAdditionalProperties: json['additionalProperties'] == null ? false : _additionalsFromJson(json['additionalProperties']), diff --git a/pubspec.yaml b/pubspec.yaml index a620aa67..ff33efd3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: swagger_dart_code_generator -version: 2.11.12 +version: 2.12.1 homepage: https://github.com/epam-cross-platform-lab/swagger-dart-code-generator repository: https://github.com/epam-cross-platform-lab/swagger-dart-code-generator diff --git a/test/generator_tests/models_generator_test.dart b/test/generator_tests/models_generator_test.dart index 496c7457..ef53e563 100644 --- a/test/generator_tests/models_generator_test.dart +++ b/test/generator_tests/models_generator_test.dart @@ -12,14 +12,12 @@ void main() { GeneratorOptions( inputFolder: '', outputFolder: '', - nullableFields: false, ), ); final generator2 = SwaggerModelsGeneratorV2( GeneratorOptions( inputFolder: '', outputFolder: '', - nullableFields: false, ), ); @@ -292,7 +290,7 @@ void main() { const className = 'Animals'; const propertyKey = 'Dog'; const jsonKeyExpectedResult = "\t@JsonKey(name: 'Dog')\n"; - const fieldExpectedResult = '\tfinal Pet dog'; + const fieldExpectedResult = '\tfinal Pet? dog'; final result = generator.generatePropertyContentBySchema( map, propertyName, @@ -317,7 +315,7 @@ void main() { const className = 'Animals'; const jsonKeyExpectedResult = "\t@JsonKey(name: 'Animals')\n"; - const fieldExpectedResult = 'final Pet animals'; + const fieldExpectedResult = 'final Pet? animals'; final result = generator.generatePropertiesContent( SwaggerRoot.empty, map, @@ -344,7 +342,7 @@ void main() { const className = 'Animals'; const jsonKeyExpectedResult = "\t@JsonKey(name: 'Animals')\n"; - const fieldExpectedResult = 'final Pet animals'; + const fieldExpectedResult = 'final Pet? animals'; final result = generator.generatePropertiesContent( SwaggerRoot.empty, map, @@ -369,7 +367,7 @@ void main() { const className = 'Animals'; const jsonKeyExpectedResult = "\t@JsonKey(name: 'animals')\n"; - const fieldExpectedResult = 'final Pet animals'; + const fieldExpectedResult = 'final Pet? animals'; final result = generator.generatePropertiesContent( SwaggerRoot.empty, map, @@ -394,7 +392,7 @@ void main() { const className = 'Animals'; const jsonKeyExpectedResult = "\t@JsonKey(name: '\\\$with')\n"; - const fieldExpectedResult = 'final Pet \$with'; + const fieldExpectedResult = 'final Pet? \$with'; final result = generator.generatePropertiesContent( SwaggerRoot.empty, map, @@ -422,7 +420,7 @@ void main() { const jsonKeyExpectedResult = "@JsonKey(name: 'Dog', defaultValue: [])"; - const propertyExpectedResult = 'final List dog'; + const propertyExpectedResult = 'final List? dog'; final result = generator.generateListPropertyContent( propertyName, propertyKey, @@ -482,7 +480,7 @@ void main() { {}, ); - expect(result, contains('final List dog;')); + expect(result, contains('final List? dog;')); }); test('Should return List by ref', () { @@ -505,7 +503,7 @@ void main() { {}, ); - expect(result, contains('final List dog;')); + expect(result, contains('final List? dog;')); }); });