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

schema() not called recursively + Config.schema_extra discards unknown JSON schema #1970

Closed
skewty opened this issue Oct 5, 2020 · 0 comments

Comments

@skewty
Copy link
Contributor

skewty commented Oct 5, 2020

Scenario / Use Case

class StreamPubSub(BaseModel):
    modules: List[SourceName]
    stream_key: StreamKey

    class Config:
        @staticmethod
        def schema_extra(schema, _):
            schema['headerTemplate'] = '{{ self.stream_key }}'
            # other modifications removed

The unknown JSON schema key headerTemplate is not present when StreamPubSub is nested inside other models and we call .schema() on the outer most model.

In order to get headerTemplate into the schema we had to move the schema modification into the model's schema method as:

class StreamPubSub(BaseModel):
    modules: List[SourceName]
    stream_key: StreamKey

    @classmethod
    def schema(cls, by_alias: bool = True):
        schema = super().schema(by_alias)
        schema['headerTemplate'] = '{{ self.stream_key }}'
        # other modifications removed
        return schema

The outer most model ignores the schema() implementation / override of all contained models. To work around this behavior we use the following function after calling schema() on the outer most model:

def reschema_from_models(schema: dict, namespace: dict) -> dict:
    definitions = schema['definitions']
    for class_name in tuple(definitions.keys()):
        try:
            definition_schema = namespace[class_name].schema()
            definition_schema.pop('definitions', None)
            definitions[class_name] = definition_schema
        except (KeyError, AttributeError):
            pass
    return schema

This is similar, but not identical to #1287 (Model.json() not called recursively).

Is there a reason the FooModel methods are not called when FooModel is nested within a BarModel?

Schema edits seem to be a PITA right now (in our code base) with the current code implementation.
See issues like: #1358 where it could be made much more simple.
As it stands it seems we often need a schema_extra and then that schema_extra doesn't work as expected.

I'd be happy to work on the code to correct all these things. I just want to know if the idea / propositions will be accepted before I start work.

PS: we love what pydantic has brought to our code base. Thanks for the many hours that have gone into this amazing library. Shout out to FastAPI too; we love you too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant