Documentation ¶
Overview ¶
Package libopenapi is a library containing tools for reading and in and manipulating Swagger (OpenAPI 2) and OpenAPI 3+ specifications into strongly typed documents. These documents have two APIs, a high level (porcelain) and a low level (plumbing).
Every single type has a 'GoLow()' method that drops down from the high API to the low API. Once in the low API, the entire original document data is available, including all comments, line and column numbers for keys and values.
There are two steps to creating a using Document. First, create a new Document using the NewDocument() method and pass in a specification []byte array that contains the OpenAPI Specification. It doesn't matter if YAML or JSON are used.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Document ¶
type Document interface { // GetVersion will return the exact version of the OpenAPI specification set for the document. GetVersion() string // GetSpecInfo will return the *datamodel.SpecInfo instance that contains all specification information. GetSpecInfo() *datamodel.SpecInfo // BuildV2Model will build out a Swagger (version 2) model from the specification used to create the document // If there are any issues, then no model will be returned, instead a slice of errors will explain all the // problems that occurred. This method will only support version 2 specifications and will throw an error for // any other types. BuildV2Model() (*DocumentModel[v2high.Swagger], []error) // BuildV3Model will build out an OpenAPI (version 3+) model from the specification used to create the document // If there are any issues, then no model will be returned, instead a slice of errors will explain all the // problems that occurred. This method will only support version 3 specifications and will throw an error for // any other types. BuildV3Model() (*DocumentModel[v3high.Document], []error) // Serialize will re-render a Document back into a []byte slice. If any modifications have been made to the // underlying data model using low level APIs, then those changes will be reflected in the serialized output. // // It's important to know that this should not be used if the resolver has been used on a specification to // for anything other than checking for circular references. If the resolver is used to resolve the spec, then this // method may spin out forever if the specification backing the model has circular references. Serialize() ([]byte, error) }
Document Represents an OpenAPI specification that can then be rendered into a model or serialized back into a string document after being manipulated.
func NewDocument ¶
NewDocument will create a new OpenAPI instance from an OpenAPI specification []byte array. If anything goes wrong when parsing, reading or processing the OpenAPI specification, there will be no document returned, instead a slice of errors will be returned that explain everything that failed.
After creating a Document, the option to build a model becomes available, in either V2 or V3 flavors. The models are about 70% different between Swagger and OpenAPI 3, which is why two different models are available.
Example (FromOpenAPI3Document) ¶
// How to read in an OpenAPI 3 Specification, into a Document. // load an OpenAPI 3 specification from bytes petstore, _ := ioutil.ReadFile("test_specs/petstorev3.json") // create a new document from specification bytes document, err := NewDocument(petstore) // if anything went wrong, an error is thrown if err != nil { panic(fmt.Sprintf("cannot create new document: %e", err)) } // because we know this is a v3 spec, we can build a ready to go model from it. v3Model, errors := document.BuildV3Model() // if anything went wrong when building the v3 model, a slice of errors will be returned if len(errors) > 0 { for i := range errors { fmt.Printf("error: %e\n", errors[i]) } panic(fmt.Sprintf("cannot create v3 model from document: %d errors reported", len(errors))) } // get a count of the number of paths and schemas. paths := len(v3Model.Model.Paths.PathItems) schemas := len(v3Model.Model.Components.Schemas) // print the number of paths and schemas in the document fmt.Printf("There are %d paths and %d schemas in the document", paths, schemas)
Output: There are 13 paths and 8 schemas in the document
Example (FromSwaggerDocument) ¶
// How to read in a Swagger / OpenAPI 2 Specification, into a Document. // load a Swagger specification from bytes petstore, _ := ioutil.ReadFile("test_specs/petstorev2.json") // create a new document from specification bytes document, err := NewDocument(petstore) // if anything went wrong, an error is thrown if err != nil { panic(fmt.Sprintf("cannot create new document: %e", err)) } // because we know this is a v2 spec, we can build a ready to go model from it. v2Model, errors := document.BuildV2Model() // if anything went wrong when building the v3 model, a slice of errors will be returned if len(errors) > 0 { for i := range errors { fmt.Printf("error: %e\n", errors[i]) } panic(fmt.Sprintf("cannot create v3 model from document: %d errors reported", len(errors))) } // get a count of the number of paths and schemas. paths := len(v2Model.Model.Paths.PathItems) schemas := len(v2Model.Model.Definitions.Definitions) // print the number of paths and schemas in the document fmt.Printf("There are %d paths and %d schemas in the document", paths, schemas)
Output: There are 14 paths and 6 schemas in the document
Example (FromUnknownVersion) ¶
// load an unknown version of an OpenAPI spec petstore, _ := ioutil.ReadFile("test_specs/burgershop.openapi.yaml") // create a new document from specification bytes document, err := NewDocument(petstore) // if anything went wrong, an error is thrown if err != nil { panic(fmt.Sprintf("cannot create new document: %e", err)) } var paths, schemas int var errors []error // We don't know which type of document this is, so we can use the spec info to inform us if document.GetSpecInfo().SpecType == utils.OpenApi3 { v3Model, errs := document.BuildV3Model() if len(errs) > 0 { errors = errs } if len(errors) <= 0 { paths = len(v3Model.Model.Paths.PathItems) schemas = len(v3Model.Model.Components.Schemas) } } if document.GetSpecInfo().SpecType == utils.OpenApi2 { v2Model, errs := document.BuildV2Model() if len(errs) > 0 { errors = errs } if len(errors) <= 0 { paths = len(v2Model.Model.Paths.PathItems) schemas = len(v2Model.Model.Definitions.Definitions) } } // if anything went wrong when building the model, report errors. if len(errors) > 0 { for i := range errors { fmt.Printf("error: %e\n", errors[i]) } panic(fmt.Sprintf("cannot create v3 model from document: %d errors reported", len(errors))) } // print the number of paths and schemas in the document fmt.Printf("There are %d paths and %d schemas in the document", paths, schemas)
Output: There are 5 paths and 6 schemas in the document
Example (MutateValuesAndSerialize) ¶
// How to mutate values in an OpenAPI Specification, without re-ordering original content. // create very small, and useless spec that does nothing useful, except showcase this feature. spec := ` openapi: 3.1.0 info: title: This is a title contact: name: Some Person email: some@emailaddress.com license: url: http://some-place-on-the-internet.com/license ` // create a new document from specification bytes document, err := NewDocument([]byte(spec)) // if anything went wrong, an error is thrown if err != nil { panic(fmt.Sprintf("cannot create new document: %e", err)) } // because we know this is a v3 spec, we can build a ready to go model from it. v3Model, errors := document.BuildV3Model() // if anything went wrong when building the v3 model, a slice of errors will be returned if len(errors) > 0 { for i := range errors { fmt.Printf("error: %e\n", errors[i]) } panic(fmt.Sprintf("cannot create v3 model from document: %d errors reported", len(errors))) } // mutate the title, to do this we currently need to drop down to the low-level API. v3Model.Model.GoLow().Info.Value.Title.Mutate("A new title for a useless spec") // mutate the email address in the contact object. v3Model.Model.GoLow().Info.Value.Contact.Value.Email.Mutate("buckaroo@pb33f.io") // mutate the name in the contact object. v3Model.Model.GoLow().Info.Value.Contact.Value.Name.Mutate("Buckaroo") // mutate the URL for the license object. v3Model.Model.GoLow().Info.Value.License.Value.URL.Mutate("https://pb33f.io/license") // serialize the document back into the original YAML or JSON mutatedSpec, serialError := document.Serialize() // if something went wrong serializing if serialError != nil { panic(fmt.Sprintf("cannot serialize document: %e", serialError)) } // print our modified spec! fmt.Println(string(mutatedSpec))
Output: openapi: 3.1.0 info: title: A new title for a useless spec contact: name: Buckaroo email: buckaroo@pb33f.io license: url: https://pb33f.io/license
Directories ¶
Path | Synopsis |
---|---|
Package datamodel contains two sets of models, high and low.
|
Package datamodel contains two sets of models, high and low. |
high
Package high contains a set of high-level models that represent OpenAPI 2 and 3 documents.
|
Package high contains a set of high-level models that represent OpenAPI 2 and 3 documents. |
high/base
Package base contains shared high-level models that are used between both versions 2 and 3 of OpenAPI.
|
Package base contains shared high-level models that are used between both versions 2 and 3 of OpenAPI. |
high/v2
Package v2 represents all Swagger / OpenAPI 2 high-level models.
|
Package v2 represents all Swagger / OpenAPI 2 high-level models. |
high/v3
Package v3 represents all OpenAPI 3+ high-level models.
|
Package v3 represents all OpenAPI 3+ high-level models. |
low
Package low contains a set of low-level models that represent OpenAPI 2 and 3 documents.
|
Package low contains a set of low-level models that represent OpenAPI 2 and 3 documents. |
low/base
Package base contains shared low-level models that are used between both versions 2 and 3 of OpenAPI.
|
Package base contains shared low-level models that are used between both versions 2 and 3 of OpenAPI. |
low/v2
Package v2 represents all Swagger / OpenAPI 2 low-level models.
|
Package v2 represents all Swagger / OpenAPI 2 low-level models. |
low/v3
Package v3 represents all OpenAPI 3+ low-level models.
|
Package v3 represents all OpenAPI 3+ low-level models. |
Package index contains an OpenAPI indexer that will very quickly scan through an OpenAPI specification (all versions) and extract references to all the important nodes you might want to look up, as well as counts on total objects.
|
Package index contains an OpenAPI indexer that will very quickly scan through an OpenAPI specification (all versions) and extract references to all the important nodes you might want to look up, as well as counts on total objects. |