Skip to content

Optional enum fields in type structs don't handle null values properly #71

@psstoev

Description

@psstoev

When an enum field in struct is made {required: false} and the value for it is set to null the null stays even if there is a default value. If there isn't a default value the null replaces undefined and the enum value is later written as null in the JSON (instead of missing from the result).
Example test:

it.('allows complex type definition (optional non-default enums)', function() {
	api.enum('TestEnum', {A: 1, B: 2});
	api.type('Test', {
		notRequiredDefault: {
			type: 'TestEnum',
			required: false,
			default: 1,
		},
		notRequiredNoDefault: {
			type: 'TestEnum',
			required: false
		}
	});

	const testType = api.type('Test');

	expect(testType.convert({
		notRequiredDefault: null,
		notRequiredNoDefault: null
	})).to.deep.eq({
		notRequiredDefault: 1,
		notRequiredNoDefault: undefined
	});
});

and the failure:

AssertionError: expected { Object (notRequiredDefault, notRequiredNoDefault) } to deeply equal { Object (notRequiredDefault, notRequiredNoDefault) }
+ expected - actual

{
-  "notRequiredDefault": [null]
-  "notRequiredNoDefault": [null]
+  "notRequiredDefault": 1
+  "notRequiredNoDefault": [undefined]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions