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

[dart][dart-dio] Generate the correct serializers #8357

Merged
merged 1 commit into from
Jan 7, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
List<CodegenOperation> operationList = (List<CodegenOperation>) operations.get("operation");

Set<Map<String, Object>> serializers = new HashSet<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure this needs to be a set of maps? why not something simpler to debug like a list of tuples?

Copy link
Contributor Author

@kuhnroyal kuhnroyal Jan 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what mustache supports here, I assume with pairs you would somehow have to access .first .second in the templates, never seen this. I just followed the same way that is used everywhere else.

Set<String> modelImports = new HashSet<>();
Set<String> fullImports = new HashSet<>();

Expand All @@ -314,6 +315,13 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
param.baseType = "MultipartFile";
param.dataType = "MultipartFile";
}
if (param.isContainer) {
final Map<String, Object> serializer = new HashMap<>();
serializer.put("isArray", param.isArray);
serializer.put("isMap", param.isMap);
serializer.put("baseType", param.baseType);
serializers.add(serializer);
}
}

op.vendorExtensions.put("x-is-json", isJson);
Expand All @@ -336,10 +344,19 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
}
modelImports.addAll(imports);
op.imports = imports;

if (op.returnContainer != null) {
final Map<String, Object> serializer = new HashMap<>();
serializer.put("isArray", Objects.equals("array", op.returnContainer));
serializer.put("isMap", Objects.equals("map", op.returnContainer));
serializer.put("baseType", op.returnBaseType);
serializers.add(serializer);
}
}

objs.put("modelImports", modelImports);
objs.put("fullImports", fullImports);
objs.put("serializers", serializers);

return objs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,26 @@ import 'package:built_value/standard_json_plugin.dart';
import 'package:{{pubName}}/local_date_serializer.dart';{{/timeMachine}}
{{#models}}{{#model}}import 'package:{{pubName}}/model/{{classFilename}}.dart';
{{/model}}{{/models}}

part 'serializers.g.dart';

@SerializersFor(const [
{{#models}}{{#model}}{{classname}},
{{/model}}{{/models}}
@SerializersFor(const [{{#models}}{{#model}}
{{classname}},{{/model}}{{/models}}
])

//allow all models to be serialized within a list
Serializers serializers = (_$serializers.toBuilder()
{{#models}}{{#model}}..addBuilderFactory(
const FullType(BuiltList, const [const FullType({{classname}})]),
() => new ListBuilder<{{classname}}>())
{{/model}}{{/models}}
..add(Iso8601DateTimeSerializer())
).build();
Serializers serializers = (_$serializers.toBuilder(){{#apiInfo}}{{#apis}}{{#serializers}}
..addBuilderFactory(
{{#isArray}}
const FullType(BuiltList, [FullType({{baseType}})]),
() => ListBuilder<{{baseType}}>(),
{{/isArray}}
{{#isMap}}
const FullType(BuiltMap, [FullType(String), FullType({{baseType}})]),
() => MapBuilder<String, {{baseType}}>(),
{{/isMap}}
){{/serializers}}{{/apis}}{{/apiInfo}}{{#timeMachine}}
..add(OffsetDateSerializer())
..add(OffsetDateTimeSerializer()){{/timeMachine}}
..add(Iso8601DateTimeSerializer()))
.build();

Serializers standardSerializers =
(serializers.toBuilder()
{{#timeMachine}}..add(OffsetDateSerializer())
..add(OffsetDateTimeSerializer())
{{/timeMachine}}..addPlugin(StandardJsonPlugin())).build();
(serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,31 @@ import 'package:openapi/model/pet.dart';
import 'package:openapi/model/tag.dart';
import 'package:openapi/model/user.dart';


part 'serializers.g.dart';

@SerializersFor(const [
ApiResponse,
Category,
Order,
Pet,
Tag,
User,

ApiResponse,
Category,
Order,
Pet,
Tag,
User,
])

//allow all models to be serialized within a list
Serializers serializers = (_$serializers.toBuilder()
..addBuilderFactory(
const FullType(BuiltList, const [const FullType(ApiResponse)]),
() => new ListBuilder<ApiResponse>())
..addBuilderFactory(
const FullType(BuiltList, const [const FullType(Category)]),
() => new ListBuilder<Category>())
..addBuilderFactory(
const FullType(BuiltList, const [const FullType(Order)]),
() => new ListBuilder<Order>())
..addBuilderFactory(
const FullType(BuiltList, const [const FullType(Pet)]),
() => new ListBuilder<Pet>())
..addBuilderFactory(
const FullType(BuiltList, const [const FullType(Tag)]),
() => new ListBuilder<Tag>())
..addBuilderFactory(
const FullType(BuiltList, const [const FullType(User)]),
() => new ListBuilder<User>())

..add(Iso8601DateTimeSerializer())
).build();
..addBuilderFactory(
const FullType(BuiltList, [FullType(Pet)]),
() => ListBuilder<Pet>(),
)
..addBuilderFactory(
const FullType(BuiltMap, [FullType(String), FullType(int)]),
() => MapBuilder<String, int>(),
)
..addBuilderFactory(
const FullType(BuiltList, [FullType(User)]),
() => ListBuilder<User>(),
)
..add(Iso8601DateTimeSerializer()))
.build();

Serializers standardSerializers =
(serializers.toBuilder()
..addPlugin(StandardJsonPlugin())).build();
(serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,31 @@ import 'package:openapi/model/pet.dart';
import 'package:openapi/model/tag.dart';
import 'package:openapi/model/user.dart';


part 'serializers.g.dart';

@SerializersFor(const [
ApiResponse,
Category,
Order,
Pet,
Tag,
User,

ApiResponse,
Category,
Order,
Pet,
Tag,
User,
])

//allow all models to be serialized within a list
Serializers serializers = (_$serializers.toBuilder()
..addBuilderFactory(
const FullType(BuiltList, const [const FullType(ApiResponse)]),
() => new ListBuilder<ApiResponse>())
..addBuilderFactory(
const FullType(BuiltList, const [const FullType(Category)]),
() => new ListBuilder<Category>())
..addBuilderFactory(
const FullType(BuiltList, const [const FullType(Order)]),
() => new ListBuilder<Order>())
..addBuilderFactory(
const FullType(BuiltList, const [const FullType(Pet)]),
() => new ListBuilder<Pet>())
..addBuilderFactory(
const FullType(BuiltList, const [const FullType(Tag)]),
() => new ListBuilder<Tag>())
..addBuilderFactory(
const FullType(BuiltList, const [const FullType(User)]),
() => new ListBuilder<User>())

..add(Iso8601DateTimeSerializer())
).build();
..addBuilderFactory(
const FullType(BuiltList, [FullType(Pet)]),
() => ListBuilder<Pet>(),
)
..addBuilderFactory(
const FullType(BuiltMap, [FullType(String), FullType(int)]),
() => MapBuilder<String, int>(),
)
..addBuilderFactory(
const FullType(BuiltList, [FullType(User)]),
() => ListBuilder<User>(),
)
..add(Iso8601DateTimeSerializer()))
.build();

Serializers standardSerializers =
(serializers.toBuilder()
..addPlugin(StandardJsonPlugin())).build();
(serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();
Loading