Skip to content

Fix unescaped string usage while generating ValueSourceClass #804

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

Merged
merged 5 commits into from
Jan 30, 2020
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ David Morgan/Google Inc. <[email protected]>
Ivan Inozemtsev/Google Inc. <[email protected]>
John Dengis <[email protected]>
Resul Alkan/MetGlobal <[email protected]>
Dao Hoang Son <[email protected]>
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

# 7.0.9

- Fix unescaped string usages while generating `ValueSourceClass`.

# 7.0.8

- Fix `analyzer` lower bound: was `0.39.0`, needs to be `0.39.3`.
Expand Down
6 changes: 4 additions & 2 deletions built_value_generator/lib/src/value_source_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:built_value/built_value.dart';
import 'package:built_value_generator/src/fixes.dart';
import 'package:built_value_generator/src/memoized_getter.dart';
import 'package:built_value_generator/src/value_source_field.dart';
import 'package:built_value_generator/src/strings.dart';
import 'package:quiver/iterables.dart';
import 'package:source_gen/source_gen.dart';

Expand Down Expand Up @@ -685,7 +686,7 @@ abstract class ValueSourceClass
for (var field in requiredFields) {
result.writeln('if (${field.name} == null) {');
result.writeln(
"throw new BuiltValueNullFieldError('$name', '${field.name}');");
"throw new BuiltValueNullFieldError('$name', '${escapeString(field.name)}');");
result.writeln('}');
}
// If there are generic parameters, check they are not "dynamic".
Expand Down Expand Up @@ -739,7 +740,8 @@ abstract class ValueSourceClass
} else {
result.writeln("return (newBuiltValueToStringHelper('$name')");
result.writeln(fields
.map((field) => "..add('${field.name}', ${field.name})")
.map((field) =>
"..add('${escapeString(field.name)}', ${field.name})")
.join(''));
result.writeln(').toString();');
}
Expand Down
3 changes: 3 additions & 0 deletions end_to_end_test/lib/values.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ abstract class SimpleValue implements Built<SimpleValue, SimpleValueBuilder> {
@nullable
String get aString;

@nullable
bool get $mustBeEscaped;

factory SimpleValue([void Function(SimpleValueBuilder) updates]) =
_$SimpleValue;
SimpleValue._();
Expand Down
33 changes: 28 additions & 5 deletions end_to_end_test/lib/values.g.dart

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

5 changes: 4 additions & 1 deletion end_to_end_test/test/values_serializer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ void main() {
group('SimpleValue', () {
var data = SimpleValue((b) => b
..anInt = 1
..aString = 'two');
..aString = 'two'
..$mustBeEscaped = true);
var serialized = [
'SimpleValue',
'anInt',
1,
'aString',
'two',
'\$mustBeEscaped',
true,
];

test('can be serialized', () {
Expand Down
26 changes: 20 additions & 6 deletions end_to_end_test/test/values_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,24 @@ void main() {
test('fields can be set via build constructor', () {
final value = SimpleValue((b) => b
..anInt = 1
..aString = 'two');
..aString = 'two'
..$mustBeEscaped = true);
expect(value.anInt, 1);
expect(value.aString, 'two');
expect(value.$mustBeEscaped, true);
});

test('fields can be updated via rebuild method', () {
final value = SimpleValue((b) => b
..anInt = 0
..aString = '').rebuild((b) => b
..aString = ''
..$mustBeEscaped = true).rebuild((b) => b
..anInt = 1
..aString = 'two');
..aString = 'two'
..$mustBeEscaped = false);
expect(value.anInt, 1);
expect(value.aString, 'two');
expect(value.$mustBeEscaped, false);
});

test('builder can be instantiated', () {
Expand Down Expand Up @@ -88,8 +93,15 @@ void main() {
test('hash matches quiver hash', () {
final value = SimpleValue((b) => b
..anInt = 73
..aString = 'seventythree');
expect(value.hashCode, hashObjects(<Object>[value.anInt, value.aString]));
..aString = 'seventythree'
..$mustBeEscaped = true);
expect(
value.hashCode,
hashObjects(<Object>[
value.anInt,
value.aString,
value.$mustBeEscaped,
]));
});

test('hashes equal when equal', () {
Expand All @@ -115,10 +127,12 @@ void main() {
test('has toString', () {
final value1 = SimpleValue((b) => b
..anInt = 0
..aString = '');
..aString = ''
..$mustBeEscaped = true);
expect(value1.toString(), '''SimpleValue {
anInt=0,
aString=,
\$mustBeEscaped=true,
}''');
});
});
Expand Down