Coder Social home page Coder Social logo

grokify / spectrum Goto Github PK

View Code? Open in Web Editor NEW
112.0 5.0 28.0 2.05 MB

OpenAPI Spec SDK and Converter for OpenAPI 3.0 and 2.0 Specs to Postman 2.0 Collections. Example RingCentral spec included.

Home Page: https://pkg.go.dev/github.com/grokify/spectrum

License: MIT License

Go 100.00%
postman swagger swagger-specification postman-collection golang ringcentral glip

spectrum's Introduction

Spectrum - OpenAPI Spec SDK and Postman Converter

Build Status Go Report Card Docs LOC License

Spectrum is a multi-purpose OpenAPI Spec SDK that includes enhanced Postman conversion. Most of the OpenAPI Spec SDK is designed to support OAS3. Some functionality for OAS2 exists.

The following article provides an overview of OpenAPI spec to Postman conversion:

  1. Blog Introduction

Use Cases

  1. Converting from OpenAPI Spec to Postman where x-tagGroups are converted to Postman folders.
  2. Programmatically editing specs for reproducible results, typically when source spec is maintained.
  3. Merging multiple specs when a single spec is desired.
  4. Splitting a spec by tags when multiple specs are desired.

Packages and Major Features

  • openapi2 (godoc)
    1. Support for OpenAPI 2 files, including serialization, deserialization, and validation.
    2. Merging of multiple specs
    3. Postman 2 Collection conversion
  • openapi3 (godoc)
    1. Support for OpenAPI 3 files, including serialization, deserialization, and validation.
    2. Merging of multiple specs
    3. Splitting specs by tag
    4. Output of spec to tabular format to HTML (API Registry), CSV, XLSX. HTML API Registry has a bonus feature that makes each line clickable. Click any line here: http://ringcentral.github.io/api-registry/
    5. Programmatic API to modify OpenAPI specs using rules
    6. Programmatic ability to "fix" spec, e.g. change response Content Type to match output (needed for Engage Voice)
    7. OpenAPI 3 linter
    8. Statistics: Counts operations, schemas, properties & parameters (with and without descriptions), etc.
    9. Postman 2 Collection conversion
    10. Ability to merge in Postman request body examples into Postman 2 Collection
    11. Functionality is built on kin-openapi: https://github.com/getkin/kin-openapi
  • openapi3edit (godoc)
    1. Programmatic SDK-based editor for OAS3 specifications.
  • openapi3lint (godoc)
    1. Extensible linter for OAS3 specifications.
  • postman2 (godoc)
    1. Support for Postman 2 Collection files, including serialization and deserialization.
    2. CLI and library to Convert OpenAPI Specs to Postman Collection
    3. Add Postman environment variables to URLs, e.g. Server URLs like https://{{HOSTNAME}}/restapi
    4. Add headers, such as environment variable based Authorization headers, such as Authorization: Bearer {{myAccessToken}}
    5. Utilize baseline Postman collection to add Postman-specific functionality including Postman prerequest scripts.
    6. Add example request bodies, e.g. JSON bodies with example parameter values.
  • raml08
    1. Support for parsing RAML v0.8
    2. Limited functionality to extracting OpenAPI v3 description and summary from description and displayName respectively.

Notes

  • Postman 4.10.7 does not natively support JSON requests so request bodies need to be entered using the raw body editor. A future task is to add Swagger request examples as default Postman request bodies.
  • Postman 2.0 spec supports polymorphism and doesn't have a canonical schema. For example, the request.url property can be populated by a URL string or a URL object. Spectrum uses the URL object since it is more flexible. The function simple.NewCanonicalCollectionFromBytes(bytes) can be used to read either a simple or object based spec into a canonical object spec.
  • This has only been used on the RingCentral Swagger spec to date but will be used for more in the future. Please feel free to use and contribute. Examples are located in the examples folder.

Installation

The following command will install the executable binary spectrum into the ~/go/bin directory.

$ go get github.com/grokify/spectrum

Usage

Simple Usage

// Instantiate a converter with default configuration
conv := spectrum.NewConverter(spectrum.Configuration{})

// Convert a Swagger spec
err := conv.Convert("path/to/swagger.json", "path/to/pman.out.json")

Usage with Features

The following can be added which are especially useful to use with environment variables.

  • Custom Hostname
  • Custom Headers
// Instantiate a converter with overrides (using Postman environment variables)
cfg := spectrum.Configuration{
	PostmanURLBase: "{{RINGCENTRAL_SERVER_URL}}",
	PostmanHeaders: []postman2.Header{
		{
			Key:   "Authorization",
			Value: "Bearer {{my_access_token}}",
		},
	},
}
conv = spectrum.NewConverter(cfg)

// Convert a Swagger spec with a default Postman spec
err := conv.MergeConvert("path/to/swagger.json", "path/to/pman.base.json", "path/to/pman.out.json")

Example

An example conversion is included, examples/ringcentral/convert.go which creates a Postman 2.0 spec for the RingCentral REST API using a base Postman 2.0 spec and the RingCentral basic Swagger 2.0 spec.

