This repository contains a Go command line tool which converts JSON and YAML OpenAPI descriptions to and from equivalent Protocol Buffer representations.
Protocol Buffers provide a language-neutral, platform-neutral, extensible mechanism for serializing structured data. gnostic's Protocol Buffer models for the OpenAPI Specification can be used to generate code that includes data structures with explicit fields for the elements of an OpenAPI description. This makes it possible for developers to work with OpenAPI descriptions in type-safe ways, which is particularly useful in strongly-typed languages like Go and Swift.
gnostic reads OpenAPI descriptions into these generated data structures, reports errors, resolves internal dependencies, and writes the results in a binary form that can be used in any language that is supported by the Protocol Buffer tools. A plugin interface simplifies integration with API tools written in a variety of different languages, and when necessary, Protocol Buffer OpenAPI descriptions can be reexported as JSON or YAML.
This is prerelease software and work in progress. Feedback and contributions are welcome, but we currently make no guarantees of function or stability.
Get this package by downloading it with
go get github.com/googleapis/gnostic
[Optional] Build and run the compiler generator. This uses the OpenAPI JSON schema to generate a Protocol Buffer language file that describes the OpenAPI specification and a Go-language file of code that will read a JSON or YAML OpenAPI representation into the generated protocol buffers. Pre-generated versions of these files are in the OpenAPIv2 directory.
cd $GOPATH/src/github.com/googleapis/gnostic/generate-gnostic go install cd .. generate-gnostic --v2
[Optional] Generate Protocol Buffer support code. A pre-generated version of this file is checked into the OpenAPIv2 directory. This step requires a local installation of protoc, the Protocol Buffer Compiler, and the Go protoc plugin. You can get protoc here. You can install the plugin with this command:
go get -u github.com/golang/protobuf/protoc-gen-go
Then use the following to recompile the Gnostic Protocol Buffer models:
[Optional] Rebuild gnostic. This is only necessary if you've performed steps 2 or 3 above.
go install github.com/googleapis/gnostic
Run gnostic. This will create a file in the current directory named "petstore.pb" that contains a binary Protocol Buffer description of a sample API.
gnostic --pb-out=. examples/v2.0/json/petstore.json
You can also compile files that you specify with a URL. Here's another way to compile the previous example. This time we're creating "petstore.text", which contains a textual representation of the Protocol Buffer description. This is mainly for use in testing and debugging.
gnostic --text-out=petstore.text https://raw.githubusercontent.com/googleapis/gnostic/master/examples/petstore.json
For a sample application, see apps/report.
go install github.com/googleapis/gnostic/apps/report report petstore.pb
gnostic supports plugins. This builds and runs a sample plugin that reports some basic information about an API. The "-" causes the plugin to write its output to stdout.
go install github.com/googleapis/gnostic/plugins/gnostic-go-sample gnostic examples/v2.0/json/petstore.json --go-sample-out=-
Copyright 2017, Google Inc.
Released under the Apache 2.0 license.
Help us keep the lights on
v0.1.0 - May 19, 2018
Tagged for initial use in Kubernetes and Istio.
v0.2.0 - May 19, 2018
This release updates the OpenAPIv3 Protocol Buffer models to OpenAPI 3.0.1 using a JSON schema generated by scraping the text of the official OpenAPI 3.0.1 specification.
The scraper and generated JSON schema are gnostic-specific and are not endorsed by the OpenAPI Technical Steering Committee, which has not yet released or approved a JSON Schema for OpenAPI v3.