Skip to content

Commit

Permalink
Fix #583, #637, #619 and update readme (#638)
Browse files Browse the repository at this point in the history
* fix #583 and update readme

* fix #637

* fix #619
  • Loading branch information
romainboucher authored Sep 8, 2023
1 parent 6ae1b7d commit ff4d14e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ targets:
| `override_equals_and_hashcode` | `-` | `true` | If need to decrease app size - you can disable generation of `hashcode` and `Equals` method. |
| `overriden_models` | `-` | `false` | List of manually written models that will replace the generated one. These models will not be generated. |
| `use_path_for_request_names` | `true` | `false` | Can be false only if all requests has unique `operationId`. It gives readable names for requests. |
| `addBasePathToRequests` | `true` | `false` | Add swagger base path to all request path. |


It's important to remember that, by default, [build](https://github.com/dart-lang/build) will follow [Dart's package layout conventions](https://dart.dev/tools/pub/package-layout), meaning that only some folders will be considered to parse the input files. So, if you want to reference files from a folder other than `lib/`, make sure you've included it on `sources`:
Expand Down
10 changes: 10 additions & 0 deletions lib/src/code_generators/enum_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ enums.$name ${name.camelCase}FromJson(
return enums.$name.values.firstWhereOrNull((e) => e.value == ${name.camelCase}) ?? defaultValue ?? enums.$name.swaggerGeneratedUnknown;
}
enums.$name? ${name.camelCase}NullableFromJson(
Object? ${name.camelCase},
[enums.$name? defaultValue,]
) {
if(${name.camelCase} == null){
return null;
}
return enums.$name.values.firstWhereOrNull((e) => e.value == ${name.camelCase}) ?? defaultValue;
}
List<$type> ${name.camelCase}ListToJson(
List<enums.$name>? ${name.camelCase}) {
Expand Down
32 changes: 23 additions & 9 deletions lib/src/code_generators/swagger_models_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -424,13 +424,16 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase {
typeName = kDynamic;
}

final propertyKey = propertyName.replaceAll('\$', '\\\$');

final unknownEnumValue = generateEnumValue(
allEnumNames: allEnumNames,
allEnumListNames: allEnumListNames,
propertyName: propertyName,
typeName: typeName.toString(),
defaultValue: prop.defaultValue,
isList: false,
isNullable: isNullable(typeName, [], propertyKey, prop),
);

final dateToJsonValue = generateToJsonForDate(prop);
Expand All @@ -441,8 +444,6 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase {
typeName += '?';
}

final propertyKey = propertyName.replaceAll('\$', '\\\$');

final jsonKeyContent =
"@JsonKey(name: '$propertyKey'$includeIfNullString$dateToJsonValue${unknownEnumValue.jsonKey})\n";
return '\t$jsonKeyContent\tfinal $typeName ${generateFieldName(propertyName)};${unknownEnumValue.fromJson}';
Expand All @@ -455,6 +456,7 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase {
required String typeName,
required dynamic defaultValue,
required bool isList,
required bool isNullable,
String className = '',
}) {
final validatedTypeName = getValidatedClassName(typeName);
Expand Down Expand Up @@ -485,7 +487,7 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase {
: 'ListFromJson';
toJsonSuffix = 'ListToJson';
} else {
fromJsonSuffix = 'FromJson';
fromJsonSuffix = isNullable ? 'NullableFromJson' : 'FromJson';
toJsonSuffix = 'ToJson';
}
final fromJsonFunction = '$fromJsonPrefix$fromJsonSuffix';
Expand Down Expand Up @@ -517,7 +519,8 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase {
'$validatedTypeName.${defaultValueCamelCase.substring(0, defaultValueCamelCase.indexOf('('))}';
}

if (options.classesWithNullabeLists.contains(className) && isList) {
if ((options.classesWithNullabeLists.contains(className) && isList) ||
isNullable) {
returnType = '$returnType?';
}

Expand Down Expand Up @@ -547,20 +550,25 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
return '';
}

bool isNullable(String className, Iterable<String> requiredProperties,
String propertyKey, SwaggerSchema prop) {
return options.nullableModels.contains(className) ||
!requiredProperties.contains(propertyKey) ||
prop.isNullable == true;
}

String nullable(
String typeName,
String className,
Iterable<String> requiredProperties,
String propertyKey,
SwaggerSchema prop,
) {
if(typeName.isEmpty) {
if (typeName.isEmpty) {
return kObject.pascalCase.makeNullable();
}

if (options.nullableModels.contains(className) ||
!requiredProperties.contains(propertyKey) ||
prop.isNullable == true) {

if (isNullable(className, requiredProperties, propertyKey, prop)) {
return typeName.makeNullable();
}
return typeName;
Expand Down Expand Up @@ -605,6 +613,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
typeName: typeName,
defaultValue: prop.defaultValue,
isList: false,
isNullable: isNullable(className, requiredProperties, propertyKey, prop),
);

final dateToJsonValue = generateToJsonForDate(prop);
Expand Down Expand Up @@ -664,6 +673,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
typeName: typeName,
defaultValue: prop.defaultValue,
isList: false,
isNullable: isNullable(className, requiredProperties, propertyKey, prop),
);

final jsonKeyContent =
Expand Down Expand Up @@ -721,6 +731,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
defaultValue: prop.defaultValue,
className: className,
isList: false,
isNullable: isNullable(className, requiredProperties, propertyKey, prop),
);

if (allEnumListNames.contains(typeName)) {
Expand Down Expand Up @@ -770,6 +781,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
typeName: enumName,
defaultValue: prop.defaultValue,
isList: false,
isNullable: isNullable(className, requiredProperties, propertyKey, prop),
);

final includeIfNullString = generateIncludeIfNullString();
Expand Down Expand Up @@ -900,6 +912,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
typeName: typeName,
defaultValue: prop.defaultValue,
isList: true,
isNullable: false,
);

final includeIfNullString = generateIncludeIfNullString();
Expand Down Expand Up @@ -970,6 +983,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
typeName: typeName,
defaultValue: prop.defaultValue,
isList: false,
isNullable: isNullable(className, requiredProperties, propertyKey, prop),
);

final dateToJsonValue = generateToJsonForDate(prop);
Expand Down
15 changes: 8 additions & 7 deletions lib/src/code_generators/swagger_requests_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,6 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase {
return;
}

if (options.addBasePathToRequests) {
path = '${swaggerRoot.basePath}$path';
}

final methodName = _getRequestMethodName(
requestType: requestType,
swaggerRequest: swaggerRequest,
Expand Down Expand Up @@ -197,6 +193,11 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase {
.any((p0) => p0.call([]).toString().contains('symbol=Part'));
});

var annotationPath = path;
if (options.addBasePathToRequests) {
annotationPath = '${swaggerRoot.basePath}$path';
}

final method = Method((m) => m
..optionalParameters.addAll(parameters)
..docs.add(_getCommentsForMethod(
Expand All @@ -206,7 +207,7 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase {
))
..name = methodName
..annotations.addAll(_getMethodAnnotation(
requestType, path, hasOptionalBody, isMultipart))
requestType, annotationPath, hasOptionalBody, isMultipart))
..returns = Reference(returns));

final allModels = _getAllMethodModels(
Expand Down Expand Up @@ -1036,9 +1037,9 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase {
final arrayType = [itemsRef, itemsOriginalRef, itemsType, kObject]
.firstWhere((element) => element?.isNotEmpty == true)!;

final mappedArrayType = kBasicTypesMap[arrayType] ?? arrayType;
final mappedArrayType = kBasicTypesMap[arrayType];

if (mappedArrayType.isEmpty) {
if (mappedArrayType == null) {
return null;
}

Expand Down

0 comments on commit ff4d14e

Please sign in to comment.