Skip to content

Return 422 ValidationError for zero-duration single-value posts on non-instantaneous sensors#2116

Open
Copilot wants to merge 3 commits intomainfrom
copilot/fix-validation-error-post-sensor-data
Open

Return 422 ValidationError for zero-duration single-value posts on non-instantaneous sensors#2116
Copilot wants to merge 3 commits intomainfrom
copilot/fix-validation-error-post-sensor-data

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 16, 2026

Posting sensor data with duration=PT0M and a single value to a non-instantaneous sensor currently falls through to a server-side error path instead of producing a client-facing validation response. This change moves that case into schema validation and adds explicit regression coverage.

  • Validation behavior

    • Added a targeted guard in PostSensorDataSchema.check_resolution_compatibility_of_sensor_data for:
      • non-instantaneous sensor resolution
      • exactly one posted value
      • inferred resolution 0:00:00
    • This now raises a ValidationError with a clear message, returning the expected 422 response shape instead of surfacing a runtime error.
  • Existing compatibility check retained

    • Kept the current incompatible-resolution validation for other non-zero/unsupported resolution combinations (inferred_resolution % required_resolution != 0).
  • Regression test

    • Added API test coverage in flexmeasures/api/v3_0/tests/test_sensor_data.py for posting:
      • num_values=1
      • duration="PT0M"
      • to a non-instantaneous sensor
    • Asserts 422 and schema-level error text.
  • Changelog

    • Added a bugfix entry documenting the endpoint behavior correction for POST /sensors/(id)/data.
inferred_resolution = data["duration"] / len(data["values"])
if len(data["values"]) == 1 and inferred_resolution == timedelta(hours=0):
    raise ValidationError(
        f"Cannot infer a non-zero resolution from one value over zero duration. "
        f"This sensor requires a resolution of {required_resolution}."
    )

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • publicsuffix.org
    • Triggering command: /home/REDACTED/work/flexmeasures/flexmeasures/.venv/bin/pytest pytest flexmeasures/ui/tests/test_utils.py flexmeasures/ui/tests/test_asset_crud.py flexmeasures/ui/tests/test_error_handling.py 76a4.pid flexmeasures/ui/tests/test_views.py flexmeasures/ui/tests/conftest.py 58611d1ccef45b6e0bae01ed96d1d581e03/log.json flexmeasures/ui/tests/test_user_crud.py flexmeasures/ui/tests/__init__.py flexmeasures/ui/tests/test_json_attributes_editor.py flexmeasures/ui/error_handlers.py be0f�� (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@read-the-docs-community
Copy link
Copy Markdown

read-the-docs-community bot commented Apr 16, 2026

Documentation build overview

📚 flexmeasures | 🛠️ Build #32289345 | 📁 Comparing 671c9e7 against latest (e97a0c4)

  🔍 Preview build  

2 files changed
± changelog.html
± api/v3_0.html

Copilot AI changed the title [WIP] Fix missing ValidationError for instantaneous sensor value Return 422 ValidationError for zero-duration single-value posts on non-instantaneous sensors Apr 16, 2026
Copilot AI requested a review from Flix6x April 16, 2026 12:28
@Flix6x Flix6x marked this pull request as ready for review April 16, 2026 15:27
@Flix6x Flix6x added bug Something isn't working API labels Apr 16, 2026
@Flix6x Flix6x added this to the 0.33.0 milestone Apr 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing ValidationError for POSTing an instantaneous value to a non-instantaneous sensor

2 participants