diff --git a/lib/src/code_generators/swagger_models_generator.dart b/lib/src/code_generators/swagger_models_generator.dart index c43ae931..56cbf092 100644 --- a/lib/src/code_generators/swagger_models_generator.dart +++ b/lib/src/code_generators/swagger_models_generator.dart @@ -23,11 +23,11 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase { required List allEnums, }); - String generateResponses({ - required SwaggerRoot root, - required String fileName, - required List allEnums, - }); + // String generateResponses({ + // required SwaggerRoot root, + // required String fileName, + // required List allEnums, + // }); String generateRequestBodies({ required SwaggerRoot root, @@ -201,14 +201,26 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase { final neededResponse = responses['200']; - if (neededResponse != null && - neededResponse.schema != null && - neededResponse.schema!.type == kObject && - neededResponse.schema!.properties.isNotEmpty) { + final neededSchema = + neededResponse?.schema ?? neededResponse?.content?.schema; + + if (neededSchema != null && + neededSchema.type == kObject && + neededSchema.properties.isNotEmpty) { final pathText = key.split('/').map((e) => e.pascalCase).join(); final requestText = operation.pascalCase; - results['$pathText$requestText\$Response'] = neededResponse.schema!; + results['$pathText$requestText\$Response'] = neededSchema; + } else { + if (neededSchema?.type == kArray) { + final itemsSchema = neededSchema?.items; + + if (itemsSchema?.properties.isNotEmpty == true) { + final pathText = key.split('/').map((e) => e.pascalCase).join(); + final requestText = operation.pascalCase; + results['$pathText$requestText\$Response'] = neededSchema!; + } + } } }); }); diff --git a/lib/src/code_generators/swagger_requests_generator.dart b/lib/src/code_generators/swagger_requests_generator.dart index 706ae470..c2645bc3 100644 --- a/lib/src/code_generators/swagger_requests_generator.dart +++ b/lib/src/code_generators/swagger_requests_generator.dart @@ -1049,6 +1049,7 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase { required SwaggerResponse swaggerResponse, required String modelPostfix, required SwaggerRoot swaggerRoot, + required String requestName, }) { final content = swaggerResponse.content; @@ -1082,6 +1083,10 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase { typeName = typeName.makeNullable(); } + if (neededSchema.isNullable) { + typeName = typeName.makeNullable(); + } + return typeName; } @@ -1121,6 +1126,13 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase { kObject; return itemsItemsType.asList().asList(); + } else if (content.schema?.items?.properties.isNotEmpty == true) { + + final requestText = requestName.pascalCase; + + final typeName = getValidatedClassName('$requestText\$Response'); + + return typeName.asList(); } else if (itemsType.isNotEmpty) { final parameterType = _mapParameterName(itemsType, itemsFormat, ''); return parameterType.asList(); @@ -1155,6 +1167,10 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase { responses: responses, ); + if (path == '/search/simple/') { + final tt = 0; + } + if (neededResponse == null) { return ''; } @@ -1175,6 +1191,7 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase { swaggerResponse: neededResponse, modelPostfix: modelPostfix, swaggerRoot: swaggerRoot, + requestName: methodName, ) ?? ''; diff --git a/lib/src/code_generators/v3/swagger_models_generator_v3.dart b/lib/src/code_generators/v3/swagger_models_generator_v3.dart index d67844a5..5e992d41 100644 --- a/lib/src/code_generators/v3/swagger_models_generator_v3.dart +++ b/lib/src/code_generators/v3/swagger_models_generator_v3.dart @@ -31,43 +31,43 @@ class SwaggerModelsGeneratorV3 extends SwaggerModelsGenerator { ); } - @override - String generateResponses({ - required SwaggerRoot root, - required String fileName, - required List allEnums, - }) { - final components = root.components; - - if (components == null) { - return ''; - } - - final responses = components.responses; - - var result = {}; - - final allModelNames = - components.schemas.keys.map((e) => getValidatedClassName(e)); - - for (var key in responses.keys) { - if (!allModelNames.contains(key)) { - final schema = responses[key]; - - if (schema != null && schema.ref.isEmpty) { - result.addAll({key: schema}); - } - } - } - - return generateBase( - root: root, - fileName: fileName, - classes: result, - allEnums: allEnums, - generateEnumsMethods: false, - ); - } + // @override + // String generateResponses({ + // required SwaggerRoot root, + // required String fileName, + // required List allEnums, + // }) { + // final components = root.components; + + // if (components == null) { + // return ''; + // } + + // final responses = components.responses; + + // var result = {}; + + // final allModelNames = + // components.schemas.keys.map((e) => getValidatedClassName(e)); + + // for (var key in responses.keys) { + // if (!allModelNames.contains(key)) { + // final schema = responses[key]; + + // if (schema != null && schema.ref.isEmpty) { + // result.addAll({key: schema}); + // } + // } + // } + + // return generateBase( + // root: root, + // fileName: fileName, + // classes: result, + // allEnums: allEnums, + // generateEnumsMethods: false, + // ); + // } @override String generateRequestBodies({ diff --git a/lib/src/swagger_code_generator.dart b/lib/src/swagger_code_generator.dart index 0dcdf270..cecf2b7c 100644 --- a/lib/src/swagger_code_generator.dart +++ b/lib/src/swagger_code_generator.dart @@ -60,17 +60,17 @@ class SwaggerCodeGenerator { _getSwaggerEnumsGenerator(root, options) .generateAllEnums(fileName: fileName, root: root); - String generateResponses( - SwaggerRoot root, - String fileName, - GeneratorOptions options, - List allEnums, - ) => - _getSwaggerModelsGenerator(root, options).generateResponses( - root: root, - fileName: fileName, - allEnums: allEnums, - ); + // String generateResponses( + // SwaggerRoot root, + // String fileName, + // GeneratorOptions options, + // List allEnums, + // ) => + // _getSwaggerModelsGenerator(root, options).generateResponses( + // root: root, + // fileName: fileName, + // allEnums: allEnums, + // ); String generateRequestBodies( SwaggerRoot root, diff --git a/lib/swagger_dart_code_generator.dart b/lib/swagger_dart_code_generator.dart index c28990fb..64148239 100644 --- a/lib/swagger_dart_code_generator.dart +++ b/lib/swagger_dart_code_generator.dart @@ -213,13 +213,6 @@ class SwaggerDartCodeGenerator implements Builder { allEnums, ); - final responses = codeGenerator.generateResponses( - contents, - fileWithoutExtension, - options, - allEnums, - ); - final requestBodies = codeGenerator.generateRequestBodies( contents, fileWithoutExtension, @@ -236,7 +229,7 @@ class SwaggerDartCodeGenerator implements Builder { final imports = codeGenerator.generateImportsContent( fileNameWithoutExtension, - models.isNotEmpty || requestBodies.isNotEmpty || responses.isNotEmpty, + models.isNotEmpty || requestBodies.isNotEmpty, options.buildOnlyModels, enums.isNotEmpty, options.separateModels, @@ -268,7 +261,6 @@ class SwaggerDartCodeGenerator implements Builder { imports, requests, options.separateModels ? '' : models, - options.separateModels ? '' : responses, options.separateModels ? '' : requestBodies, customDecoder, options.separateModels ? '' : dateToJson)); @@ -290,7 +282,6 @@ class SwaggerDartCodeGenerator implements Builder { ///Write models to separate file final formattedModels = _tryFormatCode(_generateSeparateModelsFileContent( models, - responses, requestBodies, fileNameWithoutExtension, dateToJson, @@ -310,7 +301,6 @@ class SwaggerDartCodeGenerator implements Builder { String imports, String requests, String models, - String responses, String requestBodies, String customDecoder, String dateToJson) { @@ -321,8 +311,6 @@ ${options.buildOnlyModels ? '' : requests} $models -$responses - $requestBodies ${options.buildOnlyModels ? '' : customDecoder} @@ -371,7 +359,6 @@ $dateToJson String _generateSeparateModelsFileContent( String models, - String responses, String requestBodies, String fileNameWithoutExtension, String dateToJson, @@ -399,8 +386,6 @@ $overridenModels $models - $responses - $requestBodies $dateToJson diff --git a/test/generator_tests/models_generator_test.dart b/test/generator_tests/models_generator_test.dart index f9255b2e..b865d768 100644 --- a/test/generator_tests/models_generator_test.dart +++ b/test/generator_tests/models_generator_test.dart @@ -543,28 +543,6 @@ void main() { expect(result, equals('')); }); - - test('Should generate class from responses V3', () { - final map = SwaggerRoot.parse(schemasResponsesWithResponse); - final result = generator.generateResponses( - root: map, - fileName: 'fileName', - allEnums: [], - ); - - expect(result, contains('class SpaResponse')); - }); - - test('Should generate class from responses V3 and Schemas', () { - final map = SwaggerRoot.parse(schemasResponsesWithResponseAndSchemas); - final result = generator.generateResponses( - root: map, - fileName: 'fileName', - allEnums: [], - ); - - expect(result, contains('class SpaResponse')); - }); }); group('Tests for models from responses', () {