Repository: epam-cross-platform-lab/swagger-dart-code-generator
Pull request #677 (2023-11-10 14:16:05)

Development


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4617c87..fc9e769 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,9 @@
+# 2.12.2
+
+* Fixed generation of `nullable` and `required` fields ([#650](https://github.com/epam-cross-platform-lab/swagger-dart-code-generator/issues/650))
+* Fixed generation of `putIfAbsent` for some models ([#665](https://github.com/epam-cross-platform-lab/swagger-dart-code-generator/issues/665))
+* Fixed generation of some border-cased models ([#669](https://github.com/epam-cross-platform-lab/swagger-dart-code-generator/issues/669))
+
 # 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
diff --git a/lib/src/code_generators/swagger_models_generator.dart b/lib/src/code_generators/swagger_models_generator.dart
index 3124eda..6c30a9c 100644
--- a/lib/src/code_generators/swagger_models_generator.dart
+++ b/lib/src/code_generators/swagger_models_generator.dart
@@ -585,12 +585,9 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
     String propertyKey,
     SwaggerSchema prop,
   ) {
-    if (requiredProperties.contains(propertyKey)) {
-      return false;
-    }
-
-    return options.nullableModels.contains(className) ||
-        prop.isNullable == true;
+    return prop.isNullable == true ||
+        options.nullableModels.contains(className) ||
+        !requiredProperties.contains(propertyKey);
   }
 
   String nullable(
@@ -717,14 +714,15 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
       typeName: typeName,
       defaultValue: prop.defaultValue,
       isList: false,
+      className: className,
       isNullable: isNullable(className, requiredProperties, propertyKey, prop),
     );
 
     final jsonKeyContent =
         "@JsonKey(name: '${_validatePropertyKey(propertyKey)}'$includeIfNullString${unknownEnumValue.jsonKey})\n";
 
-    if ((prop.isNullable == true ||
-            options.nullableModels.contains(className)) &&
+    if (prop.isNullable == true ||
+        options.nullableModels.contains(className) ||
         !requiredProperties.contains(propertyKey)) {
       typeName = typeName.makeNullable();
     }
@@ -772,7 +770,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
 
     final isPropertyNullable = options.nullableModels.contains(className) ||
         refSchema?.isNullable == true ||
-        isNullable(className, requiredProperties, propertyKey, prop);
+        !requiredProperties.contains(propertyName);
 
     final unknownEnumValue = generateEnumValue(
       allEnumNames: allEnumNames,
@@ -995,8 +993,8 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
 
     var listPropertyName = 'List<$typeName>';
 
-    if ((prop.isNullable == true ||
-            options.nullableModels.contains(className)) &&
+    if (prop.isNullable == true ||
+        options.nullableModels.contains(className) ||
         !requiredProperties.contains(propertyKey)) {
       listPropertyName = listPropertyName.makeNullable();
     }
@@ -1330,7 +1328,8 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
       propertyNames.add(fieldName);
 
       final isNullableProperty = options.nullableModels.contains(className) ||
-          value.isNullable == true || !requiredProperties.contains(key);
+          value.isNullable == true ||
+          !requiredProperties.contains(key);
 
       final isRequiredProperty = requiredProperties.contains(key);
 
diff --git a/lib/swagger_dart_code_generator.dart b/lib/swagger_dart_code_generator.dart
index 32c0d9f..ebd6cd1 100644
--- a/lib/swagger_dart_code_generator.dart
+++ b/lib/swagger_dart_code_generator.dart
@@ -223,7 +223,7 @@ class SwaggerDartCodeGenerator implements Builder {
       allEnums,
       options,
     );
-    
+
     final imports = codeGenerator.generateImportsContent(
       fileNameWithoutExtension,
       models.contains('@JsonSerializable'),
diff --git a/pubspec.yaml b/pubspec.yaml
index ff33efd..44a11b7 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,6 +1,6 @@
 name: swagger_dart_code_generator
 
-version: 2.12.1
+version: 2.12.2
 
 homepage: https://github.com/epam-cross-platform-lab/swagger-dart-code-generator
 repository: https://github.com/epam-cross-platform-lab/swagger-dart-code-generator


Test patch:
diff --git a/test/generator_tests/enums_generator_test.dart b/test/generator_tests/enums_generator_test.dart
index cba6117..071be26 100644
--- a/test/generator_tests/enums_generator_test.dart
+++ b/test/generator_tests/enums_generator_test.dart
@@ -4,7 +4,6 @@ import 'package:swagger_dart_code_generator/src/models/generator_options.dart';
 import 'package:swagger_dart_code_generator/src/swagger_models/requests/swagger_request_parameter.dart';
 import 'package:test/test.dart';
 
-
 void main() {
   final generator = SwaggerEnumsGeneratorV3(
     GeneratorOptions(
diff --git a/test/generator_tests/models_generator_test.dart b/test/generator_tests/models_generator_test.dart
index aea3a2a..852c6d5 100644
--- a/test/generator_tests/models_generator_test.dart
+++ b/test/generator_tests/models_generator_test.dart
@@ -266,9 +266,7 @@ void main() {
     });
 
     test('Should return validate constructor property', () {
-      final map = <String, SwaggerSchema>{
-        'Animal': SwaggerSchema()
-      };
+      final map = <String, SwaggerSchema>{'Animal': SwaggerSchema()};
       const expectedResult = 'this.animal';
       final result = generator.generateConstructorPropertiesContent(
         className: '',
@@ -315,7 +313,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,
@@ -420,7 +418,7 @@ void main() {
       const jsonKeyExpectedResult =
           "@JsonKey(name: 'Dog', defaultValue: <Object>[])";
 
-      const propertyExpectedResult = 'final List<Object> dog';
+      const propertyExpectedResult = 'final List<Object>? dog';
       final result = generator.generateListPropertyContent(
         propertyName,
         propertyKey,
@@ -480,7 +478,7 @@ void main() {
         {},
       );
 
-      expect(result, contains('final List<TestOriginalRef> dog;'));
+      expect(result, contains('final List<TestOriginalRef>? dog;'));
     });
 
     test('Should return List<Object> by ref', () {
@@ -503,7 +501,7 @@ void main() {
         {},
       );
 
-      expect(result, contains('final List<TestObject> dog;'));
+      expect(result, contains('final List<TestObject>? dog;'));
     });
   });
 
diff --git a/test/generator_tests/test_data.dart b/test/generator_tests/test_data.dart
index 3545306..a78a325 100644
--- a/test/generator_tests/test_data.dart
+++ b/test/generator_tests/test_data.dart
@@ -831,4 +831,4 @@ const carsService = '''
         }
     }
 }
-''';
\ No newline at end of file
+''';