flask-resty icon indicating copy to clipboard operation
flask-resty copied to clipboard

Allow passing a schema class instead of schema instances

Open sloria opened this issue 6 years ago • 5 comments

Passing a schema instance makes it impossible to use schema context safely.

sloria avatar Apr 02 '19 20:04 sloria

Somewhat related to https://github.com/4Catalyzer/flask-resty/issues/238.

I guess I was thinking that individual views might want to pass things into schema constructors, but that's never really happened.

Though, in practice, in a Flask context, wouldn't it be nearly equivalent to use flask.g instead of the schema context?

taion avatar Apr 05 '19 13:04 taion

Hm, we could do what DRF does and allow either a schema_class class variable or a get_schema_instance method to be defined.

flask.g could work, though I'd worry if there are cases when schemas need to get instantiated multiple times per-request. Probably not an issue, though, considering we've been using instances this whole time.

sloria avatar Apr 05 '19 13:04 sloria

@sloria if multiple schemas need to be instantiated multiple times per-request, then they will have a separate context, so no problem there?

itajaja avatar Apr 05 '19 15:04 itajaja

@itajaja If you need a separate context per-instance, then flask.g could be problematic.

That said, I guess you could work around this by setting flask.g in pre_load and then resetting it in post_load.

sloria avatar Apr 05 '19 15:04 sloria

@itajaja is saying that if you have separate instances, then you would in fact be able to set context on them normally.

taion avatar Apr 05 '19 16:04 taion