Skip to content

fix(python): reject duplicate keys when loading documents - #618

Merged
christianeu-db merged 2 commits into
apache:mainfrom
barveP:codex/reject-duplicate-document-keys
Oct 23, 2026
Merged

christianeu-db merged 2 commits into
apache:mainfrom
barveP:codex/reject-duplicate-document-keys

Conversation

@barveP

@barveP barveP commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The SDK document loaders silently keep the last value of a duplicate YAML or JSON key. For example, a repeated metric expression can replace SUM(orders.amount) with AVG(orders.amount) before validation sees it.

Reject duplicate keys before constructing the model. The SDK and validator now use one UniqueKeyLoader implementation. Repeated YAML merge directives, merge precedence, explicit overrides and reused aliases remain supported. Explicit duplicates inside merge sources are still rejected.

From a checkout, the validator loads the shared file directly, preserving converter environments that do not install the SDK. A copied validator uses an installed SDK; this is documented in the script.

Related Issues

Follow-up to #336/#337 (CLI duplicate-key checks) and #563 (SDK document loaders).

Validation

  • Full SDK suite: 219 passed on each Python 3.11, 3.12, 3.13 and 3.14.
  • Validator: 237 pytest tests and 59 unittest tests passed on each of those versions.
  • Converter suites match their unchanged-branch baselines: Cube 864 passed / 44 skipped, ThoughtSpot 1015 passed on Python 3.10, NVIDIA 146 passed.
  • All 45 SDK loader tests pass with Pydantic 2.0 and PyYAML 6.0.
  • An independent 25-case merge/duplicate probe passes against the installed wheel. Copied-validator and checkout-validator checks pass from an unrelated working directory, including a checkout environment without the SDK or Pydantic installed.
  • Five canonical examples, generated-spec consistency, Ruff (E4,E7,E9,F,I), whitespace and Apache RAT checks pass.

Checklist

  • Regression tests cover the behavior change and valid-input controls.
  • Public loader behavior and copied-validator requirements are documented.
  • Apache license headers are included.

Prepared with OpenAI Codex. Reproductions and tests were run locally.

@jbonofre
jbonofre self-requested a review October 21, 2026 10:47
Comment thread python/src/ossie/_loading.py Outdated

if isinstance(node, yaml.MappingNode):
seen = set()
merge_key = object()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now rejects a mapping with more than one << merge key, e.g. {<<: *a, <<: *b}, with "found duplicate key '<<'.
YAML 1.1 allows repeated merge keys, and PyYAML's flatten_mapping merges all of them, so this input loaded before this change.

Can we skip << duplicate check?

The merge keys should be expanded first and only explicit keys compared. The test that asserts the rejection would need to change too.

from yaml.constructor import ConstructorError


class UniqueKeyLoader(yaml.SafeLoader):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This UniqueKeyLoader is nearly identical to the one in validation/validate.py, and the comment says it is written to match it.

With two copies, a later fix to merge-key or alias handling can land in only one of them.

The validator and OssieDocument.from_ossie_yaml would then disagree on whether the same document is valid.

Could both import shared one implementation?

@barveP

barveP commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @jbonofre. Fixed both comments!

@jbonofre
jbonofre self-requested a review October 22, 2026 10:13

@jbonofre jbonofre left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks @barveP!

Sharing one UniqueKeyLoader between the SDK and the validator is great!

@jbonofre

Copy link
Copy Markdown
Member

I will merge as soon as CI is green 😄

@christianeu-db
christianeu-db merged commit 376a344 into apache:main Oct 23, 2026
13 checks passed
christianeu-db added a commit to christianeu-db/ossie that referenced this pull request Oct 23, 2026
Bring in apache#596 (compliance portability fixtures), apache#618 (reject duplicate
keys on load), and apache#629 (ATACCAMA vendor) from main.

The only conflict was the import block of validation/validate.py, where
this branch adds `difflib` plus the `Expression` dataclass/iterator
imports and apache#618 adds `importlib.util` for the shared `_loading.py`
bootstrap; resolved by keeping the union. apache#618's loader refactor merged
cleanly onto the expression-iterator refactor, and apache#629's OssieVendor
enum entry is complementary to this branch's vendors.md ATACCAMA row.

Verified against validation-ci.yml: duplicate-key tests (59),
validation pytest (268), python model tests (266), generator --check in
sync, and all five example validations pass.

Co-authored-by: Isaac <no-reply@databricks.com>
ekut added a commit to Dactopus/dactopus-ossie-clickhouse that referenced this pull request Oct 26, 2026
apache/ossie#617 adds expression.sql, the portable form in OSSIE_SQL_2026,
makes dialects optional beside it, and turns a dialect into any string.
model.expression_text is now the one place that picks the text: sql,
then an OSSIE_SQL_2026 entry, then ANSI_SQL. The pin also brings apache/ossie#618:
a repeated key in a YAML or JSON file is refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants