diff --git a/README.md b/README.md index bbaebec5..dc8e25d9 100644 --- a/README.md +++ b/README.md @@ -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`: diff --git a/lib/src/code_generators/enum_model.dart b/lib/src/code_generators/enum_model.dart index 51901661..4ce5f070 100644 --- a/lib/src/code_generators/enum_model.dart +++ b/lib/src/code_generators/enum_model.dart @@ -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? ${name.camelCase}) { diff --git a/lib/src/code_generators/swagger_models_generator.dart b/lib/src/code_generators/swagger_models_generator.dart index b4a7760c..c0b02c3b 100644 --- a/lib/src/code_generators/swagger_models_generator.dart +++ b/lib/src/code_generators/swagger_models_generator.dart @@ -424,6 +424,8 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase { typeName = kDynamic; } + final propertyKey = propertyName.replaceAll('\$', '\\\$'); + final unknownEnumValue = generateEnumValue( allEnumNames: allEnumNames, allEnumListNames: allEnumListNames, @@ -431,6 +433,7 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase { typeName: typeName.toString(), defaultValue: prop.defaultValue, isList: false, + isNullable: isNullable(typeName, [], propertyKey, prop), ); final dateToJsonValue = generateToJsonForDate(prop); @@ -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}'; @@ -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); @@ -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'; @@ -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?'; } @@ -547,6 +550,13 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr return ''; } + bool isNullable(String className, Iterable requiredProperties, + String propertyKey, SwaggerSchema prop) { + return options.nullableModels.contains(className) || + !requiredProperties.contains(propertyKey) || + prop.isNullable == true; + } + String nullable( String typeName, String className, @@ -554,13 +564,11 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr 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; @@ -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); @@ -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 = @@ -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)) { @@ -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(); @@ -900,6 +912,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr typeName: typeName, defaultValue: prop.defaultValue, isList: true, + isNullable: false, ); final includeIfNullString = generateIncludeIfNullString(); @@ -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); diff --git a/lib/src/code_generators/swagger_requests_generator.dart b/lib/src/code_generators/swagger_requests_generator.dart index 30b27809..d947c060 100644 --- a/lib/src/code_generators/swagger_requests_generator.dart +++ b/lib/src/code_generators/swagger_requests_generator.dart @@ -143,10 +143,6 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase { return; } - if (options.addBasePathToRequests) { - path = '${swaggerRoot.basePath}$path'; - } - final methodName = _getRequestMethodName( requestType: requestType, swaggerRequest: swaggerRequest, @@ -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( @@ -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( @@ -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; }