Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't set NULL for Enum properties in Structs and Messages #385

Closed
vladmu opened this issue Feb 18, 2021 · 1 comment · Fixed by #393
Closed

Can't set NULL for Enum properties in Structs and Messages #385

vladmu opened this issue Feb 18, 2021 · 1 comment · Fixed by #393
Labels
bug Something isn't working

Comments

@vladmu
Copy link
Contributor

vladmu commented Feb 18, 2021

Based on the condition below the keyForValue call based on the default Enum's _keyForValue will always return null here for null obj

} else if (
(tClass.prototype instanceof Enum && tClass.keyForValue(obj) === null)
|| (tClass.prototype instanceof RpcStruct && obj !== null && obj.constructor !== tClass)
) {
throw new Error(`${obj.name} must be of type ${tClass.name}`);
}

Then because the condition is true, the code fails in line 145 with the error

TypeError: Cannot read property 'name' of null

Currently, all Enum classes use default _keyForValue for checks in their keyForValue methods so it is impossible to set null for all Enum properties (mandatory or optional) in all Structs and Messages

Steps to reproduce:

  1. Create for example the new instance of KeyboardProperties structure
  2. Try to set KeyboardLayout parameter (according to the spec this parameter is not mandatory) by calling setKeyboardLayout method with null value
  3. ER: null is successfully set, AR: the TypeError
new SDL.rpc.structs.KeyboardProperties().setKeyboardLayout(null) // this fails with TypeError: Cannot read property 'name' of null
@vladmu
Copy link
Contributor Author

vladmu commented Feb 18, 2021

Possible solution is to skip null value for Enum the same way as for RpcStruct

 } else if ( 
    obj !== null && (
        (tClass.prototype instanceof Enum && tClass.keyForValue(obj) === null) 
        || (tClass.prototype instanceof RpcStruct && obj.constructor !== tClass) 
    )
 ) { 
     throw new Error(`${obj.name} must be of type ${tClass.name}`); 
 } 

@jordynmackool jordynmackool added bug Something isn't working and removed bug Something isn't working labels Feb 22, 2021
@crokita crokita mentioned this issue Apr 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants