📝 Seeded OpenAPI examples of `datetime` and `time` change with the current time (#1632)
What's wrong?
Found while working on #1629. With
DMR_SETTINGS = {'openapi_examples_seed': 1} and the same PYTHONHASHSEED, examples of datetime and time fields are different on every run:class _Event(msgspec.Struct):
created_at: dt.datetime
day: dt.date
at: dt.time
duration: dt.timedelta
class _EventController(Controller[MsgspecSerializer]):
def get(self) -> _Event:
raise NotImplementedError
Two runs 3 seconds apart, both with
PYTHONHASHSEED=0:02:18:43 {"created_at": "2000-10-08T03:39:12.378838", "day": "2025-09-16", "at": "18:36:00.366279", "duration": "P0D"}
02:18:46 {"created_at": "2000-10-08T03:39:16.378838", "day": "2025-09-16", "at": "18:36:03.421377", "duration": "P0D"}
polyfactory generates these with Faker, and Faker's defaults end at the current time:
datetime uses date_time_between, which ends at "now", and time uses time_object, which picks a time between 1970 and now. The seed fixes the random part, but the range moves with the clock, so the value moves too. date uses date_this_decade, which ends at today, so it changes once a day.So a schema with such fields is never the same on two runs, even though the
seed_example_factory docstring says that seeding per build "keeps a schema reproducible on its own".How it should be?
The same
openapi_examples_seed should give the same examples at any time. _ExampleFactory in dmr/openapi/mappers/example.py can override get_provider_map() and give datetime, date and time fixed bounds instead of "now".timedelta could get a range there too: its examples are always P0D now, because time_delta() without end_datetime starts and ends at "now".Used versions
django-modern-rest 0.15.0, polyfactory 3.3.0, faker 40.39.0Reproduced on c35128a.
OS information
macOS 27.0
#bug #good_first_issue #help_wanted #openapi #opensource_september #django_modern_rest
sent via relator