Categorygithub.com/creker/twirp-openapi-gen
repository
1.0.6
Repository: https://github.com/creker/twirp-openapi-gen.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# README

twirp-openapi-gen

Generate Open API V3 documentation for Twirp services. This is a fork of github.com/blockthrough/twirp-openapi-gen that fixes bugs and adds new features like maps.

GoDoc

Installation

$  go install github.com/creker/twirp-openapi-gen/cmd/twirp-openapi-gen@latest

Proto OpenAPI Mappings

ProtoOpenAPI
RPCPath
Package.Service.RPC NamePath.Key
RPC NamePath.Summary
RPC InputPath.RequestBody
RPC OutputPath.Response
RPC CommentPath.Method.Description
RPC Req Example (comments with json objects prefixed by req-example)Path.Method.RequestBody.Content.Content-Type.Example
RPC Res Example (comments with json objects prefixed by res-example)Path.Method.Responses.200.Content.Content-Type.Example
MessageComponent.Schema
Message CommentComponent.Schema.Description
Message FieldComponent.Schema.Property
Message Field CommentComponent.Schema.Property.Description
EnumComponent.Schema.Property.Enum

Google Protobuf

The generator does a best effort attempt to map google.protobuf to OpenAPI types. The following table shows the mappings:

Google ProtobufOpenAPI Type & Format
Timestampstring & date-time
DateTimestring & date-time
Durationstring
StringValuestring
BytesValuestring & byte
Int32Valueinteger & int32
UInt32Valueinteger & uint32
Int64Valuestring & int64
UInt64Valuestring & uint64
FloatValuenumber & float
DoubleValuenumber & double
BoolValueboolean
Empty-
Anyobject
ListValuearray
Structobject
Valuestring, number, integer, bolean, Struct or ListValue
google.type.Moneystruct with currency_code, units and nanos

Notes

  • The requestBody property of the path post operation only has one content-type of application/json, and its schema always references the RPC input message.
  • Comments can be added above an RPC, message, or field resources. Inline comments are not supported.
  • Path items only have one response with a 200 code using the schema of the message returned by the RPC method.
  • All imports are resolved and their proto messages are added to the schema bucket. Only google/* proto imports are skipped.

Usage

❯ twirp-openapi-gen -h
Usage of twirp-openapi-gen [flags] <input files>:
  -doc-version string
        API Document version (default "0.1")
  -format string
        Document format; json or yaml (default "json")
  -out string
        Output document file (default "./openapi-doc.json")
  -path-prefix string
        Twirp server path prefix (default "/twirp")
  -proto-path value
        Specify the directory in which to search for imports. May be specified multiple times; directories will be searched in order.  If not given, the current working directory is used.
  -servers value
        Server object URL. May be specified multiple times.
  -title string
        Document title (default "open-api-v3-docs")
  -verbose
        Log debug output
  -version
        Print version

Examples

Generate OpenAPI V3 JSON document for the Twirp PetStore service:

❯ twirp-openapi-gen \
    -in ./internal/generator/testdata/petapis/pet/v1/pet.proto \
    -out ./internal/generator/testdata/pet-api-doc.json \
    -proto-path "$(shell realpath ./internal/generator/testdata/paymentapis/)" \
    -proto-path "$(shell realpath ./internal/generator/testdata/petapis/)" \
    -servers https://petapi.example.com \
    -path-prefix "" \
    -doc-version 1.0 \
    -title "Pet API"

Why

This project is a rewrite of the twirp-swagger-gen tool adding support to the latest OpenAPI v3 spec

Even though there are other ways of generating documentation for proto files, eg; protoc-gen-doc, gnostic, buf modules, etc. Neither provides an out-of the box solution to have interactive Twirp API docs.

There is already a rich ecosystem of tools for visualizing and interacting with OpenAPI V3 spec documents. The twirp-openapi-gen tool leverages that and generates valid JSON/YAML OpenAPI V3 documents from the proto definitions. The API docs can be imported to any tool that has support for OpenAPI V3, eg; Postman, Swagger, Stoplight, etc..