Swagger API代码生成合并服务和模型类

huangapple go评论64阅读模式
英文:

Swagger API Code generation combine service and model class

问题

I understand your request. Here is the translated content:

我正在尝试使用API IDL类似swagger、grpc等的代码生成工具。
大多数这些工具会为服务客户端创建一个单独的类,以及一个模型类。
例如,如果我有一个名为User的服务,其中有一个名为get的方法,用于返回所有用户,如下所示:

"/users":{
    "get": {
        "summary": "获取用户",
        "description": "",
        "operationId": "getUsers",
        "responses": {
            "200": {
                "description": "操作成功",
                "schema": {
                    "$ref": "#/definitions/Users"
                }
            }
        }
    }
},
"definitions": {
    "User": {
        "type": "object",
        "properties": {
            "id": {
                "type": "integer",
                "format": "int64"
            },
            "username": {
                "type": "string"
            },
            "firstName": {
                "type": "string"
            },
            "lastName": {
                "type": "string"
            },
            "groupid": {
                "type": "integer"
            }
        }
    }
}

我不想为UserAPIClient和用户数据模型创建不同的类,我希望一个名为User的类具有处理API调用并返回结果的getUsers方法,就像ORM一样。在swagger OpenAPI代码生成工具或其他代码生成工具如grpc、thrift等中是否有可能实现这一点?

谢谢!

英文:

I am trying out the code generation tools with API IDL like swagger, grpc etc.
Most of these tools create a seperate class for Service Client and a Model class
for e.g. If I have a Service call User with method get which returns all Users like

 "/users":{
            "get": {
                "summary": "get a user",
                "description": "",
                "operationId": "getUsers",
                "responses": {
                    "200": {
                    "description": "successful operation",
                    "schema": {
                        "$ref": "#/definitions/Users"
                    }
                    }
                }
            }
        },
 "definitions": {
        "User": {
            "type": "object",
            "properties": {
                "id": {
                  "type": "integer",
                  "format": "int64"
                },
                "username": {
                  "type": "string"
                },
                "firstName": {
                  "type": "string"
                },
                "lastName": {
                  "type": "string"
                },
                "groupid": {
                    "type": "integer"
                }                
            }
        },

I don't want to create different classes for UserAPIClient and User data model, I want one class User to have method getUsers which will handle the API call and return the result, like an ORM, is this possible in swagger OpenAPI code gen tool or any other code gen tool like grpc, thrift etc

Thanks

答案1

得分: 0

这似乎是不可能的,因为所有的代码生成工具都会生成具有API客户端作为模型类的单独类的代码(MVC模式)。将两者混合在一起并不被建议作为良好的做法。

英文:

It seems this is not possible, since all the codegen tools generate code with API client as a separate class than the model class (MVC pattern). Mixing the two is not recommended as a good practice.

huangapple
  • 本文由 发表于 2023年4月6日 19:58:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949266.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定