Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swagger document generation, required field is invalid #665

Closed
eddycjy opened this issue Jun 5, 2018 · 2 comments
Closed

Swagger document generation, required field is invalid #665

eddycjy opened this issue Jun 5, 2018 · 2 comments
Labels

Comments

@eddycjy
Copy link

eddycjy commented Jun 5, 2018

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 ?

@achew22
Copy link
Collaborator

achew22 commented Jun 5, 2018

I don't think the code actually supports that right now. That said, it shouldn't be super difficult to add it to the parser. It's already plumed with the appropriate types. You would just need to detect when it is in the list of required fields?

@ivucica, do you have any more specific tips than I just provided? Also, looking at it, why is required a property of the message instead of of the field?

@tmc tmc added openapi and removed openapi labels Jun 19, 2018
@phamducmanh1989
Copy link

phamducmanh1989 commented Jul 4, 2019

@eddycjy You can try

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants