Skip to content

Commit

Permalink
Version/2.10.3 (#599)
Browse files Browse the repository at this point in the history
* Fixed #553 Generation putIfAbsent for names begins with "Map"

* Removed not needed property

* Removed toJson generation

* Returned generation of toJson()

* Implemented toJson only for some models feature

* Fixed some errors

* Added some fixes

* Added createToJson: false for models without toJson

* Fixed #554 generation of putIfAbsent for not needed schemas

* Updated pubspec and changelog

* Reverted change in analysis_options

* Fixed warnings

* Fixed generation of multipart data

* Fixed parameter type generation

* Changed dynamic to Object

* Some fixes

* Add recognition of int64 as int

* Fixed #571

* Fixed #572

* Fixed #570

* Fix #576

* Updated changelog

* Some fixed in configs

* Reverted changes

* implemented support of multipart for border cases

* Added fix for partFile parameters

* Updated pubspec and changelog

* Formatted lib folder

* Fixed warning

* Updated libs
Fixed generation of with base url

* Small fix

* Fixed dependencies

* Added support of nullabe responses

* Fixed #579

* Removed not needed tests

---------

Co-authored-by: Uladzimir Paliukhovich <[email protected]>
  • Loading branch information
Vovanella95 and Uladzimir Paliukhovich authored May 2, 2023
1 parent 5e73551 commit 947f63e
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 96 deletions.
32 changes: 22 additions & 10 deletions lib/src/code_generators/swagger_models_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase {
required List<EnumModel> allEnums,
});

String generateResponses({
required SwaggerRoot root,
required String fileName,
required List<EnumModel> allEnums,
});
// String generateResponses({
// required SwaggerRoot root,
// required String fileName,
// required List<EnumModel> allEnums,
// });

String generateRequestBodies({
required SwaggerRoot root,
Expand Down Expand Up @@ -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!;
}
}
}
});
});
Expand Down
17 changes: 17 additions & 0 deletions lib/src/code_generators/swagger_requests_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,7 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase {
required SwaggerResponse swaggerResponse,
required String modelPostfix,
required SwaggerRoot swaggerRoot,
required String requestName,
}) {
final content = swaggerResponse.content;

Expand Down Expand Up @@ -1082,6 +1083,10 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase {
typeName = typeName.makeNullable();
}

if (neededSchema.isNullable) {
typeName = typeName.makeNullable();
}

return typeName;
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -1155,6 +1167,10 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase {
responses: responses,
);

if (path == '/search/simple/') {
final tt = 0;
}

if (neededResponse == null) {
return '';
}
Expand All @@ -1175,6 +1191,7 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase {
swaggerResponse: neededResponse,
modelPostfix: modelPostfix,
swaggerRoot: swaggerRoot,
requestName: methodName,
) ??
'';

Expand Down
74 changes: 37 additions & 37 deletions lib/src/code_generators/v3/swagger_models_generator_v3.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,43 +31,43 @@ class SwaggerModelsGeneratorV3 extends SwaggerModelsGenerator {
);
}

@override
String generateResponses({
required SwaggerRoot root,
required String fileName,
required List<EnumModel> allEnums,
}) {
final components = root.components;

if (components == null) {
return '';
}

final responses = components.responses;

var result = <String, SwaggerSchema>{};

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<EnumModel> allEnums,
// }) {
// final components = root.components;

// if (components == null) {
// return '';
// }

// final responses = components.responses;

// var result = <String, SwaggerSchema>{};

// 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({
Expand Down
22 changes: 11 additions & 11 deletions lib/src/swagger_code_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ class SwaggerCodeGenerator {
_getSwaggerEnumsGenerator(root, options)
.generateAllEnums(fileName: fileName, root: root);

String generateResponses(
SwaggerRoot root,
String fileName,
GeneratorOptions options,
List<EnumModel> allEnums,
) =>
_getSwaggerModelsGenerator(root, options).generateResponses(
root: root,
fileName: fileName,
allEnums: allEnums,
);
// String generateResponses(
// SwaggerRoot root,
// String fileName,
// GeneratorOptions options,
// List<EnumModel> allEnums,
// ) =>
// _getSwaggerModelsGenerator(root, options).generateResponses(
// root: root,
// fileName: fileName,
// allEnums: allEnums,
// );

String generateRequestBodies(
SwaggerRoot root,
Expand Down
17 changes: 1 addition & 16 deletions lib/swagger_dart_code_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,6 @@ class SwaggerDartCodeGenerator implements Builder {
allEnums,
);

final responses = codeGenerator.generateResponses(
contents,
fileWithoutExtension,
options,
allEnums,
);

final requestBodies = codeGenerator.generateRequestBodies(
contents,
fileWithoutExtension,
Expand All @@ -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,
Expand Down Expand Up @@ -268,7 +261,6 @@ class SwaggerDartCodeGenerator implements Builder {
imports,
requests,
options.separateModels ? '' : models,
options.separateModels ? '' : responses,
options.separateModels ? '' : requestBodies,
customDecoder,
options.separateModels ? '' : dateToJson));
Expand All @@ -290,7 +282,6 @@ class SwaggerDartCodeGenerator implements Builder {
///Write models to separate file
final formattedModels = _tryFormatCode(_generateSeparateModelsFileContent(
models,
responses,
requestBodies,
fileNameWithoutExtension,
dateToJson,
Expand All @@ -310,7 +301,6 @@ class SwaggerDartCodeGenerator implements Builder {
String imports,
String requests,
String models,
String responses,
String requestBodies,
String customDecoder,
String dateToJson) {
Expand All @@ -321,8 +311,6 @@ ${options.buildOnlyModels ? '' : requests}
$models
$responses
$requestBodies
${options.buildOnlyModels ? '' : customDecoder}
Expand Down Expand Up @@ -371,7 +359,6 @@ $dateToJson

String _generateSeparateModelsFileContent(
String models,
String responses,
String requestBodies,
String fileNameWithoutExtension,
String dateToJson,
Expand Down Expand Up @@ -399,8 +386,6 @@ $overridenModels
$models
$responses
$requestBodies
$dateToJson
Expand Down
22 changes: 0 additions & 22 deletions test/generator_tests/models_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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', () {
Expand Down

0 comments on commit 947f63e

Please sign in to comment.