Skip to content
This repository has been archived by the owner on Sep 21, 2023. It is now read-only.

Commit

Permalink
Version/2.4.6 (epam-cross-platform-lab#363)
Browse files Browse the repository at this point in the history
* Fix epam-cross-platform-lab#354

* Re-generated example

* Updated changelog and pubspec

* Refactoring of generators. Added generator base class

* Added authentificator to chopper client

* Update swagger_enums_generator.dart (epam-cross-platform-lab#364)

* version 2.4.5 (epam-cross-platform-lab#358)

* Fix epam-cross-platform-lab#354

* Re-generated example

* Updated changelog and pubspec

Co-authored-by: uladzimir_paliukhovich <>

* Update swagger_enums_generator.dart

Correctly create enum Map when Swagger has x-enumNames and type is integer.

Co-authored-by: Uladzimir_Paliukhovich <Vovanella95@mail.ru>

* Updated changelog and pubspec

* Fixed issues

* Formatted code

* Fixed tests

Co-authored-by: uladzimir_paliukhovich <>
Co-authored-by: Luka Žgur <luka.zgur2@gmail.com>
  • Loading branch information
Vovanella95 and lukazgur authored Mar 25, 2022
1 parent fa6d5e6 commit 9423762
Show file tree
Hide file tree
Showing 20 changed files with 688 additions and 657 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 2.4.6

* Added authentication to `create` method ([#360](https://github.com/epam-cross-platform-lab/swagger-dart-code-generator/issues/360))
* Added ability to refactor model names ([#361](https://github.com/epam-cross-platform-lab/swagger-dart-code-generator/issues/361))

# 2.4.5

* Fixed generation on Windows ([#354](https://github.com/epam-cross-platform-lab/swagger-dart-code-generator/issues/354))
Expand Down
1 change: 1 addition & 0 deletions example/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ targets:
build_only_models: false
separate_models: false
enums_case_sensitive: false
cutFromModelNames: "V3"
ignore_headers: false
use_required_attribute_for_headers: false
include_if_null: false
Expand Down
42 changes: 10 additions & 32 deletions lib/src/code_generators/swagger_additions_generator.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import 'package:swagger_dart_code_generator/src/code_generators/swagger_generator_base.dart';
import 'package:swagger_dart_code_generator/src/extensions/file_name_extensions.dart';
import 'package:swagger_dart_code_generator/src/models/generator_options.dart';

///Generates index file content, converter and additional methods
class SwaggerAdditionsGenerator {
class SwaggerAdditionsGenerator extends SwaggerGeneratorBase {
final GeneratorOptions _options;

@override
GeneratorOptions get options => _options;

SwaggerAdditionsGenerator(this._options);

static const mappingVariableName = 'generatedMapping';

///Generates index.dart for all generated services
Expand Down Expand Up @@ -116,8 +124,7 @@ String? _dateToJson(DateTime? date) {
}

///Copy-pasted converter from internet
String generateCustomJsonConverter(
String fileName, GeneratorOptions options) {
String generateCustomJsonConverter(String fileName) {
if (!options.withConverter) {
return '';
}
Expand Down Expand Up @@ -176,33 +183,4 @@ class \$JsonSerializableConverter extends chopper.JsonConverter {
final \$jsonDecoder = \$CustomJsonDecoder(generatedMapping);
''';
}

static String getChopperClientContent(
String className,
String host,
String basePath,
GeneratorOptions options,
) {
final baseUrlString = options.withBaseUrl
? "baseUrl: baseUrl ?? 'http://$host$basePath'"
: '/*baseUrl: YOUR_BASE_URL*/';

final converterString = options.withConverter
? 'converter: \$JsonSerializableConverter(),'
: 'converter: chopper.JsonConverter(),';

final chopperClientBody = '''
if(client!=null){
return _\$$className(client);
}
final newClient = ChopperClient(
services: [_\$$className()],
$converterString
interceptors: interceptors ?? [],
$baseUrlString);
return _\$$className(newClient);
''';
return chopperClientBody;
}
}
120 changes: 31 additions & 89 deletions lib/src/code_generators/swagger_enums_generator.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import 'package:recase/recase.dart';
import 'package:swagger_dart_code_generator/src/code_generators/constants.dart';
import 'package:swagger_dart_code_generator/src/code_generators/swagger_models_generator.dart';
import 'package:swagger_dart_code_generator/src/code_generators/swagger_generator_base.dart';
import 'package:swagger_dart_code_generator/src/exception_words.dart';
import 'package:swagger_dart_code_generator/src/extensions/string_extension.dart';
import 'package:swagger_dart_code_generator/src/models/generator_options.dart';
import 'package:swagger_dart_code_generator/src/swagger_models/requests/swagger_request.dart';
import 'package:swagger_dart_code_generator/src/swagger_models/requests/swagger_request_parameter.dart';
import 'package:swagger_dart_code_generator/src/swagger_models/swagger_path.dart';
import 'package:swagger_dart_code_generator/src/swagger_models/swagger_root.dart';
import 'package:collection/collection.dart';

abstract class SwaggerEnumsGenerator {
abstract class SwaggerEnumsGenerator extends SwaggerGeneratorBase {
final GeneratorOptions _options;

@override
GeneratorOptions get options => _options;

static const String defaultEnumFieldName = 'value_';
static const String defaultEnumValueName = 'swaggerGeneratedUnknown';

SwaggerEnumsGenerator(this._options);

String generate(Map<String, dynamic> map, String fileName);

String generateFromMap(
Expand All @@ -35,7 +43,7 @@ abstract class SwaggerEnumsGenerator {
final enumsFromClasses = definitions.keys
.map((String className) {
return generateEnumsFromClasses(
SwaggerModelsGenerator.getValidatedClassName(className.pascalCase),
getValidatedClassName(className.pascalCase),
definitions[className] as Map<String, dynamic>,
definitions,
);
Expand Down Expand Up @@ -81,7 +89,7 @@ $enumsFromRequestBodies
}

return generateEnumsFromClasses(
SwaggerModelsGenerator.getValidatedClassName(className.pascalCase),
getValidatedClassName(className.pascalCase),
schema as Map<String, dynamic>,
{},
);
Expand Down Expand Up @@ -111,7 +119,7 @@ $enumsFromRequestBodies
}

return generateEnumsFromClasses(
SwaggerModelsGenerator.getValidatedClassName(className.pascalCase),
getValidatedClassName(className.pascalCase),
schema as Map<String, dynamic>,
{},
);
Expand All @@ -122,24 +130,6 @@ $enumsFromRequestBodies
return enumsFromRequestBodies;
}

static SwaggerRequestParameter getOriginalOrOverriddenRequestParameter(
SwaggerRequestParameter swaggerRequestParameter,
List<SwaggerRequestParameter> definedParameters) {
if (swaggerRequestParameter.ref.isEmpty || definedParameters.isEmpty) {
return swaggerRequestParameter;
}

final parameterClassName = swaggerRequestParameter.ref.split('/').last;

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

return neededParameter;
}

String generateEnumsContentFromRequests(
Map<String, dynamic> map, String fileName) {
final enumNames = <String>[];
Expand Down Expand Up @@ -177,10 +167,10 @@ $enumsFromRequestBodies
for (var p = 0; p < swaggerRequest.parameters.length; p++) {
final swaggerRequestParameter = swaggerRequest.parameters[p];

var name = SwaggerModelsGenerator.generateRequestEnumName(
var name = generateRequestEnumName(
path, requestType, swaggerRequestParameter.name);

name = SwaggerModelsGenerator.getValidatedClassName(name);
name = getValidatedClassName(name);

if (enumNames.contains(name)) {
continue;
Expand Down Expand Up @@ -224,7 +214,7 @@ $enumsFromRequestBodies

final enumMap = '''
\n\tconst \$${enumName}Map = {
\t${getEnumValuesMapContent(enumName, enumValues)}
\t${getEnumValuesMapContent(enumName, enumValues: enumValues, enumValuesNames: [])}
};
''';

Expand Down Expand Up @@ -277,17 +267,26 @@ $enumMap
return resultStrings.join(',\n');
}

String getEnumValuesMapContent(String enumName, List<String> enumValues) {
String getEnumValuesMapContent(String enumName,
{required List<String> enumValues,
required List<String> enumValuesNames}) {
final neededStrings = <String>[];
final fields = <String>[];

for (var value in enumValues) {
var validatedValue = getValidatedEnumFieldName(value);
for (int i = 0; i < enumValues.length; i++) {
final value = enumValues[i];
var validatedValue = value;

while (fields.contains(validatedValue)) {
validatedValue += '\$';
}

if (enumValuesNames.length == enumValues.length) {
validatedValue = enumValuesNames[i];
}

validatedValue = getValidatedEnumFieldName(validatedValue);

fields.add(validatedValue);
neededStrings.add(
'\t$enumName.$validatedValue: \'${value.replaceAll('\$', '\\\$')}\'');
Expand Down Expand Up @@ -319,52 +318,6 @@ $enumMap
return result.lower;
}

static List<String> getEnumNamesFromRequests(Map<String, dynamic> map) {
final enumNames = <String>[];
final swaggerRoot = SwaggerRoot.fromJson(map);

//Link defined parameters with requests
swaggerRoot.paths.forEach((String path, SwaggerPath swaggerPath) {
swaggerPath.requests.forEach((String req, SwaggerRequest swaggerRequest) {
swaggerRequest.parameters = swaggerRequest.parameters
.map((SwaggerRequestParameter parameter) =>
getOriginalOrOverriddenRequestParameter(parameter,
swaggerRoot.components?.parameters.values.toList() ?? []))
.toList();
});
});

swaggerRoot.paths.forEach((String path, SwaggerPath swaggerPath) {
swaggerPath.requests
.forEach((String requestType, SwaggerRequest swaggerRequest) {
if (swaggerRequest.parameters.isEmpty) {
return;
}

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

var name = SwaggerModelsGenerator.generateRequestEnumName(
path, requestType, swaggerRequestParameter.name);

if (enumNames.contains(name)) {
continue;
}

final enumValues = swaggerRequestParameter.schema?.enumValues ??
swaggerRequestParameter.items?.enumValues ??
[];

if (enumValues.isNotEmpty) {
enumNames.add(name);
}
}
});
});

return enumNames;
}

String generateEnumsContentFromModelProperties(
Map<String, dynamic> map, String className) {
if (map.isEmpty) {
Expand Down Expand Up @@ -395,14 +348,12 @@ $enumMap

String generateEnumContentIfPossible(
Map<String, dynamic> map, String enumName) {
enumName = SwaggerModelsGenerator.getValidatedClassName(enumName);
enumName = getValidatedClassName(enumName);

if (map['enum'] != null) {
final enumValues =
(map['enum'] as List<dynamic>).map((e) => e.toString()).toList();

final stringValues = enumValues.map((e) => e.toString()).toList();

final enumValuesNames =
(map[kEnumNames] ?? map[kEnumVarnames]) as List? ?? [];

Expand All @@ -411,18 +362,14 @@ $enumMap
enumValuesNamesList = enumValuesNames.map((e) => e.toString()).toList();
}

final mapValues = enumValuesNamesList.length == stringValues.length
? enumValuesNamesList
: stringValues;
final isInteger = kIntegerTypes.contains(map['type']);

final enumMap = '''
\n\tconst \$${enumName}Map = {
\t${getEnumValuesMapContent(enumName, mapValues)}
\t${getEnumValuesMapContent(enumName, enumValues: enumValues, enumValuesNames: enumValuesNamesList)}
};
''';

final isInteger = kIntegerTypes.contains(map['type']);

return """
enum ${enumName.capitalize} {
\t@JsonValue('$defaultEnumValueName')\n $defaultEnumValueName,
Expand All @@ -443,11 +390,6 @@ $enumMap
}
}

String generateEnumName(String className, String enumName) {
return SwaggerModelsGenerator.getValidatedClassName(
'${className.capitalize}_$enumName');
}

String generateEnumsFromClasses(
String className,
Map<String, dynamic> map,
Expand Down
Loading

0 comments on commit 9423762

Please sign in to comment.