Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use Schema's render_module? #747

Closed
Anti-Distinctlyminty opened this issue Aug 3, 2023 · 2 comments
Closed

How to use Schema's render_module? #747

Anti-Distinctlyminty opened this issue Aug 3, 2023 · 2 comments
Labels

Comments

@Anti-Distinctlyminty
Copy link

I am trying to use jsonref in place of the json module in a schema so that I can use json references. I have tested the jsonref module and it works just fine, however I am using it as follows in a schema

import jsonref

class BodySchema(Schema):
    class Meta:
        render_module = jsonref
        partial = True
        unknown = INCLUDE

...

# flask-smorest stuff
bp = Blueprint(...)

@bp.route("<int:model_id>/submit")
class ModelInstanceResource(MethodView):
    @bp.arguments(
        BodySchema,
        location="json",
        as_kwargs=True,
    )
    def post(self, **kwargs):
        pass

Unfortunately it just does not work, as $refs are not resolved.

Am I doing something wrong here?

@Anti-Distinctlyminty Anti-Distinctlyminty changed the title render_module in SQLAlchemyAutoSchema How to use Schema's render_module? Aug 3, 2023
@sloria
Copy link
Member

sloria commented Jan 12, 2025

sorry for the delay here. I don't see an issue with your code as is. @Anti-Distinctlyminty Can you be more specific about what the expected output vs actual output is?

@sloria sloria transferred this issue from marshmallow-code/marshmallow-sqlalchemy Jan 12, 2025
@sloria
Copy link
Member

sloria commented Jan 12, 2025

i believe this is actually a flask-smorest issue. Schema.dump (not Schema.dumps) is being called when constructing a response here:

result_dump = schema.dump(result_raw)

so render_module won't have any effect. the response will always be json

resp = jsonify(self._prepare_response_content(result_dump))
set_status_and_headers_in_response(resp, r_status_code, r_headers)
if r_status_code is None:
resp.status_code = status_code
return resp

i think you can achieve your desired output using a post-dump method

import json

import jsonref

from marshmallow import INCLUDE, Schema, post_dump


class BodySchema(Schema):
    class Meta:
        render_module = jsonref
        unknown = INCLUDE

    @post_dump
    def jsonref(self, data, many, **kwargs):
        return json.load(jsonref.dumps(data))

@sloria sloria closed this as completed Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants