django-ninja icon indicating copy to clipboard operation
django-ninja copied to clipboard

[BUG] In docs page, the payload examples do not use the default_factory attribute

Open hugochinchilla opened this issue 1 year ago • 1 comments

Describe the bug

Given this schema:

class Quote(Schema):
    checkIn: date = Field(default_factory=lambda: date.today() + timedelta(days=1))
    checkOut: date = Field(default_factory=lambda: date.today() + timedelta(days=4))

I expect the example payloads in the docs to have dates different than today. But they dont.

If I use default values, they do, but they are set once the application loads and get stalled with the pass of time.

class QuoteOptionInputDTO(Schema):
    checkIn: date = Field(default=date.today() + timedelta(days=1))
    checkOut: date = Field(default=date.today() + timedelta(days=4))

Versions (please complete the following information):

  • Python version: 3.11
  • Django version: 4.2.7
  • Django-Ninja version: 1.1.0
  • Pydantic version: 2.4.2

hugochinchilla avatar Dec 12 '23 09:12 hugochinchilla

@hugochinchilla when you set default_factory - nothing is actually brought to opneapi examples schema (what you see as today date is actually provided by swagger js)

default_factory only works during validation if you do not provide

what you need is some sort of dynamic examples generation - I'm not really sure how is it possible at pydantic - so I guess you better ask at pydantic forum how to achive json_schema to generate dynamic examples

vitalik avatar Dec 12 '23 11:12 vitalik

Thanks for the reply, and apologize for my very late response. I'm closing the issue.

hugochinchilla avatar Jun 04 '24 06:06 hugochinchilla