diff --git a/CHANGELOG.md b/CHANGELOG.md index fb23fcc9..9489006d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +# 2.3.5 + +* Fixed Issue ([#263](https://github.com/epam-cross-platform-lab/swagger-dart-code-generator/issues/263)) +* Fixed generation of Enums, defined in /parameters/ and used in request parameters + +# 2.3.4+1 + +* Fixed fromJson method for integer Enum fields in models + # 2.3.4 * Fixed Issue ([#268](https://github.com/epam-cross-platform-lab/swagger-dart-code-generator/issues/268)) diff --git a/lib/src/code_generators/swagger_enums_generator.dart b/lib/src/code_generators/swagger_enums_generator.dart index b0888c71..77528212 100644 --- a/lib/src/code_generators/swagger_enums_generator.dart +++ b/lib/src/code_generators/swagger_enums_generator.dart @@ -148,15 +148,24 @@ $enumsFromRequestBodies final map = jsonDecode(swagger) as Map; final swaggerRoot = SwaggerRoot.fromJson(map); + final definedParameters = {}; + definedParameters.addAll(swaggerRoot.parameters); + definedParameters.addAll(swaggerRoot.components?.parameters ?? {}); + //Link defined parameters with requests swaggerRoot.paths.forEach((String path, SwaggerPath swaggerPath) { swaggerPath.requests .forEach((String requestType, SwaggerRequest swaggerRequest) { - swaggerRequest.parameters = swaggerRequest.parameters - .map((SwaggerRequestParameter parameter) => - getOriginalOrOverriddenRequestParameter(parameter, - swaggerRoot.components?.parameters.values.toList() ?? [])) - .toList(); + swaggerRequest.parameters = + swaggerRequest.parameters.map((SwaggerRequestParameter parameter) { + if (definedParameters + .containsKey(parameter.ref.getUnformattedRef())) { + return definedParameters[parameter.ref.getUnformattedRef()]!; + } + + return getOriginalOrOverriddenRequestParameter(parameter, + swaggerRoot.components?.parameters.values.toList() ?? []); + }).toList(); }); }); diff --git a/lib/src/code_generators/swagger_models_generator.dart b/lib/src/code_generators/swagger_models_generator.dart index f4bfe0d0..4a150974 100644 --- a/lib/src/code_generators/swagger_models_generator.dart +++ b/lib/src/code_generators/swagger_models_generator.dart @@ -297,7 +297,7 @@ abstract class SwaggerModelsGenerator { if (exceptionWords.contains(result.camelCase) || kBasicTypes.contains(result.camelCase)) { - return '\$$parameterName'; + return '\$$result'; } if (result.isEmpty) { @@ -963,17 +963,26 @@ String? ${neededName.camelCase}ToJson(enums.$neededName? ${neededName.camelCase} return enums.\$${neededName}Map[${neededName.camelCase}]; } -enums.$neededName ${neededName.camelCase}FromJson(String? ${neededName.camelCase}) { +enums.$neededName ${neededName.camelCase}FromJson(Object? ${neededName.camelCase}) { - if(${neededName.camelCase} == null) +if(${neededName.camelCase} is int) { - return enums.$neededName.swaggerGeneratedUnknown; + return enums.\$${neededName}Map.entries + .firstWhere((element) => element.value$toLowerCaseString == ${neededName.camelCase}.toString(), + orElse: () => const MapEntry(enums.$neededName.swaggerGeneratedUnknown, '')) + .key; } - return enums.\$${neededName}Map.entries +if(${neededName.camelCase} is String) + { + return enums.\$${neededName}Map.entries .firstWhere((element) => element.value$toLowerCaseString == ${neededName.camelCase}$toLowerCaseString, orElse: () => const MapEntry(enums.$neededName.swaggerGeneratedUnknown, '')) .key; + + } + + return enums.$neededName.swaggerGeneratedUnknown; } List ${neededName.camelCase}ListToJson( diff --git a/lib/src/code_generators/swagger_requests_generator.dart b/lib/src/code_generators/swagger_requests_generator.dart index a1842daf..fbd42684 100644 --- a/lib/src/code_generators/swagger_requests_generator.dart +++ b/lib/src/code_generators/swagger_requests_generator.dart @@ -75,22 +75,18 @@ class SwaggerRequestsGenerator { ..returns = Reference(className) ..name = 'create' ..static = true - ..optionalParameters.add( - Parameter( - (p) => p - ..named = true - ..type = Reference('ChopperClient?') - ..name = 'client', - ) - ) - ..optionalParameters.add( - Parameter( - (p) => p - ..named = true - ..type = Reference('String?') - ..name = 'baseUrl', - ) - ) + ..optionalParameters.add(Parameter( + (p) => p + ..named = true + ..type = Reference('ChopperClient?') + ..name = 'client', + )) + ..optionalParameters.add(Parameter( + (p) => p + ..named = true + ..type = Reference('String?') + ..name = 'baseUrl', + )) ..body = Code(body), ); } @@ -364,7 +360,7 @@ class SwaggerRequestsGenerator { .join(); final result = - '$pathString${requestType.pascalCase}${SwaggerModelsGenerator.getValidatedClassName(parameterName)}'; + '${SwaggerModelsGenerator.getValidatedClassName(pathString)}${requestType.pascalCase}${SwaggerModelsGenerator.getValidatedClassName(parameterName)}'; return result.asEnum(); } diff --git a/pubspec.yaml b/pubspec.yaml index d8099849..58bd6cde 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: swagger_dart_code_generator -version: 2.3.4 +version: 2.3.5 homepage: https://github.com/epam-cross-platform-lab/swagger-dart-code-generator repository: https://github.com/epam-cross-platform-lab/swagger-dart-code-generator diff --git a/test/generator_tests/models_generator_test.dart b/test/generator_tests/models_generator_test.dart index ed0387d3..7617d4b0 100644 --- a/test/generator_tests/models_generator_test.dart +++ b/test/generator_tests/models_generator_test.dart @@ -519,8 +519,8 @@ void main() { }; const className = 'Animals'; - const jsonKeyExpectedResult = "\t@JsonKey(name: '\$with')\n"; - const fieldExpectedResult = 'final Pet? \$with'; + const jsonKeyExpectedResult = "\t@JsonKey(name: '\$With')\n"; + const fieldExpectedResult = 'final Pet? \$With'; final result = generator.generatePropertiesContent( carsService, map,