Skip to content

Commit

Permalink
Development (#657)
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

---------

Co-authored-by: Uladzimir Paliukhovich <[email protected]>
Co-authored-by: Romain <[email protected]>
  • Loading branch information
3 people authored Oct 13, 2023
1 parent f44a3ac commit b0091cb
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 40 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.11.11

* Fixes some stuff related to generation DateTime, reponse types and parameters types

# 2.11.10

* Bump `package:http` to `^1.0.0`
Expand Down
6 changes: 5 additions & 1 deletion lib/src/code_generators/enum_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,14 @@ const $name(this.value);
: 'return enums.$name.values.firstWhereOrNull((e) => e.value.toString().toLowerCase() == ${name.camelCase}?.toString().toLowerCase()) ?? defaultValue ?? enums.$name.swaggerGeneratedUnknown';

return '''
$type? ${name.camelCase}ToJson(enums.$name? ${name.camelCase}) {
$type? ${name.camelCase}NullableToJson(enums.$name? ${name.camelCase}) {
return ${name.camelCase}?.value;
}
$type? ${name.camelCase}ToJson(enums.$name ${name.camelCase}) {
return ${name.camelCase}.value;
}
enums.$name ${name.camelCase}FromJson(
Object? ${name.camelCase},
[enums.$name? defaultValue,]
Expand Down
77 changes: 52 additions & 25 deletions lib/src/code_generators/swagger_models_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase {
}
final responses = request.responses;

final neededResponse = responses['200'];
final neededResponse = responses['200'] ?? responses['201'];

final neededSchema =
neededResponse?.schema ?? neededResponse?.content?.schema;
Expand Down Expand Up @@ -442,8 +442,8 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase {

final includeIfNullString = generateIncludeIfNullString();

if (typeName != kDynamic) {
typeName += '?';
if (typeName != kDynamic && (prop.isNullable || options.nullableFields)) {
typeName = typeName.makeNullable();
}

final jsonKeyContent =
Expand Down Expand Up @@ -494,7 +494,7 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase {
}
final fromJsonFunction = '$fromJsonPrefix$fromJsonSuffix';
jsonKey =
', toJson: $enumNameCamelCase$toJsonSuffix, fromJson: $fromJsonFunction, $defaultValueSuffix';
', toJson: $enumNameCamelCase${isNullable && !isList ? 'Nullable$toJsonSuffix' : toJsonSuffix}, fromJson: $fromJsonFunction, $defaultValueSuffix';

if (defaultValue != null) {
var returnType = '';
Expand Down Expand Up @@ -552,10 +552,13 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
return '';
}

bool isNullable(String className, Iterable<String> requiredProperties,
String propertyKey, SwaggerSchema prop) {
bool isNullable(
String className,
Iterable<String> requiredProperties,
String propertyKey,
SwaggerSchema prop,
) {
return options.nullableModels.contains(className) ||
!requiredProperties.contains(propertyKey) ||
prop.isNullable == true;
}

Expand Down Expand Up @@ -623,8 +626,10 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
final jsonKeyContent =
"@JsonKey(name: '${_validatePropertyKey(propertyKey)}'$includeIfNullString${unknownEnumValue.jsonKey}$dateToJsonValue)\n";

typeName =
nullable(typeName, className, requiredProperties, propertyKey, prop);
if (prop.isNullable || options.nullableFields) {
typeName =
nullable(typeName, className, requiredProperties, propertyKey, prop);
}

return '\t$jsonKeyContent\tfinal $typeName ${generateFieldName(propertyName)};${unknownEnumValue.fromJson}';
}
Expand Down Expand Up @@ -652,14 +657,19 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
.length >
1) {
typeName = kDynamic;
} else {
} else if (allOf.first.ref.isNotEmpty) {
var className = allOf.first.ref.getRef();

if (allEnumNames.contains(className)) {
className = 'enums.$className';
}

typeName = getValidatedClassName(className);
} else if (allOf.first.type.isNotEmpty &&
kBasicTypesMap.containsKey(allOf.first.type)) {
typeName = kBasicTypesMap[allOf.first.type]!;
} else {
typeName = kDynamic;
}

if (basicTypesMap.containsKey(typeName)) {
Expand All @@ -681,8 +691,10 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
final jsonKeyContent =
"@JsonKey(name: '${_validatePropertyKey(propertyKey)}'$includeIfNullString${unknownEnumValue.jsonKey})\n";

typeName =
nullable(typeName, className, requiredProperties, propertyKey, prop);
if (prop.isNullable || options.nullableFields) {
typeName =
nullable(typeName, className, requiredProperties, propertyKey, prop);
}

return '\t$jsonKeyContent\tfinal $typeName $propertyName;${unknownEnumValue.fromJson}';
}
Expand Down Expand Up @@ -725,6 +737,9 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
typeName += options.modelPostfix;
}

final isPropertyNullable = refSchema?.isNullable == true ||
isNullable(className, requiredProperties, propertyKey, prop);

final unknownEnumValue = generateEnumValue(
allEnumNames: allEnumNames,
allEnumListNames: allEnumListNames,
Expand All @@ -733,7 +748,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
defaultValue: prop.defaultValue,
className: className,
isList: false,
isNullable: isNullable(className, requiredProperties, propertyKey, prop),
isNullable: isPropertyNullable,
);

if (allEnumListNames.contains(typeName)) {
Expand All @@ -745,12 +760,14 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
final jsonKeyContent =
"@JsonKey(name: '${_validatePropertyKey(propertyKey)}'$includeIfNullString${unknownEnumValue.jsonKey})\n";

typeName =
nullable(typeName, className, requiredProperties, propertyKey, prop);
if (prop.isNullable || options.nullableFields) {
typeName =
nullable(typeName, className, requiredProperties, propertyKey, prop);
}

final propertySchema = allClasses[prop.ref.getUnformattedRef()];

if (propertySchema?.isNullable == true) {
if (propertySchema?.isNullable == true || isPropertyNullable) {
typeName = typeName.makeNullable();
}

Expand Down Expand Up @@ -789,8 +806,11 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
final includeIfNullString = generateIncludeIfNullString();

var enumPropertyName = className.capitalize + key.capitalize;
enumPropertyName = nullable(
enumPropertyName, className, requiredProperties, propertyKey, prop);

if (prop.isNullable || options.nullableFields) {
enumPropertyName = nullable(
enumPropertyName, className, requiredProperties, propertyKey, prop);
}

return '''
@JsonKey(${unknownEnumValue.jsonKey.substring(2)}$includeIfNullString)
Expand Down Expand Up @@ -937,8 +957,10 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr

var listPropertyName = 'List<$typeName>';

listPropertyName = nullable(
listPropertyName, className, requiredParameters, propertyKey, prop);
if (prop.isNullable || options.nullableFields) {
listPropertyName = nullable(
listPropertyName, className, requiredParameters, propertyKey, prop);
}

return '$jsonKeyContent final $listPropertyName ${generateFieldName(propertyName)};${unknownEnumValue.fromJson}';
}
Expand Down Expand Up @@ -1006,8 +1028,10 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
jsonKeyContent += ')\n';
}

