Skip to content

Commit

Permalink
2.11.4 (#617)
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

* CUSTOM_IMPORT_AND_RETURN_TYPE : DONE

* Added support of nullabe responses

* Fixed #579

* Removed not needed tests

* Fixed generation of request parameters

* Some refactoring of generator

* Some improvements and refactoring

* Some fixes and refactoring

* Updated version in pubspec

* Changed string postfix extension

* Updated changelog

* Fixed warnings

* Fixed generation of classes from list properties of another classes

* Fixed generation of number classes

* Updated pubspec

* Some fixes and refactoring

* Some fixes and improvements

* Some fixes

* Fixed models generation

* Updated pubspec and changelog

* Fixed generation of complex models

* Updated pubspec and changelog

* Fixed generation of allOf refs

* Updated pubspec and changelog

* Resolved conflicts

* Some fixes

* Some fixes and refactoring

---------

Co-authored-by: Uladzimir Paliukhovich <uladzimir_paliukhovich@epam.com>
Co-authored-by: Yauheni_Navakolski <>
  • Loading branch information
Vovanella95 and Uladzimir Paliukhovich authored Jun 7, 2023
1 parent 35c4a75 commit 9a97e6a
Show file tree
Hide file tree
Showing 17 changed files with 187 additions and 220 deletions.
29 changes: 29 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
# 2.11.6

* Fixed generation of anyOf ref properties ([#613](https://github.com/epam-cross-platform-lab/swagger-dart-code-generator/issues/613))

# 2.11.5

* Fixed generation of very complex models inside of arrays inside of models etc.

# 2.11.4

* Fixed generation of models without properties and sample types

# 2.11.3

* Some fixed and refactoring

# 2.11.2

* Some fixed and refactoring

# 2.11.1

* Fixed generation of models with type `number`

# 2.11.0

* Code refactoring
* ***Breaking*** Changed naming of Responses, RequestBodies

# 2.10.5

* Added converter as optional parameter for create()
Expand Down
2 changes: 0 additions & 2 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
include: package:lints/recommended.yaml

analyzer:
strong-mode:
implicit-casts: false
exclude:
- example/**/*.dart
- lib/**/*.g2.dart
Expand Down
3 changes: 3 additions & 0 deletions lib/src/code_generators/swagger_additions_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ ${options.overrideToString ? "import 'dart:convert';" : ''}
if (enumsImport.isNotEmpty) {
result.write(enumsImport);
}
for (var element in options.importPaths) {
result.write('import \'$element\';');
}

if (enumsExport.isNotEmpty) {
result.write(enumsExport);
Expand Down
29 changes: 23 additions & 6 deletions lib/src/code_generators/swagger_enums_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,24 @@ abstract class SwaggerEnumsGenerator extends SwaggerGeneratorBase {
requestBodies.addAll(
SwaggerModelsGeneratorV2(options).getRequestBodiesFromRequests(root));

final formattedRequestBodies = <String, SwaggerSchema>{};
requestBodies.forEach((key, value) {
formattedRequestBodies['$key\$RequestBody'] = value;
});

final responses = root.components?.responses ?? {};
final formattedResponses = <String, SwaggerSchema>{};
responses.forEach((key, value) {
formattedResponses['$key\$Response'] = value;
});

final definitions = root.components?.schemas ?? {};
definitions.addAll(root.definitions);

final enumsFromRequests = generateEnumsContentFromRequests(root, fileName);
final enumsFromResponses = generateEnumsFromSchemaMap(responses);
final enumsFromRequestBodies = generateEnumsFromSchemaMap(requestBodies);
final enumsFromResponses = generateEnumsFromSchemaMap(formattedResponses);
final enumsFromRequestBodies =
generateEnumsFromSchemaMap(formattedRequestBodies);

final enumsFromClasses = definitions.keys
.map((String className) {
Expand Down Expand Up @@ -147,7 +158,8 @@ ${allEnums.map((e) => e.toString()).join('\n')}
.forEach((String requestType, SwaggerRequest swaggerRequest) {
final successResponse = SwaggerRequestsGenerator.getSuccessedResponse(
responses: swaggerRequest.responses);
final successResponseSchema = successResponse?.schema;
final successResponseSchema =
successResponse?.schema ?? successResponse?.content?.schema;

if (successResponseSchema != null) {
final responseEnums = generateEnumsFromSchemaMap({
Expand All @@ -157,12 +169,17 @@ ${allEnums.map((e) => e.toString()).join('\n')}
result.addAll(responseEnums);
}

if (swaggerRequest.parameters.isEmpty) {
final parameters = [
...swaggerPath.parameters,
...swaggerRequest.parameters,
];

if (parameters.isEmpty) {
return;
}

for (var p = 0; p < swaggerRequest.parameters.length; p++) {
final swaggerRequestParameter = swaggerRequest.parameters[p];
for (var p = 0; p < parameters.length; p++) {
final swaggerRequestParameter = parameters[p];

var name = generateRequestEnumName(
path, requestType, swaggerRequestParameter.name);
Expand Down
6 changes: 2 additions & 4 deletions lib/src/code_generators/swagger_generator_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ abstract class SwaggerGeneratorBase {
}

if (exceptionWords.contains(className)) {
return 'Object';
className = '\$$className';
}

if (className.isEmpty) {
Expand Down Expand Up @@ -165,9 +165,7 @@ abstract class SwaggerGeneratorBase {
final parameterClassName = swaggerRequestParameter.ref.split('/').last;

final neededParameter = definedParameters.firstWhere(
(SwaggerRequestParameter element) =>
element.name == parameterClassName ||
element.key == parameterClassName,
(SwaggerRequestParameter element) => element.key == parameterClassName,
orElse: () => swaggerRequestParameter);

return neededParameter;
Expand Down
79 changes: 47 additions & 32 deletions lib/src/code_generators/swagger_models_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,6 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase {
required List<EnumModel> allEnums,
});

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

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

String getExtendsString(SwaggerSchema schema);

List<String> getAllListEnumNames(SwaggerRoot root);
Expand All @@ -58,7 +46,11 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase {
return '';
}

if (kBasicTypes.contains(schema.type.toLowerCase())) {
if (schema.ref.isNotEmpty) {
return '';
}

if (kBasicSwaggerTypes.contains(schema.type.toLowerCase())) {
return '';
}

Expand All @@ -70,8 +62,12 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase {
return 'class $className {}';
}

if (schema.type == kObject && schema.anyOf.isNotEmpty) {
return 'typedef $className = Map<String, dynamic>;';
if (schema.anyOf.isNotEmpty) {
if (schema.type == kObject) {
return 'typedef $className = Map<String, dynamic>;';
} else {
return 'typedef $className = Object;';
}
}

if (schema.type == 'array') {
Expand All @@ -81,7 +77,8 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase {
if (items.hasRef) {
final ref = items.ref;

final itemSchema = schemas[ref.getUnformattedRef()];
final itemSchema =
allClasses[getValidatedClassName(ref.getUnformattedRef())];

if (itemSchema != null && kBasicTypes.contains(itemSchema.type)) {
return 'typedef $className = List<${kBasicTypesMap[itemSchema.type]}>;';
Expand Down Expand Up @@ -145,11 +142,28 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase {
final result = <String, SwaggerSchema>{};

classes.forEach((classKey, schema) {
final properties = schema.properties;
final properties = {
...schema.properties,
...schema.items?.properties ?? {},
};

for (var element in schema.allOf) {
properties.addAll(element.properties);

if (element.ref.isNotEmpty) {
final neededClass = classes[element.ref.getUnformattedRef()];
properties.addAll(neededClass?.properties ?? {});
}
}

final shouldUseItemsProperties =
schema.items?.properties.isNotEmpty == true;

properties.forEach((propertyKey, propSchema) {
final itemPart = shouldUseItemsProperties ? '\$Item' : '';

final innerClassName =
'${getValidatedClassName(classKey)}\$${getValidatedClassName(propertyKey)}';
'${getValidatedClassName(classKey)}$itemPart\$${getValidatedClassName(propertyKey)}';

if (propSchema.properties.isNotEmpty) {
result[innerClassName] = propSchema;
Expand All @@ -158,7 +172,8 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase {
final items = propSchema.items;

if (items != null && items.properties.isNotEmpty) {
result[innerClassName] = propSchema;
propSchema.type = 'object';
result['$innerClassName\$Item'] = propSchema;
}
});

Expand Down Expand Up @@ -257,22 +272,18 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase {
return '';
}

final allClasses = {
...root.definitions,
...root.components?.responses ?? {},
...root.components?.schemas ?? {},
};
final currentClass = classes[className]!;

return generateModelClassContent(
root,
className.pascalCase,
classes[className]!,
currentClass,
classes,
options.defaultValuesMap,
options.classesWithNullabeLists,
allEnums.map((e) => e.name).toList(),
allEnumListNames,
allClasses,
classes,
);
}).join('\n');

Expand Down Expand Up @@ -666,9 +677,9 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
final parameterName = prop.ref.split('/').last;

String typeName;
final refSchema = allClasses[parameterName];
final refSchema = allClasses[getValidatedClassName(parameterName)];
if (kBasicSwaggerTypes.contains(refSchema?.type) &&
allClasses[parameterName]?.isEnum != true) {
allClasses[getValidatedClassName(parameterName)]?.isEnum != true) {
if (refSchema?.format == 'datetime') {
typeName = 'DateTime';
} else {
Expand Down Expand Up @@ -1061,7 +1072,11 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr

final basicTypesMap = generateBasicTypesMapFromSchemas(root);

propertyName = propertyName.asParameterName();
propertyName = getValidatedParameterName(propertyName).asParameterName();

if (propertyName.isEmpty) {
propertyName = '\$';
}

propertyName = getParameterName(propertyName, propertyNames);

Expand Down Expand Up @@ -1214,7 +1229,8 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr

entityMap.forEach((key, value) {
var fieldName = generateFieldName(
getParameterName(key.asParameterName(), propertyNames),
getParameterName(
getValidatedClassName(key).asParameterName(), propertyNames),
);

propertyNames.add(fieldName);
Expand Down Expand Up @@ -1553,8 +1569,7 @@ $allHashComponents;
final schema = content.schema;
if (schema != null) {
if (schema.type == kObject && schema.properties.isNotEmpty) {
final className =
'${pathKey.pascalCase}${requestKey.pascalCase}\$$kRequestBody';
final className = '${pathKey.pascalCase}${requestKey.pascalCase}';

result[getValidatedClassName(className)] = schema;
}
Expand Down
Loading

0 comments on commit 9a97e6a

Please sign in to comment.