Skip to content

Validate assignments to Selection so values stays a tuple - #1283

Open
frostyhand wants to merge 1 commit into
CERTCC:mainfrom
frostyhand:fix/selection-values-assignment
Open

frostyhand wants to merge 1 commit into
CERTCC:mainfrom
frostyhand:fix/selection-values-assignment

Conversation

@frostyhand

Copy link
Copy Markdown

Selection.values is declared as tuple[MinimalDecisionPointValue, ...] with min_length=1, but pydantic only enforces that when the object is created. Assigning afterwards skips validation, so selection.values = [...] leaves a plain list on the model.

That is where the warnings in #1135 come from. examples.py does this for EXAMPLE_SELECTION_1, which the /examples/selections and /examples/selection-lists endpoints return, and the API example tests do it after sampling values. Serializing those objects then emits PydanticSerializationUnexpectedValue (12 times in the test suite). An empty list was also accepted on assignment, although it is rejected at init.

The change turns on validate_assignment for Selection only. An assigned list is now coerced to a tuple of MinimalDecisionPointValue, plain dicts are validated into that type, and an empty list raises a validation error, the same as at init. The other fields of Selection get the same treatment, so assigning a bad namespace or version is now caught too. Selection has no model-level validators, so nothing else runs on assignment.

I kept the declared type as a tuple rather than switching to a list (the other option in the issue), since decision point values are ordered sets (ADR 0008) and keeping the type means the JSON schema does not change.

Tests:

  • test_selection_values_assignment_is_validated: assigning a list stores a tuple, dicts become MinimalDecisionPointValue, and model_dump, model_dump_json and the parent SelectionList.model_dump_json raise no warnings when warnings are turned into errors.
  • test_selection_values_assignment_requires_one_value: assigning [] raises and leaves the old values in place.
  • Both fail on main. Full suite: 204 passed (202 on main plus these 2), and pytest -W default now shows 0 Pydantic serializer warnings (12 before).
  • The doctools pre-commit step regenerates no data beyond the registry timestamp, same as on main.

Selection.values is declared as tuple[MinimalDecisionPointValue, ...] with
min_length=1, but pydantic only checks that at init. Code that assigns
afterwards, such as `selection.values = [...]` in examples.py and in the
API example tests, left a plain list on the model. Serializing it then
emitted PydanticSerializationUnexpectedValue warnings (12 in the test
suite), and an empty list was accepted.

Turn on validate_assignment for Selection, so an assigned list is coerced
to a tuple of MinimalDecisionPointValue and an empty list is rejected,
the same as at init.

Fixes CERTCC#1135

This branch has not been deployed

No deployments
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.

Selection and SelectionList Pydantic serialization warnings

1 participant