typeName =
nullable(typeName, className, requiredProperties, propertyKey, prop);
if (prop.isNullable || options.nullableFields) {
typeName =
nullable(typeName, className, requiredProperties, propertyKey, prop);
}

return '\t$jsonKeyContent final $typeName $propertyName;${unknownEnumValue.fromJson}';
}
Expand Down Expand Up @@ -1242,7 +1266,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr

String generateConstructorPropertiesContent({
required String className,
required Map<String, dynamic> entityMap,
required Map<String, SwaggerSchema> entityMap,
required List<DefaultValueMap> defaultValues,
required List<String> requiredProperties,
required List<String> allEnumNames,
Expand All @@ -1263,8 +1287,11 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr

propertyNames.add(fieldName);

if (options.nullableModels.contains(className) ||
!requiredProperties.contains(key)) {
final isNullableProperty = options.nullableModels.contains(className) ||
options.nullableFields ||
value.isNullable;

if (isNullableProperty) {
results += '\t\tthis.$fieldName,\n';
} else {
results += '\t\t$kRequired this.$fieldName,\n';
Expand Down
4 changes: 2 additions & 2 deletions lib/src/code_generators/swagger_requests_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase {

if (!kBasicTypes.contains(itemsType) &&
schema?.items?.properties != null) {
final itemClassName = '$response\$Item';
final itemClassName = '${response.replaceAll('?', '')}\$Item';

results.add(itemClassName);
}
Expand Down Expand Up @@ -1231,7 +1231,7 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase {
final pathText = path.split('/').map((e) => e.pascalCase).join();
final requestText = method.pascalCase;

return '$pathText$requestText\$Response';
return getValidatedClassName('$pathText$requestText\$Response');
}

return kBasicTypesMap[contentSchemaType];
Expand Down
4 changes: 4 additions & 0 deletions lib/src/models/generator_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class GeneratorOptions {
this.overridenModels = const [],
this.generateToJsonFor = const [],
this.multipartFileType = 'List<int>',
this.nullableFields = false,
});

/// Build options from a JSON map.
Expand All @@ -49,6 +50,9 @@ class GeneratorOptions {
@JsonKey(defaultValue: true)
final bool withBaseUrl;

@JsonKey(defaultValue: false)
final bool nullableFields;

@JsonKey(defaultValue: false)
final bool addBasePathToRequests;

Expand Down
2 changes: 2 additions & 0 deletions lib/src/models/generator_options.g2.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion lib/src/swagger_models/responses/swagger_schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ class SwaggerSchema {
?.map((e) => e as String)
.toList()
..isNullable = (json[kIsNullable] ??
json[kIsNullable] ??
json[kNullable] ??
false) as bool;

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.11.10
version: 2.11.11

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
20 changes: 10 additions & 10 deletions test/generator_tests/models_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ void main() {
});

test('Should return validate constructor property', () {
final map = <String, dynamic>{
'Animal': {'type': 'dog'}
final map = <String, SwaggerSchema>{
'Animal': SwaggerSchema()
};
const expectedResult = 'this.animal';
final result = generator.generateConstructorPropertiesContent(
Expand All @@ -287,7 +287,7 @@ void main() {
const className = 'Animals';
const propertyKey = 'Dog';
const jsonKeyExpectedResult = "\t@JsonKey(name: 'Dog')\n";
const fieldExpectedResult = '\tfinal Pet? dog';
const fieldExpectedResult = '\tfinal Pet dog';
final result = generator.generatePropertyContentBySchema(
map,
propertyName,
Expand All @@ -312,7 +312,7 @@ void main() {

const className = 'Animals';
const jsonKeyExpectedResult = "\t@JsonKey(name: 'Animals')\n";
const fieldExpectedResult = 'final Pet? animals';
const fieldExpectedResult = 'final Pet animals';
final result = generator.generatePropertiesContent(
SwaggerRoot.empty,
map,
Expand All @@ -339,7 +339,7 @@ void main() {

const className = 'Animals';
const jsonKeyExpectedResult = "\t@JsonKey(name: 'Animals')\n";
const fieldExpectedResult = 'final Pet? animals';
const fieldExpectedResult = 'final Pet animals';
final result = generator.generatePropertiesContent(
SwaggerRoot.empty,
map,
Expand All @@ -364,7 +364,7 @@ void main() {

const className = 'Animals';
const jsonKeyExpectedResult = "\t@JsonKey(name: 'animals')\n";
const fieldExpectedResult = 'final Pet? animals';
const fieldExpectedResult = 'final Pet animals';
final result = generator.generatePropertiesContent(
SwaggerRoot.empty,
map,
Expand All @@ -389,7 +389,7 @@ void main() {

const className = 'Animals';
const jsonKeyExpectedResult = "\t@JsonKey(name: '\\\$with')\n";
const fieldExpectedResult = 'final Pet? \$with';
const fieldExpectedResult = 'final Pet \$with';
final result = generator.generatePropertiesContent(
SwaggerRoot.empty,
map,
Expand Down Expand Up @@ -417,7 +417,7 @@ void main() {
const jsonKeyExpectedResult =
"@JsonKey(name: 'Dog', defaultValue: <Object>[])";

const propertyExpectedResult = 'final List<Object>? dog';
const propertyExpectedResult = 'final List<Object> dog';
final result = generator.generateListPropertyContent(
propertyName,
propertyKey,
Expand Down Expand Up @@ -477,7 +477,7 @@ void main() {
{},
);

expect(result, contains('final List<TestOriginalRef>? dog;'));
expect(result, contains('final List<TestOriginalRef> dog;'));
});

test('Should return List<Object> by ref', () {
Expand All @@ -500,7 +500,7 @@ void main() {
{},
);

expect(result, contains('final List<TestObject>? dog;'));
expect(result, contains('final List<TestObject> dog;'));
});
});

Expand Down

0 comments on commit b0091cb

Please sign in to comment.