Skip to content

Swagger document generation, required field is invalid #665

Closed
@eddycjy

Description

Hello,

I now have a problem that when the swagger document is generated, the required fields are invalid.

  1. test.proto:
syntax = "proto3";

package test;

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

service TestService {
    rpc GetProject(ProjectRequest) returns (ProjectResponse) {
        option (google.api.http) = {
            get : "/projects"
        };
    }
}

message ProjectRequest {
    option (grpc.gateway.protoc_gen_swagger.options.openapiv2_schema) = {
        json_schema: {
             required: [ "project_id" ];
        };
    };
    int64 project_id = 1;
}

message ProjectResponse {

}
  1. command
protoc -I=. -I=$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway \
                -I=$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
                --grpc-gateway_out=logtostderr=true:. \
                --go_out=plugins=grpc:. \
                --swagger_out=logtostderr=true:. \
                ./test.proto 
  1. test.swagger.json:
{
  "swagger": "2.0",
  "info": {
    "title": "test.proto",
    "version": "version not set"
  },
  "schemes": [
    "http",
    "https"
  ],
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "paths": {
    "/projects": {
      "get": {
        "operationId": "GetProject",
        "responses": {
          "200": {
            "description": "",
            "schema": {
              "$ref": "#/definitions/testProjectResponse"
            }
          }
        },
        "parameters": [
          {
            "name": "project_id",
            "in": "query",
            "required": false,
            "type": "string",
            "format": "int64"
          }
        ],
        "tags": [
          "TestService"
        ]
      }
    }
  },
  "definitions": {
    "testProjectResponse": {
      "type": "object"
    }
  }
}

I hope parameters (project_id) is true, but got false...

Expected results, etc.

 "parameters": [
          {
            "name": "project_id",
            "in": "query",
            "required": true,
            "type": "string",
            "format": "int64"
          }
        ],

How can I solve this problem ?

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions