Thanks to visit codestin.com
Credit goes to github.com

Skip to content

The generated OpenAPI schema duplicates parameter IDs when used with dependencies #967

@tiangolo

Description

@tiangolo

Describe the bug

The generated OpenAPI schema duplicates parameter IDs when used with dependencies.

To Reproduce

Steps to reproduce the behavior with a minimum self-contained file.

  • Create a file with:
from fastapi import Depends, FastAPI
from starlette.testclient import TestClient

app = FastAPI()


async def user_exists(user_id: int):
    return True


@app.post("/users/{user_id}", dependencies=[Depends(user_exists)])
async def bug(user_id: int):
    pass


client = TestClient(app)

openapi_schema = {
    "openapi": "3.0.2",
    "info": {"title": "FastAPI", "version": "0.1.0"},
    "paths": {
        "/users/{user_id}": {
            "post": {
                "summary": "Bug",
                "operationId": "bug_users__user_id__post",
                "parameters": [
                    {
                        "required": True,
                        "schema": {"title": "User Id", "type": "integer"},
                        "name": "user_id",
                        "in": "path",
                    },
                    # This duplicated user_id shouldn't be here
                    # {
                    #     "required": True,
                    #     "schema": {"title": "User Id", "type": "integer"},
                    #     "name": "user_id",
                    #     "in": "path",
                    # },
                ],
                "responses": {
                    "200": {
                        "description": "Successful Response",
                        "content": {"application/json": {"schema": {}}},
                    },
                    "422": {
                        "description": "Validation Error",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/HTTPValidationError"
                                }
                            }
                        },
                    },
                },
            }
        }
    },
    "components": {
        "schemas": {
            "HTTPValidationError": {
                "title": "HTTPValidationError",
                "type": "object",
                "properties": {
                    "detail": {
                        "title": "Detail",
                        "type": "array",
                        "items": {"$ref": "#/components/schemas/ValidationError"},
                    }
                },
            },
            "ValidationError": {
                "title": "ValidationError",
                "required": ["loc", "msg", "type"],
                "type": "object",
                "properties": {
                    "loc": {
                        "title": "Location",
                        "type": "array",
                        "items": {"type": "string"},
                    },
                    "msg": {"title": "Message", "type": "string"},
                    "type": {"title": "Error Type", "type": "string"},
                },
            },
        }
    },
}


def test_reused_param():
    response = client.get("/openapi.json")
    data = response.json()
    assert data == openapi_schema
  • Run it with pytest.

Alternatively, you can run it with Uvicorn go to /openapi.json, copy that and validate it at: https://editor.swagger.io/ . It should be valid.

Expected behavior

The user_id used by the path operation and the dependency shouldn't be duplicated in the OpenAPI schema.

Environment

  • OS: all
  • FastAPI Version [e.g. 0.3.0], get it with: 0.48.0
python -c "import fastapi; print(fastapi.__version__)"
  • Python version, get it with: 3.7
python --version

Additional context

Supersedes #395

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions