From 3a47fe2575d84603bc3a80941969879e6db90e2f Mon Sep 17 00:00:00 2001 From: Uladzimir_Paliukhovich Date: Fri, 15 Dec 2023 13:38:46 +0300 Subject: [PATCH] Development (#694) * Fixed some issues * formatted code * updated SDK * Updated SDK and version * Fixed generation of lists of classes * Fixed generation $Items classes * Updated pubspec and changelog * Fixed #524 * Fixed #598 Generation of query enum parameters * Fixed conflicts * Fixed some issues in swaggers * Updated changelog and pubspec * Fix #583, #637, #619 and update readme (#638) * fix #583 and update readme * fix #637 * fix #619 * Fixed generation of some fields * Removed test * Fixed classes named List * Fixed generation of query parameters with ref default type * Fixed generation of DateTime parameters * Fixed generation of responses in some cases * Some fixes * Updated changelog and pubspec * Implemented not nullable fields * Fixed tests * fixed generation of some swaggers * Added ability to return String values * Returned main.dart content * Updated pubspec and changelog * Fixed generation of required and not required fields * Added check for object ref in body * Fixed some things * Fixed tests * Fixed tests * Fixed some things * Updated changelog and pubspec * Removed not needed lines in tests * Fixed generation of nullable responses * Added generation of DateTime * Updated pubspec and changelog * Fixed tests * Fixed #669 Generation models from content schema allof * Fixed #665 generation putIfAbsent for response from content schema * Fixed generation of nullable and required properties * Fixed tests * Fixed some stuff related to nullable properties * Updated changelog and pubspec * Formatted code * Formatted code * Fixed tests * Fixed generation of some enums inside classes * Implemented support of exploded parameters * Pushed constants file * Fixed generation of allOf for request bodies * Implemented overriden_models functionality * Improved overriden models logic * Fixed tests * Updated pubspec and changelog * Removed support of exploded parameters * Fixed generation of patameters contains keywords * Updated pubspec and changelog * Fixed generation of nullable and not nullable fields * Fixed generation of list parameters with specific names * Fixed formurlencoded requests * Revert "Fixed formurlencoded requests" This reverts commit d103118975e7f66ec2cbf10a1f9d28ef935be6eb. * Updated changelog and pubspec * formatted code * Fixed version in changelog * Added generation of writeOnly and readOnly fields as nullable and no required (Issue 487) --------- Co-authored-by: Uladzimir Paliukhovich Co-authored-by: Romain --- CHANGELOG.md | 4 +++ .../swagger_models_generator.dart | 30 +++++++++---------- .../swagger_requests_generator.dart | 3 +- .../responses/swagger_schema.dart | 10 +++++++ .../responses/swagger_schema.g2.dart | 4 +++ pubspec.yaml | 2 +- 6 files changed, 34 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a462b41c..87e38276 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 2.13.3 + +* Some fixes and improvements + # 2.13.2 * Fixed generation key word parameter names ([#685](https://github.com/epam-cross-platform-lab/swagger-dart-code-generator/issues/685)) diff --git a/lib/src/code_generators/swagger_models_generator.dart b/lib/src/code_generators/swagger_models_generator.dart index b30603f2..24f385b6 100644 --- a/lib/src/code_generators/swagger_models_generator.dart +++ b/lib/src/code_generators/swagger_models_generator.dart @@ -471,8 +471,7 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase { final includeIfNullString = generateIncludeIfNullString(); if (typeName != kDynamic && - (prop.isNullable == true || - options.nullableModels.contains(typeName))) { + (prop.shouldBeNullable || options.nullableModels.contains(typeName))) { typeName = typeName.makeNullable(); } @@ -488,6 +487,7 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase { final jsonKeyContent = "@JsonKey(name: '$propertyKey'$includeIfNullString$dateToJsonValue${unknownEnumValue.jsonKey})\n"; + return '\t$jsonKeyContent\tfinal $typeName ${generateFieldName(propertyName)};${unknownEnumValue.fromJson}'; } @@ -598,7 +598,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr String propertyKey, SwaggerSchema prop, ) { - return prop.isNullable == true || + return prop.shouldBeNullable || options.nullableModels.contains(className) || !requiredProperties.contains(propertyKey); } @@ -667,8 +667,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr final jsonKeyContent = "@JsonKey(name: '${_validatePropertyKey(propertyKey)}'$includeIfNullString${unknownEnumValue.jsonKey}$dateToJsonValue)\n"; - if ((prop.isNullable == true || - options.nullableModels.contains(className)) && + if ((prop.shouldBeNullable || options.nullableModels.contains(className)) && !requiredProperties.contains(propertyKey)) { typeName = typeName.makeNullable(); } @@ -734,7 +733,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr final jsonKeyContent = "@JsonKey(name: '${_validatePropertyKey(propertyKey)}'$includeIfNullString${unknownEnumValue.jsonKey})\n"; - if (prop.isNullable == true || + if (prop.shouldBeNullable || options.nullableModels.contains(className) || !requiredProperties.contains(propertyKey)) { typeName = typeName.makeNullable(); @@ -782,7 +781,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr } final isPropertyNullable = options.nullableModels.contains(className) || - refSchema?.isNullable == true || + refSchema?.shouldBeNullable == true || !requiredProperties.contains(propertyKey); final unknownEnumValue = generateEnumValue( @@ -805,15 +804,14 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr final jsonKeyContent = "@JsonKey(name: '${_validatePropertyKey(propertyKey)}'$includeIfNullString${unknownEnumValue.jsonKey})\n"; - if ((prop.isNullable == true || - options.nullableModels.contains(className)) && + if ((prop.shouldBeNullable || options.nullableModels.contains(className)) && !requiredProperties.contains(propertyKey)) { typeName = typeName.makeNullable(); } final propertySchema = allClasses[prop.ref.getUnformattedRef()]; - if (propertySchema?.isNullable == true || + if (propertySchema?.shouldBeNullable == true || isPropertyNullable || options.nullableModels.contains(className)) { typeName = typeName.makeNullable(); @@ -855,8 +853,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr var enumPropertyName = className.capitalize + key.capitalize; - if ((prop.isNullable == true || - options.nullableModels.contains(className)) && + if ((prop.shouldBeNullable || options.nullableModels.contains(className)) && !requiredProperties.contains(propertyKey)) { enumPropertyName = enumPropertyName.makeNullable(); } @@ -1006,7 +1003,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr var listPropertyName = 'List<$typeName>'; - if (prop.isNullable == true || + if (prop.shouldBeNullable || options.nullableModels.contains(className) || !requiredProperties.contains(propertyKey)) { listPropertyName = listPropertyName.makeNullable(); @@ -1079,7 +1076,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr jsonKeyContent += ')\n'; } - if (prop.isNullable == true || + if (prop.shouldBeNullable || options.nullableModels.contains(className) || !requiredProperties.contains(propertyKey)) { typeName = typeName.makeNullable(); @@ -1341,10 +1338,11 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr propertyNames.add(fieldName); final isNullableProperty = options.nullableModels.contains(className) || - value.isNullable == true || + value.shouldBeNullable || !requiredProperties.contains(key); - final isRequiredProperty = requiredProperties.contains(key); + final isRequiredProperty = + !value.shouldBeNullable && requiredProperties.contains(key); if (isRequiredProperty || !isNullableProperty) { results += '\t\t$kRequired this.$fieldName,\n'; diff --git a/lib/src/code_generators/swagger_requests_generator.dart b/lib/src/code_generators/swagger_requests_generator.dart index dc33b125..c9ff9da0 100644 --- a/lib/src/code_generators/swagger_requests_generator.dart +++ b/lib/src/code_generators/swagger_requests_generator.dart @@ -195,8 +195,7 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase { }); final isUrlencoded = parameters.any((p) => - p.type != null && - p.type!.symbol == options.urlencodedFileType); + p.type != null && p.type!.symbol == options.urlencodedFileType); var annotationPath = path; if (options.addBasePathToRequests) { diff --git a/lib/src/swagger_models/responses/swagger_schema.dart b/lib/src/swagger_models/responses/swagger_schema.dart index d086bbc0..5f91fb72 100644 --- a/lib/src/swagger_models/responses/swagger_schema.dart +++ b/lib/src/swagger_models/responses/swagger_schema.dart @@ -25,8 +25,16 @@ class SwaggerSchema { this.hasAdditionalProperties = false, this.msEnum, this.title = '', + this.readOnly = false, + this.writeOnly = false, }); + @JsonKey(name: 'readOnly', defaultValue: false) + bool readOnly; + + @JsonKey(name: 'writeOnly', defaultValue: false) + bool writeOnly; + @JsonKey(name: 'type', defaultValue: '') String type; @@ -83,6 +91,8 @@ class SwaggerSchema { @JsonKey(name: 'nullable', defaultValue: null) bool? isNullable; + bool get shouldBeNullable => isNullable == true || readOnly || writeOnly; + @JsonKey(name: 'schema') SwaggerSchema? schema; diff --git a/lib/src/swagger_models/responses/swagger_schema.g2.dart b/lib/src/swagger_models/responses/swagger_schema.g2.dart index aa58c84e..deb0075f 100644 --- a/lib/src/swagger_models/responses/swagger_schema.g2.dart +++ b/lib/src/swagger_models/responses/swagger_schema.g2.dart @@ -42,6 +42,8 @@ SwaggerSchema _$SwaggerSchemaFromJson(Map json) => : _requiredFromJson(json['required']), description: json['description'] as String? ?? '', title: json['title'] as String? ?? '', + readOnly: json['readOnly'] as bool? ?? false, + writeOnly: json['writeOnly'] as bool? ?? false, enumNames: (json['enumNames'] as List?) ?.map((e) => e as String) .toList(), @@ -73,6 +75,8 @@ Map _$SwaggerSchemaToJson(SwaggerSchema instance) => 'anyOf': instance.anyOf, 'allOf': instance.allOf, 'title': instance.title, + 'readOnly': instance.readOnly, + 'writeOnly': instance.writeOnly, 'additionalProperties': instance.hasAdditionalProperties, 'enumNames': instance.enumNames, }; diff --git a/pubspec.yaml b/pubspec.yaml index a119827f..b2b38e7e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: swagger_dart_code_generator -version: 2.13.2 +version: 2.13.3 homepage: https://github.com/epam-cross-platform-lab/swagger-dart-code-generator repository: https://github.com/epam-cross-platform-lab/swagger-dart-code-generator