gnostic icon indicating copy to clipboard operation
gnostic copied to clipboard

path parameters that refer to request message subfields are not mapped

Open sarnesjo opened this issue 2 years ago • 0 comments

protoc-gen-openapi does not map path parameters that refer to request message subfields (as in "/categories/{book_key.category}/books/{book_key.name}") as expected.

As an example, running protoc like this:

protoc library.proto \
  --proto_path=. \
  --proto_path=/path/to/googleapis \
  --openapi_out=. \
  --openapi_opt=enum_type=string \
  --openapi_opt=default_response=false

with this proto:

syntax = "proto3";

package library;

import "google/api/annotations.proto";

option go_package = "example.com/library";

service Library {
  rpc GetBook(GetBookRequest) returns (Book) {
    option (google.api.http) = {
      get: "/categories/{book_key.category}/books/{book_key.name}"
    };
  }
}

enum Category {
  CATEGORY_UNSPECIFIED = 0;
  FICTION = 1;
  NONFICTION = 2;
}

message BookKey {
  Category category = 1;
  string name = 2;
}

message GetBookRequest {
  BookKey book_key = 1;
}

message Book {
}

produces this output:

# Generated with protoc-gen-openapi
# https://github.com/google/gnostic/tree/master/cmd/protoc-gen-openapi

openapi: 3.0.3
info:
    title: Library API
    version: 0.0.1
paths:
    /categories/{book_key.category}/books/{book_key.name}:
        get:
            tags:
                - Library
            operationId: Library_GetBook
            parameters:
                - name: book_key.category
                  in: path
                  required: true
                  schema:
                    type: string
                - name: book_key.name
                  in: path
                  required: true
                  schema:
                    type: string
                - name: bookKey.category
                  in: query
                  schema:
                    enum:
                        - CATEGORY_UNSPECIFIED
                        - FICTION
                        - NONFICTION
                    type: string
                    format: enum
                - name: bookKey.name
                  in: query
                  schema:
                    type: string
            responses:
                "200":
                    description: OK
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/Book'
components:
    schemas:
        Book:
            type: object
            properties: {}
tags:
    - name: Library

Note that book_key.category and book_key.name have not been mapped to their corresponding request message subfields, which therefore also show up as query parameters (bookKey.category and bookKey.name).

As far as I know, having path parameters refer to subfields is valid. It's mentioned in http.proto and is supported by the Envoy gRPC-JSON transcoder.

sarnesjo avatar May 01 '23 07:05 sarnesjo