[python-fastapi][REQ] Add `Fastapi Request` parameter to api methods

未关闭 适合新手
#25,753 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
70/100
Issue 类型
功能
描述清晰度
描述清楚
活跃度
活跃
技术栈
fastapi, openapi, python

调研方向

首先阅读 python-fastapi 生成器模板中的 api.mustache 和 base_api.mustache,并结合 issue 中链接的 FastAPI Request 文档。重新生成一个小的 FastAPI 示例,查看端点和 base 方法是如何输出的,然后向两个模板签名中都添加 Request 参数,并将其传递到实现中。当生成的端点接受并转发 Request,且生成器的 python-fastapi 示例/测试仍能构建时,即完成。

由索引模型根据 Issue 内容生成。

描述

Enhancement: Feature
Need Fastapi Request in the implementation of the api

As the Fastapi documentation says here it would be good to have access to the request object in the implementation of the api.

Modify api.moustache and base_api.moustache

Add the request to the method in the moustache files, e.g.

async def {{operationId}}(
    request: Request,
    {{#allParams}}
    {{>endpoint_argument_definition}},
    {{/allParams}}
    {{#hasAuthMethods}}
    {{#authMethods}}
    token_{{name}}: TokenModel = Security(
        get_token_{{name}}{{#isOAuth}}, scopes=[{{#scopes}}"{{scope}}"{{^-last}}, {{/-last}}{{/scopes}}]{{/isOAuth}}
    ),
    {{/authMethods}}
    {{/hasAuthMethods}}
) -> {{returnType}}{{^returnType}}None{{/returnType}}:
    {{#notes}}"""{{.}}"""
    {{/notes}}if not Base{{classname}}.subclasses:
        raise HTTPException(status_code=500, detail="Not implemented")
    return await Base{{classname}}.subclasses[0]().{{operationId}}(request, {{#allParams}}{{>impl_argument}}{{^-last}}, {{/-last}}{{/allParams}})
{{^-last}}
class Base{{classname}}:
    subclasses: ClassVar[Tuple] = ()

    def __init_subclass__(cls, **kwargs):
        super().__init_subclass__(**kwargs)
        Base{{classname}}.subclasses = Base{{classname}}.subclasses + (cls,)
{{#operations}}
{{#operation}}
    async def {{operationId}}(
        self,
        request: Request,
        {{#allParams}}
        {{>impl_argument_definition}},
        {{/allParams}}
    ) -> {{returnType}}{{^returnType}}None{{/returnType}}:
        {{#notes}}"""{{.}}"""
        ...{{/notes}}{{^notes}}...{{/notes}}
主要语言
Java
星标
26.9k
派生
7.8k
平均合并
2 天 23 分钟
30 天内合并 PR
177

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

OpenAPITools/openapi-generator 的其他 Issue

查看 OpenAPITools/openapi-generator 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。