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

Feat: Ability to generate custom annotations for any model properties #775

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions example/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ targets:
- url: "https://raw.githubusercontent.com/epam-cross-platform-lab/swagger-dart-code-generator/master/example/input_folder/pet_service_json.json"
file_name: "some_file_name.json"
- url: "https://raw.githubusercontent.com/epam-cross-platform-lab/swagger-dart-code-generator/master/example/input_folder/pet_service_yaml.yaml"
custom_annotations:
- type_name: 'SomeCustomAnnotations'
swagger_key: 'some_custom_annotations'
with_base_url: true
with_converter: true
use_path_for_request_names: true
Expand Down
3 changes: 2 additions & 1 deletion example/input_folder/pet_service_json.json
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,8 @@
"userStatus": {
"type": "integer",
"format": "int32",
"description": "User Status"
"description": "User Status",
"some_custom_annotations": "ski-ba-bop-ba-dop-bop"
}
},
"xml": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ class User {
@JsonKey(name: 'phone', includeIfNull: false, defaultValue: '')
final String? phone;
@JsonKey(name: 'userStatus', includeIfNull: false)
@SomeCustomAnnotations('ski-ba-bop-ba-dop-bop')
final int? userStatus;
static const fromJsonFactory = _$UserFromJson;

Expand Down
85 changes: 80 additions & 5 deletions lib/src/code_generators/swagger_models_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -526,11 +526,30 @@ class $className implements json.JsonConverter<${value.type}, String> {
typeName = typeName.makeNullable();
}

var jsonCustomAnnotationContent = '';
var rawJson = prop.rawJson;
if (null != rawJson) {
for (var customAnnotation in options.customAnnotations) {
if (rawJson.containsKey(customAnnotation.swaggerKey)) {
var jsonPropKeyValue = rawJson[customAnnotation.swaggerKey];
if (null != jsonPropKeyValue) {
jsonCustomAnnotationContent += "\t@${customAnnotation.typeName}(";
if (jsonPropKeyValue is String) {
jsonCustomAnnotationContent += "'$jsonPropKeyValue'";
} else {
jsonCustomAnnotationContent += "$jsonPropKeyValue";
}
jsonCustomAnnotationContent += ")\n";
}
}
}
}

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

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

JsonEnumValue generateEnumValue({
Expand Down Expand Up @@ -875,7 +894,26 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
typeName += '?';
}

return '\t$jsonKeyContent$deprecatedContent\tfinal $typeName $propertyName;${unknownEnumValue.fromJson}';
var jsonCustomAnnotationContent = '';
var rawJson = prop.rawJson;
if (null != rawJson) {
for (var customAnnotation in options.customAnnotations) {
if (rawJson.containsKey(customAnnotation.swaggerKey)) {
var jsonPropKeyValue = rawJson[customAnnotation.swaggerKey];
if (null != jsonPropKeyValue) {
jsonCustomAnnotationContent += "\t@${customAnnotation.typeName}(";
if (jsonPropKeyValue is String) {
jsonCustomAnnotationContent += "'$jsonPropKeyValue'";
} else {
jsonCustomAnnotationContent += "$jsonPropKeyValue";
}
jsonCustomAnnotationContent += ")\n";
}
}
}
}

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

String generateEnumPropertyContent({
Expand Down Expand Up @@ -1066,8 +1104,26 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
!requiredProperties.contains(propertyKey)) {
listPropertyName = listPropertyName.makeNullable();
}

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

var jsonCustomAnnotationContent = '';
var rawJson = prop.rawJson;
if (null != rawJson) {
for (var customAnnotation in options.customAnnotations) {
if (rawJson.containsKey(customAnnotation.swaggerKey)) {
var jsonPropKeyValue = rawJson[customAnnotation.swaggerKey];
if (null != jsonPropKeyValue) {
jsonCustomAnnotationContent += "\t@${customAnnotation.typeName}(";
if (jsonPropKeyValue is String) {
jsonCustomAnnotationContent += "'$jsonPropKeyValue'";
} else {
jsonCustomAnnotationContent += "$jsonPropKeyValue";
}
jsonCustomAnnotationContent += ")\n";
}
}
}
}
return '$jsonConverterAnnotation$jsonKeyContent$deprecatedContent$jsonCustomAnnotationContent final $listPropertyName ${generateFieldName(propertyName)};${unknownEnumValue.fromJson}';
}

String generateGeneralPropertyContent({
Expand Down Expand Up @@ -1144,7 +1200,26 @@ static $returnType $fromJsonFunction($valueType? value) => $enumNameCamelCase$fr
typeName = typeName.makeNullable();
}

return '\t$jsonConverterAnnotation$jsonKeyContent$isDeprecatedContent final $typeName $propertyName;${unknownEnumValue.fromJson}';
var jsonCustomAnnotationContent = '';
var rawJson = prop.rawJson;
if (null != rawJson) {
for (var customAnnotation in options.customAnnotations) {
if (rawJson.containsKey(customAnnotation.swaggerKey)) {
var jsonPropKeyValue = rawJson[customAnnotation.swaggerKey];
if (null != jsonPropKeyValue) {
jsonCustomAnnotationContent += "\t@${customAnnotation.typeName}(";
if (jsonPropKeyValue is String) {
jsonCustomAnnotationContent += "'$jsonPropKeyValue'";
} else {
jsonCustomAnnotationContent += "$jsonPropKeyValue";
}
jsonCustomAnnotationContent += ")\n";
}
}
}
}

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

String generatePropertyContentByType(
Expand Down
23 changes: 22 additions & 1 deletion lib/src/models/generator_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class GeneratorOptions {
this.multipartFileType = 'List<int>',
this.urlencodedFileType = 'Map<String, String>',
this.generateFirstSucceedResponse = true,
this.customAnnotations = const[],
});

/// Build options from a JSON map.
Expand Down Expand Up @@ -81,6 +82,8 @@ class GeneratorOptions {
final String customReturnType;
final List<String> excludePaths;

@JsonKey(defaultValue: <CustomAnnotationMap>[])
final List<CustomAnnotationMap> customAnnotations;
/// Convert this options instance to JSON.
Map<String, dynamic> toJson() => _$GeneratorOptionsToJson(this);
}
Expand Down Expand Up @@ -200,4 +203,22 @@ class CustomScalar {
});

Map<String, dynamic> toJson() => _$CustomScalarToJson(this);
}
}

