django-rest-framework icon indicating copy to clipboard operation
django-rest-framework copied to clipboard

Schema Generator: serializer field from models.DurationField with MinValueValidator produces invalid output

Open peterthomassen opened this issue 3 years ago • 5 comments

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.

peterthomassen avatar Jun 29 '21 14:06 peterthomassen

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.

stale[bot] avatar Apr 28 '22 16:04 stale[bot]

The problem still occurs in 3.13.1. I would guess this is related to #7641

sbrandtb avatar Jun 10 '22 07:06 sbrandtb

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.

stale[bot] avatar Aug 11 '22 23:08 stale[bot]

I believe this is still current.

peterthomassen avatar Aug 11 '22 23:08 peterthomassen

Any keen code archeologists up for digging into what's needed to resolve this?

tomchristie avatar Aug 22 '22 09:08 tomchristie

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.

stale[bot] avatar Oct 22 '22 18:10 stale[bot]

I believe this is still current.

peterthomassen avatar Oct 24 '22 00:10 peterthomassen

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.

rnshaikh avatar Jun 12 '23 19:06 rnshaikh

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?

auvipy avatar Jun 13 '23 04:06 auvipy

@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

peterthomassen avatar Jun 19 '23 11:06 peterthomassen

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

auvipy avatar Jun 19 '23 13:06 auvipy