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

feat: Add support for v2 model_dump and v1 dict to pydantic plugin #3171

Conversation

kedod
Copy link
Contributor

@kedod kedod commented Mar 5, 2024

Description

  • Add support for additional V1 model_dump and V2 dict parameters in PydanticPlugin

Closes #3147

@kedod kedod requested review from a team as code owners March 5, 2024 08:27
@kedod kedod marked this pull request as draft March 5, 2024 08:28
Copy link

codecov bot commented Mar 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.26%. Comparing base (45d2523) to head (1e1a98c).

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #3171   +/-   ##
========================================
  Coverage    98.26%   98.26%           
========================================
  Files          322      322           
  Lines        14728    14738   +10     
  Branches      2347     2347           
========================================
+ Hits         14472    14482   +10     
  Misses         117      117           
  Partials       139      139           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@kedod kedod force-pushed the 3147-add-support-all-kwargs-for-model-dump-in-pydantic-plugin branch 6 times, most recently from 3d4d938 to c1bda9d Compare March 5, 2024 13:32
@guacs
Copy link
Member

guacs commented Mar 5, 2024

What if instead of taking in the arguments explicitly, we take a dictionary of the keyword arguments that we'll pass to each function? My reasoning is that, this way if pydantic adds a new kwarg or changes a default value or deprecates a kwarg, then we don't have to worry about it. The user can make the change accordingly in their code and there would be no issues. Otherwise, we would have to keep in sync with pydantic which can become difficult if the kwargs differ based on the pydantic version.

A drawback to this approach is that users lose the type checking, but I feel the flexibility it provides is nice.

Also, I was thinking that along with this we could take arguments for model_validate/pase_obj as well though I don't know if parse_obj does take any arguments other than the value as a dictionary.

@provinzkraut
Copy link
Member

provinzkraut commented Mar 5, 2024

@guacs I agree that it would be easier, but I'd say we should side with the more type safe option here, since that's what we're doing everywhere else.

There is a way to type something like this btw, but it's not pretty:

from typing import Callable, Any, Generic, ParamSpec

P = ParamSpec("P")

class _Foo(Generic[P]):
    def __init__(self, **kwargs: P.kwargs) -> None:
        pass


def _make_foo(f: Callable[P, Any]) -> type[_Foo[P]]:
    return _Foo[P]


def func(param: str) -> None:
    pass


Foo = _make_foo(func)

Foo(param=1)

This actually type-checks correctly, but it's quite ugly. Not sure if there's a better way to do something like this; If there is, I'm not aware. @sobolevn maybe you have an idea here? :)

@kedod
Copy link
Contributor Author

kedod commented Mar 5, 2024

@guacs @provinzkraut
I've met a few problems with extending PydanticSchemaPlugin.

Let's for example talk about exclude_none=True parameter.
Then we have two possible response outputs:

  • our response will have param if it's value is not None.
  • our response will not have param key if value == None

How would you see this in OpenAPI spec?
I was thinking about using oneOf. Something similar to the following example:

responses:
        '200':
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/ResponseOne'
                  - $ref: '#/components/schemas/ResponseTwo'

So there will be two possible schemas. I guess we can do the same for the examples (but IMO it's better is to keep single example with all possible fields IMO).

But there is a downsides too:

  • As I can see only SwaggerUI shows response schemas
    image
  • **There can be many response combinations of fields (exclude_none, exclude_default...) and implementing display all of them may be time consuming. Also looking at those schemas from the user POV can be overwhelming. **

Now the worst part.
We have four others params to support not only exclude_none.
How to proceed with this? Maybe I missed something but it looks like that implementing could be not so easy.

@provinzkraut
Copy link
Member

@kedod Have you checked what schema Pydantic generates in those cases?

@kedod
Copy link
Contributor Author

kedod commented Mar 5, 2024

@kedod Have you checked what schema Pydantic generates in those cases?

I've made MVCE:

from pydantic import BaseModel, Field

from litestar import Litestar, get
from litestar.contrib.pydantic import PydanticPlugin


class Model(BaseModel):
    name: str = Field(serialization_alias="alias_name")
    none: None = None
    default: str = "default"
    exclude: str


@get()
async def handler() -> Model:
    return Model(
        name="name",
        exclude="i should not be here"
    )


app = Litestar(
    [handler],
    plugins=[
        PydanticPlugin(
            prefer_alias=True, exclude_none=True, exclude_defaults=True, exclude_unset=True, exclude={"exclude"}
        )
    ],
)

This is how our response looks like:

{"alias_name":"name"}

And the SwaggerUI:
image

I'm wondering if can move fixing docs to the separate issue.
There is already opened problem for the alias -> #2870

@provinzkraut
Copy link
Member

No I meant what Pydantic itself generates, not what we generate: https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel.model_json_schema

@kedod
Copy link
Contributor Author

kedod commented Mar 5, 2024

No I meant what Pydantic itself generates, not what we generate: https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel.model_json_schema

For the following handler:

@get()
async def handler() -> dict:
    return Model(
        name="name",
        exclude="i should not be here"
    ).model_json_schema()

We get this ->

{
  "properties": {
    "name": {
      "title": "Name",
      "type": "string"
    },
    "none": {
      "default": null,
      "title": "None",
      "type": "null"
    },
    "default": {
      "type": "string",
      "default": "default",
      "title": "Default"
    },
    "exclude": {
      "title": "Exclude",
      "type": "string"
    }
  },
  "required": [
    "name",
    "exclude"
  ],
  "title": "Model",
  "type": "object"
}

@provinzkraut
Copy link
Member

Interesting. It seems like Pydantic itself simply doesn't take these particularities into account. There is the by_alias option for model_json_schema, but that's about it.

@provinzkraut
Copy link
Member

How about we simply make them not required if any of the exclude options is set that could apply to a certain field? E.g. a field that can be None would be optional if exclude_none=True?

@kedod
Copy link
Contributor Author

kedod commented Mar 5, 2024

How about we simply make them not required if any of the exclude options is set that could apply to a certain field? E.g. a field that can be None would be optional if exclude_none=True?

Great. I will try to implement this approach.

@kedod kedod force-pushed the 3147-add-support-all-kwargs-for-model-dump-in-pydantic-plugin branch from c1bda9d to 13b939f Compare March 5, 2024 18:32
@guacs
Copy link
Member

guacs commented Mar 6, 2024

@guacs I agree that it would be easier, but I'd say we should side with the more type safe option here, since that's what we're doing everywhere else.

There is a way to type something like this btw, but it's not pretty:

from typing import Callable, Any, Generic, ParamSpec

P = ParamSpec("P")

class _Foo(Generic[P]):
    def __init__(self, **kwargs: P.kwargs) -> None:
        pass


def _make_foo(f: Callable[P, Any]) -> type[_Foo[P]]:
    return _Foo[P]


def func(param: str) -> None:
    pass


Foo = _make_foo(func)

Foo(param=1)

This actually type-checks correctly, but it's quite ugly. Not sure if there's a better way to do something like this; If there is, I'm not aware. @sobolevn maybe you have an idea here? :)

I didn't understand how this black magic will allow us to type hint the arguments automatically.

@kedod kedod force-pushed the 3147-add-support-all-kwargs-for-model-dump-in-pydantic-plugin branch 7 times, most recently from 2314979 to 53d93fb Compare March 9, 2024 16:38
@kedod kedod marked this pull request as ready for review March 9, 2024 16:45
kedod and others added 19 commits March 30, 2024 08:10
In some cases, I've wanted to change the name of the "Application" to show something else instead of "Litestar".  This is usually to make a CLI feel more cohesive and part of a larger application.  In the same vein, I've found cases where I wanted to complete suppress the initial `from_env` or the Rich info table at startup.
---------

Co-authored-by: Janek Nouvertné <[email protected]>
Co-authored-by: Jacob Coffee <[email protected]>
* refactor: openapi router

This PR refactors the way that we support multiple UIs for OpenAPI.

We add `litestar.openapi.plugins` where `OpenAPIRenderPlugin` is defined, and implementations of that plugin for the frameworks we currently support.

We add `OpenAPIConfig.render_plugins` config option, where a user can explicitly declare a set of plugins for UIs they wish to support.

If a user declares a sub-class of `OpenAPIController` at `OpenAPIConfig.openapi_controller`, then existing behavior is preserved exactly.

However, if no controller is explicitly declared, we invoke the new router-based approach, which should behave identically to the controller based approach (i.e., respect `enabled_endpoints` and `root_schema_site`).

Closes litestar-org#2541

* docs: start of documentation re-write.

- creates an indexed directory for openapi
- removes the controller docs
- start of docs for plugins

* refactor: move JsonRenderPlugin into private ns

We add the json plugin, and have hardcoded refs to the path that it serves, so best not to make this public API just yet.

* docs: reference docs for plugins

* Revert "refactor: move JsonRenderPlugin into private ns"

This reverts commit 60719aa.

* docs: JsonRenderPlugin undocumented.

* docs: continue plugin docs

* test: run tests for both plugin and controller

Modifies tests where appropriate to run on both the plugin-based approach and the controller based approach.

* Implement default endpoint selection logic.

* Deprecation of OpenAPIController configs

* docs: swagger oauth examples

* Update docs/usage/openapi/ui_plugins.rst

* Update docs/usage/openapi/ui_plugins.rst

* fix: linting

* refactor: don't rely on DI for openapi schema in plugin handler.

* fix(test): there's an extra schema route to serve 404s.

* fix(docs): docstring indent

* fix(lint): remove redundant return

* refactor: plugins receive style tag instead of tag content.

* feat: allow openapi router to be handed to openapi config.

Allows for customization, such as adding guards, middleware, other routes, etc.

* feat: add `scalar` schema ui (litestar-org#2906)

* Update litestar/openapi/plugins.py

Co-authored-by: Jacob Coffee <[email protected]>

* Update litestar/openapi/plugins.py

Co-authored-by: Jacob Coffee <[email protected]>

* Update litestar/openapi/plugins.py

Co-authored-by: Jacob Coffee <[email protected]>

* Update litestar/openapi/config.py

Co-authored-by: Jacob Coffee <[email protected]>

* Update litestar/openapi/config.py

Co-authored-by: Jacob Coffee <[email protected]>

* fix: update deprecation version

* fix: use GH repo for scalar links

* fix: update default scalar version

* fix: scalar plugin style attribute render.

Plugins expect that the style value is already wrapped in `<style>` tags.

* fix: serve default static files via jsdeliver

* fix: docstring syntax

* fix: removes custom repr

Can always add if there's a need for it, but we aren't using it.

* docs: another pass

* fix: style

* fix: test for updated build openapi plugin example

* fix: absolute paths for openapi.json

Resolves litestar-org#3047 for the openapi router case.

* refactor: simplify scalar plugin.

* fix: linting

* Update litestar/_openapi/plugin.py

* refactor: test app to use scalar ui plugin

* fix: plugin customization example

Version arg is ignored if `js_url` is provided.

* fix: remove unnecessary kwargs

Removes passing default values to plugin kwargs in examples.

* fix: grammar error

* feat: make OpenAPIRenderPlugin an ABC

Abstract on `render()` method.

* fix: correct referenced lines

Referenced LoC in example had drifted.

* fix: more small docs corrections

* chore: remove dup spec of enabled endpoints.

* fix: simplify test.

---------

Co-authored-by: Jacob Coffee <[email protected]>
* Enable DTO codegen backend by default
* Update docs
* feat: Added precedence of CLI parameters over envs

* Update docs/usage/cli.rst

Co-authored-by: Peter Schutt <[email protected]>

* Remove redundant LitestarEnv fields and fix tests

* Update docs/usage/cli.rst

* Update litestar/cli/commands/core.py

* Update docs/usage/cli.rst

* Update docs/usage/cli.rst

* Update litestar/cli/commands/core.py

---------

Co-authored-by: kedod <kedod>
Co-authored-by: Peter Schutt <[email protected]>
Co-authored-by: Jacob Coffee <[email protected]>
…3204)

* feat: Support `schema_extra` in `Parameter` and `Body` (litestar-org#3022)

This adds sort of a backdoor for modifying the generated OpenAPI spec.

The value is given as `dict[str, Any]` where the key must match with the
keyword parameter name in `Schema`. The values are used to override items in
the generated `Schema` object, so they must be in correct types (ie. not in
dictionary/json format).

The values are added at main level, without recursive merging (because we're
adjusting `Schema` object and not a dictionary). Recursive merge would be much
more work.

Chose not to implement the same for `ResponseSpec` because response models are
generated as schema components, while `ResponseSpec` can be locally different.
Handling the logic of creating new components when `schema_extra` is passed in
`ResponseSpec` would be extra effort, and isn't probably as important as being
able to adjust the inbound parameters, which are actually validated (and for
which the documentation is even more important, than for the response).

* Update litestar/params.py

Co-authored-by: Jacob Coffee <[email protected]>

* Update litestar/params.py

Co-authored-by: Jacob Coffee <[email protected]>

* Update litestar/params.py

Co-authored-by: Jacob Coffee <[email protected]>

---------

Co-authored-by: Jacob Coffee <[email protected]>
Adds deprecated directives for the deprecated parameters of the config.

Adds some cross-references.
* feat: add typevar expansion litestar-org#3240

* chore: resolve all PR suggestion litestar-org#3242

* chore: resolve import formatting

* chore: resolve import formatting
* adding draft for security exclusion docs

* adding section to security toctree

* Update docs/usage/security/excluding-and-including-endpoints.rst

* Update docs/usage/security/excluding-and-including-endpoints.rst

* Update docs/usage/security/excluding-and-including-endpoints.rst

---------

Co-authored-by: Jacob Coffee <[email protected]>
…r-org#3227)

* feat: Add LITESTAR_ prefix for web concurrency env option

* Replace depacrated with versionchanged directive

* Change wc option description

* Remove depracation warning

---------

Co-authored-by: kedod <kedod>
peterschutt and others added 2 commits March 31, 2024 17:54
feat: pydantic DTO with non-instantiable types.

This PR simplifies the type that we apply to transfer models for pydantic field types in specific circumstances.

- `JsonValue`: this field type is an instance of `TypeAliasType` at runtime, and contains recursive type definitions. Pydantic allow `list`, `dict`, `str`, `bool`, `int`, `float` and `None`, and the value types of `list` and `dict` are allowed to be the same. We type this as `Any` on the transfer models as this is pretty much the same thing for msgspec ([ref][1]).
- `EmailStr`. These are typed as `EmailStr` which is a class at runtime which is not a `str`, however they are a string and `if TYPE_CHECKING` is used to get type checkers to play along. If we return a `str` from a msgspec decode hook for the type, it msgspec won't validate the input. So we must tell msgspec its a string. This also works well with encoding because it is one.
- IP/Network/Interface types. These are represented by types such as `IPvAnyAddress`, but they are actually parsed into instances of stdlib types such as `IPv4Address` and others from the `ipaddress` module. Given that an instance of `IPvAnyAddress` cannot be meaningfully returned from a decoding hook, we have to tell msgspec that these are strings on the transfer models. Encoding the stdlib ip types to str is natively handled by msgspec.

Closes litestar-org#2334

1: https://jcristharif.com/msgspec/supported-types.html#any
Copy link

github-actions bot commented Apr 2, 2024

Documentation preview will be available shortly at https://litestar-org.github.io/litestar-docs-preview/3171

@JacobCoffee JacobCoffee deleted the branch litestar-org:develop April 5, 2024 20:38
@JacobCoffee JacobCoffee closed this Apr 5, 2024
@JacobCoffee
Copy link
Member

Sorry about this, but this should be reopened and pointed to main.

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.

10 participants