A video of importing the resulting Postman collection is available on YouTube.

Example files include:

The RingCentral spec uses the following environment variables. The following is the Postman bulk edit format:

RC_SERVER_HOSTNAME:platform.devtest.ringcentral.com
RC_APP_KEY:myAppKey
RC_APP_SECRET:myAppSecret
RC_USERNAME:myMainCompanyPhoneNumber
RC_EXTENSION:myExtension
RC_PASSWORD:myPassword

For multiple apps or users, simply create a different Postman environment for each.

To set your environment variables, use the Settings Gear icon and then click "Manage Environments"

Articles and Links

spectrum's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

spectrum's Issues

Handle merging different base paths

Overview

Currently, merging expects the same base path / root server URL, however, sometimes the specs to be merged can have different base paths.

Requirements

Description: enhance merging to handle merging different base paths.

  • Create absolute paths using OpenAPI3 server urls. Optionally error out if base path is different aside from host name.
  • Build all paths with absolute path. Optionally scan all paths an extract either hard coded root, or implicit base path on similarity.
  • The priority is for OpenAPI3 but will look to make the code generic so it can be added to OpenAPI2 if needed.

Build error - specMaster.Tags undefined

When building "github.com/grokify/swaggman" on "master" or "v1.0.0" I get the following error.
vendor/github.com/grokify/swaggman/openapi3/merge.go:72:32: specMaster.Tags undefined (type *openapi3.Swagger has no field or method Tags)
The dependency is included in my Gopkg.toml as follows
[[constraint]] name = "github.com/grokify/swaggman" branch = "master"

Breaking changes in `kin-openapi` v0.124.0 - change of type definition

From go test. Appears that Types has changed types to github.com/getkin/kin-openapi/openapi3.Types from string:

openapi3/visit.go:29:5: cannot use propRef.Value.Type (variable of type *"github.com/getkin/kin-openapi/openapi3".Types) as string value in argument to visitTypeFormat
openapi3/visit.go:41:4: cannot use paramRef.Value.Schema.Value.Type (variable of type *"github.com/getkin/kin-openapi/openapi3".Types) as string value in argument to visitTypeFormat
openapi3/visit.go:59:6: cannot use paramRef.Value.Schema.Value.Type (variable of type *"github.com/getkin/kin-openapi/openapi3".Types) as string value in argument to visitTypeFormat

undefined: stringsutil.SliceTrimSpace

Hi,

I'm running into the following issue when running go get github.com/grokify/swaggman from my project:

$ go get github.com/grokify/swaggman
# github.com/grokify/swaggman/swagger2
../../../../pkg/mod/github.com/grokify/[email protected]/swagger2/count.go:15:9: undefined: stringsutil.SliceTrimSpace

This is a project using Go Modules and Go v1.13.8.

Let me know if I can provide any more info.

Breaking changes in `kin-openapi` v0.122.0 - change of map definitions

Build fails on v0.122.

% go test -v ./...
# github.com/grokify/spectrum/openapi3
openapi3/merge.go:150:29: cannot range over specExtra.Paths (variable of type *"github.com/getkin/kin-openapi/openapi3".Paths)
openapi3/merge.go:151:44: invalid operation: cannot index specMaster.Paths (variable of type *"github.com/getkin/kin-openapi/openapi3".Paths)
openapi3/merge.go:152:20: invalid operation: cannot index specMaster.Paths (variable of type *"github.com/getkin/kin-openapi/openapi3".Paths)
openapi3/merge.go:155:23: invalid operation: cannot index specMaster.Paths (variable of type *"github.com/getkin/kin-openapi/openapi3".Paths)
openapi3/merge.go:156:21: invalid operation: cannot index specMaster.Paths (variable of type *"github.com/getkin/kin-openapi/openapi3".Paths)
openapi3/merge.go:157:67: invalid operation: cannot index specMaster.Paths (variable of type *"github.com/getkin/kin-openapi/openapi3".Paths)
openapi3/merge.go:162:23: invalid operation: cannot index specMaster.Paths (variable of type *"github.com/getkin/kin-openapi/openapi3".Paths)
openapi3/merge.go:163:21: invalid operation: cannot index specMaster.Paths (variable of type *"github.com/getkin/kin-openapi/openapi3".Paths)
openapi3/merge.go:164:66: invalid operation: cannot index specMaster.Paths (variable of type *"github.com/getkin/kin-openapi/openapi3".Paths)
openapi3/merge.go:169:23: invalid operation: cannot index specMaster.Paths (variable of type *"github.com/getkin/kin-openapi/openapi3".Paths)
openapi3/merge.go:169:23: too many errors

Add oas3lint rules

Add rules:

  1. Require OperationId
  2. Require Operation Tag
  3. Require Operation Description
  4. Require Operation Parameter Descriptions
  5. Require Schema Property Descriptions

Add oas3lint filters

Add filters

  1. Filter operations by tag
  2. Filter operations by path

Narrow support objects by filtered operations, e.g. requestBodies, parameters, schema objects.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.