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

Kernel silently ignores additional properties when deserializing structs #822

Closed
3 of 4 tasks
RomainMuller opened this issue Sep 25, 2019 · 3 comments · Fixed by #919
Closed
3 of 4 tasks

Kernel silently ignores additional properties when deserializing structs #822

RomainMuller opened this issue Sep 25, 2019 · 3 comments · Fixed by #919
Assignees
Labels
bug This issue is a bug. effort/large Large work item – several weeks of effort module/kernel Issues affecting the `jsii-kernel` module

Comments

@RomainMuller
Copy link
Contributor

🐛 Bug Report

Affected Languages

  • TypeScript or Javascript
  • Python
  • Java
  • .NET (C#, F#, ...)

General Information

  • JSII Version: 0.17.0

What is the problem?

The kernel struct deserialization silently ignores any properties that the type does not declare. When deserializing type unions where the first candidate has no required property, this means serialization will ignore everything and succeed (resulting in an empty object), instead of attempting the next candidate type.

This is the root cause of aws/aws-cdk#3917

@RomainMuller RomainMuller added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Sep 25, 2019
@RomainMuller RomainMuller self-assigned this Sep 25, 2019
@RomainMuller RomainMuller added module/kernel Issues affecting the `jsii-kernel` module effort/medium Medium work item – a couple days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Sep 25, 2019
@nija-at
Copy link
Contributor

nija-at commented Sep 25, 2019

Capturing context from aws/aws-cdk#3917, this is impacting the use of L1 constructs from the SAM module.

@RomainMuller
Copy link
Contributor Author

RomainMuller commented Sep 25, 2019

An "easy" solution here involves throwing an error when encountering an un-modeled property instead of ignoring it; however this means the various runtimes may not serialize additional properties; which may require adding code there to this effect.

Alternatively, we could deserialize unknown properties as if they were present & typed any, which would effectively convey all the information back regardless. This may however evade validations on the "intended" type there.

@nija-at
Copy link
Contributor

nija-at commented Sep 25, 2019

Another instance of this in Java identified from a customer report - aws/aws-cdk#2013

@RomainMuller RomainMuller added effort/large Large work item – several weeks of effort and removed effort/medium Medium work item – a couple days of effort labels Oct 4, 2019
RomainMuller added a commit that referenced this issue Oct 30, 2019
When deserializing structures in a Union context, the Kernel used to try
options in an arbitrary order (actually - the declaration order which
often is alphanumerically sorted). In addition, it would ignore any
additional property encountered silently.

In cases where several of the union's possibilities had overlapping
required properties, the first attempted option would succeed, even if a
subsequent option would have consumed more properties. A pathologic case
of this is when the first candidate is a type with only optional
properties; as such a type would *always* successfully deserialize
anything, possibly ignoring all properties.

In order to address this, the `structs` can now be passed into the
Kernel by wrapping the object in a decorator box:
```js
{
  "$jsii.struct": {
    "fqn": "fully.qualified.struct.TypeName",
    "data": {
      /* the actual data included in the struct instance */
    }
  }
}
```

This enables "native" languages to correctly communicate the intended
type to the Kernel, so it can make an appropriate deserialization
decision.

The encoding of structs from the Kernel to "native" languages has not
changed: those are still passed by-reference in order to maximize the
compatibility; and to avoid undeterministic behavior in case where union
of structs are returned.

Fixes #822
Fixes aws/aws-cdk#3917
RomainMuller added a commit that referenced this issue Oct 30, 2019
When deserializing structures in a Union context, the Kernel used to try
options in an arbitrary order (actually - the declaration order which
often is alphanumerically sorted). In addition, it would ignore any
additional property encountered silently.

In cases where several of the union's possibilities had overlapping
required properties, the first attempted option would succeed, even if a
subsequent option would have consumed more properties. A pathologic case
of this is when the first candidate is a type with only optional
properties; as such a type would *always* successfully deserialize
anything, possibly ignoring all properties.

In order to address this, the `structs` can now be passed into the
Kernel by wrapping the object in a decorator box:
```js
{
  "$jsii.struct": {
    "fqn": "fully.qualified.struct.TypeName",
    "data": {
      /* the actual data included in the struct instance */
    }
  }
}
```

This enables "native" languages to correctly communicate the intended
type to the Kernel, so it can make an appropriate deserialization
decision.

The encoding of structs from the Kernel to "native" languages has not
changed: those are still passed by-reference in order to maximize the
compatibility; and to avoid undeterministic behavior in case where union
of structs are returned.

Fixes #822
Fixes aws/aws-cdk#3917
Fixes aws/aws-cdk#2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. effort/large Large work item – several weeks of effort module/kernel Issues affecting the `jsii-kernel` module
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants