Skip to content

Commit

Permalink
Development (#664)
Browse files Browse the repository at this point in the history
* 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

---------

Co-authored-by: Uladzimir Paliukhovich <[email protected]>
Co-authored-by: Romain <[email protected]>
  • Loading branch information
3 people authored Oct 20, 2023
1 parent f9af93a commit 59df331
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 22 deletions.
76 changes: 58 additions & 18 deletions lib/src/code_generators/swagger_models_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase {
String propertyName,
List<String> allEnumNames,
List<String> allEnumListNames,
List<String> requiredProperties,
) {
var typeName = '';

Expand Down Expand Up @@ -446,6 +447,11 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase {
typeName = typeName.makeNullable();
}

if (requiredProperties.isNotEmpty &&
!requiredProperties.contains(propertyKey)) {
typeName = typeName.makeNullable();
}

final jsonKeyContent =
"@JsonKey(name: '$propertyKey'$includeIfNullString$dateToJsonValue${unknownEnumValue.jsonKey})\n";
return '\t$jsonKeyContent\tfinal $typeName ${generateFieldName(propertyName)};${unknownEnumValue.fromJson}';
Expand Down Expand Up @@ -558,8 +564,15 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
String propertyKey,
SwaggerSchema prop,
) {
return options.nullableModels.contains(className) ||
prop.isNullable == true;
if (options.nullableModels.contains(className) || prop.isNullable == true) {
return true;
}

if (requiredProperties.contains(propertyKey)) {
return false;
}

return options.nullableFields;
}

String nullable(
Expand Down Expand Up @@ -627,8 +640,12 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
"@JsonKey(name: '${_validatePropertyKey(propertyKey)}'$includeIfNullString${unknownEnumValue.jsonKey}$dateToJsonValue)\n";

if (prop.isNullable || options.nullableFields) {
typeName =
nullable(typeName, className, requiredProperties, propertyKey, prop);
typeName = typeName.makeNullable();
}

if (requiredProperties.isNotEmpty &&
!requiredProperties.contains(propertyKey)) {
typeName = typeName.makeNullable();
}

return '\t$jsonKeyContent\tfinal $typeName ${generateFieldName(propertyName)};${unknownEnumValue.fromJson}';
Expand Down Expand Up @@ -692,8 +709,12 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
"@JsonKey(name: '${_validatePropertyKey(propertyKey)}'$includeIfNullString${unknownEnumValue.jsonKey})\n";

if (prop.isNullable || options.nullableFields) {
typeName =
nullable(typeName, className, requiredProperties, propertyKey, prop);
typeName = typeName.makeNullable();
}

if (requiredProperties.isNotEmpty &&
!requiredProperties.contains(propertyKey)) {
typeName = typeName.makeNullable();
}

return '\t$jsonKeyContent\tfinal $typeName $propertyName;${unknownEnumValue.fromJson}';
Expand Down Expand Up @@ -761,8 +782,12 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
"@JsonKey(name: '${_validatePropertyKey(propertyKey)}'$includeIfNullString${unknownEnumValue.jsonKey})\n";

if (prop.isNullable || options.nullableFields) {
typeName =
nullable(typeName, className, requiredProperties, propertyKey, prop);
typeName = typeName.makeNullable();
}

if (requiredProperties.isNotEmpty &&
!requiredProperties.contains(propertyKey)) {
typeName = typeName.makeNullable();
}

final propertySchema = allClasses[prop.ref.getUnformattedRef()];
Expand Down Expand Up @@ -808,8 +833,12 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
var enumPropertyName = className.capitalize + key.capitalize;

if (prop.isNullable || options.nullableFields) {
enumPropertyName = nullable(
enumPropertyName, className, requiredProperties, propertyKey, prop);
enumPropertyName = enumPropertyName.makeNullable();
}

if (requiredProperties.isNotEmpty &&
!requiredProperties.contains(propertyKey)) {
enumPropertyName = enumPropertyName.makeNullable();
}

return '''
Expand Down Expand Up @@ -913,7 +942,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
List<String> allEnumNames,
List<String> allEnumListNames,
Map<String, String> basicTypesMap,
List<String> requiredParameters,
List<String> requiredProperties,
Map<String, SwaggerSchema> allClasses,
) {
final typeName = _generateListPropertyTypeName(
Expand Down Expand Up @@ -958,8 +987,12 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
var listPropertyName = 'List<$typeName>';

if (prop.isNullable || options.nullableFields) {
listPropertyName = nullable(
listPropertyName, className, requiredParameters, propertyKey, prop);
listPropertyName = listPropertyName.makeNullable();
}

if (requiredProperties.isNotEmpty &&
!requiredProperties.contains(propertyKey)) {
listPropertyName = listPropertyName.makeNullable();
}

return '$jsonKeyContent final $listPropertyName ${generateFieldName(propertyName)};${unknownEnumValue.fromJson}';
Expand Down Expand Up @@ -1029,8 +1062,12 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
}

if (prop.isNullable || options.nullableFields) {
typeName =
nullable(typeName, className, requiredProperties, propertyKey, prop);
typeName = typeName.makeNullable();
}

if (requiredProperties.isNotEmpty &&
!requiredProperties.contains(propertyKey)) {
typeName = typeName.makeNullable();
}

return '\t$jsonKeyContent final $typeName $propertyName;${unknownEnumValue.fromJson}';
Expand Down Expand Up @@ -1189,6 +1226,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
propertyName,
allEnumNames,
allEnumListNames,
requiredProperties,
));
}
}
Expand Down Expand Up @@ -1291,10 +1329,12 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
options.nullableFields ||
value.isNullable;

if (isNullableProperty) {
results += '\t\tthis.$fieldName,\n';
} else {
final isRequiredProperty = requiredProperties.contains(key);

if (isRequiredProperty || !isNullableProperty) {
results += '\t\t$kRequired this.$fieldName,\n';
} else {
results += '\t\tthis.$fieldName,\n';
}
});

Expand Down
2 changes: 1 addition & 1 deletion lib/src/code_generators/swagger_requests_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase {
if (ref != null) {
final neededSchema = root.allSchemas[ref.getUnformattedRef()];

if (kBasicTypesMap.containsKey(neededSchema?.type)) {
if (neededSchema?.type != 'object' && kBasicTypesMap.containsKey(neededSchema?.type)) {
return kBasicTypesMap[neededSchema?.type]!;
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/models/generator_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class GeneratorOptions {
this.overridenModels = const [],
this.generateToJsonFor = const [],
this.multipartFileType = 'List<int>',
this.nullableFields = false,
this.nullableFields = true,
});

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

@JsonKey(defaultValue: false)
@JsonKey(defaultValue: true)
final bool nullableFields;

@JsonKey(defaultValue: false)
Expand Down
2 changes: 1 addition & 1 deletion lib/src/models/generator_options.g2.dart

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

5 changes: 5 additions & 0 deletions test/generator_tests/models_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ void main() {
GeneratorOptions(
inputFolder: '',
outputFolder: '',
nullableFields: false,
),
);
final generator2 = SwaggerModelsGeneratorV2(
GeneratorOptions(
inputFolder: '',
outputFolder: '',
nullableFields: false,
),
);

Expand Down Expand Up @@ -165,6 +167,7 @@ void main() {
propertyName,
[],
[],
[],
);

expect(result, contains(jsonKeyExpendedResult));
Expand All @@ -182,6 +185,7 @@ void main() {
propertyName,
[],
[],
[],
);

expect(result, contains(', includeIfNull: false'));
Expand All @@ -195,6 +199,7 @@ void main() {
propertyName,
[],
[],
[],
);

expect(result.contains(', includeIfNull: false'), equals(false));
Expand Down

0 comments on commit 59df331

Please sign in to comment.