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

Allow Schema model_config.extra = "allow" #1097

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

soccer99
Copy link

Allow setting the model_config key extra="allow"

# schemas.py

class ExtraAllowedSchema(Schema):
    name: str

    class Config(Schema.Config):
        extra = "allow"

# views.py
@api.post("/extra")
def extra_params(request, payload: ExtraAllowedSchema):
    """
    Test that a Schema with model_config.extra = "allow" will allow keys not defined in schema
    """
    return payload

A request with extra keys will be honored and returned

# Request
POST: /extra
JSON payload {"name": "my name", "age": 100}
Response
{"name": "my name", "age": 100}

Also works on response schemas if you return a dictionary with extra keys outside your OutSchema.

Fixes issue: #1087

@p-baum
Copy link

p-baum commented Mar 4, 2024

This change causes extra fields to be included in the main parsed data - clearly not what we want. We need the extra data stored in the model_extra attribute.

@Vulwsztyn
Copy link

This change causes extra fields to be included in the main parsed data - clearly not what we want.

Not really. It's ok.

@soccer99
Copy link
Author

soccer99 commented Mar 5, 2024

Including the extra fields at the top level is the default pydantic behavior when using extra = "allow". I suppose we could only expose those extra fields in an extra param on the view function if thats what is decided.

@Vulwsztyn
Copy link

@vitalik Could you take a look?

I'm sorry if it is not ok that I ping you, but I'm interested in this feature

@vitalik
Copy link
Owner

vitalik commented Apr 30, 2024

Still thinking about it (need more testing on like how it will behave wit schema have resolvers or handles objects outputs instead of dicts...)

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.

4 participants