Skip to content

Commit

Permalink
Allow underscores in classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Uladzimir Paliukhovich committed Sep 18, 2024
1 parent a665350 commit f301cd5
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 195 deletions.
62 changes: 24 additions & 38 deletions lib/src/code_generators/swagger_enums_generator.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:collection/collection.dart';
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/enum_model.dart';
import 'package:swagger_dart_code_generator/src/code_generators/swagger_generator_base.dart';
import 'package:swagger_dart_code_generator/src/code_generators/swagger_requests_generator.dart';
import 'package:swagger_dart_code_generator/src/code_generators/utils.dart';
import 'package:swagger_dart_code_generator/src/code_generators/v2/swagger_models_generator_v2.dart';
import 'package:swagger_dart_code_generator/src/extensions/string_extension.dart';
import 'package:swagger_dart_code_generator/src/models/generator_options.dart';
Expand Down Expand Up @@ -34,8 +34,7 @@ abstract class SwaggerEnumsGenerator extends SwaggerGeneratorBase {
required String fileName,
}) {
final requestBodies = root.components?.requestBodies ?? {};
requestBodies.addAll(
SwaggerModelsGeneratorV2(options).getRequestBodiesFromRequests(root));
requestBodies.addAll(SwaggerModelsGeneratorV2(options).getRequestBodiesFromRequests(root));

final formattedRequestBodies = <String, SwaggerSchema>{};
requestBodies.forEach((key, value) {
Expand All @@ -53,13 +52,12 @@ abstract class SwaggerEnumsGenerator extends SwaggerGeneratorBase {

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

final enumsFromClasses = definitions.keys
.map((String className) {
final result = generateEnumsFromClasses(
getValidatedClassName(className.pascalCase),
getValidatedClassName(className.toClassName),
definitions[className]!,
definitions,
);
Expand Down Expand Up @@ -112,7 +110,7 @@ ${allEnums.map((e) => e.toString()).join('\n')}
}

return generateEnumsFromClasses(
getValidatedClassName(className.pascalCase),
getValidatedClassName(className.toClassName),
schema,
{},
);
Expand All @@ -121,8 +119,7 @@ ${allEnums.map((e) => e.toString()).join('\n')}
.toList();
}

List<EnumModel> generateEnumsContentFromRequests(
SwaggerRoot swaggerRoot, String fileName) {
List<EnumModel> generateEnumsContentFromRequests(SwaggerRoot swaggerRoot, String fileName) {
final result = <EnumModel>[];

final definedParameters = <String, SwaggerRequestParameter>{};
Expand All @@ -136,12 +133,10 @@ ${allEnums.map((e) => e.toString()).join('\n')}

final enumNames = swaggerRequestParameter.schema?.enumNames ?? [];

final isInteger =
kIntegerTypes.contains(swaggerRequestParameter.schema?.type) ||
kIntegerTypes.contains(swaggerRequestParameter.items?.type);
final isInteger = kIntegerTypes.contains(swaggerRequestParameter.schema?.type) ||
kIntegerTypes.contains(swaggerRequestParameter.items?.type);

if (enumValues.isNotEmpty &&
swaggerRoot.components?.schemas.containsKey(key) != true) {
if (enumValues.isNotEmpty && swaggerRoot.components?.schemas.containsKey(key) != true) {
final enumContent = EnumModel(
name: getValidatedClassName(key),
values: enumValues,
Expand All @@ -154,19 +149,16 @@ ${allEnums.map((e) => e.toString()).join('\n')}
});

swaggerRoot.paths.forEach((String path, SwaggerPath swaggerPath) {
swaggerPath.requests
.forEach((String requestType, SwaggerRequest swaggerRequest) {
final successResponses = SwaggerRequestsGenerator.getSuccessedResponses(
responses: swaggerRequest.responses);
swaggerPath.requests.forEach((String requestType, SwaggerRequest swaggerRequest) {
final successResponses =
SwaggerRequestsGenerator.getSuccessedResponses(responses: swaggerRequest.responses);

for (final successResponse in successResponses) {
final successResponseSchema =
successResponse.schema ?? successResponse.content?.schema;
final successResponseSchema = successResponse.schema ?? successResponse.content?.schema;

if (successResponseSchema != null) {
final responseEnums = generateEnumsFromSchemaMap({
'${path.pascalCase}${requestType.pascalCase}\$$kResponse':
successResponseSchema
'${path.toClassName}${requestType.toClassName}\$$kResponse': successResponseSchema
});
result.addAll(responseEnums);
}
Expand All @@ -184,8 +176,7 @@ ${allEnums.map((e) => e.toString()).join('\n')}
for (var p = 0; p < parameters.length; p++) {
final swaggerRequestParameter = parameters[p];

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

name = getValidatedClassName(name);

Expand All @@ -201,9 +192,8 @@ ${allEnums.map((e) => e.toString()).join('\n')}

final enumNames = swaggerRequestParameter.schema?.enumNames ?? [];

final isInteger =
kIntegerTypes.contains(swaggerRequestParameter.schema?.type) ||
kIntegerTypes.contains(swaggerRequestParameter.items?.type);
final isInteger = kIntegerTypes.contains(swaggerRequestParameter.schema?.type) ||
kIntegerTypes.contains(swaggerRequestParameter.items?.type);

if (enumValues.isNotEmpty) {
final enumContent = EnumModel(
Expand Down Expand Up @@ -243,13 +233,12 @@ ${allEnums.map((e) => e.toString()).join('\n')}
final result = <EnumModel>[];

if (properties.isNotEmpty) {
final isListProperty =
enumValues.items?.properties.isNotEmpty == true;
final isListProperty = enumValues.items?.properties.isNotEmpty == true;

result.addAll(
generateEnumsContentFromModelProperties(
properties,
'$className\$${key.pascalCase}${isListProperty ? '\$Item' : ''}',
'$className\$${key.toClassName}${isListProperty ? '\$Item' : ''}',
),
);
}
Expand Down Expand Up @@ -283,8 +272,7 @@ ${allEnums.map((e) => e.toString()).join('\n')}
return enumValues.isNotEmpty && enumValues.first is int;
}

EnumModel? generateEnumContentIfPossible(
SwaggerSchema schema, String enumName) {
EnumModel? generateEnumContentIfPossible(SwaggerSchema schema, String enumName) {
enumName = getValidatedClassName(enumName);

if (schema.isEnum) {
Expand Down Expand Up @@ -320,17 +308,16 @@ ${allEnums.map((e) => e.toString()).join('\n')}

if (schema.items != null) {
if (schema.items!.isEnum) {
final enumModel =
generateEnumContentIfPossible(schema.items!, className);
final enumModel = generateEnumContentIfPossible(schema.items!, className);
return enumModel == null ? [] : [enumModel];
}

if (schema.items?.properties.isNotEmpty == true) {
var result = <EnumModel>[];

schema.items?.properties.forEach((key, value) {
final enumModel = generateEnumContentIfPossible(
value, '$className\$Item${key.pascalCase}');
final enumModel =
generateEnumContentIfPossible(value, '$className\$Item${key.toClassName}');
if (enumModel != null) {
result.add(enumModel);
}
Expand Down Expand Up @@ -366,8 +353,7 @@ ${allEnums.map((e) => e.toString()).join('\n')}
final allOfModel = schemas[ref.getUnformattedRef()];

if (allOfModel?.allOf.isNotEmpty == true) {
final allOfRef =
allOfModel?.allOf.firstWhereOrNull((e) => e.hasRef)?.ref ?? '';
final allOfRef = allOfModel?.allOf.firstWhereOrNull((e) => e.hasRef)?.ref ?? '';
final allOfModelInside = schemas[allOfRef.getUnformattedRef()];

properties.addAll(allOfModelInside?.properties ?? {});
Expand Down
27 changes: 9 additions & 18 deletions lib/src/code_generators/swagger_generator_base.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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/utils.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';
Expand Down Expand Up @@ -36,10 +37,7 @@ abstract class SwaggerGeneratorBase {
final words = className.split('\$');

final result = words
.map((e) => e.pascalCase
.split(RegExp(r'\W+|\_'))
.map((String str) => str.capitalize)
.join())
.map((e) => e.toClassName.split(RegExp(r'\W+')).map((String str) => str.capitalize).join())
.join('\$')
.replaceFirst(RegExp(options.cutFromModelNames), '');

Expand All @@ -55,17 +53,14 @@ abstract class SwaggerGeneratorBase {
return '\$$result';
}

return result
.replaceFirst(options.cutFromModelNames, '')
.replaceAll('\$\$', '\$');
return result.replaceFirst(options.cutFromModelNames, '').replaceAll('\$\$', '\$');
}

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

String generateRequestEnumName(
String path, String requestType, String parameterName) {
String generateRequestEnumName(String path, String requestType, String parameterName) {
if (path == '/') {
path = '\$';
}
Expand All @@ -92,8 +87,7 @@ abstract class SwaggerGeneratorBase {
}
}

if (jsonKey.startsWith(RegExp('[0-9]')) ||
exceptionWords.contains(jsonKey)) {
if (jsonKey.startsWith(RegExp('[0-9]')) || exceptionWords.contains(jsonKey)) {
jsonKey = '\$$jsonKey';
}

Expand All @@ -111,25 +105,22 @@ abstract class SwaggerGeneratorBase {
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() ?? []))
.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) {
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 = generateRequestEnumName(
path, requestType, swaggerRequestParameter.name);
var name = generateRequestEnumName(path, requestType, swaggerRequestParameter.name);

if (enums.any((element) => element.name == name)) {
continue;
Expand Down
Loading

0 comments on commit f301cd5

Please sign in to comment.