github.com/sanposhiho/openapi2proto@v0.0.0-20230521044535-d1080a134e37/README.md (about)

     1  # openapi2proto [![Build Status](https://travis-ci.org/nytimes/openapi2proto.svg?branch=master)](https://travis-ci.org/nytimes/openapi2proto)
     2  
     3  This tool will accept an OpenAPI/Swagger definition (yaml or JSON) and generate a Protobuf v3 schema and gRPC service definition from it.
     4  
     5  ## Install
     6  
     7  To install, have Go installed with `$GOPATH/bin` on your `$PATH` and then:
     8  ```
     9  go install github.com/sanposhiho/openapi2proto/cmd/openapi2proto@latest
    10  ```
    11  
    12  On Older versions of Go (<1.15), use:
    13  ```
    14  go get -u github.com/sanposhiho/openapi2proto/cmd/openapi2proto
    15  ```
    16  
    17  ## Run
    18  
    19  There are some CLI flags for using the tool:
    20  * `-spec` to point to the appropriate OpenAPI spec file
    21  * `-annotate` to include (google.api.http options) for [grpc-gateway](https://github.com/gengo/grpc-gateway) users. This is disabled by default.
    22  * `-out` to have the output written to a file rather than `Stdout`. Defaults to `Stdout` if this is not specified.
    23  * `-indent` to override the default indentation for Protobuf specs of 4 spaces.
    24  * `-skip-rpcs` to skip generation of rpcs. These are generated by default.
    25  * `-skip-deprecated-rpcs` to skip generation of rpcs for endpoints marked as deprecated. This is disabled by default.
    26  * `-namespace-enums` to enable inserting the enum name as an enum prefix for each value. This is disabled by default.
    27  * `-add-autogenerated-comment` to add comment on top of the generated protos that those files are autogenerated and should not be modified. This is disabled by default.
    28  
    29  ## Protobuf Tags
    30  * To allow for more control over how your protobuf schema evolves, all parameters and property definitions will accept an optional extension parameter, `x-proto-tag`, that will overide the generated tag with the value supplied.
    31  
    32  ## External Files
    33  * Any externally referenced Open API spec will be fetched and inlined.
    34  * Any externally referenced Protobuf files will be added as imports.
    35    * Example usage: `$ref: "google/protobuf/timestamp.proto#/google.protobuf.Timestamp"`
    36  
    37  ## Global Options
    38  
    39  Protocol Buffer options such as package names are supported via `x-global-options` key.
    40  
    41  ```yaml
    42  x-global-options:
    43    go_package: myawesomepackage
    44  ```
    45  
    46  Will generate:
    47  
    48  ```protobuf
    49  option go_package = "myawesomepackage"
    50  ```
    51  
    52  ## Extensions
    53  
    54  Global extensions may be generated by specifying `x-extensions` key.
    55  
    56  ```yaml
    57  x-extensions:
    58  - base: google.protobuf.MethodOptions
    59    fields:
    60    - name: role
    61      type: string
    62      number: 50001
    63    - name: visibility
    64      type: string
    65      number: 50002
    66    - name: timeout
    67      type: int32
    68      number: 50003
    69  ```
    70  
    71  Will generate:
    72  
    73  ```protobuf
    74  extend google.protobuf.MethodOptions {
    75      string role = 50001;
    76      string visibility = 50002;
    77      int32 timeout = 50003;
    78  }
    79  ```
    80  
    81  Nested extensions are currently not supported.
    82  
    83  ## Method Options
    84  
    85  Method options may be generated by specifying the `x-options` key within each method.
    86  
    87  ```yaml
    88  paths:
    89    /foo
    90      post:
    91        x-options:
    92          bar: baz
    93  ```
    94  
    95  Will generate:
    96  
    97  ```protobuf
    98      rpc foo(...) returns (...) {
    99        option (bar) = "baz";
   100      }
   101  ```
   102  
   103  ## Caveats
   104  
   105  * Fields with scalar types that can also be "null" will get wrapped with one of the `google.protobuf.*Value` types.
   106  * Fields with that have more than 1 type and the second type is not "null" will be replaced with the `google.protobuf.Any` type.
   107  * Endpoints that respond with an array will be wrapped with a message type that has a single field, 'items', that contains the array.
   108  * Only "200" and "201" responses are inspected for determining the expected return value for RPC endpoints.
   109  * To prevent enum collisions and to match the [protobuf style guide](https://developers.google.com/protocol-buffers/docs/style#enums), enum values will be `CAPITALS_WITH_UNDERSCORES` and nested enum values will have their parent types prepended to their names.
   110  
   111  
   112  ## Example
   113  
   114  ```
   115  ╰─➤  openapi2proto -spec swagger.yaml -annotate
   116  syntax = "proto3";
   117  
   118  package swaggerpetstore;
   119  
   120  import "google/api/annotations.proto";
   121  import "google/protobuf/empty.proto";
   122  
   123  message AddPetRequest {
   124      message PetMessage {
   125          int64 id = 1;
   126          string name = 2;
   127          string tag = 3;
   128      }
   129  
   130      // Pet to add to the store
   131      PetMessage pet = 1;
   132  }
   133  
   134  message AddPetResponse {
   135      int64 id = 1;
   136      string name = 2;
   137      string tag = 3;
   138  }
   139  
   140  message DeletePetRequest {
   141      // ID of pet to delete
   142      int64 id = 1;
   143  }
   144  
   145  message FindPetByIdRequest {
   146      // ID of pet to fetch
   147      int64 id = 1;
   148  }
   149  
   150  message FindPetByIdResponse {
   151      int64 id = 1;
   152      string name = 2;
   153      string tag = 3;
   154  }
   155  
   156  message FindPetsByIdsRequest {
   157      repeated string ids = 1;
   158  
   159      // maximum number of results to return
   160      int32 limit = 2;
   161  }
   162  
   163  message FindPetsByIdsResponse {
   164      message PetsMessage {
   165          int64 id = 1;
   166          string name = 2;
   167          string tag = 3;
   168      }
   169  
   170      repeated PetsMessage pets = 1;
   171  }
   172  
   173  message FindPetsRequest {
   174      // maximum number of results to return
   175      int32 limit = 1;
   176  
   177      // tags to filter by
   178      repeated string tags = 2;
   179  }
   180  
   181  message FindPetsResponse {
   182      message PetsMessage {
   183          int64 id = 1;
   184          string name = 2;
   185          string tag = 3;
   186      }
   187  
   188      repeated PetsMessage pets = 1;
   189  }
   190  
   191  service SwaggerPetstoreService {
   192      // Creates a new pet in the store.  Duplicates are allowed
   193      rpc AddPet(AddPetRequest) returns (AddPetResponse) {
   194          option (google.api.http) = {
   195              post: "/api/pets"
   196              body: "pet"
   197          };
   198      }
   199  
   200      // deletes a single pet based on the ID supplied
   201      rpc DeletePet(DeletePetRequest) returns (google.protobuf.Empty) {
   202          option (google.api.http) = {
   203              delete: "/api/pets/{id}"
   204          };
   205      }
   206  
   207      // Returns a user based on a single ID, if the user does not have access to the pet
   208      rpc FindPetById(FindPetByIdRequest) returns (FindPetByIdResponse) {
   209          option (google.api.http) = {
   210              get: "/api/pets/{id}"
   211          };
   212      }
   213  
   214      // Returns all pets from the system that the user has access to
   215      rpc FindPets(FindPetsRequest) returns (FindPetsResponse) {
   216          option (google.api.http) = {
   217              get: "/api/pets"
   218          };
   219      }
   220  
   221      // Returns all pets from the system that the user has access to
   222      rpc FindPetsByIds(FindPetsByIdsRequest) returns (FindPetsByIdsResponse) {
   223          option (google.api.http) = {
   224              get: "/api/pets/{ids}"
   225          };
   226      }
   227  }
   228  ```