Skip to content

Null value shouldn't be set if property is undefined #1197

@asvishnyakov

Description

@asvishnyakov

Now NSwag, which use NJsonSchema generate the following model for me, if I set nullValue to null:

export class OrganizationRegistration implements IOrganizationRegistration {
    organization?: Organization | null;
    organizationName?: string | null;
    address?: Address | null;
    user?: User | null;
    contact?: Contact | null;
    photoUrl?: string | null;
    firstName?: string | null;
    fullName?: string | null;
    lastName?: string | null;
    email?: string | null;
    userName!: string;
    password!: string;
    storeId?: string | null;
    name?: string | null;
    salutation?: string | null;
    middleName?: string | null;
    birthDate?: Date | null;
    timeZone?: string | null;

    constructor(data?: IOrganizationRegistration) {
        if (data) {
            for (var property in data) {
                if (data.hasOwnProperty(property))
                    (<any>this)[property] = (<any>data)[property];
            }
        }
    }

    init(_data?: any) {
        if (_data) {
            this.organization = _data["organization"] ? Organization.fromJS(_data["organization"]) : <any>null;
            this.organizationName = _data["organizationName"] !== undefined ? _data["organizationName"] : <any>null;
            this.address = _data["address"] ? Address.fromJS(_data["address"]) : <any>null;
            this.user = _data["user"] ? User.fromJS(_data["user"]) : <any>null;
            this.contact = _data["contact"] ? Contact.fromJS(_data["contact"]) : <any>null;
            this.photoUrl = _data["photoUrl"] !== undefined ? _data["photoUrl"] : <any>null;
            this.firstName = _data["firstName"] !== undefined ? _data["firstName"] : <any>null;
            this.fullName = _data["fullName"] !== undefined ? _data["fullName"] : <any>null;
            this.lastName = _data["lastName"] !== undefined ? _data["lastName"] : <any>null;
            this.email = _data["email"] !== undefined ? _data["email"] : <any>null;
            this.userName = _data["userName"] !== undefined ? _data["userName"] : <any>null;
            this.password = _data["password"] !== undefined ? _data["password"] : <any>null;
            this.storeId = _data["storeId"] !== undefined ? _data["storeId"] : <any>null;
            this.name = _data["name"] !== undefined ? _data["name"] : <any>null;
            this.salutation = _data["salutation"] !== undefined ? _data["salutation"] : <any>null;
            this.middleName = _data["middleName"] !== undefined ? _data["middleName"] : <any>null;
            this.birthDate = _data["birthDate"] ? new Date(_data["birthDate"].toString()) : <any>null;
            this.timeZone = _data["timeZone"] !== undefined ? _data["timeZone"] : <any>null;
        }
    }

    static fromJS(data: any): OrganizationRegistration {
        data = typeof data === 'object' ? data : {};
        let result = new OrganizationRegistration();
        result.init(data);
        return result;
    }

    toJSON(data?: any) {
        data = typeof data === 'object' ? data : {};
        data["organization"] = this.organization ? this.organization.toJSON() : <any>null;
        data["organizationName"] = this.organizationName !== undefined ? this.organizationName : <any>null;
        data["address"] = this.address ? this.address.toJSON() : <any>null;
        data["user"] = this.user ? this.user.toJSON() : <any>null;
        data["contact"] = this.contact ? this.contact.toJSON() : <any>null;
        data["photoUrl"] = this.photoUrl !== undefined ? this.photoUrl : <any>null;
        data["firstName"] = this.firstName !== undefined ? this.firstName : <any>null;
        data["fullName"] = this.fullName !== undefined ? this.fullName : <any>null;
        data["lastName"] = this.lastName !== undefined ? this.lastName : <any>null;
        data["email"] = this.email !== undefined ? this.email : <any>null;
        data["userName"] = this.userName !== undefined ? this.userName : <any>null;
        data["password"] = this.password !== undefined ? this.password : <any>null;
        data["storeId"] = this.storeId !== undefined ? this.storeId : <any>null;
        data["name"] = this.name !== undefined ? this.name : <any>null;
        data["salutation"] = this.salutation !== undefined ? this.salutation : <any>null;
        data["middleName"] = this.middleName !== undefined ? this.middleName : <any>null;
        data["birthDate"] = this.birthDate ? this.birthDate.toISOString() : <any>null;
        data["timeZone"] = this.timeZone !== undefined ? this.timeZone : <any>null;
        return data; 
    }
}

That's incorrect. If I don't set property or set it to undefined, it shouldn't be set as null. It should be passed as is.

Just compare:

const organization = new Organization({ userName: 'xxx', password: 'yyy', email: 'test@example.org' })

and

const organization = new Organization();
organization.userName = 'xxx';
organization.password = 'yyy';
organization.email = 'test@example.org';

In first case all properties will be set to null. That's incorrect, because I didn't set them, they're optional.
In second case all other properties with exception for these 3 will be just no defined on object. That's expected behavior.

Also, it's undefined behavior to just set nullValue to undefined: these properties are both optional and nullable.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions