-
-
Notifications
You must be signed in to change notification settings - Fork 387
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
Bug: OpenAPI schema generation for handler <...> detected multiple parameters named <...> with different types #3889
Comments
For some reason adding |
Yes, because this means there is a conflict, which Litestar cannot resolve on its own. If you define the same parameter with different examples, those are, as far as the OpenAPI schema is concerned, different definitions. That fact that they only differ in the examples doesn't matter much. Can you provide a simple, self-contained example that reproduces the behaviour you're describing? With the example repository I'm not able to reproduce the error you're referencing. |
Yes, this is a small example import uvicorn
from litestar import Controller
from litestar import Litestar
from litestar import post
from litestar import Router
from litestar.di import Provide
from litestar.openapi import OpenAPIConfig
from litestar.params import Parameter
from pydantic import BaseConfig
class Configuration(BaseConfig):
PORT: int = 24500
VERSION: str = "v0.0.1"
class SessionTempViewProvider:
storage: dict[str, str]
def put(self, key: str, value: str) -> None:
self.storage[key] = value
async def view_provider_factory() -> SessionTempViewProvider:
return SessionTempViewProvider()
class ExampleController(Controller):
"""
Example Controller
"""
path = "/example"
tags = ["Example"]
# If we remove dependencies examples: all is OK!
dependencies = {"view_repo": Provide(view_provider_factory)}
@post(path="/put") # If we remove dependencies examples: all is OK!
async def put(
self, idx: str, obj_id: str, view_repo: SessionTempViewProvider
) -> None:
view_repo.put(idx, obj_id)
example_route = Router(
path="/objects",
route_handlers=[ExampleController],
parameters={
# For simple parameter we can provide examples to solve problem
"team_id": Parameter(
str,
description="Team ID",
required=True,
),
# For cookie parameter examples will not help
"x-session-token": Parameter(
str,
description="Session JWT token",
cookie="x-session-token",
required=False,
),
},
# middleware=[auth_middleware()], # Assume that we need parameters to auth and permissions
)
def app():
return Litestar(
debug=True,
route_handlers=[example_route],
openapi_config=OpenAPIConfig(
title="API",
version=Configuration.VERSION,
description="API",
path="/docs",
create_examples=True, # If we remove create examples: all is OK!
),
)
def main():
"""Run auth service"""
uvicorn.run(
"app.run:app",
host="0.0.0.0",
port=Configuration.PORT,
reload=True,
)
if __name__ == "__main__":
main() |
Okay so here is a minimal reproducer: from litestar import Litestar
from litestar import post
from litestar.openapi import OpenAPIConfig
from litestar.params import Parameter
async def provider() -> str:
return "hello"
@post(path="/")
async def handler(dep: str) -> None:
pass
app = Litestar(
debug=True,
route_handlers=[handler],
dependencies={"dep": provider},
parameters={"team_id": Parameter()},
openapi_config=OpenAPIConfig(
title="API",
version="",
create_examples=True,
),
)
app.openapi_schema |
For some reasons i cannot run your example |
That's an unrelated issue with your Python version + how you're running it. See #3647 |
Thanks you, as quick solution without changing python version i make this, and get OpenAPI error about different types
|
I'm not sure is it possible to break something, but adding to litestar.openapi.spec.schema.Schema (#3890) solves the problem
|
Description
Hello everyone! I encountered a behavior, and I'm not sure if it's correct.
The reproduction steps are attached in the form of a repository (the following description will refer to it).
As a demonstration, I wanted to create BasicViewProvider, which is a simple key-value pair and used as dependency at Controller
At the same time, my path group requires additional parameters (in the real task, to provide object access models via middleware). So, I register the group as
The handler calls work correctly and do what they should, however, after I enter Swagger, I get an error:
It's important to note that when the application starts, I add example creation in the OpenAPI configuration.
If I turn off example generation, the error disappears.
If I disable dependency injection, the error disappears.
If I set an example parameter when registering the group, the error disappears, but I was unable to set an example for a cookie-type parameter (Swagger will open, but gives infinite spinner after clicking at route).
Next, I started investigating the cause of this behavior and found that multiple parameter declarations are being called because the examples in the Schema type differ, even when all other fields match.
litestar/_openapi/parameters.py
Which gives me next results
So my general question, is there any reasons that we need to check examples equality? Because if i ignore that field: all is OK!
URL to code causing the issue
https://github.com/Molozey/litestar-params-demo
MCVE
# Your MCVE code here
Steps to reproduce
Screenshots
No response
Logs
No response
Litestar Version
litestar==2.13.0
Platform
Note
While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.
Check out all issues funded or available for funding on our Polar.sh dashboard
The text was updated successfully, but these errors were encountered: