Skip to content

Commit

Permalink
Merge 4c7f4df into 59df331
Browse files Browse the repository at this point in the history
  • Loading branch information
Vovanella95 authored Nov 1, 2023
2 parents 59df331 + 4c7f4df commit ad23e8b
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 63 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 7 additions & 1 deletion lib/src/code_generators/swagger_additions_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResultType>(
body: \$jsonDecoder.decode<Item>(jsonRes.body) as ResultType);
Expand Down
63 changes: 22 additions & 41 deletions lib/src/code_generators/swagger_models_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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(
Expand All @@ -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();
}

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
Expand All @@ -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();
}
Expand Down Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion lib/src/code_generators/swagger_requests_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 0 additions & 4 deletions lib/src/models/generator_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class GeneratorOptions {
this.overridenModels = const [],
this.generateToJsonFor = const [],
this.multipartFileType = 'List<int>',
this.nullableFields = true,
});

/// Build options from a JSON map.
Expand All @@ -50,9 +49,6 @@ class GeneratorOptions {
@JsonKey(defaultValue: true)
final bool withBaseUrl;

@JsonKey(defaultValue: true)
final bool nullableFields;

@JsonKey(defaultValue: false)
final bool addBasePathToRequests;

Expand Down
2 changes: 0 additions & 2 deletions lib/src/models/generator_options.g2.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/src/swagger_models/responses/swagger_schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SwaggerSchema {
this.required = const [],
this.description = '',
this.enumNames,
this.isNullable = false,
this.isNullable = true,
this.hasAdditionalProperties = false,
this.msEnum,
});
Expand Down Expand Up @@ -76,7 +76,7 @@ class SwaggerSchema {
@JsonKey(name: 'properties', defaultValue: {})
Map<String, SwaggerSchema> properties;

@JsonKey(name: 'nullable', defaultValue: false)
@JsonKey(name: 'nullable', defaultValue: true)
bool isNullable;

@JsonKey(name: 'schema')
Expand Down
2 changes: 1 addition & 1 deletion lib/src/swagger_models/responses/swagger_schema.g2.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 8 additions & 10 deletions test/generator_tests/models_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ void main() {
GeneratorOptions(
inputFolder: '',
outputFolder: '',
nullableFields: false,
),
);
final generator2 = SwaggerModelsGeneratorV2(
GeneratorOptions(
inputFolder: '',
outputFolder: '',
nullableFields: false,
),
);

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -422,7 +420,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,
Expand Down Expand Up @@ -482,7 +480,7 @@ void main() {
{},
);

expect(result, contains('final List<TestOriginalRef> dog;'));
expect(result, contains('final List<TestOriginalRef>? dog;'));
});

test('Should return List<Object> by ref', () {
Expand All @@ -505,7 +503,7 @@ void main() {
{},
);

expect(result, contains('final List<TestObject> dog;'));
expect(result, contains('final List<TestObject>? dog;'));
});
});

Expand Down

0 comments on commit ad23e8b

Please sign in to comment.