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

Better support of Specification Extensions for callbacks, paths and responses #40

Open
rafalkrupinski opened this issue Aug 31, 2024 · 2 comments

Comments

@rafalkrupinski
Copy link

rafalkrupinski commented Aug 31, 2024

Currently callbacks, paths and responses are modelled as dicts, but that breaks support for specification extensions.

An alternative solution would be to use before-validation to catch all non-extension entries and put them in a dict inside the model, and leave extensions to be handled by extra. Basically all dynamic names would be pushed down into a dict. This would improve compatibility with python but break 1-1 mapping with the JSON representation, and would need to be coupled with pre-processing during serialization.

class Paths(BaseModel)
  paths = dict[str, PathItem]

  if PYDANTIC_V2:
    @validate_model
    def validate():...
    @model_serializer
    def serialize():...
  else:
    class Config:
      json_loads = load_paths
      json_dumps = dump_paths

I've done something roughly similar but only for V2 and only for parsing.

@rafalkrupinski
Copy link
Author

rafalkrupinski commented Sep 1, 2024

actual example from my project:

class Responses(ExtendableModel, ModelWithPatternProperties):
    responses: typing.Annotated[
        dict[str, Reference[Response] | Response],
        pydantic.Field(default_factory=dict, min_length=1),
        PropertyPattern(r'^[1-5](?:\d{2}|XX)|default$'),
    ]

@mike-oakley
Copy link
Owner

Hey @rafalkrupinski - happy to accept a contribution for this. In general I'd prefer to maintain support for Pydantic v1 + v2 for now until v1 support is dropped on the Pydantic side, so if you could extend the v2 approach with the compat module that would be ideal (it can be a bit fiddly though with typing)

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

No branches or pull requests

2 participants