django-rest-framework
django-rest-framework copied to clipboard
Schema Generator: serializer field from models.DurationField with MinValueValidator produces invalid output
I have a model with the following field:
max_age = models.DurationField(null=True, default=None, validators=[MinValueValidator(timedelta(0))])
The schema contains the following output:
max_age:
type: string
nullable: true
minimum: !!python/object/apply:datetime.timedelta
- 0
- 0
- 0
This cannot be rendered by Swagger / ReDoc, leading to failures when loading the schema.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
The problem still occurs in 3.13.1. I would guess this is related to #7641
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
I believe this is still current.
Any keen code archeologists up for digging into what's needed to resolve this?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
I believe this is still current.
I have found that as yaml Dumper convert time delta into python tag which is not renderable. so here we can add a custom representer for datetime.timedelta which converts it into 'str' tag which is similar to JSON OpenAPI Schema fix. #7641.
Dumper.add_representer(datetime.timedelta, encoders.CustomScalar.represent_timedelta)
I have created PR for this #9007.
I have found that as yaml Dumper convert time delta into python tag which is not renderable. so here we can add a custom representer for datetime.timedelta which converts it into 'str' tag which is similar to JSON OpenAPI Schema fix. #7641.
Dumper.add_representer(datetime.timedelta, encoders.CustomScalar.represent_timedelta)
I have created PR for this #9007.
@peterthomassen can you also review the PR please?
@peterthomassen can you also review the PR please?
Sorry for being late! I looked over the PR and couldn't spot anything problematic; OTOH I'm not too familiar with schema representations, and I'm not sure how specialized types are best represented. So, superficially, lgtm.
That said, I very MUCH appreciate that DRF is seeing some movement now. Thank you very very much for working on it! <3
That said, I very MUCH appreciate that DRF is seeing some movement now. Thank you very very much for working on it! <3
I will put effort to it. no worries