Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion lib/src/code_generators/swagger_models_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -910,12 +910,14 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr

final dateToJsonValue = generateToJsonForDate(resolvedSchemaForDetails);
final includeIfNullString = generateIncludeIfNullString();
final jsonConverterAnnotation =
generatePropertyJsonConverterAnnotation(resolvedSchemaForDetails);

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

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

String generatePropertyContentByAllOf({
Expand Down
1 change: 1 addition & 0 deletions lib/swagger_dart_code_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ $dateToJson
// ignore_for_file: type=lint

import 'package:json_annotation/json_annotation.dart';
${options.scalars.isEmpty ? '' : "import 'package:json_annotation/json_annotation.dart' as json;\n"}
import 'package:collection/collection.dart';
import 'dart:convert';
${options.importPaths.map((e) => "import '$e';").join('\n')}
Expand Down
41 changes: 39 additions & 2 deletions test/generator_tests/models_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,43 @@ void main() {
});
});

group('Tests for scalar converters on nullable (anyOf) fields', () {
test('Should apply the converter annotation to a nullable date-time field',
() {
final generator = SwaggerModelsGeneratorV3(
GeneratorOptions(
inputFolder: '',
outputFolder: '',
scalars: {
'date-time': CustomScalar(
type: 'DateTime',
deserialize: 'DateTime.parse',
),
},
),
);

final result = generator.generatePropertyContentByAnyOf(
prop: SwaggerSchema(
anyOf: [
SwaggerSchema(type: 'string', format: 'date-time'),
SwaggerSchema(type: 'null'),
],
),
propertyName: 'start',
propertyKey: 'start',
className: 'Event',
allEnumNames: [],
allEnumListNames: [],
requiredProperties: [],
isDeprecated: false,
);

expect(result, contains('@_\$DateTimeJsonConverter()'));
expect(result, contains('final DateTime? start;'));
});
});

group('Tests for overridden format types', () {
test('Should include deserialize function in JsonKey annotation', () {
final map = SwaggerRoot.parse(schemasWithUuidsInProperties);
Expand Down Expand Up @@ -690,7 +727,7 @@ void main() {
expect(
result,
contains(
'class _\$UuidJsonConverter implements json.JsonConverter<Uuid, String>'));
'class _\$UuidJsonConverter implements json.JsonConverter<Uuid, dynamic>'));
expect(result, contains('fromJson(json) => Uuid.parse(json);'));
expect(result, contains('toJson(json) => json.toString();'));
});
Expand Down Expand Up @@ -725,7 +762,7 @@ void main() {
expect(
result,
contains(
'class _\$UuidJsonConverter implements json.JsonConverter<Uuid, String>'));
'class _\$UuidJsonConverter implements json.JsonConverter<Uuid, dynamic>'));
expect(result, contains('fromJson(json) => customUuidParse(json);'));
expect(result, contains('toJson(json) => customUuidToString(json);'));
});
Expand Down
Loading