modulepackage
0.0.0-20250129152110-c196ef3165b1
Repository: https://github.com/markrosemaker/openapi.git
Documentation: pkg.go.dev
# README
[](https://pkg.go.dev/github.com/MarkRosemaker/openapi)
[](https://goreportcard.com/report/github.com/MarkRosemaker/openapi)

[](./LICENSE)
Transform and master your API specs with ease.
Package openapi provides a suite of tools for working with OpenAPI specifications, making it easier to parse, format, manipulate, and generate code from these specs.
Whether you're looking to clean up existing API documentation or integrate API design into your development pipeline, this package is built to streamline your workflow.
Introduction
The primary goals of this package are:
- Parsing OpenAPI specifications into a structured format.
- Formatting the parsed specifications, including sorting maps and merging duplicate content.
- Adding information programmatically to the specifications.
- Marshalling the modified specifications back into their original format.
- Utilizing the parsed specification for code generation.
Features
- Comprehensive parsing of OpenAPI specifications.
- Flexible formatting options to improve readability and consistency.
- Ability to merge and deduplicate content within specifications.
- Programmatic modification of specifications before marshalling.
- Code generation capabilities based on parsed specifications.
Usage
package main
import (
"fmt"
"github.com/MarkRosemaker/openapi"
)
func main() {
doc, err := openapi.LoadFromFile("path/to/openapi.json") // or openapi.yaml
if err != nil {
fmt.Println("Error parsing spec:", err)
return
}
if err := doc.Validate(); err != nil {
fmt.Println("Error validating spec:", err)
return
}
// sort keys of each component in alphabetical order
doc.Components.SortMaps()
if err := doc.WriteToFile("path/to/openapi.json"); err != nil {
fmt.Println("Error writing to file:", err)
return
}
}
Additional Information
- Go Reference: The Go reference documentation for the errpath package.
- Go Report Card: Check the code quality report.
Contributing
If you have any contributions to make, please submit a pull request or open an issue on the GitHub repository.
License
This project is licensed under the Apache 2.0 License.
# Functions
No description provided by the author
LoadFromDataJSON reads an OpenAPI specification from a byte array in JSON format and parses it into a structured format.
LoadFromDataYAML reads an OpenAPI specification from a byte array in YAML format and parses it into a structured format.
LoadFromFile reads an OpenAPI specification from a file and parses it into a structured format.
LoadFromReader reads an OpenAPI specification from an io.Reader and parses it into a structured format.
# Constants
FormatBinary represents a binary.
FormatByte represents a byte.
FormatDate represents a date.
FormatDateTime represents a date-time.
FormatDouble represents a double number.
FormatDuration represents a duration.
FormatEmail represents an email.
FormatFloat represents a float number.
FormatInt32 represents a signed 32 bits integer.
FormatInt64 represents a signed 64 bits integer.
FormatIPv4 represents an IPv4 address.
FormatIPv6 represents an IPv6 address.
FormatPassword represents a password.
FormatURI represents a URI.
FormatURIRef represents a URI reference.
FormatUUID represents a UUID.
FormatZipCode represents a zip code.
No description provided by the author
No description provided by the author
Used to pass a specific cookie value to the API.
Custom headers that are expected as part of the request.
Used together with Path Templating, where the parameter value is actually part of the operation's URL.
Parameters that are appended to the URL.
Provides a simple way of rendering nested objects using form parameters.
No description provided by the author
No description provided by the author
No description provided by the author
Pipe separated array or object values.
No description provided by the author
Space separated array or object values.
SecuritySchemeBasic is the value of SecurityScheme.Scheme for basic authentication.
SecuritySchemeBearer is the value of SecurityScheme.Scheme for bearer tokens.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Use default to documentat responses other than the ones declared for specific HTTP response codes.
No description provided by the author
No description provided by the author
format: int32, int64.
format: float, double.
No description provided by the author
format: password.
# Variables
ErrEmptyDocument is thrown if the OpenAPI document does not contain at least one paths field, a components field or a webhooks field.
ErrUnknownField is returned when a field is not recognized and also doesn't have a "x-" prefix signifying it is an extension.
# Structs
The Components object holds a set of reusable objects for different aspects of the OAS.
Contact information for the exposed API.
Document is an OpenAPI document, the root object.
Encoding is a single encoding definition applied to a single schema property.
Example represents an example of a schema.
ExternalDocs allows referencing an external resource for extended documentation.
Header represents a single header parameter to be included in the operation.
The Info object provides metadata about the API.
License information for the exposed API.
The `Link object` represents a possible design-time link for a response.
LinkParameter is an expression that is the value of a parameter map in a Link Object.
Each Media Type Object provides schema and examples for the media type identified by its key.
OAuthFlowAuthorizationCode allows configuration details for the OAuth Authorization Code flow.
OAuthFlowImplicit allows configuration details for the OAuth Implicit flow.
OAuthFlowPassword allows configuration details for the OAuth Resource Owner Password flow.
The OAuthFlows object allows configuration of the supported OAuth Flows.
Operation describes a single API operation on a path.
Parameter describes a single operation parameter.
ParsedPath is a parsed URL path template.
PathItem describes the operations available on a single path.
Reference is a simple object to allow referencing other components in the OpenAPI document, internally and externally.
RequestBody describes a single request body.
Response describes a single response from an API Operation, including design-time, static `links` to operations based on the response.
The Schema Object allows the definition of input and output data types.
SecurityScheme defines a security scheme that can be used by the operations.
Server is an object representing a Server.
An object representing a Server Variable for server URL template substitution.
No description provided by the author
Adds metadata to a single tag that is used by the [Operation] object.
# Type aliases
Callback is a map of possible out-of band callbacks related to the parent operation.
No description provided by the author
Callbacks holds a set of reusable callbacks.
The content of a request body.
Data types in the OAS are based on the types supported by the JSON Schema Specification Draft 2020-12.
Encodings is a map between a property name and its encoding information.
Examples is a map of examples.
Extensions represents additional fields that can be added to OpenAPI objects.
Format defines additional formats to provide fine detail for primitive data types.
No description provided by the author
A map representing parameters to pass to an operation as specified with `operationId` or identified via `operationRef`.
No description provided by the author
No description provided by the author
MediaRange represents a media type or media type range.
OAuthFlowClientCredentials allows configuration details for the OAuth Client Credentials flow.
ParameterList is a list of parameter references.
ParameterLocation defines the location of the parameter.
No description provided by the author
In order to support common ways of serializing simple parameters, a set of `style` values are defined.
Path represents a URL path template.
No description provided by the author
Holds the relative paths to the individual endpoints and their operations.
No description provided by the author
Responses is a map of either response name or status code to a response object.
Runtime expressions allow defining values based on information that will only be available within the HTTP message in an actual API call.
No description provided by the author
No description provided by the author
No description provided by the author
SecurityRequirement lists the required security schemes to execute this operation.
No description provided by the author
No description provided by the author
SecuritySchemeName is the name of a security scheme defined in the Security Schemes under the Components Object.
No description provided by the author
No description provided by the author
Servers is a list of server objects.
ServerVariables is an ordered map of server variable.
StatusCode is used as a key in the Responses map to describe the expected response for that HTTP status code.
A list of tags used by the document with additional metadata.
Webhooks describes requests initiated other than by an API call, for example by an out of band registration.