question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Could ParamValidation be more permissive?

See original GitHub issue

There’s a common pattern in python where you want to provide all parameters as a value or None, and expect that the receiving end treats “None” as a non-value.

The pattern simplifies and shortens code. Unfortunately it doesn’t seem possible to do so with boto3:

ec2 = boto3.resource('ec2', args.region)
volume = ec2.create_volume(
    Size=int(args.size),
    SnapshotId=args.snapshot_id or None,
    AvailabilityZone=instance.placement['AvailabilityZone'],
    VolumeType=args.volume_type,
    Iops=args.iops or None,
    Encrypted=bool(args.encrypted),
    KmsKeyId=args.encrypted or None)
botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid type for parameter Iops, value: None, type: <type 'NoneType'>, valid types: <type 'int'>, <type 'long'>
Invalid type for parameter KmsKeyId, value: None, type: <type 'NoneType'>, valid types: <type 'basestring'>
Invalid type for parameter SnapshotId, value: None, type: <type 'NoneType'>, valid types: <type 'basestring'>

Instead, one has to carefully build a dict with the arguments using one if per argument:

opt = {}
if args.iops:
    opt['Iops'] = args.iops
if args.kms_key_id:
    opt['KmsKeyId'] = args.kms_key_id
# ...

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
rterbushcommented, Jun 8, 2016

Wanted to chime in here and indicate my support of this change as well.

I’m in the process of porting some saltstack states to boto3 and this issue is definitely a huge pain. Would not be a big deal if you only cared about a few of the possible parameters, but adds quite a bit of code to deal with support for None on the optionals.

Would make more sense to me to force the corner cases mentioned by @rayluo in #331 to jump through some hoops rather than every common use case.

0reactions
joguSDcommented, May 1, 2020

Coming back to this, the logic in the previously linked ticket #331 is correct. We can’t make this change because the absence of a parameter and that parameter being None aren’t always the same. Considering that these parameters get serialized down to an API call (commonly JSON) the following aren’t equal:

{}
{"some_field": null}

Which could potentially be an important distinction for some APIs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Wikimedia\ParamValidator\ParamValidator Class Reference
A list of standard type names and types that may be passed as $typeDefs to __construct(). More ... The rest of the code...
Read more >
Permissive vs. Strict API Message validation
Permissive vs. Strict REST API. It seems to me that your question isn't really about REST; similar concerns arise in other messaging systems ......
Read more >
Class: AWS.Iot — AWS SDK for JavaScript
Constructs a service interface object. Each API operation is exposed as a function on service. Service Description.
Read more >
Plugtest 2021: Specification discussion ...
Is a more appropriate action to ignore the unknown parameter? ... discussed at the ISG level API implementation can be strict or permissive....
Read more >
aws-sdk | Yarn - Package Manager
For more information see the Developer Guide or API Reference. For release notes, see the CHANGELOG. Prior to v2.4.8, release notes can be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found