Skip to content

Commit e8f26d4

Browse files
authored
Fix nullable (#84)
* phase 1 - fix nullable vs required * phase 2 - fromJson constructor working * phase 3 - copyWith, toJson, etc - all currently working * phase 4 - toJson dealing with non-nullable non-required fields * phase 5 - niggly nullable issues
1 parent bc4c02e commit e8f26d4

24 files changed

+402
-834
lines changed

README.adoc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,12 @@ mvn clean verify
395395
The source for the tests is located in src/k8s** folders. The generated test output will be in target/it/k8s**.
396396

397397
==== Changelog
398-
- 6.1 - migrate to the *6.0.1* version of OpenAPI, move to Java 11, swap to Kotlin. This is advertised as a breaking change and has led to some classes changing their names, adding new classes which didn't exist before.
398+
- 6.1 - migrate to the *6.0.1* version of OpenAPI, move to Java 11, swap to Kotlin. This is advertised as a breaking change and has led to some classes changing their names, adding new classes which didn't exist before.
399+
* fields that are marked as `required` in the OpenAPI but have default values are not required in constructors (Dart fields with default values cannot be required)
400+
* previously `required` = non-null, now nullable = non-null, required can be null, it just means it has
401+
to be sent in the data. To revert to 5.x behaviour where !required = nullable, add an additional property
402+
of `x-use-5x-nullable=true`. For compatibility with other languages, we recommend you do not stick
403+
with this for long.
399404
- 5.13 - ability to disable the copyWith generation (see above)
400405
- 5.12 - contributed fixes for inherited types (via https://github.com/roald-di)
401406
- 5.11 - fix date/datetime strings in queries to not be encoded. Updated to use 5.2.1 of OpenAPI. Fixed a bunch

src/it/k8s/test.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ components:
398398
type: string
399399
readings:
400400
type: object
401+
nullable: true
401402
additionalProperties:
402403
type: number
403404
format: decimal
@@ -409,28 +410,33 @@ components:
409410
$ref: "#/components/schemas/TimeType"
410411
dependencies:
411412
type: object
413+
nullable: true
412414
additionalProperties:
413415
type: array
414416
items:
415417
$ref: "#/components/schemas/JSONType"
416418
otherDeps:
417419
type: object
420+
nullable: true
418421
additionalProperties:
419422
$ref: "#/components/schemas/SecondJsonType"
420423
yetMoreAdditional:
421424
type: object
425+
nullable: true
422426
additionalProperties:
423427
type: array
424428
items:
425429
type: string
426430
mapWithComplexObject:
427431
type: object
432+
nullable: true
428433
additionalProperties:
429434
type: array
430435
items:
431436
$ref: "#/components/schemas/Event"
432437
mapWithEnums:
433438
type: object
439+
nullable: true
434440
additionalProperties:
435441
type: array
436442
items:
@@ -562,6 +568,7 @@ components:
562568
type: number
563569
NumericAndSpacedEnum:
564570
type: string
571+
nullable: true
565572
enum:
566573
- Ahoy There
567574
- 18 Spaces Here
@@ -593,3 +600,17 @@ components:
593600
items:
594601
type: number
595602
format: double
603+
PatchDocument:
604+
type: object
605+
properties:
606+
value:
607+
nullable: true
608+
path:
609+
type: string
610+
nullable: true
611+
op:
612+
type: string
613+
nullable: true
614+
from:
615+
type: string
616+
nullable: true

src/it/k8s_null/test.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ components:
398398
type: string
399399
readings:
400400
type: object
401+
nullable: true
401402
additionalProperties:
402403
type: number
403404
format: decimal
@@ -409,28 +410,33 @@ components:
409410
$ref: "#/components/schemas/TimeType"
410411
dependencies:
411412
type: object
413+
nullable: true
412414
additionalProperties:
413415
type: array
414416
items:
415417
$ref: "#/components/schemas/JSONType"
416418
otherDeps:
417419
type: object
420+
nullable: true
418421
additionalProperties:
419422
$ref: "#/components/schemas/SecondJsonType"
420423
yetMoreAdditional:
421424
type: object
425+
nullable: true
422426
additionalProperties:
423427
type: array
424428
items:
425429
type: string
426430
mapWithComplexObject:
427431
type: object
432+
nullable: true
428433
additionalProperties:
429434
type: array
430435
items:
431436
$ref: "#/components/schemas/Event"
432437
mapWithEnums:
433438
type: object
439+
nullable: true
434440
additionalProperties:
435441
type: array
436442
items:
@@ -562,10 +568,11 @@ components:
562568
type: number
563569
NumericAndSpacedEnum:
564570
type: string
571+
nullable: true
565572
enum:
566573
- Ahoy There
567-
- application+json
568574
- 18 Spaces Here
575+
- application+json
569576
- 667
570577
IntTypeEnum:
571578
type: integer

src/it/k8s_null/test/api_tests.dart

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,11 @@ main() {
3737
// it wouldn't change the hash
3838
test(
3939
'hashing an object which has two fields of the same type is still different',
40-
() {
41-
var ht = HashTest()
42-
..fieldOne = false
43-
..fieldTwo = true;
44-
var ht1 = HashTest()
45-
..fieldOne = true
46-
..fieldTwo = false;
47-
expect(false, ht.hashCode == ht1.hashCode);
48-
});
40+
() {
41+
var ht = HashTest(fieldOne: false, fieldTwo: true);
42+
var ht1 = HashTest(fieldOne: true, fieldTwo: false);
43+
expect(false, ht.hashCode == ht1.hashCode);
44+
});
4945

5046
test('additional properties mappings', () {
5147
const addProp = {
@@ -92,36 +88,36 @@ main() {
9288
expect(ap.yetMoreAdditional?['sList'], ['a', 'b', 'c']);
9389
expect(
9490
ap.mapWithComplexObject?['c1']?[0],
95-
Event()
96-
..status = EventStatus.STREAMING
97-
..id = 'xx'
98-
..title = 'Scully'
99-
..img = 'img'
100-
..imageUrl = 'http://blah');
91+
Event(
92+
status: EventStatus.STREAMING,
93+
id: 'xx',
94+
title: 'Scully',
95+
img: 'img',
96+
imageUrl: 'http://blah'));
10197
expect(ap.mapWithEnums?['statuses'],
10298
[EventStatus.STREAMING, EventStatus.CLOSED]);
10399
});
104100

105101
test("List<AnyOf<MyApple,MyBanana>> - parsing json array with discriminator",
106-
() {
107-
final items =
102+
() {
103+
final items =
108104
AnyOfMyAppleMyBanana.listFromJson(jsonDecode(_dummyDiscriminatorJson));
109105

110-
expect(items, hasLength(2));
111-
expect(items[0].discriminator, AnyOfDiscriminatorMyAppleMyBanana.MyApple);
112-
expect(items[0].asMyApple().type, "apple");
113-
expect(items[0].asMyApple().kind, "Foxwhelp");
114-
expect(items[1].discriminator, AnyOfDiscriminatorMyAppleMyBanana.MyBanana);
115-
expect(items[1].asMyBanana().type, "banana");
116-
expect(items[1].asMyBanana().count, 42);
117-
});
106+
expect(items, hasLength(2));
107+
expect(items[0].discriminator, AnyOfDiscriminatorMyAppleMyBanana.MyApple);
108+
expect(items[0].asMyApple().type, "apple");
109+
expect(items[0].asMyApple().kind, "Foxwhelp");
110+
expect(items[1].discriminator, AnyOfDiscriminatorMyAppleMyBanana.MyBanana);
111+
expect(items[1].asMyBanana().type, "banana");
112+
expect(items[1].asMyBanana().count, 42);
113+
});
118114
test("int enums being generated with correct type", () {
119115
expect(IntTypeEnum.number1.toJson(), 1);
120116
expect(IntTypeEnum.number1, IntTypeEnumExtension.fromJson(1));
121117
});
122118
test(
123119
"enums included in a model via allOf with reference will be treated as"
124-
"enums and generate valid code ", () {
120+
"enums and generate valid code ", () {
125121
final testO = ObjectContainingEnum.fromJson(
126122
{"name": "foobar", "enumFieldAllOf": "667"});
127123
expect(testO.name, "foobar");

0 commit comments

Comments
 (0)