-
Notifications
You must be signed in to change notification settings - Fork 57
Accept legacy request field names alongside new canonical ones (fixes asset-level force_new_job_creation gap) #2352
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
Open
Flix6x
wants to merge
5
commits into
main
Choose a base branch
from
feat/backward-compat-request-field-aliases
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
12705fe
Accept legacy request field names alongside new canonical ones
Flix6x 07c4e1f
Fix: don't strip MultiDict semantics when no legacy field is present
Flix6x ddde7f1
Address Copilot review: preserve MultiDict semantics, add regression …
Flix6x 84cd53f
Address Copilot follow-up: test via schema.load(), not the private ho…
Flix6x 23ed023
Move force_new_job_creation legacy alias out of the shared domain schema
Flix6x File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
flexmeasures/api/v3_0/tests/test_asset_trigger_schema_v3.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| """Tests for AssetTriggerSchemaV3, the v3_0-only wrapper that adds legacy | ||
| field-name backward compatibility on top of the shared, canonical | ||
| AssetTriggerSchema (see flexmeasures/api/v3_0/assets.py for why this split | ||
| exists: sunsetting v3_0 should be able to delete this compatibility layer in | ||
| one place, without touching the domain schema also used by the CLI). | ||
| """ | ||
|
|
||
| from marshmallow.validate import ValidationError | ||
| import pytest | ||
| from werkzeug.datastructures import MultiDict | ||
|
|
||
| from flexmeasures.api.v3_0.assets import AssetTriggerSchemaV3 | ||
|
|
||
|
|
||
| def test_asset_trigger_schema_v3_accepts_legacy_force_new_job_creation_field(): | ||
| """Regression test for a gap that made flexmeasures-client#218 necessary: the | ||
| sensor-level scheduling trigger schema already accepted both | ||
| `force_new_job_creation` (legacy) and `force-new-job-creation` (canonical), | ||
| but the asset-level trigger schema only accepted the canonical spelling and | ||
| rejected the legacy one (as an unknown field, yielding a 422). | ||
| """ | ||
| schema = AssetTriggerSchemaV3() | ||
| normalized = schema._apply_legacy_field_aliases({"force_new_job_creation": True}) | ||
| assert normalized == {"force-new-job-creation": True} | ||
|
|
||
|
|
||
| def test_asset_trigger_schema_v3_load_accepts_legacy_force_new_job_creation_field( | ||
| db, app | ||
| ): | ||
| """Same regression as above, but exercised through the real `schema.load(...)` | ||
| deserialization path (not by calling the `@pre_load` helper directly), so | ||
| this fails if the hook ever stops being registered/applied by Marshmallow | ||
| (e.g. decorator removed, or an MRO change means the hook is no longer | ||
| picked up). | ||
|
|
||
| Uses a nonexistent asset id, so `load()` is expected to still raise -- but | ||
| only about the asset, never about `force_new_job_creation` being an | ||
| unrecognized field. If the legacy alias stopped working, Marshmallow's | ||
| default `unknown` handling would additionally report `force_new_job_creation` | ||
| as an unknown field. | ||
| """ | ||
| schema = AssetTriggerSchemaV3() | ||
| with pytest.raises(ValidationError) as e_info: | ||
| schema.load( | ||
| { | ||
| "id": 2**31 - 1, # some asset id that doesn't exist | ||
| "start": "2026-01-15T10:00:00+01:00", | ||
| "force_new_job_creation": True, # legacy spelling | ||
| } | ||
| ) | ||
| messages = e_info.value.messages | ||
| assert "force_new_job_creation" not in messages, ( | ||
| "the legacy field name should have been aliased to " | ||
| "`force-new-job-creation` before validation, not rejected as an " | ||
| f"unknown field; got: {messages}" | ||
| ) | ||
| assert ( | ||
| "id" in messages | ||
| ), f"expected the (nonexistent) asset id to fail; got: {messages}" | ||
|
|
||
|
|
||
| def test_asset_trigger_schema_v3_preserves_multidict_when_aliasing(): | ||
| """Regression test: aliasing a legacy field must not destroy MultiDict | ||
| semantics (e.g. `getlist`) for other, untouched keys -- this is relied on | ||
| by `AssetTriggerSchema.normalize_flex_context_format` to detect a | ||
| multi-commodity `flex-context` list sent as repeated keys. | ||
| """ | ||
| schema = AssetTriggerSchemaV3() | ||
| data = MultiDict( | ||
| [ | ||
| ("start", "2026-01-15T10:00+01:00"), | ||
| ("force_new_job_creation", True), | ||
| ("flex-context", "electricity"), | ||
| ("flex-context", "heat"), | ||
| ] | ||
| ) | ||
| normalized = schema._apply_legacy_field_aliases(data) | ||
| assert normalized.getlist("flex-context") == ["electricity", "heat"] | ||
| assert normalized["force-new-job-creation"] is True | ||
| assert "force_new_job_creation" not in normalized |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a question about moving on.
When at some day API v3 is sunset, then these older remnants of old field names will still linger in schemas. Should we plan on cleaning them out, as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question, and it's a real risk, not just a hypothetical — thanks for flagging it.
Short answer: schemas shouldn't be versioned wholesale, but version-specific compatibility shims need to live in the versioned API layer, not in shared domain schemas.
Most of
flexmeasures/data/schemas/isn't API-only — it's reused directly by the CLI and the scheduler internals (FlexContextSchema,StorageFlexModelSchema,AssetTriggerSchema, etc.). Duplicating that whole layer per API version would mean either heavy code duplication, or forcing non-API callers like the CLI to somehow pick an "API version" too, which doesn't make sense.So the right split is:
flexmeasures/data/schemas/*) stay canonical and version-agnostic: always just the current, correct field names, no compatibility cruft.flexmeasures/api/v3_0/(or a thin adapter it owns).That placement is what makes sunset cleanup free: when v3_0 is retired, we already delete/replace the whole
api/v3_0/package (same mechanism as the existing v1/v2_0 sunset blueprints inflexmeasures/api/sunset/__init__.py). If every v3-only compatibility shim lives insideapi/v3_0/, deleting that package deletes all the cruft in one motion — nothing left to hunt down in the domain layer years later.Concretely, on this PR:
TriggerScheduleKwargsSchema(sensor-level trigger) was already scoped correctly — its alias lives inflexmeasures/api/v3_0/sensors.py. But I'd initially putAssetTriggerSchema's alias directly in the shared domain module, which was exactly the kind of permanent lingering you're describing. Fixed now: addedAssetTriggerSchemaV3, a thin v3_0-only subclass inflexmeasures/api/v3_0/assets.pythat adds the legacyforce_new_job_creationalias, whileAssetTriggerSchemaitself stays canonical (and CLI-safe). Tests moved toflexmeasures/api/v3_0/tests/test_asset_trigger_schema_v3.pyaccordingly.