Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version/2.1.1 #231

Merged
merged 11 commits into from
Aug 28, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Flutter test coverage
run: flutter test --coverage
- name: Remove some unnecessary files from coverage
run: flutter pub run remove_from_coverage -f coverage/lcov.info -r 'swagger_models' -r 'models'
run: flutter pub run remove_from_coverage -f coverage/lcov.info -r 'swagger_models' -r 'models' -r 'extensions'
- uses: codecov/[email protected]
with:
file: ./coverage/lcov.info
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 2.1.1

* Stabilized generator
* Fixed issue when parameters named like int, double etc.
* Fixed Issue ([#108](https://github.com/epam-cross-platform-lab/swagger-dart-code-generator/issues/108))

# 2.1.0-prerelease.9

* Fixed Issue ([#228](https://github.com/epam-cross-platform-lab/swagger-dart-code-generator/issues/228))
Expand Down
1 change: 1 addition & 0 deletions lib/src/code_generators/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const kChopperApi = 'ChopperApi';
const kResponse = 'Response';
const kFutureResponse = 'Future<chopper.Response>';
const kString = 'string';
const kUndefinedParameter = 'undefinedParameter';

const kResponses = '/responses/';

Expand Down
52 changes: 42 additions & 10 deletions lib/src/code_generators/swagger_models_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,42 @@ abstract class SwaggerModelsGenerator {
return result;
}

static String getValidatedParameterName(String parameterName) {
if (exceptionWords.contains(parameterName) ||
basicTypes.contains(parameterName)) {
return '\$$parameterName';
}

if (parameterName.isEmpty) {
return parameterName;
}

final isEnum = parameterName.startsWith('enums.');

if (isEnum) {
parameterName = parameterName.substring(6);
}

final words = parameterName.split('\$');

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

if (isEnum) {
return 'enums.$result';
}

if (result.isEmpty) {
return kUndefinedParameter;
}

return result.camelCase;
}

String getParameterTypeName(
String className,
String parameterName,
Expand Down Expand Up @@ -317,6 +353,10 @@ abstract class SwaggerModelsGenerator {
jsonKey = '\$' + jsonKey;
}

if (basicTypes.contains(jsonKey)) {
jsonKey = '\$' + jsonKey;
}

return jsonKey;
}

Expand Down Expand Up @@ -692,7 +732,7 @@ abstract class SwaggerModelsGenerator {
typeName += '?';
}

return ''' $jsonKeyContent final $typeName ${SwaggerModelsGenerator.generateFieldName(propertyName)};''';
return '\t$jsonKeyContent final $typeName $propertyName;';
}

String generatePropertyContentByType(
Expand Down Expand Up @@ -772,12 +812,6 @@ abstract class SwaggerModelsGenerator {
propertiesMap[propertyName] as Map<String, dynamic>;
final propertyKey = propertyName;

exceptionWords.forEach((String exceptionWord) {
if (propertyName == exceptionWord) {
propertyName = '\$' + propertyName;
}
});

final basicTypesMap = generateBasicTypesMapFromSchemas(schemas);

propertyName = propertyName.asParameterName();
Expand Down Expand Up @@ -945,9 +979,7 @@ List<enums.$neededName> ${neededName.camelCase}ListFromJson(
final propertyNames = <String>[];

entityMap.forEach((key, value) {
var fieldName = SwaggerModelsGenerator.generateFieldName(key);

fieldName = getParameterName(fieldName.asParameterName(), propertyNames);
var fieldName = getParameterName(key.asParameterName(), propertyNames);

propertyNames.add(fieldName);

Expand Down
28 changes: 21 additions & 7 deletions lib/src/code_generators/swagger_requests_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,6 @@ class SwaggerRequestsGenerator {
return (parameter.schema!.ref.getRef() + modelPostfix).asList();
}
return (parameter.schema!.ref.getRef() + modelPostfix);
} else if (parameter.schema?.ref.isNotEmpty == true) {
return parameter.schema!.ref.getRef() + modelPostfix;
} else if (parameter.schema?.type == kArray &&
parameter.schema?.items?.type.isNotEmpty == true) {
return _mapParameterName(parameter.schema!.items!.type, '').asList();
Expand Down Expand Up @@ -634,8 +632,11 @@ class SwaggerRequestsGenerator {
return null;
}

String? _getReturnTypeFromContent(
SwaggerResponse swaggerResponse, String modelPostfix) {
String? _getReturnTypeFromContent({
required SwaggerResponse swaggerResponse,
required String modelPostfix,
required SwaggerRoot swaggerRoot,
}) {
final content = swaggerResponse.content;

if (content == null) {
Expand All @@ -650,10 +651,19 @@ class SwaggerRequestsGenerator {

final schemaRef = content.schema?.ref ?? '';
if (schemaRef.isNotEmpty) {
final type =
final neededSchema = swaggerRoot.components?.schemas[schemaRef.getRef()];
final typeName =
SwaggerModelsGenerator.getValidatedClassName(schemaRef.getRef())
.withPostfix(modelPostfix);
return kBasicTypesMap[type] ?? type;

if (neededSchema?.type == kArray) {
return neededSchema?.items?.ref
.getRef()
.withPostfix(modelPostfix)
.asList();
}

return typeName;
}

final responseType = content.responseType;
Expand Down Expand Up @@ -726,7 +736,11 @@ class SwaggerRequestsGenerator {
final type = _getReturnTypeFromType(neededResponse, modelPostfix) ??
_getReturnTypeFromSchema(neededResponse, modelPostfix, swaggerRoot) ??
_getReturnTypeFromOriginalRef(neededResponse, modelPostfix) ??
_getReturnTypeFromContent(neededResponse, modelPostfix) ??
_getReturnTypeFromContent(
swaggerResponse: neededResponse,
modelPostfix: modelPostfix,
swaggerRoot: swaggerRoot,
) ??
'';

if (type.isNotEmpty) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/extensions/string_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extension TypeExtension on String {
String asFutureResponse() => 'Future<chopper.Response<$this>>';

String asParameterName() {
return SwaggerModelsGenerator.getValidatedClassName(this).camelCase;
return SwaggerModelsGenerator.getValidatedParameterName(this);
}

String asParameterType() {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: swagger_dart_code_generator

version: 2.1.0-prerelease.9
version: 2.1.1

homepage: https://github.com/epam-cross-platform-lab/swagger-dart-code-generator
repository: https://github.com/epam-cross-platform-lab/swagger-dart-code-generator
Expand Down
30 changes: 28 additions & 2 deletions test/generator_tests/requests_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,39 @@ void main() {
inputFolder: '',
outputFolder: '',
ignoreHeaders: true,
responseOverrideValueMap: [
ResponseOverrideValueMap(
method: 'get',
url: '/cars/schemaRefBody',
overriddenValue: 'String',
)
],
),
);

expect(result, contains('Future<chopper.Response<CarModel>>'));
final result2 = generator.generate(
code: carsService,
className: 'CarsService',
fileName: 'cars_service',
options: GeneratorOptions(
inputFolder: '',
outputFolder: '',
defaultHeaderValuesMap: [
DefaultHeaderValueMap(
defaultValue: '120',
headerName: 'id',
),
],
),
);

expect(result2, contains('Future<chopper.Response<CarModel>>'));
expect(result, contains('Future<chopper.Response<CarModel>> carsGet'));
expect(result, contains('Future<chopper.Response<CarModel>> carsPost'));
expect(result, contains('Future<chopper.Response<CarModel>> carsAdditionalPut({@Body() required Object? body});'));
expect(
result,
contains(
'Future<chopper.Response<CarModel>> carsAdditionalPut({@Body() required Object? body});'));
});
});
}
Loading