gnostic icon indicating copy to clipboard operation
gnostic copied to clipboard

Custom Response Code

Open krak3n opened this issue 3 years ago • 3 comments

Hey

I have an API endpoint that returns a 201 and not a 200. I can't see how I can configure this, I seem to always get a 200 in the generated OpenAPI v3 yaml.

Is it possible to change the response code?

krak3n avatar Oct 20 '22 10:10 krak3n

For example:

service FooService {
  rpc CreateFoo(CreateFooRequest) returns (CreateFooResponse) {
    option (google.api.http) = {
      post: "/v2/foos"
      body: "foo"
    };

    option (gnostic.openapi.v3.operation) = {
      description: "Creates a foo.";
      responses: {
        response_or_reference: {
          name: "201"
          value: {
            response: {
              description: "Created"
              content: {
                additional_properties: [
                  {
                    name: "application/json";
                    value: {
                      schema: {
                        reference: {
                          _ref: "#/components/schemas/Foo";
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    };
  }
}

Will generate a 200 and a 201, I just want a 201.

            responses:
                "200":
                    description: OK
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/Foo'
                "201":
                    description: Created
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/Foo'

krak3n avatar Oct 20 '22 11:10 krak3n

@krak3n Hi, thanks for this. How are you getting the REST service that returns the 201? Is it produced by a proxy (Envoy or grpc-gateway)? I'd like to keep this generator consistent with what those proxies provide.

This seems to have some relevant links. What I'm seeing suggests that the 201 status code is being set by custom code that runs independently of information in the protos. I think it would be good for overall consistency if the code was set by a proto option, but we might need to define a new one (and it would be good to have that supported in Envoy and gRPC Gateway if we do). Maybe there's an existing one that I haven't found yet?

timburks avatar Nov 30 '22 17:11 timburks

@krak3n how are you transcoding the gRPC response to JSON/REST? As far as I know both envoy and grpc-gateway follow the codes defined here: https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto

So, for any successful response (whether it's get/put/post/patch/delete) you will get 200 back. And protoc-gen-openapi follows the same mapping.

jeffsawatzky avatar Apr 07 '23 01:04 jeffsawatzky