diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d89f8e6..9314ff8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 2.1.1+2 + +* Fixed issues when content has no application/json child + # 2.1.1+1 * Fixed Issue ([#235](https://github.com/epam-cross-platform-lab/swagger-dart-code-generator/issues/235)) diff --git a/lib/src/code_generators/swagger_models_generator.dart b/lib/src/code_generators/swagger_models_generator.dart index 717a9a3e..56893546 100644 --- a/lib/src/code_generators/swagger_models_generator.dart +++ b/lib/src/code_generators/swagger_models_generator.dart @@ -112,7 +112,8 @@ abstract class SwaggerModelsGenerator { paths.forEach((path, pathValue) { final requests = pathValue as Map; - requests.removeWhere((key, value) => key == 'parameters'); + requests + .removeWhere((key, value) => key == 'parameters' || key == 'summary'); requests.forEach((request, requestValue) { if (options.excludePaths.isNotEmpty && diff --git a/lib/src/code_generators/swagger_requests_generator.dart b/lib/src/code_generators/swagger_requests_generator.dart index 80f91f6d..85d150ba 100644 --- a/lib/src/code_generators/swagger_requests_generator.dart +++ b/lib/src/code_generators/swagger_requests_generator.dart @@ -474,6 +474,18 @@ class SwaggerRequestsGenerator { return result.distinctParameters(); } + bool _isBasicTypeRef(String ref, SwaggerRoot root) { + final schemas = _getAllReusableObjects(root); + + final neededSchema = schemas[ref.getUnformattedRef()]; + + if (neededSchema == null) { + return false; + } + + return kBasicTypes.contains(neededSchema.type); + } + bool _isEnumRef(String ref, SwaggerRoot root) { final schemas = root.components?.schemas ?? {}; schemas.addAll(root.definitions); @@ -507,6 +519,10 @@ class SwaggerRequestsGenerator { return ref.asEnum().asList(); } + if (_isBasicTypeRef(ref, root)) { + return kObject.pascalCase; + } + if (ref.isNotEmpty) { return SwaggerModelsGenerator.getValidatedClassName( ref.withPostfix(modelPostfix)) @@ -524,6 +540,10 @@ class SwaggerRequestsGenerator { return schema.ref.getRef().asEnum(); } + if (_isBasicTypeRef(schema.ref, root)) { + return kObject.pascalCase; + } + return SwaggerModelsGenerator.getValidatedClassName( schema.ref.getRef().withPostfix(modelPostfix)); } @@ -610,10 +630,14 @@ class SwaggerRequestsGenerator { final ref = swaggerResponse.schema?.ref ?? swaggerResponse.ref; if (ref.isNotEmpty) { - final responses = root.components?.responses ?? {}; - final neededResponse = responses[ref.getRef()]; + final allReusableObjects = _getAllReusableObjects(root); + final neededResponse = allReusableObjects[ref.getUnformattedRef()]; - if (neededResponse?.ref.isNotEmpty == true) { + if (neededResponse == null) { + return kObject.pascalCase; + } + + if (neededResponse.ref.isNotEmpty) { return kObject.pascalCase; } @@ -623,6 +647,16 @@ class SwaggerRequestsGenerator { return null; } + Map _getAllReusableObjects(SwaggerRoot root) { + final results = {}; + results.addAll(root.definitions); + results.addAll(root.components?.schemas ?? {}); + results.addAll(root.components?.responses ?? {}); + results.addAll(root.components?.requestBodies ?? {}); + + return results; + } + String? _getReturnTypeFromOriginalRef( SwaggerResponse swaggerResponse, String modelPostfix) { if (swaggerResponse.schema?.originalRef.isNotEmpty == true) { @@ -651,13 +685,23 @@ class SwaggerRequestsGenerator { final schemaRef = content.schema?.ref ?? ''; if (schemaRef.isNotEmpty) { - final neededSchema = swaggerRoot.components?.schemas[schemaRef.getRef()]; + final allRefs = _getAllReusableObjects(swaggerRoot); + final neededSchema = allRefs[schemaRef.getUnformattedRef()]; + + if (neededSchema == null) { + return kObject.pascalCase; + } + + if (kBasicTypes.contains(neededSchema.type)) { + return kObject.pascalCase; + } + final typeName = SwaggerModelsGenerator.getValidatedClassName(schemaRef.getRef()) .withPostfix(modelPostfix); - if (neededSchema?.type == kArray) { - return neededSchema?.items?.ref + if (neededSchema.type == kArray) { + return neededSchema.items?.ref .getRef() .withPostfix(modelPostfix) .asList(); diff --git a/lib/src/extensions/string_extension.dart b/lib/src/extensions/string_extension.dart index c119b0c3..b81a4a5a 100644 --- a/lib/src/extensions/string_extension.dart +++ b/lib/src/extensions/string_extension.dart @@ -26,6 +26,8 @@ extension TypeExtension on String { String getRef() => split('/').last.pascalCase; + String getUnformattedRef() => split('/').last; + String withPostfix(String postfix) => '${this}$postfix'; String asList() => 'List<$this>'; diff --git a/lib/src/swagger_models/swagger_components.dart b/lib/src/swagger_models/swagger_components.dart index 9a93e6e4..ba94ce08 100644 --- a/lib/src/swagger_models/swagger_components.dart +++ b/lib/src/swagger_models/swagger_components.dart @@ -42,18 +42,17 @@ Map _mapResponses(Map? json) { final content = (value as Map)['content'] as Map?; - final appJsonKey = content?.keys.firstWhere( - (element) => element.startsWith('application/json'), - orElse: () => '') ?? - ''; - - if (appJsonKey.isNotEmpty) { - final appJson = content?[appJsonKey] as Map?; - - if (appJson != null && appJson['schema'] != null) { - results[key] = - SwaggerSchema.fromJson(appJson['schema'] as Map); - } + Map? appJson; + + if (content?.length == 1) { + appJson = content?.values.first as Map?; + } else { + appJson = content?['application/json'] as Map?; + } + + if (appJson != null && appJson['schema'] != null) { + results[key] = + SwaggerSchema.fromJson(appJson['schema'] as Map); } }); diff --git a/lib/src/swagger_models/swagger_root.dart b/lib/src/swagger_models/swagger_root.dart index b29bac0b..b04c45fb 100644 --- a/lib/src/swagger_models/swagger_root.dart +++ b/lib/src/swagger_models/swagger_root.dart @@ -64,7 +64,7 @@ Map _mapPaths(Map? paths) { return paths.map((path, pathValue) { final value = pathValue as Map; final parameters = value['parameters'] as List?; - value.removeWhere((key, value) => key == 'parameters'); + value.removeWhere((key, value) => key == 'parameters' || key == 'summary'); return MapEntry( path, diff --git a/pubspec.yaml b/pubspec.yaml index b75d976c..7f6c04fa 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: swagger_dart_code_generator -version: 2.1.1+1 +version: 2.1.1+2 homepage: https://github.com/epam-cross-platform-lab/swagger-dart-code-generator repository: https://github.com/epam-cross-platform-lab/swagger-dart-code-generator