Skip to content

Commit 21dcfc0

Browse files
authored
phase 4 - testing testing testing (#86)
Running across hundreds of Ogurets based tests now complete
1 parent 357d4f0 commit 21dcfc0

File tree

11 files changed

+138
-40
lines changed

11 files changed

+138
-40
lines changed

README.adoc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,11 @@ The source for the tests is located in src/k8s** folders. The generated test out
409409
property is used, in which case fields will always be be added).
410410
* numeric fields are no longer "cast", they are always converted to their
411411
respective variants, toInt() or toDouble() - down through lists and so forth.
412-
412+
* arrays and maps which have the special provided default values are always
413+
`const` following Dart's requirements around constructors. This means you
414+
cannot "add" to them - i.e. create a model object and then add to the
415+
items in of the lists. If you wish to do this, you _must_ provide a non-const
416+
value for the Map or List in the constructor.
413417
- 5.13 - ability to disable the copyWith generation (see above)
414418
- 5.12 - contributed fixes for inherited types (via https://github.com/roald-di)
415419
- 5.11 - fix date/datetime strings in queries to not be encoded. Updated to use 5.2.1 of OpenAPI. Fixed a bunch

sample-app/SampleRunner/test/api_tests.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ main() {
9191
expect(ap.otherDeps['info'], 'this is top secret');
9292
expect(ap.yetMoreAdditional['sList'], ['a', 'b', 'c']);
9393
expect(
94-
ap.mapWithComplexObject['c1'][0],
94+
ap.mapWithComplexObject['c1']?[0],
9595
Event()
9696
..status = EventStatus.STREAMING
9797
..id = 'xx'
@@ -133,6 +133,15 @@ main() {
133133
expect(data.doubleList, [1.0, 2.6]);
134134
expect(data.doubleMap, {'one': 1.0, 'two': 2.7});
135135
});
136+
test('data serialisation', () {
137+
final data = DoubleAndIntConversion(
138+
basicInt: 43, basicDouble: 26.2, intList: [], doubleMap: {});
139+
expect(data.toJson(), {
140+
'basicInt': 43, 'basicDouble': 26.2, 'intList': [],
141+
'doubleList': [], // because we can't tell between null and empty
142+
'doubleMap': {}
143+
});
144+
});
136145
test("int enums being generated with correct type", () {
137146
expect(IntTypeEnum.number1.toJson(), 1);
138147
expect(IntTypeEnum.number1, IntTypeEnumExtension.fromJson(1));

src/it/k8s/test.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,14 @@ components:
350350
required:
351351
- basicDouble
352352
- basicInt
353+
- intList
354+
- doubleMap
353355
properties:
354356
basicInt:
355357
type: integer
358+
int64Int:
359+
type: integer
360+
format: int64
356361
basicDouble:
357362
type: number
358363
format: double

src/it/k8s/test/api_tests.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ main() {
133133
expect(data.doubleList, [1.0, 2.6]);
134134
expect(data.doubleMap, {'one': 1.0, 'two': 2.7});
135135
});
136+
test('data serialisation', () {
137+
final data = DoubleAndIntConversion(
138+
basicInt: 43, basicDouble: 26.2, intList: [], doubleMap: {});
139+
expect(data.toJson(), {
140+
'basicInt': 43, 'basicDouble': 26.2, 'intList': [],
141+
'doubleList': [], // because we can't tell between null and empty
142+
'doubleMap': {}
143+
});
144+
});
136145
test("int enums being generated with correct type", () {
137146
expect(IntTypeEnum.number1.toJson(), 1);
138147
expect(IntTypeEnum.number1, IntTypeEnumExtension.fromJson(1));

src/it/k8s_null/test.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,14 @@ components:
350350
required:
351351
- basicDouble
352352
- basicInt
353+
- intList
354+
- doubleMap
353355
properties:
354356
basicInt:
355357
type: integer
358+
int64Int:
359+
type: integer
360+
format: int64
356361
basicDouble:
357362
type: number
358363
format: double

src/it/k8s_null/test/api_tests.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ main() {
129129
expect(data.doubleList, [1.0, 2.6]);
130130
expect(data.doubleMap, {'one': 1.0, 'two': 2.7});
131131
});
132+
test('data serialisation', () {
133+
final data = DoubleAndIntConversion(
134+
basicInt: 43, basicDouble: 26.2, intList: [], doubleMap: {});
135+
expect(data.toJson(),
136+
{'basicInt': 43, 'basicDouble': 26.2, 'intList': [], 'doubleMap': {}});
137+
});
132138
test("int enums being generated with correct type", () {
133139
expect(IntTypeEnum.number1.toJson(), 1);
134140
expect(IntTypeEnum.number1, IntTypeEnumExtension.fromJson(1));

src/main/kotlin/com/bluetrainsoftware/openapi/DartV3ApiGenerator.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ class DartV3ApiGenerator : DartClientCodegen() {
216216
return cp.isNullable
217217
}
218218

219+
// in arraysThatHaveADefaultAreNullSafe we force arrays to be non-null if they
220+
// are optional - this does not affect required/optional serialisation
221+
if ((cp.isArray || cp.isMap) && arraysThatHaveADefaultAreNullSafe) {
222+
return false
223+
}
224+
219225
return !cp.required || cp.isNullable
220226
}
221227

@@ -232,6 +238,9 @@ class DartV3ApiGenerator : DartClientCodegen() {
232238
cp.vendorExtensions["x-dart-nullable"] = "1"
233239
}
234240

241+
if ("int" == cp.dataType) {
242+
cp.isInteger = true // OpenAPI is saying int64 int is not an integer
243+
}
235244
if (("DateTime" == cp.complexType) || ("Date" == cp.complexType)) {
236245
cp.isDateTime = "date-time" == cp.getDataFormat()
237246
cp.isDate = "date" == cp.getDataFormat()
@@ -327,10 +336,6 @@ class DartV3ApiGenerator : DartClientCodegen() {
327336
cp.defaultValue = "const " + cp.defaultValue
328337
}
329338

330-
if (!cp.required && nullable(cp)) {
331-
cp.vendorExtensions["x-dont-tojson-null"] = "true"
332-
}
333-
334339
// now allow arrays to be non nullable by making them empty. Only operates on 1st level because
335340
// it affects the constructor and defaults of top level fields
336341
if (classLevelField && cp.isArray && arraysThatHaveADefaultAreNullSafe && cp.defaultValue != null) {

src/main/resources/dart2-v3template/_class_to_json.mustache

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,67 @@
22
{{#vars}}
33
{{^vendorExtensions.x-var-is-binary}}
44
{{^vendorExtensions.x-dart-inherited}}
5-
{{#x-dont-tojson-null}}
6-
} != null) {
7-
{{/x-dont-tojson-null}}
85
{{#isArray}}
6+
{{^required}}
97
{{#vendorExtensions.x-ns-default-val}}
108
if ({{{name}}}{{#vendorExtensions.x-dart-nullable}}{{#nullSafe}}?{{/nullSafe}}{{/vendorExtensions.x-dart-nullable}}.isNotEmpty{{#vendorExtensions.x-dart-nullable}}{{#nullSafe}} == true{{/nullSafe}}{{/vendorExtensions.x-dart-nullable}}) {
119
{{/vendorExtensions.x-ns-default-val}}
12-
13-
json[r'{{baseName}}'] = {{{name}}}{{^nullSafe}}?{{/nullSafe}}{{#vendorExtensions.x-dart-nullable}}{{#nullSafe}}?{{/nullSafe}}{{/vendorExtensions.x-dart-nullable}}.map((v) => LocalApiClient.serialize(v)){{^nullSafe}}?{{/nullSafe}}.toList();
10+
{{/required}}
11+
json[r'{{baseName}}'] = ((v) => {{> _to_json_serialise }} )({{{name}}});
12+
{{^required}}
1413
{{#vendorExtensions.x-ns-default-val}}
1514
}{{> __class_to_json_required_null }}
1615
{{/vendorExtensions.x-ns-default-val}}
17-
16+
{{/required}}
1817
{{/isArray}}
1918
{{#isMap}}
2019
{{#vendorExtensions.x-dart-nullable}}if ({{{name}}}{{#nullSafe}}?{{/nullSafe}}.isNotEmpty{{#nullSafe}} == true{{/nullSafe}}) { {{/vendorExtensions.x-dart-nullable}}
21-
json[r'{{baseName}}'] = Map.fromIterables({{{name}}}{{#vendorExtensions.x-dart-nullable}}{{#nullSafe}}!{{/nullSafe}}{{/vendorExtensions.x-dart-nullable}}.keys,
22-
{{{name}}}{{#vendorExtensions.x-dart-nullable}}{{#nullSafe}}!{{/nullSafe}}{{/vendorExtensions.x-dart-nullable}}.values.map((v) => LocalApiClient.serialize(v)));
20+
{{^required}}{{^vendorExtensions.x-dart-nullable}}if ({{{name}}}.isNotEmpty) { {{/vendorExtensions.x-dart-nullable}}{{/required}}
21+
json[r'{{baseName}}'] = ((v) => {{> _to_json_serialise }} )({{{name}}});
2322
{{#vendorExtensions.x-dart-nullable}}
2423
}{{> __class_to_json_required_null }}
2524
{{/vendorExtensions.x-dart-nullable}}
25+
{{^required}}{{^vendorExtensions.x-dart-nullable}}
26+
}{{> __class_to_json_required_null }}
27+
{{/vendorExtensions.x-dart-nullable}}{{/required}}
2628
{{/isMap}}
2729
{{^items}}
28-
{{#x-dont-tojson-null}}
29-
if ({{{name}}} != null) {
30-
{{/x-dont-tojson-null}}
31-
{{#isDateTime}}
32-
json[r'{{baseName}}'] = {{{name}}}{{^nullSafe}}?{{/nullSafe}}{{#vendorExtensions.x-dart-nullable}}{{#nullSafe}}?{{/nullSafe}}{{/vendorExtensions.x-dart-nullable}}.toUtc(){{^nullSafe}}?{{/nullSafe}}.toIso8601String();
33-
{{/isDateTime}}
34-
{{#isDate}}
35-
json[r'{{baseName}}'] = {{{name}}}{{^nullSafe}}?{{/nullSafe}}{{#vendorExtensions.x-dart-nullable}}{{#nullSafe}}?{{/nullSafe}}{{/vendorExtensions.x-dart-nullable}}.toDateString();
36-
{{/isDate}}
37-
{{#isEnum}}
38-
json[r'{{baseName}}'] = {{{name}}}{{^nullSafe}}?{{/nullSafe}}{{#vendorExtensions.x-dart-nullable}}{{#nullSafe}}?{{/nullSafe}}{{/vendorExtensions.x-dart-nullable}}.toJson();
39-
{{/isEnum}}
30+
{{^required}}
31+
{{#vendorExtensions.x-dart-nullable}}
32+
{{^generateNullValuesToJson}}
33+
if ({{{name}}} != null) {
34+
{{/generateNullValuesToJson}}
35+
{{/vendorExtensions.x-dart-nullable}}
36+
{{/required}}
37+
json[r'{{baseName}}'] =
4038
{{#isModel}}
41-
json[r'{{baseName}}'] = {{{name}}}{{^nullSafe}}?{{/nullSafe}}{{#vendorExtensions.x-dart-nullable}}{{#nullSafe}}?{{/nullSafe}}{{/vendorExtensions.x-dart-nullable}}.toJson();
39+
{{{name}}}{{#vendorExtensions.x-dart-nullable}}?{{/vendorExtensions.x-dart-nullable}}.toJson()
4240
{{/isModel}}
4341
{{#isPrimitiveType}}
44-
json[r'{{baseName}}'] = {{{name}}};
42+
{{{name}}}
4543
{{/isPrimitiveType}}
46-
{{#isAnyType}}
47-
json[r'{{baseName}}'] = {{{name}}};
48-
{{/isAnyType}}
49-
{{#x-dont-tojson-null}}
50-
}{{> __class_to_json_required_null }}
51-
{{/x-dont-tojson-null}}
52-
44+
{{#isEnum}}
45+
{{{name}}}{{#vendorExtensions.x-dart-nullable}}?{{/vendorExtensions.x-dart-nullable}}.toJson()
46+
{{/isEnum}}
47+
{{#isDate}}
48+
{{{name}}}{{#vendorExtensions.x-dart-nullable}}?{{/vendorExtensions.x-dart-nullable}}.toUtc()
49+
{{#vendorExtensions.x-dart-nullable}}{{^nullSafe}}?{{/nullSafe}}{{/vendorExtensions.x-dart-nullable}}.toIso8601String()
50+
{{#vendorExtensions.x-dart-nullable}}{{^nullSafe}}?{{/nullSafe}}{{/vendorExtensions.x-dart-nullable}}.substring(0, 10)
51+
{{/isDate}}
52+
{{#isDateTime}}
53+
{{{name}}}{{#vendorExtensions.x-dart-nullable}}?{{/vendorExtensions.x-dart-nullable}}.toUtc()
54+
{{#vendorExtensions.x-dart-nullable}}{{^nullSafe}}?{{/nullSafe}}{{/vendorExtensions.x-dart-nullable}}.toIso8601String()
55+
{{/isDateTime}}
56+
{{#isAnyType}}{{{name}}}{{/isAnyType}}
57+
;
58+
{{^required}}
59+
{{#vendorExtensions.x-dart-nullable}}
60+
{{^generateNullValuesToJson}}
61+
}
62+
{{/generateNullValuesToJson}}
63+
{{/vendorExtensions.x-dart-nullable}}
64+
{{/required}}
5365
{{/items}}
54-
{{#x-dont-tojson-null}} } {{#generateNullValuesToJson}} else {
55-
json[r'{{baseName}}'] = null;
56-
}{{/generateNullValuesToJson}}{{/x-dont-tojson-null}}
5766
{{/vendorExtensions.x-dart-inherited}}
5867
{{/vendorExtensions.x-var-is-binary}}
5968
{{/vars}}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{{#isArray}}
2+
v{{#vendorExtensions.x-dart-nullable}}?{{/vendorExtensions.x-dart-nullable}}.map((v) => {{#items}}{{> _to_json_serialise }} {{/items}} ){{#vendorExtensions.x-dart-nullable}}?{{/vendorExtensions.x-dart-nullable}}.toList()
3+
{{/isArray}}
4+
{{#isMap}}
5+
Map.fromIterables({{{name}}}{{#vendorExtensions.x-dart-nullable}}{{#nullSafe}}!{{/nullSafe}}{{/vendorExtensions.x-dart-nullable}}.keys,
6+
{{{name}}}{{#vendorExtensions.x-dart-nullable}}{{#nullSafe}}!{{/nullSafe}}{{/vendorExtensions.x-dart-nullable}}.values.map((v) => {{#items}} {{> _to_json_serialise }} {{/items}} ))
7+
{{/isMap}}
8+
{{^items}}
9+
{{#isModel}}
10+
v.toJson()
11+
{{/isModel}}
12+
{{#isPrimitiveType}}
13+
v
14+
{{/isPrimitiveType}}
15+
{{#isEnum}}
16+
(v as {{{dataType}}}){{#vendorExtensions.x-dart-nullable}}?{{/vendorExtensions.x-dart-nullable}}.toJson()
17+
{{/isEnum}}
18+
{{#isDate}}
19+
v{{#vendorExtensions.x-dart-nullable}}?{{/vendorExtensions.x-dart-nullable}}.toUtc()
20+
{{#vendorExtensions.x-dart-nullable}}{{^nullSafe}}?{{/nullSafe}}{{/vendorExtensions.x-dart-nullable}}.toIso8601String()
21+
{{#vendorExtensions.x-dart-nullable}}{{^nullSafe}}?{{/nullSafe}}{{/vendorExtensions.x-dart-nullable}}.substring(0, 10)
22+
{{/isDate}}
23+
{{#isDateTime}}
24+
v{{#vendorExtensions.x-dart-nullable}}?{{/vendorExtensions.x-dart-nullable}}.toUtc()
25+
{{#vendorExtensions.x-dart-nullable}}{{^nullSafe}}?{{/nullSafe}}{{/vendorExtensions.x-dart-nullable}}.toIso8601String()
26+
{{/isDateTime}}
27+
{{#isAnyType}}v{{/isAnyType}}
28+
{{^isModel}}
29+
{{^isPrimitiveType}}
30+
{{^isEnum}}
31+
{{^isDateTime}}
32+
{{^isDate}}
33+
{{^isAnyType}}
34+
--please report--register a YAML snippet that reproduces this error
35+
{{/isAnyType}}
36+
{{/isDate}}
37+
{{/isDateTime}}
38+
{{/isEnum}}
39+
{{/isPrimitiveType}}
40+
{{/isModel}}
41+
{{/items}}

src/test/java/cd/connect/openapi/SampleRunner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public void runGenerator() {
2525
// "--import-mappings", "IntOrString=./int_or_string.dart",
2626
// "--global-property", "skipFormModel=false",
2727
// "--additional-properties", "x-use-5x-nullable=true",
28-
// "--additional-properties", "nullSafe=true",
29-
// "--additional-properties", "nullSafe-array-default=true",
28+
"--additional-properties", "nullSafe=true",
29+
"--additional-properties", "nullSafe-array-default=true",
3030
"--additional-properties", "listAnyOf=true",
3131
"--output", "sample-app/" + getClass().getSimpleName())
3232
.toArray(new String[0]));

0 commit comments

Comments
 (0)