Skip to content

Fix OptionSet variant JSON encoding for empty arrays and scalars#1757

Closed
LivingLikeKrillin wants to merge 1 commit into
eclipse-milo:mainfrom
LivingLikeKrillin:fix-json-optionset-variant-encoding
Closed

Fix OptionSet variant JSON encoding for empty arrays and scalars#1757
LivingLikeKrillin wants to merge 1 commit into
eclipse-milo:mainfrom
LivingLikeKrillin:fix-json-optionset-variant-encoding

Conversation

@LivingLikeKrillin

Copy link
Copy Markdown
Contributor
  • You have signed an Eclipse Contributor Agreement and are committing using the same email address
  • Your code contains any tests relevant to the problem you are solving
  • All new and existing tests passed
  • Code follows the style guidelines and Checkstyle passes

The OPTION_SET branch in OpcUaJsonEncoder.encodeVariantValue derived the JSON typeId by calling Array.get(value, 0) on the variant value. This failed in two real cases: empty arrays threw ArrayIndexOutOfBoundsException, and single (non-array) OptionSet values threw IllegalArgumentException("Argument is not an array"). The latter is reachable via the existing scalar pattern in production code, e.g. new Variant(defaultAccessRestrictions) in NamespaceMetadataTypeNode.

Derive the typeId from the value's class instead, using the existing OptionSetUI8/UI16/UI32/UI64 subclasses — the approach the original TODO comment in this branch proposed. OptionSetUI64 is supported preemptively even though no DataType in stack-core currently extends it.

Test coverage in OpcUaJsonEncoderTest.encodeVariantOptionSet covers the width × cardinality matrix for the OptionSet subclasses in current use: scalar UI8, non-empty UI8/UI16/UI32, and empty UI8/UI16/UI32.

The OPTION_SET branch in encodeVariantValue derived the JSON typeId by
peeking at Array.get(value, 0) on the variant value. This failed in two
real cases:

- Empty arrays threw ArrayIndexOutOfBoundsException
- Single (non-array) OptionSet values threw IllegalArgumentException
  ("Argument is not an array")

Derive the typeId from the value's class instead, using the existing
OptionSetUI8/UI16/UI32/UI64 subclasses. This is the approach the
original TODO comment proposed.

OptionSetUI64 is supported preemptively even though no DataType in
stack-core currently extends it.

Signed-off-by: Jooyoung Jung <livinglikekrillin@gmail.com>
@kevinherron

Copy link
Copy Markdown
Contributor

Thank you for contributing.

I've already fixed this as part of a larger batch of JSON encoding fixes, and your PR just reminded me that I need to push/PR/merge them :)

See #1758

So I will close this, but thank you anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants