Skip to content

Optionally skip normalize schema during marshal#323

Open
illusional wants to merge 4 commits intomasterfrom
mfranklin/skip-normalize-schema-during-marshal
Open

Optionally skip normalize schema during marshal#323
illusional wants to merge 4 commits intomasterfrom
mfranklin/skip-normalize-schema-during-marshal

Conversation

@illusional
Copy link
Copy Markdown
Contributor

@illusional illusional commented Jul 8, 2025

Hey! I think we're duplicating normalize_schema work when parsing data from requests and sending the response. We normalize all the relevant schemas on url handler creation:

headers_schema = normalize_schema(headers_schema)
request_body_schema = normalize_schema(request_body_schema)
query_string_schema = normalize_schema(query_string_schema)
if response_body_schema:
# Ensure we wrap in appropriate default (200) dict if we were passed a single Schema or class:
if not isinstance(response_body_schema, Mapping):
response_body_schema = {200: response_body_schema}
# use normalize_schema to convert any class reference(s) to instantiated schema(s):
response_body_schema = {
code: normalize_schema(schema)
for (code, schema) in response_body_schema.items()
}

it looks like calls through the URL handlers are:

  • calling marshal:
    def marshal(data: Any, schema: Schema) -> Dict[str, Any]:
    """
    Dumps an object with the given marshmallow.Schema.
    :raises: marshmallow.ValidationError if the given data fails validation
    of the schema.
    """
    schema = normalize_schema(schema)
    return compat.dump(schema=schema, data=data)
  • and calling _get_data_or_400:
    def _get_data_or_400(
    schema: Schema, data: Any, message: messages.ErrorMessage
    ) -> Dict[str, Any]:
    schema = normalize_schema(schema)
    try:
    return compat.load(schema=schema, data=data)

There is only one internal usage of marshal in our codebase so in my opinion should be safe to remove, but understand that others might be relying on it. Some other options include:

  • creating a marshal_without_normalize
  • (actioned) adding a should_normalize_schema: bool = True flag to marshal to preserve that existing functionality.

I think removing normalize_schema from the _get_data_or_400 is safer, but happy to consider other options too!

Edit: Running some future testing, it looks like normalizing already normalized schema is fairly quick.

@illusional illusional requested a review from a team as a code owner July 8, 2025 17:43
@illusional illusional requested a review from Zeerocious July 8, 2025 17:43
@illusional illusional force-pushed the mfranklin/skip-normalize-schema-during-marshal branch from 3d1a0f1 to 2266c96 Compare July 8, 2025 17:56
Comment thread flask_rebar/rebar.py Outdated
@illusional illusional changed the title Mfranklin/skip normalize schema during marshal Optionally skip normalize schema during marshal Jul 23, 2025
@illusional illusional force-pushed the mfranklin/skip-normalize-schema-during-marshal branch from a00c6e2 to 533d01c Compare July 24, 2025 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants