Skip to content

Commit

Permalink
Development (#701)
Browse files Browse the repository at this point in the history
* Fixed some issues

* formatted code

* updated SDK

* Updated SDK and version

* Fixed generation of lists of classes

* Fixed generation $Items classes

* Updated pubspec and changelog

* Fixed #524

* Fixed #598 Generation of query enum parameters

* Fixed conflicts

* Fixed some issues in swaggers

* Updated changelog and pubspec

* Fix #583, #637, #619 and update readme (#638)

* fix #583 and update readme

* fix #637

* fix #619

* Fixed generation of some fields

* Removed test

* Fixed classes named List

* Fixed generation of query parameters with ref default type

* Fixed generation of DateTime parameters

* Fixed generation of responses in some cases

* Some fixes

* Updated changelog and pubspec

* Implemented not nullable fields

* Fixed tests

* fixed generation of some swaggers

* Added ability to return String values

* Returned main.dart content

* Updated pubspec and changelog

* Fixed generation of required and not required fields

* Added check for object ref in body

* Fixed some things

* Fixed tests

* Fixed tests

* Fixed some things

* Updated changelog and pubspec

* Removed not needed lines in tests

* Fixed generation of nullable responses

* Added generation of DateTime

* Updated pubspec and changelog

* Fixed tests

* Fixed #669 Generation models from content schema allof

* Fixed #665 generation putIfAbsent for response from content schema

* Fixed generation of nullable and required properties

* Fixed tests

* Fixed some stuff related to nullable properties

* Updated changelog and pubspec

* Formatted code

* Formatted code

* Fixed tests

* Fixed generation of some enums inside classes

* Implemented support of exploded parameters

* Pushed constants file

* Fixed generation of allOf for request bodies

* Implemented overriden_models functionality

* Improved overriden models logic

* Fixed tests

* Updated pubspec and changelog

* Removed support of exploded parameters

* Fixed generation of patameters contains keywords

* Updated pubspec and changelog

* Fixed generation of nullable and not nullable fields

* Fixed generation of list parameters with specific names

* Fixed formurlencoded requests

* Revert "Fixed formurlencoded requests"

This reverts commit d103118.

* Updated changelog and pubspec

* formatted code

* Fixed version in changelog

* Added generation of writeOnly and readOnly fields as nullable and no required (Issue 487)

* Fixed generatino of DateTime query parameters (Issue 536)

* Updated pubspec and changelog

* Fixed generation in some cases

* Added ability to rename downloaded files

* Updated example

* Implemented support of deprecated fields annotations

* Added support of deprecated requests annotations

* Fixed tests, updated pubspec and changelog

* Fixed putIfAbsent for allOf schemas #700

* Fixed tests

---------

Co-authored-by: Uladzimir Paliukhovich <uladzimir_paliukhovich@epam.com>
Co-authored-by: Romain <romain@rb-dev.fr>
  • Loading branch information
3 people authored Dec 29, 2023
1 parent 73cb4bd commit 892fd14
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 136 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* ***Breaking*** Added ability to rename downloaded swagger files ([#558](https://github.com/epam-cross-platform-lab/swagger-dart-code-generator/issues/558))

* Added support of deprecated fields and methods ([#699](https://github.com/epam-cross-platform-lab/swagger-dart-code-generator/issues/699))

# 2.13.5

* Added `ErrorConverter` to create method
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 @@ -60,6 +60,7 @@ const kFormData = 'formData';
const kMultipart = 'multipart';
const kDateTimeFormat = 'date-time';
const kFactoryConverter = 'factoryConverter';
const kDeprecatedAnnotation = '@deprecated';

const kDefaultBodyParameter = 'Object';
const kField = 'Field';
Expand Down
171 changes: 97 additions & 74 deletions lib/src/code_generators/swagger_models_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -437,13 +437,14 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase {
return ', includeIfNull: ${options.includeIfNull}';
}

String generatePropertyContentByDefault(
SwaggerSchema prop,
String propertyName,
List<String> allEnumNames,
List<String> allEnumListNames,
List<String> requiredProperties,
) {
String generatePropertyContentByDefault({
required SwaggerSchema prop,
required String propertyName,
required List<String> allEnumNames,
required List<String> allEnumListNames,
required List<String> requiredProperties,
required bool isDeprecated,
}) {
var typeName = '';

if (prop.hasOriginalRef) {
Expand Down Expand Up @@ -487,8 +488,9 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase {

final jsonKeyContent =
"@JsonKey(name: '$propertyKey'$includeIfNullString$dateToJsonValue${unknownEnumValue.jsonKey})\n";
final deprecatedContent = isDeprecated ? '@deprecated\n' : '';

return '\t$jsonKeyContent\tfinal $typeName ${generateFieldName(propertyName)};${unknownEnumValue.fromJson}';
return '\t$jsonKeyContent$deprecatedContent\tfinal $typeName ${generateFieldName(propertyName)};${unknownEnumValue.fromJson}';
}

JsonEnumValue generateEnumValue({
Expand Down Expand Up @@ -666,14 +668,16 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr

final jsonKeyContent =
"@JsonKey(name: '${_validatePropertyKey(propertyKey)}'$includeIfNullString${unknownEnumValue.jsonKey}$dateToJsonValue)\n";
final deprecatedContent =
propertySchema.deprecated ? kDeprecatedAnnotation : '';

if (prop.shouldBeNullable ||
(options.nullableModels.contains(className) &&
!requiredProperties.contains(propertyKey))) {
typeName = typeName.makeNullable();
}

return '\t$jsonKeyContent\tfinal $typeName ${generateFieldName(propertyName)};${unknownEnumValue.fromJson}';
return '\t$jsonKeyContent$deprecatedContent\tfinal $typeName ${generateFieldName(propertyName)};${unknownEnumValue.fromJson}';
}

String _validatePropertyKey(String key) {
Expand Down Expand Up @@ -734,13 +738,15 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
final jsonKeyContent =
"@JsonKey(name: '${_validatePropertyKey(propertyKey)}'$includeIfNullString${unknownEnumValue.jsonKey})\n";

final deprecatedContent = prop.deprecated ? kDeprecatedAnnotation : '';

if (prop.shouldBeNullable ||
options.nullableModels.contains(className) ||
!requiredProperties.contains(propertyKey)) {
typeName = typeName.makeNullable();
}

return '\t$jsonKeyContent\tfinal $typeName $propertyName;${unknownEnumValue.fromJson}';
return '\t$jsonKeyContent$deprecatedContent\tfinal $typeName $propertyName;${unknownEnumValue.fromJson}';
}

String generatePropertyContentByRef(
Expand Down Expand Up @@ -806,6 +812,9 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
final jsonKeyContent =
"@JsonKey(name: '${_validatePropertyKey(propertyKey)}'$includeIfNullString${unknownEnumValue.jsonKey})\n";

final deprecatedContent =
refSchema?.deprecated == true ? kDeprecatedAnnotation : '';

if (prop.shouldBeNullable ||
options.nullableModels.contains(className) ||
!requiredProperties.contains(propertyKey)) {
Expand All @@ -826,18 +835,19 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
typeName += '?';
}

return '\t$jsonKeyContent\tfinal $typeName $propertyName;${unknownEnumValue.fromJson}';
return '\t$jsonKeyContent$deprecatedContent\tfinal $typeName $propertyName;${unknownEnumValue.fromJson}';
}

String generateEnumPropertyContent(
String key,
String className,
String propertyKey,
List<String> allEnumNames,
List<String> allEnumListNames,
SwaggerSchema prop,
List<String> requiredProperties,
) {
String generateEnumPropertyContent({
required String key,
required String className,
required String propertyKey,
required List<String> allEnumNames,
required List<String> allEnumListNames,
required SwaggerSchema prop,
required List<String> requiredProperties,
required bool isDeprecated,
}) {
final enumName = getValidatedClassName(generateEnumName(className, key));

allEnumNames.add(enumName);
Expand All @@ -863,6 +873,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr

return '''
@JsonKey(${unknownEnumValue.jsonKey.substring(2)}$includeIfNullString)
${isDeprecated ? kDeprecatedAnnotation : ''}
final $enumPropertyName ${generateFieldName(key)};
${unknownEnumValue.fromJson}''';
Expand Down Expand Up @@ -953,18 +964,19 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
return typeName;
}

String generateListPropertyContent(
String propertyName,
String propertyKey,
String className,
SwaggerSchema prop,
List<String> classesWithNullableLists,
List<String> allEnumNames,
List<String> allEnumListNames,
Map<String, String> basicTypesMap,
List<String> requiredProperties,
Map<String, SwaggerSchema> allClasses,
) {
String generateListPropertyContent({
required String propertyName,
required String propertyKey,
required String className,
required SwaggerSchema prop,
required List<String> classesWithNullableLists,
required List<String> allEnumNames,
required List<String> allEnumListNames,
required Map<String, String> basicTypesMap,
required List<String> requiredProperties,
required Map<String, SwaggerSchema> allClasses,
required bool isDeprecated,
}) {
final typeName = _generateListPropertyTypeName(
allEnumListNames: allEnumListNames,
allEnumNames: allEnumNames,
Expand Down Expand Up @@ -1004,6 +1016,8 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
"@JsonKey(name: '$validatedPropertyKey'$includeIfNullString${unknownEnumValue.jsonKey})\n";
}

final deprecatedContent = isDeprecated ? kDeprecatedAnnotation : '';

var listPropertyName = 'List<$typeName>';

if (prop.shouldBeNullable ||
Expand All @@ -1012,24 +1026,27 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
listPropertyName = listPropertyName.makeNullable();
}

return '$jsonKeyContent final $listPropertyName ${generateFieldName(propertyName)};${unknownEnumValue.fromJson}';
return '$jsonKeyContent$deprecatedContent final $listPropertyName ${generateFieldName(propertyName)};${unknownEnumValue.fromJson}';
}

String generateGeneralPropertyContent(
String propertyName,
String propertyKey,
String className,
List<DefaultValueMap> defaultValues,
SwaggerSchema prop,
List<String> allEnumNames,
List<String> allEnumListNames,
List<String> requiredProperties,
) {
String generateGeneralPropertyContent({
required String propertyName,
required String propertyKey,
required String className,
required List<DefaultValueMap> defaultValues,
required SwaggerSchema prop,
required List<String> allEnumNames,
required List<String> allEnumListNames,
required List<String> requiredProperties,
required bool isDeprecated,
}) {
final includeIfNullString = generateIncludeIfNullString();

var jsonKeyContent =
"@JsonKey(name: '${_validatePropertyKey(propertyKey)}'$includeIfNullString";

final isDeprecatedContent = isDeprecated ? kDeprecatedAnnotation : '';

var typeName = '';

if (prop.hasAdditionalProperties && prop.type == 'object') {
Expand Down Expand Up @@ -1085,7 +1102,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
typeName = typeName.makeNullable();
}

return '\t$jsonKeyContent final $typeName $propertyName;${unknownEnumValue.fromJson}';
return '\t$jsonKeyContent$isDeprecatedContent final $typeName $propertyName;${unknownEnumValue.fromJson}';
}

String generatePropertyContentByType(
Expand All @@ -1100,41 +1117,45 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
Map<String, String> basicTypesMap,
List<String> requiredProperties,
Map<String, SwaggerSchema> allClasses,
bool isDeprecated,
) {
switch (prop.type) {
case 'array':
return generateListPropertyContent(
propertyName,
propertyKey,
className,
prop,
classesWithNullableLists,
allEnumsNames,
allEnumListNames,
basicTypesMap,
requiredProperties,
allClasses,
propertyName: propertyName,
propertyKey: propertyKey,
className: className,
prop: prop,
classesWithNullableLists: classesWithNullableLists,
allEnumNames: allEnumsNames,
allEnumListNames: allEnumListNames,
basicTypesMap: basicTypesMap,
requiredProperties: requiredProperties,
allClasses: allClasses,
isDeprecated: isDeprecated,
);
case 'enum':
return generateEnumPropertyContent(
propertyName,
className,
propertyKey,
allEnumsNames,
allEnumListNames,
prop,
requiredProperties,
key: propertyName,
className: className,
propertyKey: propertyKey,
allEnumNames: allEnumsNames,
allEnumListNames: allEnumListNames,
prop: prop,
requiredProperties: requiredProperties,
isDeprecated: isDeprecated,
);
default:
return generateGeneralPropertyContent(
propertyName,
propertyKey,
className,
defaultValues,
prop,
allEnumsNames,
allEnumListNames,
requiredProperties,
propertyName: propertyName,
propertyKey: propertyKey,
className: className,
defaultValues: defaultValues,
prop: prop,
allEnumNames: allEnumsNames,
allEnumListNames: allEnumListNames,
requiredProperties: requiredProperties,
isDeprecated: isDeprecated,
);
}
}
Expand Down Expand Up @@ -1198,6 +1219,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
basicTypesMap,
requiredProperties,
allClasses,
prop.deprecated,
));
} else if (prop.allOf.isNotEmpty) {
results.add(
Expand Down Expand Up @@ -1237,11 +1259,12 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
));
} else {
results.add(generatePropertyContentByDefault(
prop,
propertyName,
allEnumNames,
allEnumListNames,
requiredProperties,
prop: prop,
propertyName: propertyName,
allEnumNames: allEnumNames,
allEnumListNames: allEnumListNames,
requiredProperties: requiredProperties,
isDeprecated: prop.deprecated,
));
}
}
Expand Down
34 changes: 28 additions & 6 deletions lib/src/code_generators/swagger_requests_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,14 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase {
componentsParameters: swaggerRoot.components?.parameters ?? {},
))
..name = methodName
..annotations.addAll(_getMethodAnnotation(requestType, annotationPath,
hasOptionalBody, isMultipart, isUrlencoded))
..annotations.addAll(_getMethodAnnotation(
requestType: requestType,
path: annotationPath,
hasOptionalBody: hasOptionalBody,
isMultipart: isMultipart,
isUrlencoded: isUrlencoded,
isDeprecated: swaggerRequest.deprecated,
))
..returns = Reference(returns));

final allModels = _getAllMethodModels(
Expand All @@ -227,7 +233,11 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase {
);

final privateMethod = _getPrivateMethod(method);
final publicMethod = _getPublicMethod(method, allModels);
final publicMethod = _getPublicMethod(
method,
allModels,
swaggerRequest.deprecated,
);
methods.addAll([publicMethod, privateMethod]);
});
});
Expand Down Expand Up @@ -352,6 +362,9 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase {
} else if (successResponse?.content?.schema?.properties.isNotEmpty ==
true) {
results.add(response);
} else if (successResponse?.content?.schema?.allOf.isNotEmpty == true &&
successResponse?.content?.schema?.title.isNotEmpty == true) {
results.add(response);
}

return results.where((element) => _isValidModelName(element)).toList();
Expand Down Expand Up @@ -412,7 +425,8 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase {
);
}

Method _getPublicMethod(Method method, List<String> allModels) {
Method _getPublicMethod(
Method method, List<String> allModels, bool isDeprecated) {
final parameters =
method.optionalParameters.map((p) => p.copyWith(annotations: []));

Expand All @@ -422,6 +436,7 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase {
..docs.addAll(method.docs)
..name = method.name
..returns = method.returns
..annotations.addAll([if (isDeprecated) refer('deprecated')])
..body = _generatePublicMethodCode(
method.optionalParameters,
method.name!,
Expand Down Expand Up @@ -472,9 +487,16 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase {
'$allModelsString\nreturn _$publicMethodName($parametersListString);');
}

List<Expression> _getMethodAnnotation(String requestType, String path,
bool hasOptionalBody, bool isMultipart, bool isUrlencoded) {
List<Expression> _getMethodAnnotation({
required String requestType,
required String path,
required bool hasOptionalBody,
required bool isMultipart,
required bool isUrlencoded,
required bool isDeprecated,
}) {
return [
if (isDeprecated) refer('deprecated'),
refer(requestType.pascalCase).call(
[],
{
Expand Down
Loading

0 comments on commit 892fd14

Please sign in to comment.