Skip to content

Commit

Permalink
Fixed arrays of scalar types not getting converter added to their pro…
Browse files Browse the repository at this point in the history
…perty
  • Loading branch information
dan3988 committed Sep 17, 2024
1 parent 3ed63a8 commit c053405
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/src/code_generators/swagger_models_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase {
return '';
}

return '\n@_\$${schema.format.pascalCase}JsonConverter()';
return '@_\$${schema.format.pascalCase}JsonConverter()';
}

String generateJsonConverters() {
Expand Down Expand Up @@ -1017,6 +1017,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
required Map<String, SwaggerSchema> allClasses,
required bool isDeprecated,
}) {
final jsonConverterAnnotation = prop.items == null ? '' : generatePropertyJsonConverterAnnotation(prop.items!);
final typeName = _generateListPropertyTypeName(
allEnumListNames: allEnumListNames,
allEnumNames: allEnumNames,
Expand Down Expand Up @@ -1066,7 +1067,7 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
listPropertyName = listPropertyName.makeNullable();
}

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

String generateGeneralPropertyContent({
Expand Down
7 changes: 7 additions & 0 deletions test/code_examples.dart
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,13 @@ const String schemasWithUuidsInProperties = '''
"format": "uuid",
"description": "Some description"
},
"list": {
"type": "array",
"items": {
"type": "string",
"format": "uuid"
}
},
"showPageAvailable": {
"type": "boolean",
"description": "Flag indicating showPage availability"
Expand Down
1 change: 1 addition & 0 deletions test/generator_tests/models_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,7 @@ void main() {
);

expect(result, contains(RegExp(r'''@_\$UuidJsonConverter\(\)\s*@JsonKey\(name: 'id'\)\s*final Uuid\? id;''')));
expect(result, contains(RegExp(r'''@_\$UuidJsonConverter\(\)\s*@JsonKey\(name: 'list', defaultValue: <Uuid>\[\]\)\s*final List<Uuid>\? list;''')));
expect(result, contains('class _\$UuidJsonConverter implements json.JsonConverter<Uuid, String>'));
expect(result, contains('fromJson(json) => Uuid.parse(json);'));
expect(result, contains('toJson(json) => json.toString();'));
Expand Down

0 comments on commit c053405

Please sign in to comment.