Skip to content

Commit

Permalink
fix: escape strings containing reserved characters (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
tnc1997 authored Aug 26, 2024
1 parent f7077f7 commit 0cf42f8
Show file tree
Hide file tree
Showing 31 changed files with 539 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:source_helper/source_helper.dart';

import 'builder_generator.dart';

class XmlAttributeBuilderGenerator extends BuilderGenerator {
Expand Down Expand Up @@ -25,10 +27,10 @@ class XmlAttributeBuilderGenerator extends BuilderGenerator {
buffer.write('if ($expression != null) { ');
}

buffer.write('$builder.attribute(\'$_name\', $expression');
buffer.write('$builder.attribute(${escapeDartString(_name)}, $expression');

if (_namespace != null) {
buffer.write(', namespace: \'$_namespace\'');
buffer.write(', namespace: ${escapeDartString(_namespace!)}');
}

buffer.write(');');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:source_helper/source_helper.dart';

import 'builder_generator.dart';

class XmlConverterXmlElementBuilderGenerator extends BuilderGenerator {
Expand Down Expand Up @@ -39,10 +41,10 @@ class XmlConverterXmlElementBuilderGenerator extends BuilderGenerator {
buffer.write('if ($expression != null) { ');
}

buffer.write('$builder.element(\'$_name\'');
buffer.write('$builder.element(${escapeDartString(_name)}');

if (_namespace != null) {
buffer.write(', namespace: \'$_namespace\'');
buffer.write(', namespace: ${escapeDartString(_namespace!)}');
}

if (_isSelfClosing != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:source_helper/source_helper.dart';

import 'builder_generator.dart';

class XmlRootElementBuilderGenerator extends BuilderGenerator {
Expand Down Expand Up @@ -30,10 +32,10 @@ class XmlRootElementBuilderGenerator extends BuilderGenerator {
buffer.write('if ($expression != null) { ');
}

buffer.write('$builder.element(\'$_name\'');
buffer.write('$builder.element(${escapeDartString(_name)}');

if (_namespace != null) {
buffer.write(', namespace: \'$_namespace\'');
buffer.write(', namespace: ${escapeDartString(_namespace!)}');
}

buffer.write(', namespaces: namespaces');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:source_helper/source_helper.dart';

import 'builder_generator.dart';

class XmlSerializableXmlElementBuilderGenerator extends BuilderGenerator {
Expand Down Expand Up @@ -35,10 +37,10 @@ class XmlSerializableXmlElementBuilderGenerator extends BuilderGenerator {
buffer.write('if ($expression != null) { ');
}

buffer.write('$builder.element(\'$_name\'');
buffer.write('$builder.element(${escapeDartString(_name)}');

if (_namespace != null) {
buffer.write(', namespace: \'$_namespace\'');
buffer.write(', namespace: ${escapeDartString(_namespace!)}');
}

if (_isSelfClosing != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:source_helper/source_helper.dart';

import 'builder_generator.dart';

class XmlTextXmlElementBuilderGenerator extends BuilderGenerator {
Expand Down Expand Up @@ -35,10 +37,10 @@ class XmlTextXmlElementBuilderGenerator extends BuilderGenerator {
buffer.write('if ($expression != null) { ');
}

buffer.write('$builder.element(\'$_name\'');
buffer.write('$builder.element(${escapeDartString(_name)}');

if (_namespace != null) {
buffer.write(', namespace: \'$_namespace\'');
buffer.write(', namespace: ${escapeDartString(_namespace!)}');
}

if (_isSelfClosing != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:source_helper/source_helper.dart';

import 'constructor_generator.dart';

class XmlAttributeConstructorGenerator extends ConstructorGenerator {
Expand Down Expand Up @@ -25,10 +27,10 @@ class XmlAttributeConstructorGenerator extends ConstructorGenerator {
buffer.write('$expression != null ? ');
}

buffer.write('XmlAttribute(XmlName(\'$_name\'');
buffer.write('XmlAttribute(XmlName(${escapeDartString(_name)}');

if (_namespace != null) {
buffer.write(', namespaces[\'$_namespace\']');
buffer.write(', namespaces[${escapeDartString(_namespace!)}]');
}

buffer.write('), $expression)');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:source_helper/source_helper.dart';

import 'constructor_generator.dart';

class XmlConverterXmlElementConstructorGenerator extends ConstructorGenerator {
Expand Down Expand Up @@ -39,10 +41,10 @@ class XmlConverterXmlElementConstructorGenerator extends ConstructorGenerator {
buffer.write('$expression != null ? ');
}

buffer.write('XmlElement(XmlName(\'$_name\'');
buffer.write('XmlElement(XmlName(${escapeDartString(_name)}');

if (_namespace != null) {
buffer.write(', namespaces[\'$_namespace\']');
buffer.write(', namespaces[${escapeDartString(_namespace!)}]');
}

buffer.write('), ');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:source_helper/source_helper.dart';

import 'constructor_generator.dart';

class XmlRootElementConstructorGenerator extends ConstructorGenerator {
Expand Down Expand Up @@ -30,10 +32,10 @@ class XmlRootElementConstructorGenerator extends ConstructorGenerator {
buffer.write('$expression != null ? ');
}

buffer.write('XmlElement(XmlName(\'$_name\'');
buffer.write('XmlElement(XmlName(${escapeDartString(_name)}');

if (_namespace != null) {
buffer.write(', namespaces[\'$_namespace\']');
buffer.write(', namespaces[${escapeDartString(_namespace!)}]');
}

buffer.write(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:source_helper/source_helper.dart';

import 'constructor_generator.dart';

class XmlSerializableXmlElementConstructorGenerator
Expand Down Expand Up @@ -36,10 +38,10 @@ class XmlSerializableXmlElementConstructorGenerator
buffer.write('$expression != null ? ');
}

buffer.write('XmlElement(XmlName(\'$_name\'');
buffer.write('XmlElement(XmlName(${escapeDartString(_name)}');

if (_namespace != null) {
buffer.write(', namespaces[\'$_namespace\']');
buffer.write(', namespaces[${escapeDartString(_namespace!)}]');
}

buffer.write('), $expression');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:source_helper/source_helper.dart';

import 'constructor_generator.dart';

class XmlTextXmlElementConstructorGenerator extends ConstructorGenerator {
Expand Down Expand Up @@ -35,10 +37,10 @@ class XmlTextXmlElementConstructorGenerator extends ConstructorGenerator {
buffer.write('$expression != null ? ');
}

buffer.write('XmlElement(XmlName(\'$_name\'');
buffer.write('XmlElement(XmlName(${escapeDartString(_name)}');

if (_namespace != null) {
buffer.write(', namespaces[\'$_namespace\']');
buffer.write(', namespaces[${escapeDartString(_namespace!)}]');
}

buffer.write('), [], ');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:source_helper/source_helper.dart';

import 'getter_generator.dart';

class XmlAttributeGetterGenerator extends GetterGenerator {
Expand All @@ -21,10 +23,10 @@ class XmlAttributeGetterGenerator extends GetterGenerator {
String generateGetter(String expression) {
final buffer = StringBuffer(expression);

buffer.write('.getAttribute(\'$_name\'');
buffer.write('.getAttribute(${escapeDartString(_name)}');

if (_namespace != null) {
buffer.write(', namespace: \'$_namespace\'');
buffer.write(', namespace: ${escapeDartString(_namespace!)}');
}

buffer.write(')');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:source_helper/source_helper.dart';

import 'getter_generator.dart';

abstract class XmlElementGetterGenerator extends GetterGenerator {
Expand All @@ -21,10 +23,10 @@ abstract class XmlElementGetterGenerator extends GetterGenerator {
String generateGetter(String expression) {
final buffer = StringBuffer(expression);

buffer.write('.getElement(\'$_name\'');
buffer.write('.getElement(${escapeDartString(_name)}');

if (_namespace != null) {
buffer.write(', namespace: \'$_namespace\'');
buffer.write(', namespace: ${escapeDartString(_namespace!)}');
}

buffer.write(')');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:source_helper/source_helper.dart';

import 'getter_generator.dart';

abstract class XmlElementIterableGetterGenerator extends GetterGenerator {
Expand All @@ -21,10 +23,10 @@ abstract class XmlElementIterableGetterGenerator extends GetterGenerator {
String generateGetter(String expression) {
final buffer = StringBuffer(expression);

buffer.write('.getElements(\'$_name\'');
buffer.write('.getElements(${escapeDartString(_name)}');

if (_namespace != null) {
buffer.write(', namespace: \'$_namespace\'');
buffer.write(', namespace: ${escapeDartString(_namespace!)}');
}

buffer.write(')');
Expand Down
1 change: 1 addition & 0 deletions xml_serializable/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies:
build_config: ^1.0.0
recase: ^4.0.0
source_gen: ^1.0.0
source_helper: ^1.0.0
xml: '>=5.0.0 <7.0.0'
xml_annotation: ^2.4.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@ void main() {
},
);

test(
'should generate a builder with an escaped name if constructed with a name that contains a reserved character',
() {
expect(
XmlAttributeBuilderGenerator('\$name').generateBuilder('value'),
equals(
'builder.attribute(r\'\$name\', value);',
),
);
},
);

test(
'should generate a builder with an escaped name and namespace if constructed with a name and namespace that contain a reserved character',
() {
expect(
XmlAttributeBuilderGenerator(
'\$name',
namespace: '\$https://www.example.com',
).generateBuilder('value'),
equals(
'builder.attribute(r\'\$name\', value, namespace: r\'\$https://www.example.com\');',
),
);
},
);

test(
'should generate a null-aware builder if constructed with a nullable type',
() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,37 @@ void main() {
},
);

test(
'should generate a builder with an escaped name if constructed with a name that contains a reserved character',
() {
expect(
XmlConverterXmlElementBuilderGenerator(
'\$name',
'TestConverter',
).generateBuilder('value'),
equals(
'builder.element(r\'\$name\', nest: () { const TestConverter().buildXmlChildren(value, builder, namespaces: namespaces); });',
),
);
},
);

test(
'should generate a builder with an escaped name and namespace if constructed with a name and namespace that contain a reserved character',
() {
expect(
XmlConverterXmlElementBuilderGenerator(
'\$name',
'TestConverter',
namespace: '\$https://www.example.com',
).generateBuilder('value'),
equals(
'builder.element(r\'\$name\', namespace: r\'\$https://www.example.com\', nest: () { const TestConverter().buildXmlChildren(value, builder, namespaces: namespaces); });',
),
);
},
);

test(
'should generate a null-aware builder if constructed with a nullable type',
() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@ void main() {
},
);

test(
'should generate a builder with an escaped name if constructed with a name that contains a reserved character',
() {
expect(
XmlRootElementBuilderGenerator('\$name')
.generateBuilder('value'),
equals(
'builder.element(r\'\$name\', namespaces: namespaces, nest: () { value.buildXmlChildren(builder, namespaces: namespaces); });',
),
);
},
);

test(
'should generate a builder with an escaped name and namespace if constructed with a name and namespace that contain a reserved character',
() {
expect(
XmlRootElementBuilderGenerator(
'\$name',
namespace: '\$https://www.example.com',
).generateBuilder('value'),
equals(
'builder.element(r\'\$name\', namespace: r\'\$https://www.example.com\', namespaces: namespaces, nest: () { value.buildXmlChildren(builder, namespaces: namespaces); });',
),
);
},
);

test(
'should generate a null-aware builder if constructed with a nullable type',
() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,34 @@ void main() {
},
);

test(
'should generate a builder with an escaped name if constructed with a name that contains a reserved character',
() {
expect(
XmlSerializableXmlElementBuilderGenerator('\$name')
.generateBuilder('value'),
equals(
'builder.element(r\'\$name\', nest: () { value.buildXmlChildren(builder, namespaces: namespaces); });',
),
);
},
);

test(
'should generate a builder with an escaped name and namespace if constructed with a name and namespace that contain a reserved character',
() {
expect(
XmlSerializableXmlElementBuilderGenerator(
'\$name',
namespace: '\$https://www.example.com',
).generateBuilder('value'),
equals(
'builder.element(r\'\$name\', namespace: r\'\$https://www.example.com\', nest: () { value.buildXmlChildren(builder, namespaces: namespaces); });',
),
);
},
);

test(
'should generate a null-aware builder if constructed with a nullable type',
() {
Expand Down
Loading

0 comments on commit 0cf42f8

Please sign in to comment.