cloudformation-cli-java-plugin icon indicating copy to clipboard operation
cloudformation-cli-java-plugin copied to clipboard

Codegen doesn't support nullable types

Open rjlohan opened this issue 6 years ago • 1 comments

This is the property that I’m trying to define:

    "Canceled": {
        "description": "POSIX Timestamp of cancellation of this downtime (null if not canceled)",
        "type": ["integer", "null"]
    },

This should be ok according to JsonSchema, but cfn-cli generate fails and rpdk.log contains this:

Traceback (most recent call last):
  File "/Users/.../programming/aws-cloudformation-rpdk-nightly/env/lib/python3.7/site-packages/rpdk/core/cli.py", line 95, in main
    args.command(args)
  File "/Users/.../programming/aws-cloudformation-rpdk-nightly/env/lib/python3.7/site-packages/rpdk/core/generate.py", line 15, in generate
    project.generate()
  File "/Users/.../programming/aws-cloudformation-rpdk-nightly/env/lib/python3.7/site-packages/rpdk/core/project.py", line 190, in generate
    return self._plugin.generate(self)
  File "/Users/.../programming/aws-cloudformation-rpdk-nightly/env/lib/python3.7/site-packages/rpdk/java/codegen.py", line 255, in generate
    pojos = pojo_resolver.resolve_pojos()
  File "/Users/.../programming/aws-cloudformation-rpdk-nightly/env/lib/python3.7/site-packages/rpdk/java/pojo_resolver.py", line 58, in resolve_pojos
    java_property_map[prop_name] = self._java_property_type(prop_schema)
  File "/Users/.../programming/aws-cloudformation-rpdk-nightly/env/lib/python3.7/site-packages/rpdk/java/pojo_resolver.py", line 88, in _java_property_type
    return primitive_types_map[json_type]
TypeError: unhashable type: 'list'

rjlohan avatar Aug 29 '19 16:08 rjlohan

We don't allow this right now for two separate reasons. We'd struggle generating code for compound types in statically typed languages. This is also true of oneOf (https://github.com/aws-cloudformation/aws-cloudformation-rpdk/issues/242). We can explore union types for codegen in future, it would be a good additions. However, we could support null without union types via e.g. Optional in Java. But since CloudFormation doesn't allow (the equivalent of) null values in templates, it wouldn't really make sense. Marking a property as required or not kind of does the same.

(For this particular use-case, it might be possible to use a negative number as a sentinel for null?)

tobywf avatar Aug 29 '19 17:08 tobywf