@JsonSerializable(fieldRename: FieldRename.snake)
class CustomAnnotationMap {
CustomAnnotationMap({required this.typeName, required this.swaggerKey});

/// Build a default value map from a JSON map.
factory CustomAnnotationMap.fromJson(Map<String, dynamic> json) =>
_$CustomAnnotationMapFromJson(json);

@JsonKey(defaultValue: '')
final String typeName;

@JsonKey(defaultValue: '')
final String swaggerKey;

/// Convert this default value map instance to JSON.
Map<String, dynamic> toJson() => _$CustomAnnotationMapToJson(this);
}
16 changes: 16 additions & 0 deletions lib/src/models/generator_options.g.dart

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

5 changes: 5 additions & 0 deletions lib/src/swagger_models/responses/swagger_schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class SwaggerSchema {
this.readOnly = false,
this.writeOnly = false,
this.deprecated = false,
this.rawJson,
}) : _type = type;

@JsonKey(name: 'readOnly')
Expand All @@ -36,6 +37,9 @@ class SwaggerSchema {
@JsonKey(name: 'writeOnly')
bool writeOnly;

@JsonKey(name: 'rawJson')
Map<String, dynamic>? rawJson;

@JsonKey(name: 'type')
dynamic _type;
String get type {
Expand Down Expand Up @@ -132,6 +136,7 @@ class SwaggerSchema {

factory SwaggerSchema.fromJson(Map<String, dynamic> json) =>
_$SwaggerSchemaFromJson(json)
..rawJson = json
..enumNames = ((json[kEnumNames] ?? json[kEnumVarnames]) as List?)
?.map((e) => e as String)
.toList()
Expand Down
2 changes: 2 additions & 0 deletions lib/src/swagger_models/responses/swagger_schema.g.dart

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