Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork0
A tool for generating Protobuf v3 schemas and gRPC service definitions from OpenAPI specifications
License
go-language-server/nytimes-openapi2proto
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This tool will accept an OpenAPI/Swagger definition (yaml or JSON) and generate a Protobuf v3 schema and gRPC service definition from it.
To install, have Go installed with$GOPATH/bin on your$PATH and then:
go install -v go.lsp.dev/openapi2proto/cmd/openapi2proto@latest
There are some CLI flags for using the tool:
-specto point to the appropriate OpenAPI spec file-annotateto include (google.api.http options) forgrpc-gateway users. This is disabled by default.-outto have the output written to a file rather thanStdout. Defaults toStdoutif this is not specified.-indentto override the default indentation for Protobuf specs of 4 spaces.-skip-rpcsto skip generation of rpcs. These are generated by default.-skip-deprecated-rpcsto skip generation of rpcs for endpoints marked as deprecated. This is disabled by default.-namespace-enumsto enable inserting the enum name as an enum prefix for each value. This is disabled by default.-add-autogenerated-commentto add comment on top of the generated protos that those files are autogenerated and should not be modified. This is disabled by default.
- 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.
- Any externally referenced Open API spec will be fetched and inlined.
- Any externally referenced Protobuf files will be added as imports.
- Example usage:
$ref: "google/protobuf/timestamp.proto#/google.protobuf.Timestamp"
- Example usage:
Protocol Buffer options such as package names are supported viax-global-options key.
x-global-options:go_package:myawesomepackage
Will generate:
optiongo_package="myawesomepackage"
Global extensions may be generated by specifyingx-extensions key.
x-extensions:-base:google.protobuf.MethodOptionsfields: -name:roletype:stringnumber:50001 -name:visibilitytype:stringnumber:50002 -name:timeouttype:int32number:50003
Will generate:
extendgoogle.protobuf.MethodOptions {stringrole=50001;stringvisibility=50002;int32timeout=50003;}
Nested extensions are currently not supported.
Method options may be generated by specifying thex-options key within each method.
paths:/foopost:x-options:bar:baz
Will generate:
rpc foo(...) returns (...) {option(bar)="baz"; }- Fields with scalar types that can also be "null" will get wrapped with one of the
google.protobuf.*Valuetypes. - Fields with that have more than 1 type and the second type is not "null" will be replaced with the
google.protobuf.Anytype. - Endpoints that respond with an array will be wrapped with a message type that has a single field, 'items', that contains the array.
- Only "200" and "201" responses are inspected for determining the expected return value for RPC endpoints.
- To prevent enum collisions and to match theprotobuf style guide, enum values will be
CAPITALS_WITH_UNDERSCORESand nested enum values will have their parent types prepended to their names.
╰─➤ openapi2proto -spec swagger.yaml -annotatesyntax="proto3";packageswaggerpetstore;import"google/api/annotations.proto";import"google/protobuf/empty.proto";messageAddPetRequest {messagePetMessage {int64id=1;stringname=2;stringtag=3; }// Pet to add to the storePetMessagepet=1;}messageAddPetResponse {int64id=1;stringname=2;stringtag=3;}messageDeletePetRequest {// ID of pet to deleteint64id=1;}messageFindPetByIdRequest {// ID of pet to fetchint64id=1;}messageFindPetByIdResponse {int64id=1;stringname=2;stringtag=3;}messageFindPetsByIdsRequest {repeatedstringids=1;// maximum number of results to returnint32limit=2;}messageFindPetsByIdsResponse {messagePetsMessage {int64id=1;stringname=2;stringtag=3; }repeatedPetsMessagepets=1;}messageFindPetsRequest {// maximum number of results to returnint32limit=1;// tags to filter byrepeatedstringtags=2;}messageFindPetsResponse {messagePetsMessage {int64id=1;stringname=2;stringtag=3; }repeatedPetsMessagepets=1;}serviceSwaggerPetstoreService {// Creates a new pet in the store. Duplicates are allowedrpcAddPet(AddPetRequest)returns (AddPetResponse) {option(google.api.http)= {post:"/api/pets"body:"pet" }; }// deletes a single pet based on the ID suppliedrpcDeletePet(DeletePetRequest)returns (google.protobuf.Empty) {option(google.api.http)= {delete:"/api/pets/{id}" }; }// Returns a user based on a single ID, if the user does not have access to the petrpcFindPetById(FindPetByIdRequest)returns (FindPetByIdResponse) {option(google.api.http)= {get:"/api/pets/{id}" }; }// Returns all pets from the system that the user has access torpcFindPets(FindPetsRequest)returns (FindPetsResponse) {option(google.api.http)= {get:"/api/pets" }; }// Returns all pets from the system that the user has access torpcFindPetsByIds(FindPetsByIdsRequest)returns (FindPetsByIdsResponse) {option(google.api.http)= {get:"/api/pets/{ids}" }; }}
About
A tool for generating Protobuf v3 schemas and gRPC service definitions from OpenAPI specifications
Resources
License
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Languages
- Go100.0%