使用GRPC中的swagger生成响应示例值

使用GRPC中的swagger生成响应示例值,swagger,grpc,openapi,swagger-codegen,grpc-go,Swagger,Grpc,Openapi,Swagger Codegen,Grpc Go,我正在使用protoc gen swagger和gRPC服务生成swagger json文件。输出json是用空响应示例生成的,我想将响应示例添加到定义中,以便在生成的json中自动填充 这是我目前的定义 service UserService { rpc GetUser (GetUserRequest) returns (UserResponse){ option (google.api.http) = { get: "/api/v1/user/{userna

我正在使用protoc gen swagger和gRPC服务生成swagger json文件。输出json是用空响应示例生成的,我想将响应示例添加到定义中,以便在生成的json中自动填充

这是我目前的定义

service UserService {
  rpc GetUser (GetUserRequest) returns (UserResponse){
    option (google.api.http) = {
      get: "/api/v1/user/{username}"
      response_body: "*"
    };
    option (grpc.gateway.protoc_gen_swagger.options.openapiv2_operation) = {
      description: "Returns user object";
      operation_id: "get_user";
      summary: "Get User";
    };
  }
}

message GetUserRequest {
  string username = 1;
}

message UserResponse {
  User user = 1;
}

message User {
  string first_name = 1;
  string last_name = 2;
  string username = 3;
}
当我使用命令生成swagger文件时

protoc -I ${PROTOPATH} \
           -I $GOPATH/src \
           --swagger_out=logtostderr=true:${OUT_PATH}
我用这个用户对象定义得到了一个大摇大摆的文件

"User": {
  "type": "object",
  "properties": {
    "first_name": {
      "type": "string"
    },
    "last_name": {
      "type": "string"
    },
    "username": {
      "type": "string"
    },
  }
}
我想用如下示例值生成它:

"User": {
  "type": "object",
  "properties": {
    "first_name": {
      "type": "string",
      "example": "Adam"
    },
    "last_name": {
      "type": "string",
      "example": "Smith"
    },
    "username": {
      "type": "string",
      "example": "asmith79"
    },
  }
}

在这里找到了答案:

简单地将grpc.gateway.protoc_gen_swagger.options.openapiv2_模式作为选项添加到消息中

import "protoc-gen-swagger/options/annotations.proto";

message User {
  option (grpc.gateway.protoc_gen_swagger.options.openapiv2_schema) = {
    example: { value: '{ "first_name": "Adam", "last_name":"Smith", "username":"asmith79"}' }
  };
  string first_name = 1;
  string last_name = 2;
  string username = 3;
}

在这里找到了答案:

简单地将grpc.gateway.protoc_gen_swagger.options.openapiv2_模式作为选项添加到消息中

import "protoc-gen-swagger/options/annotations.proto";

message User {
  option (grpc.gateway.protoc_gen_swagger.options.openapiv2_schema) = {
    example: { value: '{ "first_name": "Adam", "last_name":"Smith", "username":"asmith79"}' }
  };
  string first_name = 1;
  string last_name = 2;
  string username = 3;
}