package openapi2
import "github.com/getkin/kin-openapi/openapi2"
Package openapi2 parses and writes OpenAPIv2 specification documents.
Does not cover all elements of OpenAPIv2. When OpenAPI version 3 is backwards-compatible with version 2, version 3 elements have been used.
See https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md
Code:play
Example¶
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"reflect"
"github.com/getkin/kin-openapi/openapi2"
)
func main() {
input, err := ioutil.ReadFile("testdata/swagger.json")
if err != nil {
panic(err)
}
var doc openapi2.T
if err = json.Unmarshal(input, &doc); err != nil {
panic(err)
}
output, err := json.Marshal(doc)
if err != nil {
panic(err)
}
var docAgain openapi2.T
if err = json.Unmarshal(output, &docAgain); err != nil {
panic(err)
}
if !reflect.DeepEqual(doc, docAgain) {
fmt.Println("objects doc & docAgain should be the same")
}
}
Index ¶
- type Header
- func (header *Header) MarshalJSON() ([]byte, error)
- func (header *Header) UnmarshalJSON(data []byte) error
- type Operation
- func (operation *Operation) MarshalJSON() ([]byte, error)
- func (operation *Operation) UnmarshalJSON(data []byte) error
- type Parameter
- func (parameter *Parameter) MarshalJSON() ([]byte, error)
- func (parameter *Parameter) UnmarshalJSON(data []byte) error
- type Parameters
- func (ps Parameters) Len() int
- func (ps Parameters) Less(i, j int) bool
- func (ps Parameters) Swap(i, j int)
- type PathItem
- func (pathItem *PathItem) GetOperation(method string) *Operation
- func (pathItem *PathItem) MarshalJSON() ([]byte, error)
- func (pathItem *PathItem) Operations() map[string]*Operation
- func (pathItem *PathItem) SetOperation(method string, operation *Operation)
- func (pathItem *PathItem) UnmarshalJSON(data []byte) error
- type Response
- func (response *Response) MarshalJSON() ([]byte, error)
- func (response *Response) UnmarshalJSON(data []byte) error
- type SecurityRequirements
- type SecurityScheme
- func (securityScheme *SecurityScheme) MarshalJSON() ([]byte, error)
- func (securityScheme *SecurityScheme) UnmarshalJSON(data []byte) error
- type T
Examples ¶
Types ¶
type Header ¶
type Header struct { openapi3.ExtensionProps Ref string `json:"$ref,omitempty"` Description string `json:"description,omitempty"` Type string `json:"type,omitempty"` }
func (*Header) MarshalJSON ¶
func (*Header) UnmarshalJSON ¶
type Operation ¶
type Operation struct { openapi3.ExtensionProps Summary string `json:"summary,omitempty"` Description string `json:"description,omitempty"` ExternalDocs *openapi3.ExternalDocs `json:"externalDocs,omitempty"` Tags []string `json:"tags,omitempty"` OperationID string `json:"operationId,omitempty"` Parameters Parameters `json:"parameters,omitempty"` Responses map[string]*Response `json:"responses"` Consumes []string `json:"consumes,omitempty"` Produces []string `json:"produces,omitempty"` Security *SecurityRequirements `json:"security,omitempty"` }
func (*Operation) MarshalJSON ¶
func (*Operation) UnmarshalJSON ¶
type Parameter ¶
type Parameter struct { openapi3.ExtensionProps Ref string `json:"$ref,omitempty"` In string `json:"in,omitempty"` Name string `json:"name,omitempty"` Description string `json:"description,omitempty"` CollectionFormat string `json:"collectionFormat,omitempty"` Type string `json:"type,omitempty"` Format string `json:"format,omitempty"` Pattern string `json:"pattern,omitempty"` AllowEmptyValue bool `json:"allowEmptyValue,omitempty"` Required bool `json:"required,omitempty"` UniqueItems bool `json:"uniqueItems,omitempty"` ExclusiveMin bool `json:"exclusiveMinimum,omitempty"` ExclusiveMax bool `json:"exclusiveMaximum,omitempty"` Schema *openapi3.SchemaRef `json:"schema,omitempty"` Items *openapi3.SchemaRef `json:"items,omitempty"` Enum []interface{} `json:"enum,omitempty"` MultipleOf *float64 `json:"multipleOf,omitempty"` Minimum *float64 `json:"minimum,omitempty"` Maximum *float64 `json:"maximum,omitempty"` MaxLength *uint64 `json:"maxLength,omitempty"` MaxItems *uint64 `json:"maxItems,omitempty"` MinLength uint64 `json:"minLength,omitempty"` MinItems uint64 `json:"minItems,omitempty"` Default interface{} `json:"default,omitempty"` }
func (*Parameter) MarshalJSON ¶
func (*Parameter) UnmarshalJSON ¶
type Parameters ¶
type Parameters []*Parameter
func (Parameters) Len ¶
func (ps Parameters) Len() int
func (Parameters) Less ¶
func (ps Parameters) Less(i, j int) bool
func (Parameters) Swap ¶
func (ps Parameters) Swap(i, j int)
type PathItem ¶
type PathItem struct { openapi3.ExtensionProps Ref string `json:"$ref,omitempty"` Delete *Operation `json:"delete,omitempty"` Get *Operation `json:"get,omitempty"` Head *Operation `json:"head,omitempty"` Options *Operation `json:"options,omitempty"` Patch *Operation `json:"patch,omitempty"` Post *Operation `json:"post,omitempty"` Put *Operation `json:"put,omitempty"` Parameters Parameters `json:"parameters,omitempty"` }
func (*PathItem) GetOperation ¶
func (*PathItem) MarshalJSON ¶
func (*PathItem) Operations ¶
func (*PathItem) SetOperation ¶
func (*PathItem) UnmarshalJSON ¶
type Response ¶
type Response struct { openapi3.ExtensionProps Ref string `json:"$ref,omitempty"` Description string `json:"description,omitempty"` Schema *openapi3.SchemaRef `json:"schema,omitempty"` Headers map[string]*Header `json:"headers,omitempty"` Examples map[string]interface{} `json:"examples,omitempty"` }
func (*Response) MarshalJSON ¶
func (*Response) UnmarshalJSON ¶
type SecurityRequirements ¶
type SecurityScheme ¶
type SecurityScheme struct { openapi3.ExtensionProps Ref string `json:"$ref,omitempty"` Description string `json:"description,omitempty"` Type string `json:"type,omitempty"` In string `json:"in,omitempty"` Name string `json:"name,omitempty"` Flow string `json:"flow,omitempty"` AuthorizationURL string `json:"authorizationUrl,omitempty"` TokenURL string `json:"tokenUrl,omitempty"` Scopes map[string]string `json:"scopes,omitempty"` Tags openapi3.Tags `json:"tags,omitempty"` }
func (*SecurityScheme) MarshalJSON ¶
func (securityScheme *SecurityScheme) MarshalJSON() ([]byte, error)
func (*SecurityScheme) UnmarshalJSON ¶
func (securityScheme *SecurityScheme) UnmarshalJSON(data []byte) error
type T ¶
type T struct { openapi3.ExtensionProps Swagger string `json:"swagger"` Info openapi3.Info `json:"info"` ExternalDocs *openapi3.ExternalDocs `json:"externalDocs,omitempty"` Schemes []string `json:"schemes,omitempty"` Consumes []string `json:"consumes,omitempty"` Host string `json:"host,omitempty"` BasePath string `json:"basePath,omitempty"` Paths map[string]*PathItem `json:"paths,omitempty"` Definitions map[string]*openapi3.SchemaRef `json:"definitions,omitempty,noref"` Parameters map[string]*Parameter `json:"parameters,omitempty,noref"` Responses map[string]*Response `json:"responses,omitempty,noref"` SecurityDefinitions map[string]*SecurityScheme `json:"securityDefinitions,omitempty"` Security SecurityRequirements `json:"security,omitempty"` Tags openapi3.Tags `json:"tags,omitempty"` }
T is the root of an OpenAPI v2 document
func (*T) AddOperation ¶
func (*T) MarshalJSON ¶
func (*T) UnmarshalJSON ¶
Source Files ¶
- Version
- v0.63.0
- Published
- Jun 7, 2021
- Platform
- windows/amd64
- Imports
- 5 packages
- Last checked
- 43 minutes ago –
Tools for package owners.