marshmallow-jsonapi icon indicating copy to clipboard operation
marshmallow-jsonapi copied to clipboard

Support schema introspection

Open jmcarp opened this issue 10 years ago • 7 comments

The envelope used to serialize responses is implemented in a set of helper methods (format_items, wrap_response, etc.) that could be challenging to introspect (e.g. for building swagger docs). Implementing the envelope using fields and nested schemas would be more amenable to introspection--it should Just Work with apispec.

One option would be adding some magic to Schema--for example, overriding get_declared_fields to replace the original fields with attributes, a Nested that wraps the original fields. This might be too opaque, so we could also define primary schemas separately and explicitly wrap them in JsonSchema classes.

Or maybe this complicates the implementation too much, and it's best to sacrifice purity and just introspect schemas manually--it shouldn't be too hard to write an apispec hook that generates the appropriate markup. The only other caveat there would be that introspection would be brittle, and likely stop working if users subclass Schema and change the envelopes.

Help @sloria!

jmcarp avatar Oct 19 '15 20:10 jmcarp

I'm not necessarily opposed to generating a nested schema if it would simplify introspection. Also, I like the idea of not using @pre_* and @post_* methods in this package (since the order of execution of decorated methods isn't guaranteed). IMO, @pre_ and @post_ belong in userland.

That said, I haven't given much thought to how much this would complicate the internals.

sloria avatar Oct 21 '15 00:10 sloria

I expected apispec to "just work", but I ended up with docs representing a simplified marshmallow schema definition instead of docs matching the serialized jsonapi-structured data.

It seems like implementing the jsonapi spec by composing marshmallow_jsonapi out of pure marshmallow fields and schemas would remove the need for @pre/post_* glue.

I image that the implementation will involve transforming the class attributes in the Schema constructor before the super Schema constructor is called. This should eliminate the need for post_dump and change the pre_load to a post_load.

deckar01 avatar Apr 29 '16 18:04 deckar01

Has any progress been made on this? I'm also wondering how you would handle lists. You use the same schema to dump both a single entity as well as multiple entities (with many=True), so how would that work with apispec?

jeffsawatzky avatar Oct 04 '18 15:10 jeffsawatzky

bump

dperconti avatar Jan 31 '19 01:01 dperconti

I would happily review a PR for this, even just a sketch. I don't have time to work on this myself, as I am not currently using this project.

sloria avatar Jan 31 '19 02:01 sloria

Switching to https://github.com/thomaxxl/safrs ended up being my solution, may save others time until this issue is resolved.

tjbanks avatar Sep 08 '21 00:09 tjbanks

@sloria I've started toying around with this idea here: https://github.com/vladmunteanu/marshmallow-jsonapi-exp/

It's too early for me to tell if schema introspection would work well with this implementation, but I would appreciate some thoughts.

vladmunteanu avatar Sep 04 '22 11:09 vladmunteanu