mike-oakley / openapi-pydantic

Pydantic OpenAPI schema implementation
Other
66 stars 10 forks source link

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

Open rafalkrupinski opened 3 months ago

rafalkrupinski commented 3 months ago

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 commented 3 months ago

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 commented 4 weeks ago

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)