Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A tool for generating Protobuf v3 schemas and gRPC service definitions from OpenAPI specifications

License

NotificationsYou must be signed in to change notification settings

go-language-server/nytimes-openapi2proto

 
 

Repository files navigation

Build Status

This tool will accept an OpenAPI/Swagger definition (yaml or JSON) and generate a Protobuf v3 schema and gRPC service definition from it.

Install

To install, have Go installed with$GOPATH/bin on your$PATH and then:

go install -v go.lsp.dev/openapi2proto/cmd/openapi2proto@latest

Run

There are some CLI flags for using the tool:

  • -spec to point to the appropriate OpenAPI spec file
  • -annotate to include (google.api.http options) forgrpc-gateway users. This is disabled by default.
  • -out to have the output written to a file rather thanStdout. Defaults toStdout if this is not specified.
  • -indent to override the default indentation for Protobuf specs of 4 spaces.
  • -skip-rpcs to skip generation of rpcs. These are generated by default.
  • -skip-deprecated-rpcs to skip generation of rpcs for endpoints marked as deprecated. This is disabled by default.
  • -namespace-enums to enable inserting the enum name as an enum prefix for each value. This is disabled by default.
  • -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.

Protobuf Tags

  • 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.

External Files

  • 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"

Global Options

Protocol Buffer options such as package names are supported viax-global-options key.

x-global-options:go_package:myawesomepackage

Will generate:

optiongo_package="myawesomepackage"

Extensions

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

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";    }

Caveats

  • Fields with scalar types that can also be "null" will get wrapped with one of thegoogle.protobuf.*Value types.
  • Fields with that have more than 1 type and the second type is not "null" will be replaced with thegoogle.protobuf.Any type.
  • 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 beCAPITALS_WITH_UNDERSCORES and nested enum values will have their parent types prepended to their names.

Example

╰─➤  openapi2proto -spec swagger.yaml -annotate
syntax="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

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages

  • Go100.0%

[8]ページ先頭

©2009-2025 Movatter.jp